使用python分离图像上的合并点

2024-09-28 19:20:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Python和OpenCV创建一个脚本来计算图像上的点(非常原始)

import numpy as np
import cv2

# Image read
img = cv2.imread("img.tif", 0)

# Denoising
denoisedImg = cv2.fastNlMeansDenoising(img);

# Threshold (binary image)
# thresh – threshold value.
# maxval – maximum value to use with the THRESH_BINARY and THRESH_BINARY_INV thresholding types.
# type – thresholding type
th, threshedImg = cv2.threshold(denoisedImg, 100, 255,cv2.THRESH_BINARY_INV|cv2.THRESH_OTSU) # src, thresh, maxval, type

# Perform morphological transformations using an erosion and dilation as basic operations
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (4,8))
morphImg = cv2.morphologyEx(threshedImg, cv2.MORPH_CLOSE, kernel)

# Find and draw contours
contours, hierarchy = cv2.findContours(morphImg, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contoursImg = cv2.cvtColor(morphImg, cv2.COLOR_GRAY2RGB)
cv2.drawContours(contoursImg, contours, -1, (255,100,0), 3)

cv2.imwrite("image.tif", contoursImg)

print("Dots number: {}".format(len(contours)))

在左图中,我使用上面的代码获得的结果。在正确的形象上,我想要达到的目标

1 正如你们所看到的,我正在寻找一种方法,将有点相连的点分成两个独立的点,我想知道是否有一种“神奇”的功能允许我这样做

提前感谢你的帮助


Tags: andimageimportimgastypecv2binary