如何在python中的opencv中检测药盒的细胞轮廓? [英] How to detect the outlines of cells of a pillbox in opencv in python?

查看:197
本文介绍了如何在python中的opencv中检测药盒的细胞轮廓?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试制作一个opencv程序,该程序可以检测药盒中的哪些单元格中装有药丸.

So I am trying to make an opencv program that can detect which cells of a pillbox have pills in them.

我最初的想法是:

  • 使用canny查找所有边缘
  • 运行hough变换以查找单元格的边界
  • 查看14个单元中的哪个单元的白色像素大于某个阈值.

但是,实际上,由于该相机是劣质的树莓派相机,因此我似乎无法使用Canny来找到良好的边缘.

However, in practice, I can't really seem to find good edges using canny, since the camera is a poor quality raspberry pi camera.

有人对我如何找到14个细胞的轮廓有任何建议吗?

Does anybody have any suggestions of how I can go about finding the outlines of the 14 cells?

更新:

因此,我在墙壁的边缘上放置了胶带,并通过一些处理能够获得如下图像

So I've put masking tape on the edges of the walls, and through some processing, am able to obtain images like the following

现在,如何获得14个单元的内部的14个蒙版?我尝试对这张图片进行霍夫变换,但它仍在创建本不应该的虚假线条,而不在创建应有的线条

Now, how do I get the 14 masks of the interiors of the 14 cells? I tried doing a hough transform on this image, but it's still creating spurious lines it shouldn't, and not creating lines it should

推荐答案

考虑到您控制成像条件,建议您稍微改善一下它们.

Considering that you control the imaging conditions, I would suggest you improve them a bit.

首先,将您的相机指向垂直向下指向药盒所在的表面.这将使药盒的几何结构更加简单.

First of all have your camera point straight down to the surface that the pillbox is on. That will make the geometry of the pillbox a lot simpler.

也请尝试避免阴影.他们不是没有可能解决,但是没有这个问题会更容易.良好的照明使大多数计算机视觉问题都变得容易.

Also try to avoid shadows. They're not impossible to deal with, but the problem is easier without. A good illumination makes most computer vision problems easy.

您在白色背景上有一个红色框.分割框应该很容易(只需检测红色像素).找到最大的连接组件,然后确定四个角.这为您提供了一个矩形.既然您知道盒子的几何形状,那么现在要做的就是将矩形分成7x2的网格.这消除了检测单个细胞的需要.

You have a red box on a white background. Segmenting out the box should be easy (just detect red pixels). Find the largest connected component, then identify the four corners. This gives you a rectangle. Since you know the geometry of the box, all you need to do now is divide the rectangle into a 7x2 grid. This removes the need to detect individual cells.

如果您真的想检测单个细胞,请考虑将细胞的边缘涂成黑色.再次,面对困难的视力问题,调整您的世界,直到问题变得容易为止. :)

If you really want to detect individual cells, consider painting the edges of the cells in black. Again, facing a difficult vision problem, adjust your world until the problem is easy. :)

编辑

使用蓝色美纹纸胶带是个好主意.照片中的蓝色很好看. JPEG压缩确实使区分颜色变得更加困难,我希望您可以读取和处理图像而无需通过JPEG.这就是我所拥有的:

It's a good idea to use blue masking tape. The blue comes out nicely in the photograph. The JPEG compression does make it more difficult than necessary to distinguish colors, I hope you can read and process the images without passing through JPEG. Here's what I've got:

我没有打扰过一些透视失真.由于查找框的轮廓很简单,而且应该告诉您所有您需要了解的失真,因此也应该可以对此进行校正.

There is a bit of perspective distortion that I didn't bother with. It should be possible to correct for that too, since finding the box outline is simple, and that tells you all you need to know about the distortion.

我使用过MATLAB,因为这是我最熟悉的快速原型制作方法.希望您在将其转换为Python时不会遇到麻烦.我使用了DIPimage工具箱,该工具箱具有Python对应的PyDIP(我正在使用的所有功能都可以在Python中获得). 这是GitHub存储库.

I've used MATLAB, because that is what I'm most familiar with to do a quick prototype. I hope you won't have trouble translating this to Python. I used the DIPimage toolbox, which has a Python counterpart, PyDIP (all functionality I'm using is available from within Python). Here is the GitHub repository.

% Read in image
img = imread('https://i.stack.imgur.com/AXkGH.jpg');
img = joinchannels('rgb',img);
% Convert to Lab color space
lab = colorspace(img,'lab');
% Get red area, make it into a single large blob, and measure its orientation
red = lab{2} > 30;
red = closing(red,20);
msr = measure(red,[],'feret');
[~,indx] = max(msr);
angle = msr(indx(1)).feret(5);
% Rotate the original image to make the box horizontal
img = colorspace(rotation(img,-angle-pi/2,'linear','add max'),'rgb'); % BUG! rotation loses color space
% Convert to Lab color space again
lab = colorspace(img,'lab');
% Get blue masking tape
blue = lab{3} < -10;
% Poor man's Radon transform -- horizontal lines
horiz = sum(blue,[],1);
% Expect three peaks
horiz = gaussf(horiz,3);
peaks = find(maxima(horiz));
horiz = double(horiz(peaks));
[~,indx] = sort(horiz,'descend');
horiz = sort(peaks(indx(1:3))); % 3 largest maxima
% Poor man's Radon transform -- vertical lines
vert = sum(blue,[],2);
% Expect eight peaks
vert = gaussf(vert,3);
peaks = find(maxima(vert));
vert = double(vert(peaks));
[~,indx] = sort(vert,'descend');
vert = sort(peaks(indx(1:8))); % 8 largest maxima
% Draw lines over rotated input image
for ii=1:8
   img(vert(ii),horiz(1):horiz(end)) = [0,255,0];
end
for ii=1:3
   img(vert(1):vert(end),horiz(ii)) = [0,255,0];
end

这篇关于如何在python中的opencv中检测药盒的细胞轮廓?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆