高级正方形检测(带连接区域)

11

如果图像中的正方形有连通区域,我应该如何检测它们?

我已经尝试了在OpenCV C++/Obj-C: Advanced square detection中提到的方法,但效果不佳。

有什么好的想法吗?

具有连通区域的正方形

import cv2
import numpy as np

def angle_cos(p0, p1, p2):
    d1, d2 = (p0-p1).astype('float'), (p2-p1).astype('float')
    return abs( np.dot(d1, d2) / np.sqrt( np.dot(d1, d1)*np.dot(d2, d2) ) )

def find_squares(img):
    squares = []
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # cv2.imshow("gray", gray)

    gaussian = cv2.GaussianBlur(gray, (5, 5), 0)

    temp,bin = cv2.threshold(gaussian, 80, 255, cv2.THRESH_BINARY)
    # cv2.imshow("bin", bin)

    contours, hierarchy = cv2.findContours(bin, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)

    cv2.drawContours( gray, contours, -1, (0, 255, 0), 3 )

    #cv2.imshow('contours', gray)
    for cnt in contours:
        cnt_len = cv2.arcLength(cnt, True)
        cnt = cv2.approxPolyDP(cnt, 0.02*cnt_len, True)
        if len(cnt) == 4 and cv2.contourArea(cnt) > 1000 and cv2.isContourConvex(cnt):
            cnt = cnt.reshape(-1, 2)
            max_cos = np.max([angle_cos( cnt[i], cnt[(i+1) % 4], cnt[(i+2) % 4] ) for i in xrange(4)])
            if max_cos < 0.1:
                squares.append(cnt)
    return squares

if __name__ == '__main__':
    img = cv2.imread('123.bmp')

    #cv2.imshow("origin", img)

    squares = find_squares(img)  
    print "Find %d squres" % len(squares)
    cv2.drawContours( img, squares, -1, (0, 255, 0), 3 )
    cv2.imshow('squares', img)

    cv2.waitKey()

我在OpenCV的示例中使用了一些方法,但结果不太好。

3个回答

15
应用基于距离变换的分水岭变换将分隔对象:

enter image description here

Handling objects at the border is always problematic, and often discarded, so that pink rectangle at top left not separated is not a problem at all.
给定一张二进制图像,我们可以应用距离变换(DT),并从中获取Watershed的标记。理想情况下,应该有一个现成的函数来查找区域最小值/最大值,但由于它不存在,我们可以猜测如何对DT进行阈值处理。基于标记,我们可以使用Watershed进行分割,问题就解决了。现在你可以担心如何区分是矩形还是其他组件。
import sys
import cv2
import numpy
import random
from scipy.ndimage import label

def segment_on_dt(img):
    dt = cv2.distanceTransform(img, 2, 3) # L2 norm, 3x3 mask
    dt = ((dt - dt.min()) / (dt.max() - dt.min()) * 255).astype(numpy.uint8)
    dt = cv2.threshold(dt, 100, 255, cv2.THRESH_BINARY)[1]
    lbl, ncc = label(dt)

    lbl[img == 0] = lbl.max() + 1
    lbl = lbl.astype(numpy.int32)
    cv2.watershed(cv2.cvtColor(img, cv2.COLOR_GRAY2BGR), lbl)
    lbl[lbl == -1] = 0
    return lbl


img = cv2.cvtColor(cv2.imread(sys.argv[1]), cv2.COLOR_BGR2GRAY)
img = cv2.threshold(img, 0, 255, cv2.THRESH_OTSU)[1]
img = 255 - img # White: objects; Black: background

ws_result = segment_on_dt(img)
# Colorize
height, width = ws_result.shape
ws_color = numpy.zeros((height, width, 3), dtype=numpy.uint8)
lbl, ncc = label(ws_result)
for l in xrange(1, ncc + 1):
    a, b = numpy.nonzero(lbl == l)
    if img[a[0], b[0]] == 0: # Do not color background.
        continue
    rgb = [random.randint(0, 255) for _ in xrange(3)]
    ws_color[lbl == l] = tuple(rgb)

cv2.imwrite(sys.argv[2], ws_color)

从上面的图像中,您可以考虑在每个组件中拟合椭圆以确定矩形。然后,您可以使用一些测量方法来定义组件是否为矩形。这种方法更有可能适用于完全可见的矩形,并且对于部分可见的矩形可能会产生不良结果。以下图像显示了这种方法的结果,如果拟合椭圆的矩形在组件面积的10%之内,则认为该组件是矩形。

enter image description here

# Fit ellipse to determine the rectangles.
wsbin = numpy.zeros((height, width), dtype=numpy.uint8)
wsbin[cv2.cvtColor(ws_color, cv2.COLOR_BGR2GRAY) != 0] = 255

ws_bincolor = cv2.cvtColor(255 - wsbin, cv2.COLOR_GRAY2BGR)
lbl, ncc = label(wsbin)
for l in xrange(1, ncc + 1):
    yx = numpy.dstack(numpy.nonzero(lbl == l)).astype(numpy.int64)
    xy = numpy.roll(numpy.swapaxes(yx, 0, 1), 1, 2)
    if len(xy) < 100: # Too small.
        continue

    ellipse = cv2.fitEllipse(xy)
    center, axes, angle = ellipse
    rect_area = axes[0] * axes[1]
    if 0.9 < rect_area / float(len(xy)) < 1.1:
        rect = numpy.round(numpy.float64(
                cv2.cv.BoxPoints(ellipse))).astype(numpy.int64)
        color = [random.randint(60, 255) for _ in xrange(3)]
        cv2.drawContours(ws_bincolor, [rect], 0, color, 2)

cv2.imwrite(sys.argv[3], ws_bincolor)

2
没错,真的是个很好的方法,加一。将图像索引格式化到正确的格式非常麻烦,我曾经使用过你在这里展示的相同zip解压方法,但最近我意识到它可能比转置和复制慢得多(如果速度很重要的话..)。不幸的是,至少对我来说,为了避免opencv异常,需要进行复制。 - fraxel
区别在于numpy默认使用(y, x)坐标,而OpenCV则期望(x, y)。@fraxel我没有测量性能,但更新后的代码在这一特定点上可能更好。 - mmgp
@mmgp - 是的,我知道,这真的很烦人 - 但再次感谢你,你的更新对我很有帮助!它比你之前的方法快得多,也比我的快。我将在未来使用它和watershed(如果我需要的话...) :) - fraxel
@Yang,你知道被接受的答案根本没有解决问题吗?首先,它将每个组件都识别为矩形,无论它们是什么。其次,它任意地侵蚀n次以断开组件,这只是在等待失败。我不知道对你来说什么速度太慢,从未提到过,但对我来说运行得足够快。 - mmgp
@mmgp 你能用C++实现吗? - sayvortana
显示剩余4条评论

2

解决方案1:

对图像进行膨胀以删除连接的组件。 找到检测到的组件的轮廓。通过引入一些度量值(例如周长/面积比)来消除不是矩形的轮廓。

此解决方案将无法检测与边界相连的矩形。

解决方案2:

膨胀以删除连接的组件。 查找轮廓。 近似轮廓以减少它们的点数(对于矩形轮廓应该是4个点)。 检查轮廓线之间的角度是否为90度。 消除没有90度的轮廓。

这应该解决与边界相连的矩形问题。


1

你有三个问题:

  1. 矩形不是非常严格的矩形(边缘通常有些弯曲)
  2. 它们有很多。
  3. 它们经常相互连接。

看起来你所有的矩形基本上都是相同的大小(?),并且没有重叠太多,但预处理已将它们连接在一起。

对于这种情况,我会尝试的方法是:

  1. 扩大图像几次(正如@krzych所建议的那样) - 这将消除连接,但会导致稍小的矩形。
  2. 使用scipy 标记查找对象 - 现在您知道图像中每个剩余blob的位置和切片。
  3. 使用minAreaRect查找每个矩形的中心、方向、宽度和高度。

您可以使用步骤3来测试blob是否为有效矩形,通过其面积、尺寸比或接近边缘。

这是一种不错的方法,我们假设每个blob都是一个矩形,因此minAreaRect将找到最小外接矩形的参数。如果绝对必要,我们可以使用类似humoments的东西来测试每个blob。以下是我建议的操作,红色表示边界碰撞匹配。

enter image description here

代码:

import numpy as np
import cv2
from cv2 import cv
import scipy
from scipy import ndimage

im_col = cv2.imread('jdjAf.jpg')
im = cv2.imread('jdjAf.jpg',cv2.CV_LOAD_IMAGE_GRAYSCALE)

im = np.where(im>100,0,255).astype(np.uint8)
im = cv2.erode(im, None,iterations=8)
im_label, num = ndimage.label(im)
for label in xrange(1, num+1):
    points = np.array(np.where(im_label==label)[::-1]).T.reshape(-1,1,2).copy()
    rect = cv2.minAreaRect(points)
    lines = np.array(cv2.cv.BoxPoints(rect)).astype(np.int)
    if any([np.any(lines[:,0]<=0), np.any(lines[:,0]>=im.shape[1]-1), np.any(lines[:,1]<=0), np.any(lines[:,1]>=im.shape[0]-1)]):
        cv2.drawContours(im_col,[lines],0,(0,0,255),1)
    else:
        cv2.drawContours(im_col,[lines],0,(255,0,0),1)

cv2.imshow('im',im_col)
cv2.imwrite('rects.png',im_col)
cv2.waitKey()

我认为@mmgp展示的“分水岭”和“距离变换”方法在图像分割方面显然更优,但根据您的需求,这种简单方法也可以有效。

非常感谢,我认为这种方法比分水岭方法更快。您能否对代码进行评论?例如,“ndimage.label”是什么意思?还有“np.array(np.where(im_label==label)[::-1]).T.reshape(-1,1,2).copy()”是什么意思?谢谢您的回答~ - Yang
还有这段代码 "any([np.any(lines[:,0]<=0), np.any(lines[:,0]>=im.shape[1]-1), np.any(lines[:,1]<=0), np.any(lines[:,1]>=im.shape[0]-1)])" 我不太容易理解。谢谢! - Yang
1
@Yang - 嘿,没问题,这里是一份代码:ndimage.label(im) 用于对图像进行分割:每个未连接的斑点值都被顺序地替换为一个整数,从而得到一个新的标记图像 im_labelnp.where(im_label==label) 获取这个新的标记图像,并返回该图像中等于 label 的每个像素的索引 - 即单个斑点的所有索引值 - 注意我们正在通过仅考虑一个标签值来迭代斑点。.T.reshape(-1,1,2).copy() 是一个小技巧,将数据格式正确地传递给 minAreaRect - fraxel
@Yang - any是一个逻辑运算符,相当于链接的or。其中的条件是检查矩形点是否落在或超出图像区域,因为如果它们这样做,那么我们就知道它是一个边框矩形,所以我们将其着色为红色,或者可以丢弃它,或者其他操作...希望这有所帮助 ;) - fraxel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接