gpt4 book ai didi

python - 使用opencv查找图像中的亮点

转载 作者:太空宇宙 更新时间:2023-11-03 22:02:52 25 4
gpt4 key购买 nike

This is the image in which I want to find the bright spots and tag them.

我想找到上图中的亮点并使用一些符号标记它们。为此,我尝试使用 OpenCV 已经提供的 Hough Circle Transform 算法。但是当我运行代码时它给出了某种断言错误。我还尝试了 OpenCV 中也提供的 Canny 边缘检测 算法,但它也给出了某种断言错误。我想知道是否有某种方法可以完成此操作,或者我是否可以防止出现这些错误消息。

我是 OpenCV 的新手,非常感谢任何帮助。

附言- 如有必要,我也可以使用 Scikit-image。因此,如果这可以使用 Scikit-image 完成,请告诉我怎么做。

下面是我的预处理代码:

import cv2
import numpy as np



image = cv2.imread("image1.png")

gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

binary_image = np.where(gray_image > np.mean(gray_image),1.0,0.0)

binary_image = cv2.Laplacian(binary_image, cv2.CV_8UC1)

最佳答案

如果您只是要处理简单的图像,例如您的黑色背景示例,您可以使用相同的基本预处理/阈值处理,然后找到连接的组件。使用此示例代码在图像中的所有圆圈内绘制一个圆圈。

import cv2 
import numpy as np

image = cv2.imread("image1.png")

# constants
BINARY_THRESHOLD = 20
CONNECTIVITY = 4
DRAW_CIRCLE_RADIUS = 4

# convert to gray
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# extract edges
binary_image = cv2.Laplacian(gray_image, cv2.CV_8UC1)

# fill in the holes between edges with dilation
dilated_image = cv2.dilate(binary_image, np.ones((5, 5)))

# threshold the black/ non-black areas
_, thresh = cv2.threshold(dilated_image, BINARY_THRESHOLD, 255, cv2.THRESH_BINARY)

# find connected components
components = cv2.connectedComponentsWithStats(thresh, CONNECTIVITY, cv2.CV_32S)

# draw circles around center of components
#see connectedComponentsWithStats function for attributes of components variable
centers = components[3]
for center in centers:
cv2.circle(thresh, (int(center[0]), int(center[1])), DRAW_CIRCLE_RADIUS, (255), thickness=-1)

cv2.imwrite("res.png", thresh)
cv2.imshow("result", thresh)
cv2.waitKey(0)

这是生成的图像: Picture showing drawn circles inside found connected components

编辑:connectedComponentsWithStats 将二值图像作为输入,并返回该图像中连接的像素组。如果您想自己实现该功能,天真的方法是:
1- 从左上角到右下角扫描图像像素,直到遇到没有标签 (id) 的非零像素。
2- 当你遇到一个非零像素时,递归地搜索它的所有邻居(如果你使用 4 个连通性你检查 UP-LEFT-DOWN-RIGHT,8 个连通性你也检查对角线)直到你完成那个区域。为每个像素分配一个标签。增加标签计数器。
3- 从您离开的地方继续扫描。

关于python - 使用opencv查找图像中的亮点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51846933/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com