如何使用JavaCV识别另一个轮廓内的轮廓?

时间:2012-07-12 14:33:22

标签: image-processing opencv javacv

如何识别另一个轮廓内的轮廓?我试图通过许多OpenCV教程,但我无法识别它。请一位专家提供简单的代码来解释一下吗?

这是我的输入文件

enter image description here

这个黑暗的部分是我需要识别的轮廓。

enter image description here

请善意与我分享您的经历。

2 个答案:

答案 0 :(得分:3)

对JavaCV不太满意,所以这就是我在OpenCV和C(古老的东西)中解决这个问题的方法:

  1. 使用cvFindContours()
  2. 查找图像中的所有轮廓
  3. 在这些轮廓上运行两个循环(迭代指针h_next或JavaCV中的任何内容)。对于外环中的每个轮廓,将其与基于检测到的每个其他轮廓匹配。 。 。
  4. 计算每个轮廓的边界框。这将是CvRect结构。
  5. 将两个CvRects传递给计算两个矩形之间交叉区域(重叠)的函数。
  6. 如果此区域等于两个矩形中较小的一个的区域,则对应于较小矩形的轮廓完全被较大的矩形包围。

    这是查找交叉区域的代码。它一定是漂浮在网络的某个地方。

    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;
    

    }

答案 1 :(得分:2)

这是Python代码中的一种简单方法。 (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

找到轮廓后你正在做的是检查每个轮廓的面积是否小于指定值(我给出10000,只是一个猜测),如果不是更大的轮廓,则避免它。如果小于指定值,则它可能是我们旁边的方形或矩形。

所以我们找到它的宽度和高度,并检查纵横比是否接近1.如果是,那就是我们的正方形。

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()

结果:

enter image description here