gpt4 book ai didi

python - OpenCV python 重叠粒子大小和数量

转载 作者:行者123 更新时间:2023-12-04 13:28:34 25 4
gpt4 key购买 nike

我得到了显示表面颗粒的灰度图像。我喜欢编写一个程序,它发现粒子在周围画一个圆圈,并计算圆圈和圆圈内的像素。
主要问题之一是粒子重叠。下一个问题是图像的对比度正在变化,从一个图像到另一个。
这是我的第一次尝试:

import matplotlib.pyplot as plt
import cv2 as cv
import imutils
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import os.path

fileref="test.png"
original = cv.imread(fileref)
img = original
cv.imwrite( os.path.join("inverse_"+fileref[:-4]+".png"), ~img );

img = cv.medianBlur(img,5)
img_grey = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

ret,th1 = cv.threshold(img_grey,130,255,cv.THRESH_BINARY)
th2 = cv.adaptiveThreshold(img_grey,255,cv.ADAPTIVE_THRESH_MEAN_C,\
cv.THRESH_BINARY,11,2)
th3 = cv.adaptiveThreshold(img_grey,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,\
cv.THRESH_BINARY,11,2)

titles = ['Original Image', 'Global Thresholding (v = 127)',
'Adaptive Mean Thresholding', 'Adaptive Gaussian Thresholding']
images = [img, th1]

cv.imwrite( os.path.join("threshhold_"+fileref[:-4]+".jpg"), th1 );
cv.imwrite( os.path.join("adaptivthreshhold-m_"+fileref[:-4]+".jpg"), th2 );
cv.imwrite( os.path.join("adaptivthreshhold-g_"+fileref[:-4]+".jpg"), th3 );


imghsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)


imghsv[:,:,2] = [[max(pixel - 25, 0) if pixel < 190 else min(pixel + 25, 255) for pixel in row] for row in imghsv[:,:,2]]
cv.imshow('contrast', cv.cvtColor(imghsv, cv.COLOR_HSV2BGR))

# Setup SimpleBlobDetector parameters.
params = cv.SimpleBlobDetector_Params()

# Change thresholds
params.minThreshold = 0
params.maxThreshold = 150


# Filter by Convexity
params.filterByConvexity = True
params.minConvexity = 0.87

# Filter by Inertia
params.filterByInertia = True
params.minInertiaRatio = 0.08 # 0.08
# Set edge gradient
params.thresholdStep = 0.5

# Filter by Area.
params.filterByArea = True
params.minArea = 300

# Set up the detector with default parameters.
detector = cv.SimpleBlobDetector_create(params)

# Detect blobs.
keypoints = detector.detect(original)

# Draw detected blobs as red circles.
# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures the size of the circle corresponds to the size of blob
im_with_keypoints = cv.drawKeypoints(original, keypoints, np.array([]), (0, 0, 255),
cv.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
print(len(keypoints))
# Show keypoints
display=cv.resize(im_with_keypoints,None,fx=0.5,fy=0.5)
cv.imshow("Keypoints", display)
cv.waitKey(0)
cv.imwrite( os.path.join("keypoints_"+fileref[:-4]+".jpg"), im_with_keypoints );
它圈出大多数粒子,但需要为每个图像更改参数以获得更好的结果,圆圈不能重叠,我不知道如何计算圆圈或计算圆圈内的像素。
非常感谢为我指明正确方向的任何帮助或提示。
我添加了几个示例图片
Image with great contrast
Result Image with great contrast
Image with medium contrast
Result Image with medium contrast

最佳答案

这是一种替代方法,不一定能提供比您已有的更好的结果。您可以尝试为参数插入不同的值,看看它是否给您可接受的结果。

import numpy as np
import cv2
import matplotlib.pyplot as plt

rgb = cv2.imread('/your/image/path/blobs_0002.jpeg')
gray = cv2.cvtColor(rgb, cv2.COLOR_BGR2GRAY)
imh, imw = gray.shape

cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,21,2)
th = cv2.adaptiveThreshold(gray,255, cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY_INV,15,15)

contours, hier = cv2.findContours(th.copy(),cv2.RETR_CCOMP,cv2.CHAIN_APPROX_SIMPLE)
out_img = rgb.copy()

for i in range(len(contours)):
if hier[0][i][3] != -1:
continue

x,y,w,h = cv2.boundingRect(contours[i])
ar = min(w,h)/max(w,h)
area = cv2.contourArea(contours[i])
extent = area / (w*h)
if 20 < w*h < 1000 and \
ar > 0.5 and extent > 0.4:
cv2.circle(out_img, (int(x+w/2), int(y+h/2)), int(max(w,h)/2), (255, 0, 0), 1)

plt.imshow(out_img)
对于较大的合并 Blob ,您可以尝试运行霍夫圆以查看部分轮廓是否适合测试。只是一个想法。只是承认您正在处理的图像很难提出一个干净的解决方案。
enter image description here
enter image description here

关于python - OpenCV python 重叠粒子大小和数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66640474/

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