如何检测和计数药片(胶囊)

时间:2014-07-29 06:46:12

标签: android opencv hough-transform

我想知道哪种算法用于检测药丸和胶囊?这是使用opencv和android检测表上的胶囊数量。

我使用的程序:

首先拍摄图像,我们应用灰度,然后应用阈值处理后应用侵蚀然后使用houghcircles后我尝试检测胶囊但不能检测任何胶囊。

请给出解决方案.....

到目前为止我尝试了什么:

enter image description here

enter image description here

这是我在android opencv中检测药丸的代码。

        Bitmap i = getBitmap(imgPath + "orignal.jpg");

        //Log.i("after Bitmap i",""+imgPath);
        Bitmap bmpImg = i.copy(Bitmap.Config.ARGB_8888, false);
        bmpImg =SetBrightness(bmpImg,-60);
        //Log.i("after Bitmap bmpImg",""+imgPath);
        Mat srcMat = new Mat ( bmpImg.getHeight(), bmpImg.getWidth(), CvType.CV_8UC3);
        Bitmap myBitmap32 = bmpImg.copy(Bitmap.Config.ARGB_8888, true);
        Utils.bitmapToMat(bmpImg, srcMat);

        //convert to gray scale and save image
        Mat gray = new Mat(srcMat.size(), CvType.CV_8UC1);
        //Imgproc.cvtColor(srcMat, gray, Imgproc.COLOR_RGB2GRAY,4);
        Imgproc.cvtColor(srcMat, gray, Imgproc.COLOR_BGRA2GRAY);
        //write bitmap
        Boolean grayBool = Highgui.imwrite(imgPath + "gray.jpg", gray);             
        Imgproc.medianBlur(gray, gray,51);      
        Utils.matToBitmap(gray, bmpImg);

        //thresholding
        Mat threshed = new Mat(bmpImg.getWidth(),bmpImg.getHeight(), CvType.CV_8UC1);
        Imgproc.adaptiveThreshold(gray, threshed, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 75, 5);//15, 8 were original tests. Casey was 75,10//(smoothed, threshed, 250, 250, 0);
        Core.bitwise_not(threshed, threshed);
        Boolean threshedBool = Highgui.imwrite(imgPath + "threshed.jpg", threshed); 
        Utils.matToBitmap(threshed, bmpImg);

        Imgproc.GaussianBlur(threshed, threshed, new org.opencv.core.Size(9, 9), 2, 2);
        Utils.matToBitmap(threshed, bmpImg);

        //erosion
        Mat eroded = new Mat(bmpImg.getWidth(),bmpImg.getHeight(), CvType.CV_8UC1);
        Imgproc.erode(threshed, eroded, Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new org.opencv.core.Size(15, 15)));
        Utils.matToBitmap(eroded, bmpImg);
        //write bitmap
        Boolean boolEroded = Highgui.imwrite(imgPath + "eroded.jpg", eroded);


        //smoothing
        //Imgproc.GaussianBlur(threshed, threshed, new org.opencv.core.Size(3,3), 50);
        Imgproc.GaussianBlur(edge, threshed, new org.opencv.core.Size(9, 9), 2, 2);
        Utils.matToBitmap(threshed, bmpImg);

        //hough circles
        Mat circles = new Mat();

        Imgproc.HoughCircles( eroded, circles, Imgproc.CV_HOUGH_GRADIENT,1, eroded.rows()/8, 200,100, eroded.cols()/25, eroded.cols()/6 );
        //Imgproc.HoughCircles( threshed, circles, Imgproc.CV_HOUGH_GRADIENT,1, threshed.rows()/8,100, 80, 10, 100);        

请协助。谢谢。

1 个答案:

答案 0 :(得分:1)

可以采用多种方法,您可以尝试一些选项:

1>如果您确定可以预期的那种药片,您可以训练自己的HAAR分类器。阅读本教程以了解如何执行此操作:http://coding-robin.de/2013/07/22/train-your-own-opencv-haar-classifier.html 如果你环顾四周,你可以找到更多的教程

2 - ;从它的外观来看,你的药丸似乎是圆形或椭圆形。为什么不使用Houghs Circle Transform找到圆圈?点击此处查看更多信息:http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/hough_circle/hough_circle.html

3>色度键控。我怀疑你会有绿色的药片。将所有药片放在绿色图表纸上,然后您可以非常轻松地删除图像中的背景(表格)。你剩下的就是药片,然后只需要找到外部轮廓来确定药丸的数量。就此而言,要了解绿色不是必需品,只要您可以在背景和药丸之间保持巨大的色差并且背景具有单一颜色。