首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于opencv的线条缺陷检测

基于opencv的线条缺陷检测
EN

Stack Overflow用户
提问于 2020-11-26 22:54:27
回答 1查看 94关注 0票数 2

我正在研究一种算法来找出像这样的印刷电子图片中的缺陷,

我正在尝试使用findContours,但我得到的最好结果是:

我使用的是以下代码:

代码语言:javascript
复制
import cv2
import numpy as np


def damageDetection(img):
   
    imgBW = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    imgThresh = cv2.adaptiveThreshold(imgBW, 255,
                                      cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                      cv2.THRESH_BINARY, 801, 0)

    kernel = np.ones((5,5), np.uint8)
    imgEro = cv2.erode(imgThresh, kernel, iterations=1)
    imgDil = cv2.dilate(imgEro, kernel, iterations=1)

    contours, hierarchy = cv2.findContours(imgDil, cv2.RETR_EXTERNAL,
                                           cv2.CHAIN_APPROX_NONE)

    for cnt in contours:
        if cv2.contourArea(cnt) > 500:
            cv2.drawContours(img, cnt, -1, (0, 255, 0), 3)

    return img

我是一名本科生,这是我的学士论文。如果有人能给我一个建议,那就太好了!谢谢。

EN

回答 1

Stack Overflow用户

发布于 2020-12-02 18:34:38

以下是部分代码和输出(用于检测行):

代码语言:javascript
复制
import cv2
import numpy as np
from matplotlib import pyplot as plt
im = cv2.imread("line.png")

imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
thresh = cv2.adaptiveThreshold(imgray,255,cv2.ADAPTIVE_THRESH_MEAN_C,\
            cv2.THRESH_BINARY,101,3)

lines = cv2.HoughLinesP(thresh, 1, np.pi/180, 400, minLineLength=10, maxLineGap=200)
for line in lines:
    x1, y1, x2, y2 = line[0]
    cv2.line(im, (x1, y1), (x2, y2), (255, 0, 0), 5)

plt.imshow(im)

输出:

在检测到线条(因此是分段的)之后,您可以很容易地找到缺陷。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65024395

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档