简体   繁体   English

使用OpenCV查找图像中的亮点

[英]Finding bright spots in a image using opencv

这是我要查找亮点并对其进行标记的图像。

I want to find the bright spots in the above image and tag them using some symbol. 我想在上图中找到亮点,并用一些符号标记它们。 For this i have tried using the Hough Circle Transform algorithm that OpenCV already provides. 为此,我尝试使用OpenCV已经提供的霍夫圆变换算法。 But it is giving some kind of assertion error when i run the code. 但是当我运行代码时,它给出了某种断言错误。 I also tried the Canny edge detection algorithm which is also provided in OpenCV but it is also giving some kind of assertion error. 我还尝试了OpenCV中也提供的Canny边缘检测算法,但它也给出了某种断言错误。 I would like to know if there is some method to get this done or if i can prevent those error messages. 我想知道是否有某种方法可以完成此操作,或者是否可以防止出现这些错误消息。

I am new to OpenCV and any help would be really appreciated. 我是OpenCV的新手,我们将不胜感激。

PS - I can also use Scikit-image if necessary. PS-如果需要,我也可以使用Scikit映像。 So if this can be done using Scikit-image then please tell me how. 因此,如果可以使用Scikit-image完成此操作,请告诉我如何操作。

Below is my preprocessing code: 下面是我的预处理代码:

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)

If you are just going to work with simple images like your example where you have black background, you can use same basic preprocessing/thresholding then find connected components. 如果您只是要处理简单的图像(例如,您的示例中背景为黑色),则可以使用相同的基本预处理/阈值,然后找到连接的组件。 Use this example code to draw a circle inside all circles in the image. 使用此示例代码在图像的所有圆内绘制一个圆。

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)

Here is resulting image: 这是结果图像: 该图显示了找到的已连接组件内部的绘制圆

Edit: connectedComponentsWithStats takes a binary image as input, and returns connected pixel groups in that image. 编辑:connectedComponentsWithStats将二进制图像作为输入,并返回该图像中的已连接像素组。 If you would like to implement that function yourself, naive way would be: 如果您想自己实现该功能,那么幼稚的方法是:
1- Scan image pixels from top left to bottom right until you encounter a non-zero pixel that does not have a label (id). 1-从左上到右下扫描图像像素,直到遇到没有标签(id)的非零像素。
2- When you encounter a non-zero pixel, search all its neighbours recursively( If you use 4 connectivity you check UP-LEFT-DOWN-RIGHT, with 8 connectivity you also check diagonals) until you finish that region. 2-当遇到非零像素时,递归搜索所有相邻像素(如果使用4连接,则检查UP-LEFT-DOWN-RIGHT,使用8连接,还检查对角线),直到完成该区域。 Assign each pixel a label. 为每个像素分配标签。 Increase your label counter. 增加标签计数器。
3- Continue scanning from where you left. 3-从您继续的位置继续扫描。

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

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