繁体   English   中英

找到两个 colors 之间的交线

[英]Find the intersection line between two colors

我试图找到一组与此类似的图像的绿色和蓝色之间的交集:

在此处输入图像描述

所以我需要的是这样的东西:

在此处输入图像描述

因为在那之后我需要使用轮廓进行一些微积分,所以我想知道是否有可能将曲线的点放在数组或其他东西中......但我不知道这是否可能。 我试着做面具,但我不认为它会起作用......这是代码:

        lower_val = np.array([0, 0, 0])

        upper_val = np.array([150, 255, 150])

        mask = cv2.inRange(image, lower_val, upper_val)
        only_lumen = cv2.bitwise_and(image, image, mask=mask)
        gray = cv2.cvtColor(only_lumen, cv2.COLOR_BGR2GRAY)
        blur = cv2.GaussianBlur(gray, (3, 3), 0)
        thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
        thresh = 255 - thresh

这就是我试图显示这些点的方式:

    x, y = [i[0] for i in pts_list], [i[1] for i in pts_list]
    max_x, max_y = max(x), max(y)

    image = np.zeros((max_y + 1, max_x + 1))

    for i in range(len(pts_list)):
        image[max_y - y[i], x[i]] = 1

这是我获得的图像: 在此处输入图像描述

我不明白为什么这些点会在一个角落里相遇,为什么背景是紫色的......应该在中间的线条

现在,我将为您提供可以尝试的最简单的解决方案(可以改进很多):

边界像素(获得蒙版后)将彼此相邻,但如果您希望最小为 1 个像素宽度的边界,则必须选择一侧。 对此的替代方法是设置搜索 kernel 并根据您的搜索 kernel 使用形态学来扩大您的颜色蒙版。 扩张蒙版中的公共点将是您可以通过简单地执行按位与来提取的邻域点。

# Define the color ranges for each color of interest for creating masks.
COLOR1_RANGE = [(30, 0, 0), (255, 50, 50)]  # Blue in BGR, [(low), (high)].
COLOR2_RANGE = [(0, 30, 0), (50, 255, 50)]  # Green in BGR, [(low), (high)].

# Create masks:
color1_mask = cv2.inRange(self.img, COLOR1_RANGE[0], COLOR1_RANGE[1])
color2_mask = cv2.inRange(self.img, COLOR2_RANGE[0], COLOR2_RANGE[1])

# Adjust according to your adjacency requirement.
kernel = np.ones((3, 3), dtype=np.uint8)

# Dilating masks to expand boundary.
color1_mask = cv2.dilate(color1_mask, kernel, iterations=1)
color2_mask = cv2.dilate(color2_mask, kernel, iterations=1)

# Required points now will have both color's mask val as 255.
common = cv2.bitwise_and(color1_mask, color2_mask)

# Common is binary np.uint8 image, min = 0, max = 255.
# SOME_THRESHOLD can be anything within the above range. (not needed though)
# Extract/Use it in whatever way you want it.
intersection_points = np.where(common > SOME_THRESHOLD)

# Say you want these points in a list form, then you can do this.
pts_list = [[r, c] for r, c in zip(*intersection_points)]
print(pts_list)

一些示例输出:

输入 01(简单):
简单输入

Output 01(简单):
简单输出

Output 点列表(部分):

pts_list = [[99, 104], [99, 105], [100, 104], [100, 105], [100, 106], ...]

输入 02(复杂):
复杂输入

Output 02(复杂):
复杂输出

Output 点列表(部分):

pts_list = [[127, 309], [127, 310], [127, 311], [127, 312], [127, 313], ...]

更新1:
我对代码注释做了一些小改动,使其更易于理解。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM