i trying place png image image of hat on head of webcam feed.
import cv2 import numpy np face_cascade = cv2.cascadeclassifier('haarcascades/haarcascade_frontalface_default.xml') # load overlay image: hat.png imghat = cv2.imread('hat2.png', -1) print imghat none # create mask hat imghatgray = cv2.cvtcolor(imghat, cv2.color_bgr2gray) #cv2.imwrite("imghatgray.png", imghatgray) ret, orig_mask = cv2.threshold(imghatgray, 0, 255, cv2.thresh_binary) #cv2.imwrite("orig_mask.png", orig_mask) # create inverted mask hat orig_mask_inv = cv2.bitwise_not(orig_mask) #cv2.imwrite("orig_mask_inv.png", orig_mask_inv) # convert hat image bgr # , save original image size (used later when re-sizing image) imghat = imghat[:,:,0:3] orighatheight, orighatwidth = imghat.shape[:2] video_capture = cv2.videocapture(0) while true: ret, frame = video_capture.read() gray = cv2.cvtcolor(frame, cv2.color_bgr2gray) faces = face_cascade.detectmultiscale(gray, 1.3, 5, flags=cv2.cv.cv_haar_scale_image) (x, y, w, h) in faces: print "x : %d , y : %d, w: %d, h: %d " %(x,y,w,h) cv2.rectangle(frame, (x,y), (x+w, y+h), (255,0,0), 2) #cv2.rectangle(frame, (x-15,y-h), (x+w+15, y), (255,255,0), 2) hatwidth = w hatheight = hatwidth * orighatheight / orighatwidth roi_gray = gray[y:y+h, x:x+w] roi_color = frame[y:y+h, x:x+w] # center hat x1 = x - 15 y1 = y - h x2 = x1 + w + 30 y2 = y1 + h #cv2.rectangle(frame, (x1,y1), (x2, y2), (0,255,0), 2) # check clipping if x1 < 0: x1 = 0 if y1 < 0: y1 = 0 if x2 > 640: x2 = w if y2 > 360: y2 = h print "x1: %d , y1 : %d, x2: %d, y2: %d " %(x1,y1,x2,y2) # re-calculate width , height of hat image hatwidth = x2 - x1 hatheight = y2 - y1 cv2.rectangle(frame, (x1,y1), (x2, y2), (255,255,0), 2) print "hatwidth: %d, hatheight: %d" %(hatwidth, hatheight) # re-size original image , masks hat sizes # calcualted above hat = cv2.resize(imghat, (hatwidth,hatheight), interpolation = cv2.inter_area) mask = cv2.resize(orig_mask, (hatwidth,hatheight), interpolation = cv2.inter_area) mask_inv = cv2.resize(orig_mask_inv, (hatwidth,hatheight), interpolation = cv2.inter_area) # take roi hat background equal size of hat image roi = roi_color[y1:y2, x1:x2] # roi_bg contains original image hat not # in region size of hat. roi_bg = cv2.bitwise_and(roi,roi,mask = mask_inv) # roi_fg contains image of hat hat roi_fg = cv2.bitwise_and(hat,hat,mask = mask) # join roi_bg , roi_fg dst = cv2.add(roi_bg,roi_fg) # place joined image, saved dst on original image roi_color[y1:y2, x1:x2] = dst break # display resulting frame cv2.imshow('video', frame) if cv2.waitkey(1) & 0xff == ord('q'): break # when done, release capture video_capture.release() cv2.destroyallwindows()
when execute program, camera opens , closes abruptly. error shown in below line -
roi_bg = cv2.bitwise_and(roi, roi, mask = mask_inv)
the error
assertion failed (mask.size == src1.size)
i.e. size of mask image does't match size of source image you're doing bitwise_and on
the source image roi
roi = roi_color[ y1:y2, x1:x2 ]
which looks y2-y1
x x2-x1
the mask size
mask = cv2.resize(orig_mask, (hatwidth,hatheight), interpolation = cv2.inter_area)
which looks hatwidth
x hatheight
i.e. (x2-x1)
x (y2-y1)
unless parentheses invert order of width , height - i'm not python guru seems unlikely.
Comments
Post a Comment