Make an image contour black and white using open CV and Python
<p>I'm trying to paint part of an image as black and white using OpenCV2 and Python3. This is the code I'm trying:</p>

<pre><code>(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 a gaussian blur on this new recangle image
# sub_face = cv2.GaussianBlur(sub_face,(9, 9), 30, borderType = 0)
sub_face = cv2.cvtColor(sub_face, cv2.COLOR_BGR2GRAY)

# merge this blurry rectangle to our final image
result_frame[y:y+sub_face.shape[0], x:x+sub_face.shape[1]] = sub_face
</code></pre>

<p>When I apply the GaussianBlur method, it works properly, but when I try the cvtColor method it fails with a message (on the last line): could not broadcast input array from shape (268,182) into shape (268,182,3). What am I doing wrong?</p>

<p>The <strong><em>c</em></strong> variable in first line is a contour (from motion detection).</p>

<p>I'm new into Python and OpenCV.</p>

<p>Thanks!</p>

<p>You converted sub_face to a single channel image, but result_frame is a 3 channel image.</p>

<p>In the last line you are trying to assign a single channel array to a 3 channel slice.</p>

<p>You could do this:</p>

<pre><code>result_frame[y:y+sub_face.shape[0], x:x+sub_face.shape[1], 0] = sub_face
result_frame[y:y+sub_face.shape[0], x:x+sub_face.shape[1], 1] = sub_face
result_frame[y:y+sub_face.shape[0], x:x+sub_face.shape[1], 2] = sub_face
</code></pre>

python opencv image-processing