basic_cv2_kmeans.py 2.67 KB
Newer Older
tilman's avatar
tilman committed
1
2
3
4
5
import numpy as np
import cv2
import os 


6
7
8
9
# IN_DIR = "../images/images_imdahl/"
IN_DIR = "../images/out/fuse_1_bsds500/"
DISPLAY_RASTER_ELEMENTS=500

tilman's avatar
tilman committed
10
images = [os.path.join(os.getcwd(), IN_DIR, f) for f in os.listdir(IN_DIR)]
11
os.chdir('../images/out/images_imdahl/bdcn_filtering/test1') #save images in this dir
tilman's avatar
tilman committed
12
for img_name in images:
tilman's avatar
tilman committed
13
    # img_name=images[0]
tilman's avatar
tilman committed
14
15
    print(img_name)
    img = cv2.imread(img_name)
tilman's avatar
tilman committed
16
17
18
    # cv2.imshow('res2'+img_name,img)
    # cv2.waitKey(0)

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
    # image = cv2.imread('../images/out/fuse_1_bsds500/0.png')
    max_lw = max(len(img),len(img[0]))
    esz = max_lw / DISPLAY_RASTER_ELEMENTS
    print("max_lw,esz",max_lw,esz)
    # img = cv2.bitwise_not(img)

    cv2.imshow('res2'+img_name,img)
    blurred = cv2.medianBlur(img,int(esz*5)+1 if int(esz*5)%2==0 else int(esz*5))
    print("med1")
    cv2.waitKey(0)
    cv2.imshow('res2'+img_name,blurred)
    blurred = cv2.bilateralFilter(blurred,30,60,60)
    print("bil1")
    cv2.waitKey(0)
    cv2.imshow('res2'+img_name,blurred)
    blurred = cv2.bilateralFilter(blurred,30,60,60)
    print("bil2")
    cv2.waitKey(0)
    cv2.imshow('res2'+img_name,blurred)
    blurred = cv2.bilateralFilter(blurred,30,60,60)
    print("bil3")
    cv2.waitKey(0)
    cv2.imshow('res2'+img_name,blurred)
    blurred = cv2.bilateralFilter(blurred,30,60,60)
    print("bil4")
    cv2.waitKey(0)
    cv2.imshow('res2'+img_name,blurred)

tilman's avatar
tilman committed
47
    #applying filters
48
49
50
51
52
53
54
55
56
57
58
59
    # bil = cv2.bilateralFilter(img,50,30,30)
    # # cv2.imshow('res2'+img_name,bil)
    # # cv2.waitKey(0)
    # #applying filters
    # bil = cv2.bilateralFilter(bil,15,60,60)
    # # cv2.imshow('res2'+img_name,bil)
    # # cv2.waitKey(0)
    # #applying filters
    # median = cv2.medianBlur(bil,5)
    # # cv2.imshow('res2'+img_name,median)
    # # cv2.waitKey(0)
    # median = cv2.medianBlur(median,5)
tilman's avatar
tilman committed
60
61
    # cv2.imshow('res2'+img_name,median)
    # cv2.waitKey(0)
tilman's avatar
tilman committed
62

tilman's avatar
tilman committed
63
64
    # # img = cv2.imread(img_name)
    # Z = median.reshape((-1,3))
tilman's avatar
tilman committed
65

tilman's avatar
tilman committed
66
67
68
69
70
71
    # # convert to np.float32
    # Z = np.float32(Z)
    # # define criteria, number of clusters(K) and apply kmeans()
    # criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
    # K = 12
    # ret,label,center=cv2.kmeans(Z,K,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS)
tilman's avatar
tilman committed
72

tilman's avatar
tilman committed
73
74
75
76
    # # Now convert back into uint8, and make original image
    # center = np.uint8(center)
    # res = center[label.flatten()]
    # res2 = res.reshape((img.shape))
tilman's avatar
tilman committed
77

78
79
80
81
    cv2.imwrite(os.path.basename(img_name)+"_med5esz_30_60_60.png",blurred)
    
    cv2.imshow('res2'+img_name,blurred)
    cv2.waitKey(0)
tilman's avatar
tilman committed
82
83
    #cv2.imwrite('/Users/Tilman/Documents/Programme/Python/forschungspraktikum/images/out/images_imdahl/kmean/'+img_name,res2)
# cv2.waitKey(0)
tilman's avatar
tilman committed
84
cv2.destroyAllWindows()