Python图像中每个晶体的不规则面积测量

Python图像中每个晶体的不规则面积测量,python,opencv,image-processing,image-segmentation,cv2,Python,Opencv,Image Processing,Image Segmentation,Cv2,我试图在图像中找到每个晶体的面积。对于晶体分离,我使用了分水岭算法。我也附上了结果。 我正试图在python中使用OpenCV进行一些图像分析,但我认为图像本身将非常棘手,而且我以前从未做过类似的事情,因此我想试探一下我的逻辑,或许可以获得一些想法/实用代码来实现我想做的事情,以免我投入大量时间走上错误的道路 这个线程非常接近我想要实现的目标,在我看来,它使用了一个比我更难分析的图像。不过,我对这些彩色斑点的大小感兴趣,而不是它们与左上角的距离。我也一直在关注这段代码,尽管我对参考对象不是特别感

我试图在图像中找到每个晶体的面积。对于晶体分离,我使用了分水岭算法。我也附上了结果。 我正试图在python中使用OpenCV进行一些图像分析,但我认为图像本身将非常棘手,而且我以前从未做过类似的事情,因此我想试探一下我的逻辑,或许可以获得一些想法/实用代码来实现我想做的事情,以免我投入大量时间走上错误的道路

这个线程非常接近我想要实现的目标,在我看来,它使用了一个比我更难分析的图像。不过,我对这些彩色斑点的大小感兴趣,而不是它们与左上角的距离。我也一直在关注这段代码,尽管我对参考对象不是特别感兴趣(仅以像素为单位的尺寸就足够了,以后可以转换)

这里输入代码


这是否回答了您的问题?这回答了你的问题吗?
import numpy as np
import cv2

img = cv2.imread('ESlPT.png')
blur = cv2.GaussianBlur(img, (7, 7), 2)
h, w = img.shape[:2]

# Morphological gradient

kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (7, 7))
gradient = cv2.morphologyEx(blur, cv2.MORPH_GRADIENT, kernel)
cv2.imshow('Morphological gradient', gradient)
cv2.waitKey()
# Binarize gradient

lowerb = np.array([0, 0, 0])
upperb = np.array([15, 15, 15])
binary = cv2.inRange(gradient, lowerb, upperb)
cv2.imshow('Binarized gradient', binary)
cv2.waitKey()
# Flood fill from the edges to remove edge crystals

for row in range(h):
    if binary[row, 0] == 255:
        cv2.floodFill(binary, None, (0, row), 0)
    if binary[row, w-1] == 255:
        cv2.floodFill(binary, None, (w-1, row), 0)

for col in range(w):
    if binary[0, col] == 255:
        cv2.floodFill(binary, None, (col, 0), 0)
    if binary[h-1, col] == 255:
        cv2.floodFill(binary, None, (col, h-1), 0)

cv2.imshow('Filled binary gradient', binary)
cv2.waitKey()
# Cleaning up mask

foreground = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel)
foreground = cv2.morphologyEx(foreground, cv2.MORPH_CLOSE, kernel)
cv2.imshow('Cleanup up crystal foreground mask', foreground)
cv2.waitKey()
# Creating background and unknown mask for labeling

kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (17, 17))
background = cv2.dilate(foreground, kernel, iterations=3)
unknown = cv2.subtract(background, foreground)
cv2.imshow('Background', background)
cv2.waitKey()
# Watershed

markers = cv2.connectedComponents(foreground)[1]
markers += 1  # Add one to all labels so that background is 1, not 0
markers[unknown==255] = 0  # mark the region of unknown with zero
markers = cv2.watershed(img, markers)
# Assign the markers a hue between 0 and 179

hue_markers = np.uint8(179*np.float32(markers)/np.max(markers))
blank_channel = 255*np.ones((h, w), dtype=np.uint8)
marker_img = cv2.merge([hue_markers, blank_channel, blank_channel])
marker_img = cv2.cvtColor(marker_img, cv2.COLOR_HSV2BGR)
cv2.imshow('Colored markers', marker_img)

cv2.waitKey()
#Label the original image with the watershed markers

labeled_img = img.copy()
labeled_img[markers>1] = marker_img[markers>1]  # 1 is background color
labeled_img = cv2.addWeighted(img, 0.5, labeled_img, 0.5, 0)
cv2.imshow('watershed_result.png', labeled_img)

cv2.waitKey()