【问题标题】:how to detect and count pills (capsules) [closed]如何检测和计数药丸(胶囊)[关闭]
【发布时间】:2014-07-29 06:46:11
【问题描述】:

我想知道使用哪种算法来检测药丸和胶囊?即使用 opencv 和 android 检测一张桌子上的胶囊数量。

我使用过的程序:

首先捕获图像,然后我们应用灰度,然后在阈值化后应用阈值,我应用了腐蚀,然后在使用 houghcircles 之后,我尝试检测胶囊但无法检测到任何胶囊。

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

到目前为止我所做的尝试:

这是我在 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);        

请帮忙。谢谢。

【问题讨论】:

    标签: android opencv hough-transform


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

    【讨论】:

    • 我也尝试使用 houghcircle 查看上面的代码,但我无法检测到任何椭圆。
    • 那么我建议使用 1 和 3。请考虑 3。它既快速又有效。
    • 我正在使用选项 3,但我数不清。丸。我能够识别胶囊,但我无法计数胶囊。请帮助如何计算药丸的数量
    • 获取图像的二进制垫,其中白色代表已识别的胶囊。之后使用 findContours 计算药丸的数量。
    • 根据我们的讨论,我使用 findContours 然后在 find area 和 drawContours 但相机距离从不同距离给出不同数量的药丸。如何解决距离问题?您对此有任何想法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-03
    • 2017-11-03
    • 2020-01-01
    • 2021-04-13
    • 1970-01-01
    • 1970-01-01
    • 2012-01-25
    相关资源
    最近更新 更多