opencv - Make an image contour black and white using open CV and Python -


i'm trying paint part of image black , white using opencv2 , python3. code i'm trying:

(x, y, w, h) = cv2.boundingrect(c) cv2.rectangle(frame, (x,y), (x+w,y+h),0,0) sub_face = frame[y:y+h, x:x+w] # apply gaussian blur on new recangle image # sub_face = cv2.gaussianblur(sub_face,(9, 9), 30, bordertype = 0) sub_face = cv2.cvtcolor(sub_face, cv2.color_bgr2gray)  # merge blurry rectangle our final image result_frame[y:y+sub_face.shape[0], x:x+sub_face.shape[1]] = sub_face 

when apply gaussianblur method, works properly, when try cvtcolor method fails message (on last line): not broadcast input array shape (268,182) shape (268,182,3). doing wrong?

the c variable in first line contour (from motion detection).

i'm new python , opencv.

thanks!

you trying assign single channel results cv2.cvtcolor call 3 channels @ once result_frame rgb / 3 channel image. wanting assign single channel 3 channels. 1 way cleanly exploit numpy broadcasting creating singleton channel in third dimension, broadcasting result on channels. since using cv2 interface opencv, native datatype used manipulating images numpy array:

# merge blurry rectangle our final image result_frame[y:y+sub_face.shape[0], x:x+sub_face.shape[1]] = sub_face[:,:,none] 

the : operation in context accesses values in particular dimension. in case, want first , second dimensions. therefore, sub_face[:,:,none] make single channel image 3d third dimension being singleton (i.e. 1). using numpy broadcasting broadcast single channel image channels simultaneously.

note didn't have explicitly access third dimension when assigning result_frame. because result_frame[y:y+sub_face.shape[0], x:x+sub_face.shape[1]] , result_frame[y:y+sub_face.shape[0], x:x+sub_face.shape[1],:] same thing dropping indexing after last dimension specify implicitly assumes :.


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -