Unverified Commit 6f6bb182 authored by tilman's avatar tilman
Browse files

before applying erosure filter

parent 4b5b9716
...@@ -13,6 +13,7 @@ from openpose import pyopenpose as op ...@@ -13,6 +13,7 @@ from openpose import pyopenpose as op
from lib.triangles import * from lib.triangles import *
from lib.bisection import * from lib.bisection import *
from lib.misc import * from lib.misc import *
from lib.kmeans import *
SHOW_WINDOW = True SHOW_WINDOW = True
SAVE_FILE = not SHOW_WINDOW SAVE_FILE = not SHOW_WINDOW
...@@ -31,13 +32,13 @@ OVERLAY_ALPHA = 0.2 ...@@ -31,13 +32,13 @@ OVERLAY_ALPHA = 0.2
BISEC_SKIP_LOWER_LEVELS = False BISEC_SKIP_LOWER_LEVELS = False
DISPLAY_RASTER_ELEMENTS = 500 DISPLAY_RASTER_ELEMENTS = 500
KMEANS_AREA_MASK_THRESHOLD = 0.09 #max 1.0 (percent of pixels), cur 0.09 KMEANS_AREA_MASK_THRESHOLD = 0.09 #max 1.0 (percent of pixels), cur 0.09
KMEANS_K = 20 #10 KMEANS_K = 10 #10
OUT_DIR = 'images/out/images_imdahl/kmeans_output_canvas/new' OUT_DIR = 'images/out/images_imdahl/kmeans_output_canvas/new'
#IN_DIR = "images/first_email/" # images from first email #IN_DIR = "images/first_email/" # images from first email
IN_DIR = "images/images_imdahl/" # images from imdahl IN_DIR = "images/images_imdahl/" # images from imdahl
IN_DIR_BDCN = "images/out/images_imdahl/bdcn_output/" IN_DIR_BDCN = "images/out/images_imdahl/bdcn_output/"
SKIP_OPENPOSE = True SKIP_OPENPOSE = False
OPENPOSE_DEMO_KEYPOINTS = np.array([[[4.7613028e+02, 3.3695804e+02, 9.0203685e-01],[5.3667474e+02, 3.8633786e+02, 6.6615295e-01],[5.1645105e+02, 3.8405157e+02, 5.1514143e-01],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[5.5459924e+02, 3.8859457e+02, 6.4240879e-01],[5.6353766e+02, 4.7384988e+02, 1.8810490e-01],[5.3886292e+02, 5.2543573e+02, 9.0144195e-02],[5.4566248e+02, 5.3215259e+02, 3.6083767e-01],[5.2768524e+02, 5.3213129e+02, 3.1196830e-01],[5.4556714e+02, 6.3534674e+02, 1.8182488e-01],[5.8149310e+02, 7.2958716e+02, 1.3625422e-01],[5.6579541e+02, 5.3216382e+02, 3.6866242e-01],[5.8822272e+02, 6.2862476e+02, 1.7708556e-01],[6.0843213e+02, 7.2955762e+02, 2.2736737e-01],[4.7597812e+02, 3.2798129e+02, 5.7176876e-01],[4.8729745e+02, 3.3027243e+02, 9.1296065e-01],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[5.2090784e+02, 3.3472034e+02, 7.7942842e-01],[5.7928674e+02, 7.5646222e+02, 2.0351715e-01],[5.9049512e+02, 7.5648248e+02, 2.0819387e-01],[6.2183606e+02, 7.3853394e+02, 1.7312977e-01],[5.8145673e+02, 7.5420642e+02, 1.2660497e-01],[5.7701074e+02, 7.5417773e+02, 1.2881383e-01],[5.8374255e+02, 7.3627380e+02, 9.4869599e-02]] OPENPOSE_DEMO_KEYPOINTS = np.array([[[4.7613028e+02, 3.3695804e+02, 9.0203685e-01],[5.3667474e+02, 3.8633786e+02, 6.6615295e-01],[5.1645105e+02, 3.8405157e+02, 5.1514143e-01],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[5.5459924e+02, 3.8859457e+02, 6.4240879e-01],[5.6353766e+02, 4.7384988e+02, 1.8810490e-01],[5.3886292e+02, 5.2543573e+02, 9.0144195e-02],[5.4566248e+02, 5.3215259e+02, 3.6083767e-01],[5.2768524e+02, 5.3213129e+02, 3.1196830e-01],[5.4556714e+02, 6.3534674e+02, 1.8182488e-01],[5.8149310e+02, 7.2958716e+02, 1.3625422e-01],[5.6579541e+02, 5.3216382e+02, 3.6866242e-01],[5.8822272e+02, 6.2862476e+02, 1.7708556e-01],[6.0843213e+02, 7.2955762e+02, 2.2736737e-01],[4.7597812e+02, 3.2798129e+02, 5.7176876e-01],[4.8729745e+02, 3.3027243e+02, 9.1296065e-01],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[5.2090784e+02, 3.3472034e+02, 7.7942842e-01],[5.7928674e+02, 7.5646222e+02, 2.0351715e-01],[5.9049512e+02, 7.5648248e+02, 2.0819387e-01],[6.2183606e+02, 7.3853394e+02, 1.7312977e-01],[5.8145673e+02, 7.5420642e+02, 1.2660497e-01],[5.7701074e+02, 7.5417773e+02, 1.2881383e-01],[5.8374255e+02, 7.3627380e+02, 9.4869599e-02]]
,[[6.4435681e+02, 3.6383255e+02, 8.9096022e-01],[6.6903070e+02, 3.9760306e+02, 8.7681645e-01],[6.4430103e+02, 3.9525812e+02, 7.9584122e-01],[6.3310535e+02, 4.5589160e+02, 3.7108111e-01],[5.9046979e+02, 4.2451276e+02, 4.0277350e-01],[6.9366602e+02, 4.0197583e+02, 8.9528430e-01],[6.8247137e+02, 4.6042902e+02, 5.5132395e-01],[6.0616620e+02, 4.3569894e+02, 3.4303352e-01],[6.5551196e+02, 5.1196445e+02, 2.9572365e-01],[6.3529651e+02, 5.0747903e+02, 2.8629595e-01],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[6.7573169e+02, 5.1421967e+02, 3.0180413e-01],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[6.4206000e+02, 3.5276721e+02, 7.2430253e-01],[6.5327673e+02, 3.5271103e+02, 9.4265050e-01],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[6.7577380e+02, 3.5269864e+02, 8.9672232e-01],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00]] ,[[6.4435681e+02, 3.6383255e+02, 8.9096022e-01],[6.6903070e+02, 3.9760306e+02, 8.7681645e-01],[6.4430103e+02, 3.9525812e+02, 7.9584122e-01],[6.3310535e+02, 4.5589160e+02, 3.7108111e-01],[5.9046979e+02, 4.2451276e+02, 4.0277350e-01],[6.9366602e+02, 4.0197583e+02, 8.9528430e-01],[6.8247137e+02, 4.6042902e+02, 5.5132395e-01],[6.0616620e+02, 4.3569894e+02, 3.4303352e-01],[6.5551196e+02, 5.1196445e+02, 2.9572365e-01],[6.3529651e+02, 5.0747903e+02, 2.8629595e-01],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[6.7573169e+02, 5.1421967e+02, 3.0180413e-01],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[6.4206000e+02, 3.5276721e+02, 7.2430253e-01],[6.5327673e+02, 3.5271103e+02, 9.4265050e-01],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[6.7577380e+02, 3.5269864e+02, 8.9672232e-01],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00]]
,[[7.2723553e+02, 4.0875150e+02, 8.3982950e-01],[7.6091986e+02, 4.6032086e+02, 5.0676465e-01],[7.3178253e+02, 4.5359366e+02, 3.5797939e-01],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[7.8784674e+02, 4.6483188e+02, 5.6356871e-01],[7.6320721e+02, 5.6802844e+02, 3.7939239e-01],[7.2953772e+02, 5.4564911e+02, 1.5424372e-01],[7.6546356e+02, 6.1964557e+02, 1.7308682e-01],[7.3854327e+02, 6.1513757e+02, 1.5351829e-01],[7.3855487e+02, 7.3405249e+02, 5.6986582e-02],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[7.8789227e+02, 6.2636108e+02, 1.8666090e-01],[7.9010718e+02, 7.5197815e+02, 9.0752751e-02],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[7.2722571e+02, 3.9980579e+02, 4.9854943e-01],[7.4074554e+02, 4.0420221e+02, 8.2562774e-01],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[7.6537799e+02, 4.0880304e+02, 6.8228495e-01],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00]] ,[[7.2723553e+02, 4.0875150e+02, 8.3982950e-01],[7.6091986e+02, 4.6032086e+02, 5.0676465e-01],[7.3178253e+02, 4.5359366e+02, 3.5797939e-01],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[7.8784674e+02, 4.6483188e+02, 5.6356871e-01],[7.6320721e+02, 5.6802844e+02, 3.7939239e-01],[7.2953772e+02, 5.4564911e+02, 1.5424372e-01],[7.6546356e+02, 6.1964557e+02, 1.7308682e-01],[7.3854327e+02, 6.1513757e+02, 1.5351829e-01],[7.3855487e+02, 7.3405249e+02, 5.6986582e-02],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[7.8789227e+02, 6.2636108e+02, 1.8666090e-01],[7.9010718e+02, 7.5197815e+02, 9.0752751e-02],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[7.2722571e+02, 3.9980579e+02, 4.9854943e-01],[7.4074554e+02, 4.0420221e+02, 8.2562774e-01],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[7.6537799e+02, 4.0880304e+02, 6.8228495e-01],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00],[0.0000000e+00, 0.0000000e+00, 0.0000000e+00]]
...@@ -122,38 +123,38 @@ for img_name, img_bdcn in (list(zip(images, images_bdcn))[15:16] if SKIP_OPENPOS ...@@ -122,38 +123,38 @@ for img_name, img_bdcn in (list(zip(images, images_bdcn))[15:16] if SKIP_OPENPOS
inpainted = cv2.medianBlur(inpainted,int(esz*5)+1 if int(esz*5)%2==0 else int(esz*5)) inpainted = cv2.medianBlur(inpainted,int(esz*5)+1 if int(esz*5)%2==0 else int(esz*5))
inpainted = cv2.bilateralFilter(inpainted,30,60,60) inpainted = cv2.bilateralFilter(inpainted,30,60,60)
Z = inpainted.reshape((-1,3))
Z = np.float32(Z) #to kmeans
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0) kmeans_out = imgKmeans(inpainted, KMEANS_K)
K = KMEANS_K
ret, label, center = cv2.kmeans(Z,K,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS)
center = np.uint8(center)
res = center[label.flatten()]
kmeans = res.reshape((inpainted.shape))
# cv2.imshow(img_name+"inpainted", inpainted) # cv2.imshow(img_name+"inpainted", inpainted)
# cv2.imshow(img_name+"mask", kmout_mask) # cv2.imshow(img_name+"mask", kmout_mask)
kmout_mask=cv2.cvtColor(kmout_mask,cv2.COLOR_GRAY2BGR) #change mask to a 3 channel image kmout_mask=cv2.cvtColor(kmout_mask,cv2.COLOR_GRAY2BGR) #change mask to a 3 channel image
km_mask_out=cv2.subtract(kmout_mask,kmeans) km_mask_out=cv2.subtract(kmout_mask,kmeans_out)
km_mask_out=cv2.subtract(kmout_mask,km_mask_out) km_mask_out=cv2.subtract(kmout_mask,km_mask_out)
#idea: count how many pixels the white mask has, count how many pixels each color from kmeans result appears in mask -> area > 80% is foreground #idea: count how many pixels the white mask has, count how many pixels each color from kmeans result appears in mask -> area > 80% is foreground
km_colors, km_counts = np.unique(km_mask_out.reshape(-1, km_mask_out.shape[-1]),axis=0,return_counts=True) kmeans_colors, kmeans_counts = np.unique(kmeans_out.reshape(-1, kmeans_out.shape[-1]),axis=0,return_counts=True)
km_mask_colors, km_mask_counts = np.unique(km_mask_out.reshape(-1, km_mask_out.shape[-1]),axis=0,return_counts=True)
mask_colors, mask_counts = np.unique(kmout_mask.reshape(-1, kmout_mask.shape[-1]),axis=0,return_counts=True) mask_colors, mask_counts = np.unique(kmout_mask.reshape(-1, kmout_mask.shape[-1]),axis=0,return_counts=True)
threshold_count = int(KMEANS_AREA_MASK_THRESHOLD*mask_counts[(np.argwhere(mask_colors>0))[0,0]]) #select counts from white color and generate threshold with it. threshold_count = int(KMEANS_AREA_MASK_THRESHOLD*mask_counts[(np.argwhere(mask_colors>0))[0,0]]) #select counts from white color and generate threshold with it.
km_colors = km_colors[km_counts.argsort()][::-1] # sort colors by count km_mask_colors = km_mask_colors[km_mask_counts.argsort()][::-1] # sort colors by count
km_counts = km_counts[km_counts.argsort()][::-1] # sort counts by count km_mask_counts = km_mask_counts[km_mask_counts.argsort()][::-1] # sort counts by count
filtered_km_colors = km_colors[np.argwhere(km_counts>threshold_count)] filtered_km_mask_FG_colors = km_mask_colors[np.argwhere(km_mask_counts>=threshold_count)]
filtered_km_colors = filtered_km_colors[np.sum(filtered_km_colors, axis=2)>0] #filter out black from mask filtered_km_mask_FG_colors = filtered_km_mask_FG_colors[np.sum(filtered_km_mask_FG_colors, axis=2)>0] #filter out black from mask
print(km_colors, km_counts) filtered_km_mask_BG_colors = km_mask_colors[np.argwhere(km_mask_counts<threshold_count)]
filtered_km_mask_BG_colors = filtered_km_mask_BG_colors[np.sum(filtered_km_mask_BG_colors, axis=2)>0] #filter out black from mask
print("kmeans_colors,kmeans_counts", kmeans_colors, kmeans_counts)
print(km_mask_colors, km_mask_counts)
print(mask_colors, mask_counts) print(mask_colors, mask_counts)
print(filtered_km_colors) print(filtered_km_mask_FG_colors)
print(len(km_colors)) print(len(km_mask_colors))
cv2.putText(km_mask_out, "Foreground colors:", (10,25), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255,255,255)) cv2.putText(km_mask_out, "Foreground colors:", (10,25), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255,255,255))
offset=220; offset=220;
for color in filtered_km_colors: for color in filtered_km_mask_FG_colors:
#print colors on screen #print colors on screen
print("color",color[0],color[1],color[2]) print("color",color[0],color[1],color[2])
x = offset x = offset
...@@ -161,18 +162,35 @@ for img_name, img_bdcn in (list(zip(images, images_bdcn))[15:16] if SKIP_OPENPOS ...@@ -161,18 +162,35 @@ for img_name, img_bdcn in (list(zip(images, images_bdcn))[15:16] if SKIP_OPENPOS
cv2.rectangle(km_mask_out, (x,y), (x+10,y+10), (int(color[0]),int(color[1]),int(color[2])), -1) cv2.rectangle(km_mask_out, (x,y), (x+10,y+10), (int(color[0]),int(color[1]),int(color[2])), -1)
offset += 15 offset += 15
#replace colors #replace colors
kmeans[((kmeans[:,:,0] == color[0]) & (kmeans[:,:,1] == color[1]) & (kmeans[:,:,2] == color[2]))]=filtered_km_colors[0] #search in kmeans image for specific color and replace all by first color # kmeans_out[((kmeans_out[:,:,0] == color[0]) & (kmeans_out[:,:,1] == color[1]) & (kmeans_out[:,:,2] == color[2]))]=filtered_km_mask_FG_colors[0] #search in kmeans image for specific color and replace all by first color
# kmeans[((kmeans[:,:,0] == color[0]) & (kmeans[:,:,1] == color[1]) & (kmeans[:,:,2] == color[2]))]=[0,0,255] #search in kmeans image for specific color and replace all by first color kmeans_out[((kmeans_out[:,:,0] == color[0]) & (kmeans_out[:,:,1] == color[1]) & (kmeans_out[:,:,2] == color[2]))]=[255,255,255] #search in kmeans_out image for specific color and replace all by first color
for color in filtered_km_mask_BG_colors:
kmeans_out[((kmeans_out[:,:,0] == color[0]) & (kmeans_out[:,:,1] == color[1]) & (kmeans_out[:,:,2] == color[2]))]=[0,0,0] #search in kmeans_out image for specific color and replace all by first color
for color in kmeans_colors[np.invert(np.isin(kmeans_colors, km_mask_colors).all(axis=1))]: #colors in kmean_out wich do not appear in any mask
kmeans_out[((kmeans_out[:,:,0] == color[0]) & (kmeans_out[:,:,1] == color[1]) & (kmeans_out[:,:,2] == color[2]))]=[0,0,0] #search in kmeans_out image for specific color and replace all by first color
#apply erosure/dilation morphing filters
# kmeans_out =
# cv2.imshow(img_name+"kmeans_masked", km_mask_out) # cv2.imshow(img_name+"kmeans_masked", km_mask_out)
#replace colors #replace colors
# kmeans = cv2.bilateralFilter(kmeans,30,60,60) # kmeans_out = cv2.medianBlur(kmeans_out,53)
# cv2.imshow(img_name+"kmeans", kmeans) # kmeans_out = cv2.bilateralFilter(kmeans_out,30,60,60)
# cv2.waitKey(0) # kmeans_bgfg = imgKmeans(kmeans_out, 3)
cv2.rectangle(kmeans, (0,0), (235,30), (0,0,0), -1) # cv2.namedWindow("kmeans_bgfg", cv2.WINDOW_NORMAL)
cv2.putText(kmeans, "Foreground color:", (10,25), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255,255,255)) # cv2.imshow("kmeans_bgfg", kmeans_bgfg)
cv2.rectangle(kmeans, (220,15), (230,25), (int(filtered_km_colors[0][0]),int(filtered_km_colors[0][1]),int(filtered_km_colors[0][2])), -1) cv2.namedWindow("kmeans_out", cv2.WINDOW_NORMAL)
cv2.rectangle(kmeans, (220,15), (230,25), (255,255,255), 1) cv2.imshow("kmeans_out", kmeans_out)
output_canvas = kmeans cv2.waitKey(0)
cv2.rectangle(kmeans_out, (0,0), (235,30), (0,0,0), -1)
cv2.putText(kmeans_out, "Foreground color:", (10,25), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255,255,255))
cv2.rectangle(kmeans_out, (220,15), (230,25), (int(filtered_km_mask_FG_colors[0][0]),int(filtered_km_mask_FG_colors[0][1]),int(filtered_km_mask_FG_colors[0][2])), -1)
cv2.rectangle(kmeans_out, (220,15), (230,25), (255,255,255), 1)
output_canvas = kmeans_out
if BDCN_EDGES: if BDCN_EDGES:
...@@ -278,9 +296,8 @@ for img_name, img_bdcn in (list(zip(images, images_bdcn))[15:16] if SKIP_OPENPOS ...@@ -278,9 +296,8 @@ for img_name, img_bdcn in (list(zip(images, images_bdcn))[15:16] if SKIP_OPENPOS
if SHOW_WINDOW: if SHOW_WINDOW:
cv2.namedWindow(img_name, cv2.WINDOW_NORMAL) cv2.namedWindow(img_name, cv2.WINDOW_NORMAL)
cv2.imshow(img_name, img) cv2.imshow(img_name, img)
if BDCN_EDGES: cv2.namedWindow("canvas", cv2.WINDOW_NORMAL)
cv2.namedWindow(img_bdcn, cv2.WINDOW_NORMAL) cv2.imshow("canvas", output_canvas)
cv2.imshow(img_bdcn, output_canvas)
cv2.waitKey(0) cv2.waitKey(0)
if SHOW_WINDOW: if SHOW_WINDOW:
cv2.waitKey(0) cv2.waitKey(0)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment