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

add cones and angles

parent 9c8b40f5
#np.array([[[-842,975],[-960,467],[ 536,386],[-842,975]],[[-685, 1038],[-823,535],[ 669,397],[-685, 1038]],[[-524, 1232],[-
from shapely.geometry import Polygon
import numpy as np
import itertools
......@@ -8,7 +7,7 @@ from lib.bisection import *
def overlay_two_image_v2(image, overlay, ignore_color=[0,0,0]):
ignore_color = np.asarray(ignore_color)
mask = (overlay==ignore_color).all(-1,keepdims=True)
out = np.where(mask,image,(image * 0.5 + overlay * 0.5).astype(image.dtype))
out = np.where(mask,image,(image * 0.2 + overlay * 0.2).astype(image.dtype))
return out
bisecCones = np.array([
......@@ -41,12 +40,16 @@ def overlay_two_image_v2(image, overlay, ignore_color=[0,0,0]):
return out
intersections = coneIntersections(bisecCones)
maxlevel = max(map(lambda t: len(t), intersections.keys()))
for combi in intersections:
#overlay = orig.copy()
is_not_last_level = len(combi) < maxlevel
overlay = np.zeros((len(img),len(img[0]),3), np.uint8)
color = min(((len(combi)-2)*150,255))
cv2.drawContours(overlay, [polyToArr(intersections[combi])], 0, (color,0,255), -1)
#cv2.addWeighted(overlay, 0.5, img, 1 - 0.5, 0, img)
if not is_not_last_level: #draw centroid of last polygon
xy = (int(intersections[combi].centroid.x),int(intersections[combi].centroid.y))
cv2.circle(overlay, xy, 10, (0,255,0), -1)
img = overlay_two_image_v2(img, overlay, [0,0,0])
......
......@@ -16,18 +16,21 @@ SAVE_FILE = False
TRIANGLES = True
BISEC_VECTORS = True
BISEC_CONES = True
GLOBAL_LINE = True
BISEC_CONE_ANGLE = 40
CORRECTION_ANGLE = 20
OVERLAY_ALPHA = 0.2
OUT_DIR = 'images/out/images_imdahl/'
BISEC_SKIP_LOWER_LEVELS = False
OUT_DIR = 'images/out/images_imdahl/main_structure_cones'
#IN_DIR = "images/first_email/" # images from first email
IN_DIR = "images/images_imdahl/" # images from imdahl
## misc functions
def overlay_two_image_v2(image, overlay, ignore_color=[0,0,0]):
def overlay_two_image_v2(image, overlay, ignore_color=[0,0,0], alpha=0.1):
ignore_color = np.asarray(ignore_color)
mask = (overlay==ignore_color).all(-1,keepdims=True)
out = np.where(mask,image,(image * (1-OVERLAY_ALPHA) + overlay * OVERLAY_ALPHA).astype(image.dtype))
out = np.where(mask,image,(image * (1-alpha) + overlay * alpha).astype(image.dtype))
return out
## misc functions end
......@@ -54,30 +57,43 @@ for img_name in images:
datum.cvInputData = img
opWrapper.emplaceAndPop([datum])
img = datum.cvOutputData
overlay = img.copy()
# img = datum.cvOutputData
#overlay = img.copy()
#print("poses:"+str(len(datum.poseKeypoints)))
if TRIANGLES:
triangles = [poseToTriangle(pose) for pose in datum.poseKeypoints]
for triangle in triangles:
if triangle is not None:
cv2.drawContours(overlay, [triangle], 0, 255, -1)
cv2.drawContours(img, [triangle], 0, 255, -1)
if BISEC_VECTORS:
bisecVectors = [poseToBisectVector(pose) for pose in datum.poseKeypoints]
bisecVectors = [poseToBisectVector(pose, CORRECTION_ANGLE) for pose in datum.poseKeypoints]
for bisecVector in bisecVectors:
if bisecVector is not None:
cv2.arrowedLine(overlay, trp(bisecVector[1]), trp(bisecVector[0]), (0,0,255), 2)
cv2.arrowedLine(img, trp(bisecVector[1]), trp(bisecVector[0]), (0,0,255), 2)
if BISEC_CONES:
bisecCones = [poseToBisectCone(pose, BISEC_CONE_ANGLE) for pose in datum.poseKeypoints]
bisecCones = [poseToBisectCone(pose, BISEC_CONE_ANGLE, CORRECTION_ANGLE) for pose in datum.poseKeypoints]
bisecCones = [v for v in bisecCones if v] #remove None values
#print([polyToArr(bisecCone) for bisecCone in bisecCones])
intersections = coneIntersections(bisecCones)
maxlevel = max(map(lambda t: len(t), intersections.keys()))
for combi in intersections:
overlay = np.zeros((len(img),len(img[0]),3), np.uint8)
color = min(((len(combi)-1)*150,255))
cv2.drawContours(overlay, [polyToArr(intersections[combi])], 0, (color,0,255), -1)
img = overlay_two_image_v2(img, overlay, [0,0,0])
is_not_last_level = len(combi) < maxlevel
if is_not_last_level and BISEC_SKIP_LOWER_LEVELS:
continue;
else:
overlay = np.zeros((len(img),len(img[0]),3), np.uint8)
color = min(((len(combi)-1)*150,255))
cv2.drawContours(overlay, [polyToArr(intersections[combi])], 0, (color,0,(0 if is_not_last_level else 255)), -1)
if not is_not_last_level and GLOBAL_LINE: #draw centroid of last polygon
xy = (int(intersections[combi].centroid.x),int(intersections[combi].centroid.y))
cv2.circle(overlay, xy, 10, (0,255,0), -1)
global_angle = getGlobalLineAngle(datum.poseKeypoints, CORRECTION_ANGLE)
dist = 1000
d = (int(dist * np.cos(global_angle)), int(dist * np.sin(global_angle))) #with origin zero
cv2.line(overlay, xy, (xy[0]+d[0],xy[1]+d[1]), (0,0,255), 2)
img = overlay_two_image_v2(img, overlay, [0,0,0], (OVERLAY_ALPHA if is_not_last_level else 0.9))
if SAVE_FILE:
cv2.imwrite(os.path.basename(img_name),img)
if SHOW_WINDOW:
......
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