繁体   English   中英

使用 python 的图像处理检测图像中的点

[英]Detect dots in a image using image processing with python

我需要识别给定图像中的以下点。 但它没有给出正确的检测。 有人可以提供一种方法来识别这样的图像中的这些点吗? 在此处输入图像描述

我对此做了一些改进,如下所示, 在此处输入图像描述

  • 增强图像,先膨胀再锐化

    我使用模板匹配来检测图像中的这些点。 但效果并不好。 代码如下。 有没有其他方法可以检测到这些?

导入 cv2 导入 numpy 作为 np

img_rgb = cv2.imread(file)
cv2.imwrite("D:/4/Detect/"+str(i)+".0.jpg",img_rgb)
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)

template = cv2.imread('a.jpg',0)
w, h = template.shape[::-1]

res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
threshold = 0.455
loc = np.where( res >= threshold)

for pt in zip(*loc[::-1]):
    cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,255,255), 0)

cv2.imshow('Detected',img_rgb)
cv2.imwrite("D:/4/Detect/"+str(i)+".1.jpg",img_rgb)
cv2.waitKey(0)
cv2.destroyAllWindows()

这是我快速开发的代码的主要部分以获得更好的结果(如下图所示):

import cv2 as cv
img = cv.imread('med.jpg',0)
th2 = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_MEAN_C,\
            cv.THRESH_BINARY,11,2)
th3 = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,\
            cv.THRESH_BINARY,11,2)
titles = ['Adaptive Mean Thresholding', 'Adaptive Gaussian Thresholding']
images = [th2, th3]
inv = ~th3
res = cv.bitwise_and(img,inv)
cv.imshow(titles[1],res)
cv.waitKey(0)
cv.imwrite("result.jpg",res)
cv.destroyAllWindows()

在此处输入图像描述

调整参数以获得所需的结果。

暂无
暂无

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

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