-1

I am new in OpenCV and stuck at a point actually, I study lots of QA but all are unable or I can say not fully satisfied my requirement. Suppose I have an image and I want to find counters for objectifying specified area only. It is easy to counter a complete image but I want to counters only masking area.

For masking purpose I have x,y coordinates with me. So my question is how to counter only masking area in the image using OpenCV

This is the one solution matching with my question->Stackoverflow suggestion -: But it does not solving my problem completely. The given solution in this solution is quite complicated and not solving my problem. as I want my counters ROI to be show in the same image using mask coordinates(xmin, ymin, xmax, ymax). Means i first drawing reactangle mask into the image and i want to draw counters boundary only into that rectangle mask in the same image.

This is my Demo.py file:-

import cv2
img = cv2.imread("F:/mycar.jpg",1)
crop_img = cv2.rectangle(img, (631,181), (698,386), (0,255,0), 3)
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
retval, thresh = cv2.threshold(gray_img, 127, 255, 0)
img_contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img, img_contours, -1, (255, 0, 0))
cv2.imshow("dflj",img)
cv2.waitKey(0)
cv2.destroyAllWindows()

output

enter image description here

From this image, you can see that the complete image is countering. But I want to just counter only that green-rectangle part of the image.

Means that blue counter boundary lines is visible inside whole image but I want to show only it inside of green-rectangle box.

I have rectangle coordinates with me (eg. minx-631, miny-181, maxx-698, maxy-386). Please help.

Ram Agrawal
  • 99
  • 4
  • 13
  • 1
    You can extract that specific part of the image using ROI and then you can find contours in that region. – Rick M. Aug 13 '19 at 11:10
  • 1
    Possible duplicate of [How can I find contours inside ROI using opencv and Python?](https://stackoverflow.com/questions/42004652/how-can-i-find-contours-inside-roi-using-opencv-and-python) – Rick M. Aug 13 '19 at 11:11

1 Answers1

1

You can apply functions to ROIs using Numpy indexing as explained in this tutoral:

top,left = 338,282
h,w = 216,106

gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
crop_img = cv2.rectangle(img, (left,top), (left+w,top+h), (0,255,0), 3)

retval, thresh = cv2.threshold(gray_img, 127, 255, 0)
img_contours, _ = cv2.findContours(thresh[top:top+h,left:left+w], cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img[top:top+h,left:left+w], img_contours, -1, (255, 0, 0))

Please note that with Numpy indexing y comes first then x, as opposed to point coordinates with (x,y).

With this image we get:enter image description here

Stef
  • 28,728
  • 2
  • 24
  • 52
  • yup... It's working. Thank you very much@Stef. Your solution will help other peoples too, who stuck like me.... keep doing it, great job – Ram Agrawal Aug 13 '19 at 13:15