OpenCV - 关闭图像边缘的轮廓

时间:2017-09-01 22:35:27

标签: python opencv

如何高精度地找到光滑表面的区域。我试图在气缸中找到气泡区域。

此代码相对较好,但不提供开放表面的区域。

它还会找到一个虚假的泡泡(如果运行代码,你可以看到它)

图像:

enter image description here

有没有办法让图像的边缘成为轮廓的终点?

import cv2
import numpy as np
import imutils

image = cv2.imread('Image.png')

# Convert Image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.bitwise_not(gray)
(thresh, bw) = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)

# Show greyscale image
cv2.namedWindow("main", cv2.WINDOW_NORMAL)
cv2.imshow('main', bw)
cv2.waitKey(0)
cv2.destroyAllWindows()

_, cnts, _ = cv2.findContours(bw, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
print("no of shapes {}".format(len(contours)))
for c in cnts: 
    if cv2.contourArea(c) > 0:
    # compute the center of the contour
        M = cv2.moments(c)
        cX = int(M["m10"] / M["m00"])
        cY = int(M["m01"] / M["m00"])

        # draw the contour and center of the shape on the image
        cv2.drawContours(image, [c], -1, (0, 255, 0), 2)
        cv2.circle(image, (cX, cY), 7, (255,0,0), -1)
        cv2.putText(image, "center", (cX - 20, cY - 20),
        cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,0,0), 2)

        # show the image
        cv2.namedWindow("main", cv2.WINDOW_NORMAL)
        cv2.imshow("main", image)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
        area = cv2.contourArea(c)
        print(area)

我对图像输出(线条颜色,背景颜色,线条粗细)有充分的灵活性,因为输出来自模拟。

什么设置最适合OpenCV?

0 个答案:

没有答案