是否可以检测成对的连接像素? [英] Is it possible to detect pairs of connected pixels?

查看:91
本文介绍了是否可以检测成对的连接像素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过Python 3.7使用OpenCV.我有以下图片(请注意白色区域上的一些红色像素):

I'm using OpenCV via Python 3.7. I have a following image (please take note of some red pixels on white areas):

我知道图像中每个红色像素的x和y坐标.我想找到所有由单个白线互连的红色像素对.

I know x and y coordinates of every red pixel in the image. I want to find all red pixels pairs that're interconnected by single white lines.

让每个带有id(蓝色数字)的红色像素标签:

Let's label every red pixel with id (blue number):

如您所见,标记为"1"的最上面的红色像素只有两个直线连接:一个带有标记为"2"的红色像素,另一个带有标记为"3"的红色像素.我想获取一个元组列表,其中每个元组都是一对相互连接的像素ID.对于上面的图片,正确的结果是:

As you can see, the topmost red pixel labeled "1" has only two straight connections: one with a red pixel labeled "2" and one with a red pixel labeled "3". I'd like to get a list of tuples, where every tuple is a pair of interconnected pixels ids. For the image above the correct result is:

[(1,2),
(1,3),
(2,4),
(4,5),
(3,5),
(5,7),
(7,9),
(4,6),
(6,8),
(6,7),
(8,10),
(9,11),
(10,11),
(11,13),
(10,12),
(12,13),
(12,14),
(13,14)]

我还没有编写任何代码,因为我只能使用笨拙的自制算法,该算法扫描每个红色像素的N个邻居以检测方向.我敢肯定,有更多利用内置功能的有效解决方案.

I haven't composed any code yet, because I can only go with a clumsy homemade algorythm that scans N neighbours of every red pixel to detect directions. I'm sure there're more efficient solutions that utilize built-in functions.

有没有可以帮助完成此任务的OpenCV功能?

Are there any OpenCV functions that can help with this task?

推荐答案

此答案说明了如何使用np.count_nonzero()确定是否用白线连接了两个点.

This answer explains how to use np.count_nonzero() to determine if two points are connected by a white line.

首先,绘制图像并计算非零像素.此示例图片中有 18896 个非零像素.

First, draw your image and count the non-zero pixels. There are 18896 non-zero pixels in this example image.

import cv2
import numpy as np
import itertools

# Function that converts an image to single channel and counts non-black pixels
def count_non_zero(img):
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    return np.count_nonzero(gray)

# Create source image
img = np.zeros((456,456, 3), np.uint8)
redCoordinates = [(123,123),(345,123),(345,345)]
cv2.line(img, redCoordinates[0], redCoordinates[1], (255,255,255), 65)
for coordinate in redCoordinates: cv2.circle(img, coordinate, 14, (0,0,255), -1)
# Count the non-zero pixels in the image
base = count_non_zero(img)

接下来,迭代红色对Coordaintes对的每个组合.在两点之间画一条线.检查图像是否具有相同数量的非零像素.

Next, iterate through each combination of pairs of red coordaintes. Draw a line between the points. Check if the image has the same number of non zero pixels.

# Iterate through each combination of the redCoordinates
idx = 0
for a,b in list(itertools.combinations(redCoordinates, 2)):
    # Draw a line between the two points
    test_img = cv2.line(img.copy(), a, b, (234,0,234), 5)
    # Recount to see if the images are the same
    if count_non_zero(test_img) == base: print(a, b, " are connected.")
    else: print(a,b, " are NOT connected.")

以下是一些相互联系的点:

These are some points that are connected:

以下是一些未连接的点:

These are some points that are not connected:

这是脚本的输出:

(123, 123) (345, 123)  are connected.
(123, 123) (345, 345)  are NOT connected.
(345, 123) (345, 345)  are NOT connected.

这篇关于是否可以检测成对的连接像素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆