Unverified Commit 6884a3f2 authored by tilman's avatar tilman
Browse files

moved kmeans res mask downwards and deleted bdcn

parent 5a517402
......@@ -17,8 +17,6 @@ from lib.kmeans import *
SHOW_WINDOW = False
SAVE_FILE = not SHOW_WINDOW
SAVE_BDCN = False
BDCN_EDGES = False
TRIANGLES = False
BODY_LINES = True
BISEC_VECTORS = False
......@@ -32,13 +30,12 @@ OVERLAY_ALPHA = 0.2
COLORED_CANVAS = True
BISEC_SKIP_LOWER_LEVELS = False
DISPLAY_RASTER_ELEMENTS = 500
KMEANS_AREA_MASK_THRESHOLD = 0.08 #0.08 #max 1.0 (percent of pixels), cur 0.09, sonntag mittag 0.05, #smaller threshold -> more colors, higher threshold -> less colors
KMEANS_AREA_MASK_THRESHOLD = 0.04 #0.08 #max 1.0 (percent of pixels), cur 0.09, sonntag mittag 0.05, #smaller threshold -> more colors, higher threshold -> less colors
KMEANS_K = 7 #10
#KMEANS_K = 6 #10
OUT_DIR = 'images/out/images_imdahl/kmeans_output_canvas/final'
#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/"
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]]
......@@ -71,21 +68,19 @@ if not SKIP_OPENPOSE:
opWrapper.start()
images = [os.path.join(os.getcwd(), IN_DIR, f) for f in os.listdir(IN_DIR)] #make path absolute so os.chdir has no side effects
images_bdcn = [os.path.join(os.getcwd(), IN_DIR_BDCN, f) for f in os.listdir(IN_DIR_BDCN)]
images.sort()
images_bdcn.sort()
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))[0:1]:
# for img_name in images[0:1]:
for img_name in images:
# 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)
bdcn_image = cv2.imread(img_bdcn)
max_lw = max(len(img),len(img[0]))
esz = max_lw / DISPLAY_RASTER_ELEMENTS
if SKIP_OPENPOSE:
......@@ -116,6 +111,7 @@ for img_name, img_bdcn in list(zip(images, images_bdcn))[0:1]:
#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
print("filter vals: med",int(esz*5)+1 if int(esz*5)%2==0 else int(esz*5), "bil1", int(esz*25), "bil2,3", int(esz*40))
cv2.imwrite(os.path.basename(img_name)+'_inkm_step1_crackremoval.jpg',target)
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
......@@ -130,10 +126,18 @@ for img_name, img_bdcn in list(zip(images, images_bdcn))[0:1]:
cv2.drawContours(mask, [polyToArr(sconvexhull)], 0, 255, -1)
#kmeans check
sconvexhull = affinity.scale(convexhull, xfact=1, yfact=0.5, origin=convexhull.centroid)
sconvexhull = affinity.scale(convexhull, xfact=1, 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
#shift kmeans mask pixels downwards 40px
# shift = -40
# for i in range(kmout_mask.shape[0] -1, kmout_mask.shape[0] - shift, -1):
# kmout_mask = np.roll(kmout_mask, -1, axis=0)
# kmout_mask[-1, :] = 0
kmout_mask = cv2.warpAffine(kmout_mask, np.float32([ [1,0,0], [0,1,30] ]), (kmout_mask.shape[:2][1], kmout_mask.shape[:2][0]))
cv2.imwrite(os.path.basename(img_name)+'_inkm_step2_inpaintmask.jpg',mask)
cv2.imwrite(os.path.basename(img_name)+'_inkm_step2_kmeansresmask.jpg',kmout_mask)
inpainted = cv2.inpaint(target, mask, 3, cv2.INPAINT_TELEA)
......@@ -293,46 +297,6 @@ for img_name, img_bdcn in list(zip(images, images_bdcn))[0:1]:
print("WARNING: skipped fg/bg calc")
if BDCN_EDGES:
#overlay = np.array([[[255,255,255]]*len(img[0])]*len(img),np.uint8) #white image canvas
#output_canvas = overlay_two_image_v2(output_canvas, overlay, [0,0,0], 0.2)
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
for pose in fposes:
convexhull = Polygon(pose).convex_hull
sconvexhull = affinity.scale(convexhull, xfact=1.6, yfact=1.6, origin=convexhull.centroid)
# print(convexhull)
#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(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(bdcn_image, (0,0), (len(img[0]),len(img)), (255,255,255), int(40*esz)) #eliminate rectangle from frames
blurred = cv2.medianBlur(bdcn_image,int(esz*5)+1 if int(esz*5)%2==0 else int(esz*5))
print("med1")
blurred = cv2.bilateralFilter(blurred,30,60,60)
print("bil1")
blurred = cv2.bilateralFilter(blurred,30,60,60)
print("bil2")
blurred = cv2.bilateralFilter(blurred,30,60,60)
print("bil3")
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)
......@@ -393,15 +357,15 @@ for img_name, img_bdcn in list(zip(images, images_bdcn))[0:1]:
if SAVE_FILE:
#cv2.imwrite(os.path.basename(img_name),img)
if COLORED_CANVAS:
cv2.imwrite(os.path.basename(img_bdcn)+'_final_colored_canvas.jpg',output_canvas)
cv2.imwrite(os.path.basename(img_name)+'_final_colored_canvas.jpg',output_canvas)
print("saved _final_colored_canvas")
else:
cv2.imwrite(os.path.basename(img_bdcn)+'_final_binary_canvas.jpg',output_canvas)
cv2.imwrite(os.path.basename(img_name)+'_final_binary_canvas.jpg',output_canvas)
if SHOW_WINDOW:
cv2.namedWindow(img_name, cv2.WINDOW_NORMAL)
cv2.imshow(img_name, img)
cv2.namedWindow(img_bdcn+"canvas", cv2.WINDOW_NORMAL)
cv2.imshow(img_bdcn+"canvas", output_canvas)
cv2.namedWindow(img_name+"canvas", cv2.WINDOW_NORMAL)
cv2.imshow(img_name+"canvas", output_canvas)
cv2.waitKey(0)
if SHOW_WINDOW:
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