How to define paths in another path using JavaCV?

How to define contours inside another contour? I tried to go through many OpenCV tutorials, but I could not identify it. Can any of the experts provide simple code to explain this?

This is my input file.

enter image description here

This dark part is the outline I need to identify.

enter image description here

Please be so kind as to share your experience with me.

+6
source share
2 answers

Here is a simple approach in Python code. ( But as I stated in my comment below, It is not an universal answer applicable everywhere, It is just to show finding contour inside a contour can be done. And if your images are different, then you have to come with some different constraints other than i mentioned below )

What you do after searching for Contours is that you check to see if the area of ​​each contour is less than a given value (I gave 10000, just an assumption), if not more than a contour, avoid it. If the value is less than the specified, it can be our square or the rectangle next to it.

So, we find its width and height and check whether the aspect ratio is close to 1. If so, this is our square.

 import cv2 import numpy as np img = cv2.imread('sofcnt.jpg') gray = cv2.imread('sofcnt.jpg',0) ret,thresh = cv2.threshold(gray,127,255,1) cont,hier = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE) for cnt in cont: approx = cv2.approxPolyDP(cnt,0.02*cv2.arcLength(cnt,True),True) if cv2.contourArea(cnt) < 10000: x,y,w,h = cv2.boundingRect(cnt) if w/float(h) < 2: cv2.drawContours(img,[cnt],0,255,-1) cv2.imshow('a',img) cv2.waitKey(0) cv2.destroyAllWindows() 

Result:

enter image description here

+2
source

Not very convenient with JavaCV, so here is how I decided to solve this problem in OpenCV and C (ancient material):

  • Find all image outlines using cvFindContours()
  • Run two loops (iterating h_next pointers or whatever JavaCV has) on these loops. For each circuit in the outer loop, compare it with every other circuit defined on the basis of.,
  • Calculate the bounding box of each path. This will be the CvRect structure.
  • Pass two CvRects function that calculates the intersection area (overlap) between two rectangles.
  • If this area is equal to the area of ​​the smaller of the two rectangles, then the outline corresponding to the smaller rectangle is completely enclosed in the larger one.

    Here is the code to find the intersection area. He must have been floating around the Internet somewhere.

    CvRect intersect(CvRect r1, CvRect r2) { CvRect intersection;

     // find overlapping region intersection.x = (r1.x < r2.x) ? r2.x : r1.x; intersection.y = (r1.y < r2.y) ? r2.y : r1.y; intersection.width = (r1.x + r1.width < r2.x + r2.width) ? r1.x + r1.width : r2.x + r2.width; intersection.width -= intersection.x; intersection.height = (r1.y + r1.height < r2.y + r2.height) ? r1.y + r1.height : r2.y + r2.height; intersection.height -= intersection.y; // check for non-overlapping regions if ((intersection.width <= 0) || (intersection.height <= 0)) { intersection = cvRect(0, 0, 0, 0); } return intersection; 

    }

+3
source

Source: https://habr.com/ru/post/920332/


All Articles