亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Android Studio 中使用 OpenCV 檢測和計數隨機大小的白色物體

如何在 Android Studio 中使用 OpenCV 檢測和計數隨機大小的白色物體

繁花不似錦 2021-10-28 16:47:59
我正在嘗試使用 OpenCV 在 Android Studio 中制作一個應用程序,該應用程序基本上查看一組點的圖像并提取所有亮點,然后對它們進行計數。點的形狀不一致,但點的顏色應該是白色背景黑色。我首先上傳這樣的圖像:然后我獲取圖像的位圖并將其轉換為灰度。然后我檢查灰度值是否在 aa 范圍內并返回二進制值的位圖。返回的圖像如下所示:我已經失敗嘗試了幾種不同的方法來計算這些點包括使用Imgproc.findContours和沿教程,例如以下這一個,但我不是在尋找一個特定的形狀。它通常只是一次 1 到 50 個像素的分組,RGB 值分別為 225、255、255 和不規則形狀。如何計算這些單獨的形狀?這是我的代碼的圖像處理部分,countNonZero 部分只是讓我知道有多少白色像素,對于這個特定的圖像是 179public void convertToGray(View v){    int whitePix;    Mat Rgba = new Mat();    Mat grayMat = new Mat();    Mat dots = new Mat();    BitmapFactory.Options o = new BitmapFactory.Options();    o.inDither=false;    o.inSampleSize=4;    int width = imageBitmap.getWidth();    int height = imageBitmap.getHeight();    grayBitmap = Bitmap.createBitmap(width,height,Bitmap.Config.RGB_565);    //bitmap to MAT    Utils.bitmapToMat(imageBitmap,Rgba);    Imgproc.cvtColor(Rgba,Rgba,Imgproc.COLOR_RGB2GRAY);    Core.inRange(Rgba,scalarLow,scalarHigh,grayMat);    whitePix = Core.countNonZero(grayMat);    Utils.matToBitmap(grayMat,grayBitmap);     MediaStore.Images.Media.insertImage(getApplicationContext().getContentResolver(), grayBitmap, "Result", "Descrip");    mImageView.setImageBitmap(grayBitmap);}在將圖像上傳到應用程序后單擊按鈕時會調用此函數。
查看完整描述

2 回答

?
拉丁的傳說

TA貢獻1789條經驗 獲得超8個贊

我使用此代碼迭代模板匹配結果 Mat 以查找可能的匹配項。


public static List<Point> getPointsFromMatAboveThreshold(Mat m, float t){

    List<Point> matches = new ArrayList<Point>();

    FloatIndexer indexer = m.createIndexer();

    for (int y = 0; y < m.rows(); y++) {

        for (int x = 0; x < m.cols(); x++) {

            if (indexer.get(y,x)>t) {

                System.out.println("(" + x + "," + y +") = "+ indexer.get(y,x));

                matches.add(new Point(x, y));                   

            }

        }           

    }       

    return matches;

}

這將為您提供一定數量的白色坐標列表。然后你需要將這些聚類


查看完整回答
反對 回復 2021-10-28
?
GCT1015

TA貢獻1827條經驗 獲得超4個贊

事實證明我錯誤地使用了 findContours,我能夠通過在我的函數中附加以下代碼來解決我的問題:


    Mat dots = new Mat();

    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();

    Imgproc.findContours(grayMat, contours, dots, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE, new Point(0,0));


    Imgproc.drawContours(grayMat, contours, -1, new Scalar(Math.random()*255, Math.random()*255, Math.random()*255));//, 2, 8, hierarchy, 0, new Point());

countours 列表包含輪廓的整體數,這是我正在尋找的斑點數。


查看完整回答
反對 回復 2021-10-28
  • 2 回答
  • 0 關注
  • 248 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號