python - match template which crop non-rectangular region and get wrong -
the code here can crop image choose.
i want match template image , roi image,but code can crop non-rectangular region.(other area black.)
like picture,you can see black area.
so when match image roi image,i wrong.
any idea ?
# -*- coding: utf-8 -*- import cv2 import numpy np drawing = false ix,iy = -1,-1 def getimage(drawline): print drawline image = cv2.imread('op1.jpg', -1) mask = np.zeros(image.shape, dtype=np.uint8) roi_corners = np.array([drawline], dtype=np.int32) channel_count = image.shape[2] ignore_mask_color = (255,)*channel_count cv2.fillpoly(mask, roi_corners, ignore_mask_color) masked_image = cv2.bitwise_and(image, mask) gti(masked_image) def gti(masked_image): iii = 0 while not np.sum(masked_image[iii,:,:]): resized_top = masked_image[iii+1:,:,:] iii = iii + 1 size_img = resized_top.shape iii = size_img[0] while not np.sum(resized_top[iii-2:iii-1,:,:]): resized_bottom = resized_top[0:iii-1,:,:] iii = iii - 1 iii = 0 while not np.sum(resized_bottom[:,iii,:]): resized_left = resized_bottom[:,iii+1:,:] iii = iii + 1 size_img = resized_left.shape iii = size_img[1] while not np.sum(resized_left[:,iii-2:iii-1,:]): resized_right = resized_left[:,0:iii-1:,:] iii = iii - 1 cv2.imshow('masked image', resized_right) cv2.waitkey() cv2.destroyallwindows() def draw_circle(event,x,y,flags,param): global ix,iy,drawing,temp,point if event == cv2.event_lbuttondown: if point == []: point.append((x,y)) drawing = true elif event == cv2.event_mousemove: if drawing == true: cv2.line(temp,point[-1],(x,y),(0,255,0),5) cv2.imshow("image",temp) temp = img.copy() elif event == cv2.event_lbuttonup: if drawing == true: point.append((x,y)) if abs(point[0][0]-point[-1][0]) + abs(point[0][1]-point[-1][1]) < 30: drawing = false cv2.line(img,point[-2],point[-1],(0,255,0),5) point[-1] = point[0] getimage(point) else: cv2.line(img,point[-2],point[-1],(0,255,0),5) cv2.imshow("image",img) img = cv2.imread("op1.jpg") cv2.imshow("image",img) cv2.setmousecallback('image',draw_circle) global temp,point point = [] temp = img.copy() while(1): k = cv2.waitkey(1) & 0xff if k == ord('m'): mode = not mode elif k == 27: cv2.destroyallwindows() break
Comments
Post a Comment