7

Im trying to find contours in a specific area of the image. Is it possible to just show the contours inside the ROI and not the contours in the rest of the image? I read in another similar post that I should use a mask, but I dont think I used it correctly. Im new to openCV and Python, so any help is much appriciated.

import numpy as np
import cv2

cap = cv2.VideoCapture('size4.avi')
x, y, w, h= 150, 50, 400 ,350
roi = (x, y, w, h)

while(True): 
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    _, thresh = cv2.threshold(gray, 127, 255, 0)
    im2, contours, hierarchy = cv2.findContours(thresh,    cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

    roi = cv2.rectangle(frame, (x,y), (x+w, y+h), (0,0,255), 2)
    mask = np.zeros(roi.shape,np.uint8)
    cv2.drawContours(mask, contours, -1, (0,255,0), 3)

    cv2.imshow('img', frame)
agrom
  • 386
  • 2
  • 5
  • 19

2 Answers2

12

Since you claim to be a novice, I have worked out a solution along with an illustration.

Consider the following to be your original image:

enter image description here

Assume that the following region in red is your region of interest (ROI), where you would like to find your contours:

enter image description here

First, construct an image of black pixels of the same size. It MUST BE OF SAME size:

black = np.zeros((img.shape[0], img.shape[1], 3), np.uint8) #---black in RGB

enter image description here

Now to form the mask and highlight the ROI:

black1 = cv2.rectangle(black,(185,13),(407,224),(255, 255, 255), -1)   #---the dimension of the ROI
gray = cv2.cvtColor(black,cv2.COLOR_BGR2GRAY)               #---converting to gray
ret,b_mask = cv2.threshold(gray,127,255, 0)                 #---converting to binary image

enter image description here

Now mask the image above with your original image:

fin = cv2.bitwise_and(th,th,mask = mask)

enter image description here

Now use cv2.findContours() to find contours in the image above.

Then use cv2.drawContours() to draw contours on the original image. You will finally obtain the following:

enter image description here

There might be better methods as well, but this was done so as to get you aware of the bitwise AND operation availabe in OpenCV which is exclusively used for masking

Community
  • 1
  • 1
Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
  • 2
    I gave this solution because you wanted to know how to perform masking in an image. The key is to efficiently use the function `cv2.bitwise_and()` – Jeru Luke Feb 03 '17 at 10:56
  • Yes, it was a great way to understand how to mask, thanks ;) A small follow up question: when i draw the contours I also get a contour around the frame of the ROI as well as the contours inside, is that supposed to happend? I see you dont have it in your example. – agrom Feb 04 '17 at 14:12
  • 1
    That may happen if you have not inverted your image – Jeru Luke Feb 05 '17 at 04:59
6

For setting a ROI in Python, one uses standard NumPy indexing such as in this example.

So, to select the right ROI, you don't use the cv2.rectangle function (that is for drawing a rectangle), but you do this instead:

 _, thresh = cv2.threshold(gray, 127, 255, 0)
 roi = thresh[x:(x+w), y:(y+h)]
 im2, contours, hierarchy = cv2.findContours(roi, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
Tomáš M.
  • 752
  • 7
  • 24
  • 2
    You can pass to `findContours` the offset of the rectangle, so that the retrieved contour points are referred to the original image – Miki Feb 02 '17 at 15:12
  • Thank you, it worked when I changed the way I wrote the roi. How would I offset the rectangle like you suggest Miki? – agrom Feb 02 '17 at 16:34
  • 3
    The docs: http://docs.opencv.org/2.4/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=findcontours say the last argument of the function is a tuple that specifies the offset; i.e.: `im2, contours, hierarchy = cv2.findContours(roi, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE, (x, y))` – Tomáš M. Feb 02 '17 at 16:37