首页 > 解决方案 > 使用OpenCV做裂缝检测的问题

问题描述

import cv2 as cv
import numpy as np

src = cv.imread("Image set/Positive/00010.jpg")
cv.imshow("input", src)

gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)

se = cv.getStructuringElement(cv.MORPH_RECT, (10, 10), (-1, -1))
binary = cv.morphologyEx(binary, cv.MORPH_OPEN, se)
cv.imshow("binary", binary)

contours,hierachy=cv.findContours(binary,cv.RETR_EXTERNAL,cv.CHAIN_APPROX_SIMPLE)
height, width = src.shape[:2]
for c in range(len(contours)):
    x, y, w, h = cv.boundingRect(contours[c])
    area = cv.contourArea(contours[c])
    if h > (height//2):
        continue
    if area < 150:
        continue
    cv.rectangle(src, (x, y), (x+w, y+h), (0, 0, 255), 1, 8, 0)
    cv.drawContours(src, contours, c, (0, 255, 0), 1, 8)

cv.imshow("result", src)
cv.imwrite("result.jpg", src)

cv.waitKey(0)
cv.destroyAllWindows()

以上是我写的代码,我使用OpenCV尝试检测裂缝,但我只检测到部分裂缝而不是所有裂缝。我不知道如何解决这个问题。

在此处输入图像描述在此处输入图像描述

但在其他情况下,检测性能是完美的 在此处输入图像描述

标签: pythonopencv

解决方案


推荐阅读