title: How to extract a specific section of an image using OpenCV in Python?
body: <p>I am trying to extract a portion of an image by performing Canny edge detection. I have succesfully created a mask of that object. But when I perform a <code>bitwise_and</code> operation with the original image, to extract the foreground section, I am getting the following error .</p>
  
  <pre><code>OpenCV Error: Assertion failed ((mtype == CV_8U || mtype == CV_8S) &amp;&amp; _mask.sameSize(*psrc1)) in cv::binary_op, file C:\projects\opencv-python\opencv\modules\core\src\arithm.cpp, line 241
      Traceback (most recent call last):
        File "C:\Users\Boudhayan Dev\Desktop\extraction.py", line 37, in &lt;module&gt;
          new_image = cv2.bitwise_and(img_rgb,img_rgb,mask=mask)
      cv2.error: C:\projects\opencv-python\opencv\modules\core\src\arithm.cpp:241: error: (-215) (mtype == CV_8U || mtype == CV_8S) &amp;&amp; _mask.sameSize(*psrc1) in function cv::binary_op
  </code></pre>
  
  <p>My code is as follows -</p>
  
  <pre><code>import cv2
  import numpy as np
  
  img_rgb = cv2.imread("3.jpg")
  cv2.namedWindow("Original Image",cv2.WINDOW_NORMAL)
  
  img = cv2.cvtColor(img_rgb,cv2.COLOR_RGB2HSV)
  img = cv2.bilateralFilter(img,9,105,105)
  r,g,b=cv2.split(img)
  equalize1= cv2.equalizeHist(r)
  equalize2= cv2.equalizeHist(g)
  equalize3= cv2.equalizeHist(b)
  equalize=cv2.merge((r,g,b))
  
  equalize = cv2.cvtColor(equalize,cv2.COLOR_RGB2GRAY)
  
  ret,thresh_image = cv2.threshold(equalize,0,255,cv2.THRESH_OTSU+cv2.THRESH_BINARY)
  equalize= cv2.equalizeHist(thresh_image)
  
  
  canny_image = cv2.Canny(equalize,250,255)
  canny_image = cv2.convertScaleAbs(canny_image)
  kernel = np.ones((3,3), np.uint8)
  dilated_image = cv2.dilate(canny_image,kernel,iterations=1)
  
  
  new,contours, hierarchy = cv2.findContours(dilated_image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  contours= sorted(contours, key = cv2.contourArea, reverse = True)[:10]
  c=contours[0]
  print(cv2.contourArea(c))
  final = cv2.drawContours(img, [c], -1, (255,0, 0), 3)
  
  
  
  mask = np.zeros(img_rgb.shape,np.uint8)
  new_image = cv2.drawContours(mask,[c],0,255,-1,)
  new_image = cv2.bitwise_and(img_rgb,img_rgb,mask=mask)
  
  
  cv2.namedWindow("new",cv2.WINDOW_NORMAL)
  cv2.imshow("new",new_image)
  
  cv2.imshow("Original Image",img)
  cv2.waitKey() 
  </code></pre>
  
  <p>NOTE :- The code works fine if I try to perform the <code>bitwise_and</code> with a grayscale version of the image. However RGB,HSV or any other color spaces give the error above.</p>
  
  <p>Please help.</p>
  
  <hr>
  
  <p><strong>EDIT 1 -</strong>
   The image in question is this -</p>
  
  <p><a href="https://i.stack.imgur.com/J8LDr.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/J8LDr.jpg" alt="3.jpeg"></a></p>
  
  <p><strong>EDIT 2-</strong> </p>
  
  <p>The following is the result after using Numpy method. As you can see, the extracted image is the same size as the orange but it does not contain the orange instead the mask itself.</p>
  
  <p><a href="https://i.stack.imgur.com/6YqEH.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/6YqEH.jpg" alt="result"></a></p>
  
  <p><strong>EDIT 3-</strong> @DanMašek and @lightalchemist , I could finally extract any foreground image.</p>
  
  <p><a href="https://i.stack.imgur.com/TsoX0.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/TsoX0.jpg" alt="result1"></a></p>
  
  <p><a href="https://i.stack.imgur.com/r7Tfs.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/r7Tfs.jpg" alt="result2"></a></p>
  
  <p>Thank you </p>