Unverified Commit c7f19c9c authored by tilman's avatar tilman
Browse files

working good, results in new_smaller_mask

parent 72b39436
......@@ -31,9 +31,9 @@ CORRECTION_ANGLE = 23
OVERLAY_ALPHA = 0.2
BISEC_SKIP_LOWER_LEVELS = False
DISPLAY_RASTER_ELEMENTS = 500
KMEANS_AREA_MASK_THRESHOLD = 0.09 #max 1.0 (percent of pixels), cur 0.09
KMEANS_K = 10 #10
OUT_DIR = 'images/out/images_imdahl/kmeans_output_canvas/new2'
KMEANS_AREA_MASK_THRESHOLD = 0.05 #max 1.0 (percent of pixels), cur 0.09
KMEANS_K = 7 #10
OUT_DIR = 'images/out/images_imdahl/kmeans_output_canvas/new_smaller_mask'
#IN_DIR = "images/first_email/" # images from first email
IN_DIR = "images/images_imdahl/" # images from imdahl
IN_DIR_BDCN = "images/out/images_imdahl/bdcn_output/"
......@@ -115,13 +115,13 @@ for img_name, img_bdcn in (list(zip(images, images_bdcn))[15:16] if SKIP_OPENPOS
for pose in fposes: #remove bodys
convexhull = Polygon(pose).convex_hull
#inpainting
sconvexhull = affinity.scale(convexhull, xfact=1.4, yfact=1.8, origin=convexhull.centroid)
cv2.drawContours(mask, [polyToArr(sconvexhull)], 0, 255, int(20*esz))
sconvexhull = affinity.scale(convexhull, xfact=1.3, yfact=1.6, origin=convexhull.centroid)
cv2.drawContours(mask, [polyToArr(sconvexhull)], 0, 255, int(15*esz))
cv2.drawContours(mask, [polyToArr(sconvexhull)], 0, 255, -1)
#kmeans check
sconvexhull = affinity.scale(convexhull, xfact=1.1, yfact=1.1, origin=convexhull.centroid)
cv2.drawContours(kmout_mask, [polyToArr(sconvexhull)], 0, 255, int(10*esz))
sconvexhull = affinity.scale(convexhull, xfact=0.7, yfact=0.7, origin=convexhull.centroid)
cv2.drawContours(kmout_mask, [polyToArr(sconvexhull)], 0, 255, int(7*esz))
cv2.drawContours(kmout_mask, [polyToArr(sconvexhull)], 0, 255, -1)
cv2.rectangle(mask, (0,0), (len(img[0]),len(img)), 255, int(40*esz)) #remove frames
inpainted = cv2.inpaint(target, mask, 3, cv2.INPAINT_TELEA)
......@@ -147,8 +147,11 @@ for img_name, img_bdcn in (list(zip(images, images_bdcn))[15:16] if SKIP_OPENPOS
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_mask_colors = km_mask_colors[km_mask_counts.argsort()][::-1] # sort colors by count
km_mask_counts = km_mask_counts[km_mask_counts.argsort()][::-1] # sort counts by count
kmeans_colors = kmeans_colors[kmeans_counts.argsort()][::-1] # sort kmeans_counts by count
kmeans_counts = kmeans_counts[kmeans_counts.argsort()][::-1] # sort kmeans_counts by count
km_mask_colors = km_mask_colors[km_mask_counts.argsort()][::-1] # sort mask colors by count
km_mask_counts = km_mask_counts[km_mask_counts.argsort()][::-1] # sort mask counts by count
filtered_km_mask_FG_colors = km_mask_colors[np.argwhere(km_mask_counts>=threshold_count)]
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
......@@ -156,8 +159,16 @@ for img_name, img_bdcn in (list(zip(images, images_bdcn))[15:16] if SKIP_OPENPOS
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
cv2.putText(km_mask_out, "Foreground colors:", (10,25), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255,255,255))
kmeans_temp_save = kmeans_out.copy()
cv2.putText(km_mask_out, "Foreground colors:", (10,25), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255,255,255))
cv2.rectangle(kmeans_temp_save, (0,0), (235,30), (0,0,0), -1)
cv2.putText(kmeans_temp_save, "Foreground color:", (10,25), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255,255,255))
cv2.rectangle(kmeans_temp_save, (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_temp_save, (220,15), (230,25), (255,255,255), 1)
cv2.imwrite(os.path.basename(img_bdcn)+'_km_mask_out_before.jpg',kmeans_temp_save)
offset=220;
for color in filtered_km_mask_FG_colors:
......@@ -165,25 +176,32 @@ for img_name, img_bdcn in (list(zip(images, images_bdcn))[15:16] if SKIP_OPENPOS
print("filtered_km_mask_FG_colors",color)
x = offset
y = 15
cv2.rectangle(km_mask_out, (x,y), (x+10,y+10), (int(color[0]),int(color[1]),int(color[2])), -1)
offset += 15
#replace colors
# 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_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
kmeans_temp_save[((kmeans_temp_save[:,:,0] == color[0]) & (kmeans_temp_save[:,:,1] == color[1]) & (kmeans_temp_save[:,:,2] == color[2]))]=filtered_km_mask_FG_colors[0] #search in kmeans_temp_save image for specific color and replace all by first color
km_mask_out[((km_mask_out[:,:,0] == color[0]) & (km_mask_out[:,:,1] == color[1]) & (km_mask_out[:,:,2] == color[2]))]=filtered_km_mask_FG_colors[0] #search in kmeans_out image for specific color and replace all by first color
# km_mask_out[((km_mask_out[:,:,0] == color[0]) & (km_mask_out[:,:,1] == color[1]) & (km_mask_out[:,:,2] == color[2]))]=filtered_km_mask_FG_colors[0] #search in kmeans_out image for specific color and replace all by first color
cv2.rectangle(km_mask_out, (x,y), (x+10,y+10), (int(color[0]),int(color[1]),int(color[2])), -1)
for color in filtered_km_mask_BG_colors:
print("filtered_km_mask_BG_colors",color)
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
kmeans_out[((kmeans_out[:,:,0] == color[0]) & (kmeans_out[:,:,1] == color[1]) & (kmeans_out[:,:,2] == color[2]))]=filtered_km_mask_BG_colors[0] #search in kmeans_out image for specific color and replace all by first color
km_mask_out[((km_mask_out[:,:,0] == color[0]) & (km_mask_out[:,:,1] == color[1]) & (km_mask_out[:,:,2] == color[2]))]=filtered_km_mask_BG_colors[0] #search in kmeans_out 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_BG_colors[0] #search in kmeans_out image for specific color and replace all by first color
# km_mask_out[((km_mask_out[:,:,0] == color[0]) & (km_mask_out[:,:,1] == color[1]) & (km_mask_out[:,:,2] == color[2]))]=filtered_km_mask_BG_colors[0] #search in kmeans_out image for specific color and replace all by first color
colors_only_BG = 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
for color in colors_only_BG:
print("colors_only_BG",color)
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
kmeans_temp_save[((kmeans_temp_save[:,:,0] == color[0]) & (kmeans_temp_save[:,:,1] == color[1]) & (kmeans_temp_save[:,:,2] == color[2]))]=colors_only_BG[0] #search in kmeans_temp_save image for specific color and replace all by first color
km_mask_out[((km_mask_out[:,:,0] == color[0]) & (km_mask_out[:,:,1] == color[1]) & (km_mask_out[:,:,2] == color[2]))]=colors_only_BG[0] #search in kmeans_out image for specific color and replace all by first color
# kmeans_temp_save[((kmeans_temp_save[:,:,0] == color[0]) & (kmeans_temp_save[:,:,1] == color[1]) & (kmeans_temp_save[:,:,2] == color[2]))]=colors_only_BG[0] #search in kmeans_temp_save image for specific color and replace all by first color
# km_mask_out[((km_mask_out[:,:,0] == color[0]) & (km_mask_out[:,:,1] == color[1]) & (km_mask_out[:,:,2] == color[2]))]=colors_only_BG[0] #search in kmeans_out image for specific color and replace all by first color
#remove small kmeans fragments
kmeans_temp_save = cv2.medianBlur(kmeans_temp_save,7)
cv2.rectangle(kmeans_temp_save, (0,0), (235,30), (0,0,0), -1)
cv2.putText(kmeans_temp_save, "Foreground color:", (10,25), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255,255,255))
cv2.rectangle(kmeans_temp_save, (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_temp_save, (220,15), (230,25), (255,255,255), 1)
cv2.imwrite(os.path.basename(img_bdcn)+'_kmeans_temp_save_after.jpg',kmeans_temp_save)
cv2.imwrite(os.path.basename(img_bdcn)+'_km_mask_out.jpg',km_mask_out)
# cv2.namedWindow("kmeans_out", cv2.WINDOW_NORMAL)
......@@ -193,10 +211,11 @@ for img_name, img_bdcn in (list(zip(images, images_bdcn))[15:16] if SKIP_OPENPOS
#apply erosure/dilation morphing filters
# kernel = np.ones((5,5),np.uint8)
kmeans_out = cv2.medianBlur(kmeans_out,int(esz*60)+1 if int(esz*60)%2==0 else int(esz*60))
#reprint text because medianBlur destroys it
# cv2.imshow("kmeans_out", kmeans_out)
# cv2.waitKey(0)
kmeans_out = cv2.medianBlur(kmeans_out,int(esz*60)+1 if int(esz*60)%2==0 else int(esz*60))
kernel = np.array([
[0,0,1,0,0],
[0,1,1,1,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