检测网格的角点

时间:2018-07-01 01:48:04

标签: python opencv image-processing opencv-contour corner-detection

我正在尝试检测必须处理的各种图片中的网格角。图片可能会歪斜,有些可能会相对对齐,但是我们不能保证所有图片都会像这样。

要确定网格的角,我尝试使用霍夫线,但无济于事。有时,霍夫线无法识别网格的边缘,因此很难确定绘制的哪些线属于网格的边缘,而哪些是网格线。

然后,我决定使用轮廓来检测网格的边缘。但是,它拾取了许多轮廓,并导致了相同的问题,即确定所有轮廓中的哪些位于拐角处。

为帮助解决这个问题,我使用了双边过滤,Canny边缘检测,形态学扩张和Harris边缘检测,在与我的问题类似的一个问题中可以看到。即使采取了所有这些措施,我仍然会遇到很多假角,有时甚至无法识别出真角。

我在想,是否有人可以帮助我改善拐角检测的结果,或者有人是否有完全不同的建议可以帮助解决我的问题。目的是获得优势,以便我可以使用10 X 10的网格进行单应性处理,以解决图像中的倾斜问题。它还将有助于将网格正方形映射到像素空间,这非常有用。

这是我的代码(命名和所有内容都有些草率,但稍后我会尝试修复它)。另外,是的,我全力以赴进行双边过滤,这似乎有助于摆脱不必要的轮廓和拐角。

当我尝试将Hough线应用于轮廓图像时,我似乎也遇到了一个错误:

  

error: (-215) img.type() == (((0) & ((1 << 3) - 1)) + (((1)-1) << 3)) in function cv::HoughLinesStandard

from PIL import Image
import numpy as np
import cv2
import glob

#import images using opencv
images = [cv2.imread(file) for file in glob.glob("SpAMImages/*.jpg")]
for image in images:
    #resizes image gotten from folder and performs bilateral filtering
    img = cv2.bilateralFilter(cv2.resize(image, (715,715)), 15, 800, 800)

    #applies a canny edge detection filter on the images loaded from the folder
    gridEdges = cv2.Canny(img, 140, 170)

    #apply image dilation
    kernel = np.ones((5,5), np.uint8)
    gridEdges = cv2.dilate(gridEdges, kernel, iterations=1)
    gridEdges = np.float32(gridEdges)
    gridEdges = cv2.blur(gridEdges,(10,10))
    gridEdges = cv2.cornerHarris(gridEdges,2,3,0.04)
    gridEdges = cv2.dilate(gridEdges,None)
    img[gridEdges>0.01*gridEdges.max()]=[0,0,255]

    #draw contours on current image
    imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    ret, thresh = cv2.threshold(imgray, 127, 255, 0)
    contourImage, contours, hierarchy = 
    cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    contour = cv2.drawContours(img, contours, -1, (0,255,0), 3)

    '''
    def largest_4_sided_contour(thresh, show_contours=True):
        contourImage, contours, hierarchy = 
        cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
        contour = cv2.drawContours(img, contours, -1, (0,255,0), 3)
        contours = sorted(contours, key = cv2.contourArea, reverse =  True)
        for cnt in contours[:min(5, len(contours))]:
            print(len(cnt))
            if len(cnt) == 4:
                return cnt
        return None
        print(largest_4_sided_contour(thresh))

    #applies a hough transformation to extract gridlines from the image

    -----------THIS LINE BELOW GIVES ME THE ERROR-----------------
    lines = cv2.HoughLines(img, 1, np.pi/180, 245)

    #iterates through an array of lines gottne from the hough transform
    #and draws them unto the image
    for i in range(len(lines)):
        for rho,theta in lines[i]:
            a = np.cos(theta)
            b = np.sin(theta)
            x0 = a * rho
            y0 = b * rho
            x1 = int(x0 + 1000*(-b))
            y1 = int(y0 + 1000*(a))
            x2 = int(x0 - 1000*(-b))
            y2 = int(y0 - 1000*(a))
            cv2.line(img, (x1,y1),(x2,y2),(0,0,255),2)
    cv2.imwrite('houghlines.jpg', img)
    '''
    #resize window because for some reason they are too large.
    cv2.namedWindow('image', cv2.WINDOW_NORMAL)
    cv2.resizeWindow('image', 800, 800)

    #display all the images produced from above processes
    cv2.imshow('image', img)
    cv2.imshow('dilated edges', gridEdges)
    #cv2.imshow('contour', contour)
    cv2.waitKey(0)
    '''
    retrieve image size from imported image.
    testImageWidth, testImageHeight = img.shape[:2]
    print(testImageHeight, testImageWidth)'''

这些是我尝试使用轮廓检测​​和哈里斯角检测获得角的一些图像。

使用轮廓和哈里斯拐角检测识别拐角:

identifying corners using contours and harris corner detection

identifying corners using contours and harris corner detection

还有一些我必须使用的图像示例。

主要示例网格:

prime example grid

有些偏斜的网格:

somewhat skewed grid

感谢您的提前帮助!

2 个答案:

答案 0 :(得分:2)

您正在使用OpenCV在Python中工作,但我将为您提供使用带有DIPimage的MATLAB的答案。我希望这个答案与概念有关,而不与代码有关。我确信可以使用OpenCV在Python中完成所有这些工作。

我在这里的目的是找到木板的四个角。网格本身可以猜测,因为它只是板子的等距划分,因此无需尝试检测所有线。四个角提供了有关透视变换的所有信息。

检测板的最简单方法是识别板是浅色且背景是深色。从灰度值图像开始,我应用一个小闭合(我使用了一个直径为7像素的圆,这适合于我作为示例使用的下采样图像,但是您可能需要适当地增加尺寸全尺寸图片)。这样得出结果:

enter image description here

接下来,我使用Otsu阈值选择进行二值化,并删除空洞(该部分并不重要,如果也有空洞,其余部分将起作用)。现在,我们看到的已连接组件与电路板和相邻电路板(或电路板周围的其他白色物体)相对应。

选择最大的连接组件是一个相当常见的过程。在下面的代码中,我标记图像(标识连接的组件),计算每个连接的组件的像素数,然后选择像素最多的像素。

最后,从该结果中减去其侵蚀,只剩下板边缘的像素(此处是蓝色覆盖在输入图像上):

enter image description here

我用来找到拐角的技巧很简单,但是在这里失败了,因为其中一个拐角不在图像中。在这四个边缘上使用Hough可能是一种更强大的解决方案。使用this other answer来获得一些想法和代码,以解决该问题。

无论如何,我发现最接近图像左上角的边缘像素作为电路板的左上角。其他3个角落也一样。这些结果是上图中的红点。

这里的第三个选择是将轮廓转换为多边形,使用Douglas–Peucker算法对其进行简化,丢弃沿图像边缘延伸的边缘(这是图像中没有拐角的位置),并扩展两侧的两个边缘可以找到图像外部的顶点。

随后是MATLAB(带有DIPimage)代码。

img = readim('https://i.stack.imgur.com/GYZGa.jpg');
img = colorspace(img,'gray');
% Downsample, makes display easier
img = gaussf(img,2);
img = img(0:4:end,0:4:end);
% Simplify and binarize
sim = closing(img,7);
brd = threshold(sim); % uses Otsu threshold selection
% Fill the holes
brd = fillholes(brd);
% Keep only the largest connected component
brd = label(brd);
msr = measure(brd);
[~,I] = max(msr,'size');
brd = brd == msr(I).id;
% Extract edges
brd = brd - erosion(brd,3,'rectangular');
% Find corners
pts = findcoord(brd);
[~,top_left] = min(sum(pts.^2,2));
[~,top_right] = min(sum((pts-[imsize(brd,1),0]).^2,2));
[~,bottom_left] = min(sum((pts-[0,imsize(brd,2)]).^2,2));
[~,bottom_right] = min(sum((pts-[imsize(brd,1),imsize(brd,2)]).^2,2));
% Make an image with corner pixels set
cnr = newim(brd,'bin');
cnr(pts(top_left,1),pts(top_left,2)) = 1;
cnr(pts(top_right,1),pts(top_right,2)) = 1;
cnr(pts(bottom_left,1),pts(bottom_left,2)) = 1;
cnr(pts(bottom_right,1),pts(bottom_right,2)) = 1;
cnr = dilation(cnr,3);
% Save images
writeim(sim,'so1.png')
out = overlay(img,brd,[0,0,255]);
out = overlay(out,cnr,[255,0,0]);
writeim(out,'so2.png')

答案 1 :(得分:1)

我为您提供了一个答案,尽管还不完整,但可能会对您有所帮助。 我使用Ramer–Douglas–Peucker algorithm确定轮廓,然后从轮廓中提取矩形框。然后,我使用“框”区域占图像区域的百分比来删除较小的框。这将删除大多数垃圾箱。

这是我在python代码中所做的一个示例:

寻找轮廓:

    def findcontours(self):
        logging.info("Inside findcontours Contours...")
        # Pre-process image
        imgGray = self.imgProcess.toGrey(self.img)
        logging.info("Success on converting image to greyscale")

        imgThresh = self.imgProcess.toBinary(imgGray)
        logging.info("Success on converting image to binary")

        logging.info("Finding contours...")
        image, contours, hierarchy = cv2.findContours(imgThresh.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
        logging.info("Contours found: %d", len(contours))

       return contours

使用轮廓查找框:

    def getRectangles(self, contours):
        arrrect = []
        imgArea = self.getArea()
        logging.info("Image Area is: %d", imgArea)

        for cnt in contours:
            epsilon = 0.01*cv2.arcLength(cnt, True)
            approx = cv2.approxPolyDP(cnt, epsilon, False)
            area = cv2.contourArea(approx)
            rect = cv2.minAreaRect(approx)
            box = cv2.boxPoints(rect)
            box = np.int0(box)
            percentage = (area * 100) / imgArea
            if percentage > 0.3:
                arrrect.append(box)

        return arrrect

要结合这两种方法:

    def process(self):
        logging.info("Processing image...")
        self.shape_handler = ShapeHandler(self.img)

        contours = self.shape_handler.findcontours()

        logging.info("Finding Rectangles from contours...")
        rectangles = self.shape_handler.getRectangles(contours)

        img = self.imgDraw.draw(self.img, rectangles, "Green", 10)
        cv2.drawContours(img, array, -1, (0,255,0), thickness)
        self.display(img)

        logging.info("Amount of Rectangles Found: %d", len(rectangles))

显示图像:

    def display(self, img):
        cv2.namedWindow('image', cv2.WINDOW_NORMAL)
        cv2.imshow("image", img)
        cv2.waitKey(0)
        cv2.destroyAllWindows()

最后一步是合并所有相交的框,因为您只对边缘/角感兴趣,然后才获得面积最大的框。查看here,查看如何组合框。

我的编码来源:OpenCV 3.1 Documentation

图像结果:

正常:

Normal

倾斜:

Skew

希望这会有所帮助!