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

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

我使用的是以下代码:
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我是一名本科生,这是我的学士论文。如果有人能给我一个建议,那就太好了!谢谢。
发布于 2020-12-02 18:34:38
以下是部分代码和输出(用于检测行):
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)输出:

在检测到线条(因此是分段的)之后,您可以很容易地找到缺陷。
https://stackoverflow.com/questions/65024395
复制相似问题