在图像上应用两个阈值来检测边缘



我正在尝试检测焊接图像上的边缘。在图像的一侧,我可以通过在开cv中使用简单的阈值来容易地检测边缘,该阈值在该区域中匹配,但该阈值对于另一侧来说太低。是否可以区分灰度值,从而使用不同的阈值来检测边缘?

原始图片

想要的边缘

import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
def region_of_interest(img, vertices):
mask = np.zeros_like(img)
#channel_count = img.shape[2] # for RGB image
match_mask_color = (255,) # * channel_count # only if you want RGB image
cv.fillPoly(mask, vertices, match_mask_color)
masked_img = cv.bitwise_and(img, mask)
return masked_img

path = "resources/weld_6.PNG"
img = cv.imread(path)
img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
start_x = 615
end_x = 900
start_y = 510
end_y = 560
gray_img = cv.cvtColor(img, cv.COLOR_RGB2GRAY)
img_blur = cv.GaussianBlur(gray_img, (5, 5), 8)
roi_test = region_of_interest(img_blur,
np.array([region_of_interest_vertices], np.int32))
canny_img = cv.Canny(roi_test, 10, 35)
ret, img_binary1 = cv.threshold(roi_test, 225, 255, 0)
img_cropped = img_binary1[530:540, 625:890]
img_canny = cv.Canny(img_cropped, 150, 200)
# Sum down the columns
columnTotals = np.sum(img_canny, axis=0)
# Now look for non-zero (non-black) entries
nz = np.nonzero(columnTotals)
# Now get left and right edges of white parts of image
left, right = nz[0][0], nz[0][-1]

最后一部分给了我最左边的白色像素和最右边的白色像素,利用它们的差异,我能够检测边缘之间的距离。

您的问题:

是否可以区分灰度值,因此使用不同的阈值来检测边缘

不是特别清楚。

在您的特定图像中,看起来您的阈值是255255,0,因此您正在红色和绿色通道中寻找饱和度。

查看您的示例图像,您可以使用200200200来获得两个边缘。

最新更新