OpenCV检测浴缸里的药片数量

2024-05-17 11:34:53 发布

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

我有一个透明的塑料管,里面正好有10个黑色的小球。但有时里面会有11到9片药。有没有办法检测管子里的球数。在

enter image description here

现在使用cv2,我能做的最好的事情是:

import cv2
import numpy as np

original = cv2.imread("d.jpg", cv2.IMREAD_GRAYSCALE)
retval, image = cv2.threshold(original, 50, 255, cv2.THRESH_BINARY)

cv2.imshow('image',image)
cv2.waitKey(0)
cv2.destroyAllWindows()

我得到了一个黑白图像以获得更好的对比度。在

enter image description here

我试着计算黑色像素的数量,然后除以一个数字得到球的数量。但由于有许多球相互重叠,所以无论我如何调整这个数字,它都不能很好地工作。在

有没有其他方法可以数数。在

以下是更多示例:

{{a4^}a5}


Tags: imageimportnumpy数量数字事情cv2黑色
1条回答
网友
1楼 · 发布于 2024-05-17 11:34:53

您可能需要尝试黑白图像>距离变换>模糊>分水岭变换。这是我在MATLAB中得到的结果

enter image description here

im=imread('tube.png');

% Transform
im=rgb2gray(im);

% Threshold
im=im>80;
im=imclose(im,strel('disk',2));
im=bwareaopen(im,10);

figure,
subplot(121);
imshow(im,[]);axis image;colormap gray

% Distance transform
imb=bwdist(im);

% Blur
sigma=4;
kernel = fspecial('gaussian',4*sigma+1,sigma);
im2=imfilter(imb,kernel,'symmetric');

% Watershed transform
L = watershed(max(im2(:))-im2);

% Plot
lblImg = bwlabel(L&~im);

subplot(122);
imshow(label2rgb(lblImg,'jet','k','shuffle'));

相关问题 更多 >