Unverified Commit 72b39436 authored by tilman's avatar tilman
Browse files

Sonntag morgen

parent 6342aad6
......@@ -15,7 +15,7 @@ from lib.bisection import *
from lib.misc import *
from lib.kmeans import *
SHOW_WINDOW = True
SHOW_WINDOW = False
SAVE_FILE = not SHOW_WINDOW
SAVE_BDCN = False
BDCN_EDGES = False
......@@ -33,7 +33,7 @@ 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/new'
OUT_DIR = 'images/out/images_imdahl/kmeans_output_canvas/new2'
#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/"
......@@ -105,23 +105,29 @@ for img_name, img_bdcn in (list(zip(images, images_bdcn))[15:16] if SKIP_OPENPOS
# target = cv2.bilateralFilter(target,13,90,90) #bring back edges and remove textures
# target = cv2.medianBlur(target,53) #remove big color patches
# target = cv2.bilateralFilter(target,13,90,90) #bring back edges and remove textures
target = cv2.medianBlur(target,11) #smoothen cracks
target = cv2.bilateralFilter(target,60,90,90) #remove cracks
#target = cv2.medianBlur(target,11) #smoothen cracks
target = cv2.medianBlur(target,int(esz*5)+1 if int(esz*5)%2==0 else int(esz*5)) #smoothen cracks
target = cv2.bilateralFilter(target,int(esz*25),int(esz*40),int(esz*40)) #remove cracks
fposes = np.array([np.array([line[:2] for line in pose if line[2] > 0]) for pose in datum.poseKeypoints]) #filtered poses without zero lines
mask = np.zeros((len(img),len(img[0]),1), np.uint8)
kmout_mask = np.zeros((len(img),len(img[0]),1), np.uint8)
for pose in fposes: #remove bodys
convexhull = Polygon(pose).convex_hull
sconvexhull = affinity.scale(convexhull, xfact=1.7, yfact=2, origin=convexhull.centroid)
#inpainting
sconvexhull = affinity.scale(convexhull, xfact=1.4, yfact=1.8, origin=convexhull.centroid)
cv2.drawContours(mask, [polyToArr(sconvexhull)], 0, 255, int(20*esz))
cv2.drawContours(kmout_mask, [polyToArr(sconvexhull)], 0, 255, int(20*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))
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)
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.medianBlur(inpainted,int(esz*5)+1 if int(esz*5)%2==0 else int(esz*5))
# inpainted = cv2.bilateralFilter(inpainted,30,40,40)
cv2.imwrite(os.path.basename(img_bdcn)+'_inpainted3.jpg',inpainted)
#to kmeans
......@@ -135,28 +141,28 @@ for img_name, img_bdcn in (list(zip(images, images_bdcn))[15:16] if SKIP_OPENPOS
#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
kmeans_colors, kmeans_counts = np.unique(kmeans_out.reshape(-1, kmeans_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.
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
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
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(filtered_km_mask_FG_colors)
print(len(km_mask_colors))
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.imwrite(os.path.basename(img_bdcn)+'_km_mask_out_before.jpg',kmeans_temp_save)
offset=220;
for color in filtered_km_mask_FG_colors:
#print colors on screen
print("color",color[0],color[1],color[2])
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)
......@@ -164,22 +170,32 @@ for img_name, img_bdcn in (list(zip(images, images_bdcn))[15:16] if SKIP_OPENPOS
#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
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
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]))]=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
cv2.namedWindow("kmeans_out", cv2.WINDOW_NORMAL)
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)
cv2.imshow("kmeans_out", kmeans_out)
cv2.waitKey(0)
# cv2.imshow("kmeans_out", kmeans_out)
# cv2.waitKey(0)
#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))
cv2.imshow("kmeans_out", kmeans_out)
cv2.waitKey(0)
# cv2.imshow("kmeans_out", kmeans_out)
# cv2.waitKey(0)
kernel = np.array([
[0,0,1,0,0],
......@@ -188,20 +204,20 @@ for img_name, img_bdcn in (list(zip(images, images_bdcn))[15:16] if SKIP_OPENPOS
[0,1,1,1,0],
[0,0,0.5,0,0]], dtype=np.uint8)
kmeans_out = cv2.erode(kmeans_out,kernel,iterations = 12)
cv2.imshow("kmeans_out", kmeans_out)
cv2.waitKey(0)
# cv2.imshow("kmeans_out", kmeans_out)
# cv2.waitKey(0)
kmeans_out = cv2.dilate(kmeans_out,kernel,iterations = 15)
cv2.imshow("kmeans_out", kmeans_out)
cv2.waitKey(0)
# 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))
cv2.imshow("kmeans_out", kmeans_out)
cv2.waitKey(0)
# cv2.imshow("kmeans_out", kmeans_out)
# cv2.waitKey(0)
kmeans_out = cv2.dilate(kmeans_out,kernel,iterations = 14)
cv2.imshow("kmeans_out", kmeans_out)
cv2.waitKey(0)
# cv2.imshow("kmeans_out", kmeans_out)
# cv2.waitKey(0)
kmeans_out = cv2.erode(kmeans_out,kernel,iterations = 8)
cv2.imshow("kmeans_out", kmeans_out)
cv2.waitKey(0)
# cv2.imshow("kmeans_out", kmeans_out)
# cv2.waitKey(0)
# kmeans_out =
# cv2.imshow(img_name+"kmeans_masked", km_mask_out)
......@@ -317,7 +333,7 @@ for img_name, img_bdcn in (list(zip(images, images_bdcn))[15:16] if SKIP_OPENPOS
if SAVE_FILE:
cv2.imwrite(os.path.basename(img_name),img)
cv2.imwrite(os.path.basename(img_bdcn)+'canvas_cracksNewFilterd_ne2.jpg',output_canvas)
cv2.imwrite(os.path.basename(img_bdcn)+'_canvas_erosureDilate2.jpg',output_canvas)
if SHOW_WINDOW:
cv2.namedWindow(img_name, cv2.WINDOW_NORMAL)
cv2.imshow(img_name, img)
......
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