Unverified Commit 4b5b9716 authored by tilman's avatar tilman
Browse files

save cur params

parent e200b533
......@@ -17,8 +17,7 @@ from lib.misc import *
SHOW_WINDOW = True
SAVE_FILE = not SHOW_WINDOW
SAVE_BDCN = False
BDCN_EDGES = True
WHITE_OUTPUT_CANVAS = False
BDCN_EDGES = False
TRIANGLES = False
BODY_LINES = True
BISEC_VECTORS = False
......@@ -31,9 +30,9 @@ CORRECTION_ANGLE = 23
OVERLAY_ALPHA = 0.2
BISEC_SKIP_LOWER_LEVELS = False
DISPLAY_RASTER_ELEMENTS = 500
KMEANS_AREA_MASK_THRESHOLD = 0.1 #max 1.0 (percent of pixels)
KMEANS_K = 5 #10
OUT_DIR = 'images/out/images_imdahl/main_structure_final_canvas'
KMEANS_AREA_MASK_THRESHOLD = 0.09 #max 1.0 (percent of pixels), cur 0.09
KMEANS_K = 20 #10
OUT_DIR = 'images/out/images_imdahl/kmeans_output_canvas/new'
#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/"
......@@ -77,12 +76,13 @@ os.chdir(OUT_DIR) #save images in this dir
#for img_name in images:
# for img_name, img_bdcn in list(zip(images, images_bdcn)):
# for img_name, img_bdcn in list(zip(images, images_bdcn)):
for img_name, img_bdcn in list(zip(images, images_bdcn))[15:16]: #jesus
for img_name, img_bdcn in (list(zip(images, images_bdcn))[15:16] if SKIP_OPENPOSE else list(zip(images, images_bdcn))): #jesus
# for img_name, img_bdcn in list(zip(images, images_bdcn))[6:7]: #fusswaschung
# Process Image
print("calculating: "+img_name)
img = cv2.imread(img_name)
output_canvas = np.array([[[255,255,255]]*len(img[0])]*len(img),np.uint8) if WHITE_OUTPUT_CANVAS else cv2.imread(img_bdcn)
output_canvas = np.array([[[255,255,255]]*len(img[0])]*len(img),np.uint8)
bdcn_image = cv2.imread(img_bdcn)
max_lw = max(len(img),len(img[0]))
esz = max_lw / DISPLAY_RASTER_ELEMENTS
if SKIP_OPENPOSE:
......@@ -97,27 +97,31 @@ for img_name, img_bdcn in list(zip(images, images_bdcn))[15:16]: #jesus
if INPAINT_AND_KMEANS:
target = cv2.imread(img_name)
#remove cracks and prepare for kmeans
# target = cv2.GaussianBlur(target,(5,5),0) #unsharpen all
# target = cv2.medianBlur(target,33) #remove cracks with with 33 pixel
# target = cv2.bilateralFilter(target,33,90,90) #bring back edges and remove textures
# 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
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:
for pose in fposes: #remove bodys
convexhull = Polygon(pose).convex_hull
sconvexhull = affinity.scale(convexhull, xfact=1.6, yfact=1.6, origin=convexhull.centroid)
cv2.drawContours(mask, [polyToArr(sconvexhull)], 0, 255, int(10*esz))
cv2.drawContours(kmout_mask, [polyToArr(sconvexhull)], 0, 255, int(10*esz))
sconvexhull = affinity.scale(convexhull, xfact=1.7, yfact=2, 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)
cv2.drawContours(kmout_mask, [polyToArr(sconvexhull)], 0, 255, -1)
cv2.rectangle(mask, (0,0), (len(img[0]),len(img)), 255, int(40*esz))
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))
print("kmeans_med1")
inpainted = cv2.bilateralFilter(inpainted,30,60,60)
print("kmeans_bil1")
# inpainted = cv2.bilateralFilter(inpainted,30,60,60)
# print("kmeans_bil2")
# inpainted = cv2.bilateralFilter(inpainted,30,60,60)
# print("kmeans_bil3")
# inpainted = cv2.bilateralFilter(inpainted,30,60,60)
Z = inpainted.reshape((-1,3))
Z = np.float32(Z)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
......@@ -128,7 +132,7 @@ for img_name, img_bdcn in list(zip(images, images_bdcn))[15:16]: #jesus
kmeans = res.reshape((inpainted.shape))
# cv2.imshow(img_name+"inpainted", inpainted)
# 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,km_mask_out)
......@@ -137,6 +141,8 @@ for img_name, img_bdcn in list(zip(images, images_bdcn))[15:16]: #jesus
km_colors, km_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)
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_counts = km_counts[km_counts.argsort()][::-1] # sort counts by count
filtered_km_colors = km_colors[np.argwhere(km_counts>threshold_count)]
filtered_km_colors = filtered_km_colors[np.sum(filtered_km_colors, axis=2)>0] #filter out black from mask
print(km_colors, km_counts)
......@@ -148,18 +154,25 @@ for img_name, img_bdcn in list(zip(images, images_bdcn))[15:16]: #jesus
cv2.putText(km_mask_out, "Foreground colors:", (10,25), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255,255,255))
offset=220;
for color in filtered_km_colors:
#print colors on screen
print("color",color[0],color[1],color[2])
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
cv2.imshow(img_name+"kmeans_masked", km_mask_out)
#replace colors
cv2.imshow(img_name+"kmeans", kmeans)
cv2.waitKey(0)
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[((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
# cv2.imshow(img_name+"kmeans_masked", km_mask_out)
#replace colors
# kmeans = cv2.bilateralFilter(kmeans,30,60,60)
# cv2.imshow(img_name+"kmeans", kmeans)
# cv2.waitKey(0)
cv2.rectangle(kmeans, (0,0), (235,30), (0,0,0), -1)
cv2.putText(kmeans, "Foreground color:", (10,25), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255,255,255))
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.rectangle(kmeans, (220,15), (230,25), (255,255,255), 1)
output_canvas = kmeans
if BDCN_EDGES:
......@@ -173,22 +186,37 @@ for img_name, img_bdcn in list(zip(images, images_bdcn))[15:16]: #jesus
#cv2.line(img, (int(pose[0][0]),int(pose[0][1])), (int(pose[1][0]),int(pose[1][1])), (0,255,0), int(6*esz))
# cv2.drawContours(img, [polyToArr(sconvexhull)], 0, 255, int(10*esz))
# cv2.drawContours(img, [polyToArr(sconvexhull)], 0, 255, -1)
cv2.drawContours(output_canvas, [polyToArr(sconvexhull)], 0, (255,255,255), int(10*esz))
cv2.drawContours(output_canvas, [polyToArr(sconvexhull)], 0, (255,255,255), -1)
cv2.drawContours(bdcn_image, [polyToArr(sconvexhull)], 0, (255,255,255), int(10*esz))
cv2.drawContours(bdcn_image, [polyToArr(sconvexhull)], 0, (255,255,255), -1)
#draw rectangle
# cv2.rectangle(img, (0,0), (len(img[0]),len(img)), (255,0,0), int(40*esz))
cv2.rectangle(output_canvas, (0,0), (len(img[0]),len(img)), (255,255,255), int(40*esz))
cv2.rectangle(bdcn_image, (0,0), (len(img[0]),len(img)), (255,255,255), int(40*esz)) #eliminate rectangle from frames
output_canvas = cv2.medianBlur(output_canvas,int(esz*5)+1 if int(esz*5)%2==0 else int(esz*5))
blurred = cv2.medianBlur(bdcn_image,int(esz*5)+1 if int(esz*5)%2==0 else int(esz*5))
print("med1")
output_canvas = cv2.bilateralFilter(output_canvas,30,60,60)
blurred = cv2.bilateralFilter(blurred,30,60,60)
print("bil1")
output_canvas = cv2.bilateralFilter(output_canvas,30,60,60)
blurred = cv2.bilateralFilter(blurred,30,60,60)
print("bil2")
output_canvas = cv2.bilateralFilter(output_canvas,30,60,60)
blurred = cv2.bilateralFilter(blurred,30,60,60)
print("bil3")
output_canvas = cv2.bilateralFilter(output_canvas,30,60,60)
blurred = cv2.bilateralFilter(blurred,30,60,60)
print("bil4")
#output_canvas = overlay_two_image_v2(output_canvas, blurred, [0,0,0], 0.5)
#output_canvas = cv2.addWeighted(blurred, 0.2, output_canvas, 0.8, 0.0)
# cv2.imshow(img_name+"blurred", blurred)
rows,cols,channels = blurred.shape
roi = output_canvas[0:rows, 0:cols ]
img2gray = cv2.cvtColor(blurred,cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 100, 255, cv2.THRESH_BINARY_INV)
mask_inv = cv2.bitwise_not(mask)
# cv2.imshow(img_name+"mask_inv", mask_inv)
output_canvas = cv2.bitwise_and(roi,roi,mask = mask_inv)
if TRIANGLES or BODY_LINES:
triangles = [poseToTriangle(pose) for pose in datum.poseKeypoints]
......@@ -246,15 +274,14 @@ for img_name, img_bdcn in list(zip(images, images_bdcn))[15:16]: #jesus
if SAVE_FILE:
cv2.imwrite(os.path.basename(img_name),img)
if BDCN_EDGES:
cv2.imwrite('CANVAS_'+os.path.basename(img_bdcn),output_canvas)
cv2.imwrite(os.path.basename(img_bdcn)+'canvas_cracksNewFilterd_ne2.jpg',output_canvas)
if SHOW_WINDOW:
cv2.namedWindow(img_name, cv2.WINDOW_NORMAL)
cv2.imshow(img_name, img)
if BDCN_EDGES:
cv2.namedWindow(img_bdcn, cv2.WINDOW_NORMAL)
cv2.imshow(img_bdcn, output_canvas)
# cv2.waitKey(0)
cv2.waitKey(0)
if SHOW_WINDOW:
cv2.waitKey(0)
cv2.destroyAllWindows()
\ No newline at end of file
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