如何将Mat对象设置为颜色模式? [OpenCV的]

时间:2017-08-31 08:48:32

标签: java android opencv image-segmentation watershed

我正在做一个关于使用分水岭算法的图像分割的教程,我想知道在将图像分割成前景和背景后是否得到了正确的结果。

要求是使用imageView将我的finalMat对象的前景视图返回到颜色模式。我正在使用'一片叶子'照片作为一个例子,我想看到绿色回来取悦我自己,我正在从背景中获取对象。

这是我的代码(请随时评论我做错了什么,或者是我提出的更好的解决方案。我是Android开发和OpenCV的新手):

    Mat finalMat = markersMat;
    finalMat.convertTo(markersMat, CvType.CV_32S);
    Imgproc.watershed(originalPicMat, finalMat);

    //then return as CV_8U:
    finalMat.convertTo(finalMat, CvType.CV_8U);
    //test at final output:
    Bitmap testBmp = Bitmap.createBitmap(grayscaleMat.cols(), grayscaleMat.rows(),Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(finalMat, testBmp);
    imgView_finalOutput.setImageBitmap(testBmp);

以下是我的标记代码:

    //Markers image:
    Mat markersMat = new Mat(grayscaleMat.size(), CvType.CV_8U, new Scalar(0));
    Core.add(foregroundMat, backgroundMat, markersMat);
    //show markers:
    Bitmap markersBmp = Bitmap.createBitmap(grayscaleMat.cols(), grayscaleMat.rows(),Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(markersMat, markersBmp);
    imgView_markers.setImageBitmap(markersBmp);

同样,我的问题是如何在颜色模式下设置finalMat(返回叶子的颜色)?

以下是我的活动的完整代码:

        //Set Original pic:
    String originalFilePathStr= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) +"/healthy1.jpg";
    Mat originalPicMat = Imgcodecs.imread(originalFilePathStr);
    //set imageview:
    //File originalPicFilePath = new File(originalFilePathStr+"/healthy.jpg");
    Uri originalPicUriPath = Uri.parse(originalFilePathStr);
    imgView_original.setImageURI(originalPicUriPath);

    /* IMAGE SEGMENTATION USING WATERSHED ALGORITHM */
    //Create a Mat Object using originalPicture as is:

    /* OTSU'S BINARIZATION */
    Mat grayscaleMat = new Mat();
    //check if i dont need to load_image as grayscale, if cvtColor does does work or vice versa:
    Imgproc.cvtColor(originalPicMat, grayscaleMat, Imgproc.COLOR_BGR2GRAY);
    Imgproc.threshold(grayscaleMat, grayscaleMat, 0, 255, Imgproc.THRESH_BINARY_INV + Imgproc.THRESH_OTSU);
    //test grayscale: WORKS:
    Bitmap grayscaleBmp = Bitmap.createBitmap(grayscaleMat.cols(), grayscaleMat.rows(),Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(grayscaleMat, grayscaleBmp);
    imgView_grayscale.setImageBitmap(grayscaleBmp);

    //Part 2: Create marker: foregroun dnd background
    //Sure fg area:
    Mat foregroundMat = new Mat();
    Imgproc.erode(grayscaleMat, foregroundMat, new Mat(),new Point(-1,-1),2);
    //Show fg area:
    Bitmap foregroundBmp = Bitmap.createBitmap(grayscaleMat.cols(), grayscaleMat.rows(),Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(grayscaleMat, foregroundBmp);
    imgView_foreground.setImageBitmap(foregroundBmp);


    //Mat Bg:
    Mat backgroundMat = new Mat();
    Imgproc.dilate(grayscaleMat, backgroundMat, new Mat(), new Point(-1,-1), 3);
    Imgproc.threshold(backgroundMat, backgroundMat, 1,128, Imgproc.THRESH_BINARY_INV);
    Bitmap backgroundBmp = Bitmap.createBitmap(grayscaleMat.cols(), grayscaleMat.rows(),Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(backgroundMat, backgroundBmp);
    imgView_background.setImageBitmap(backgroundBmp);

    //Markers image:
    Mat markersMat = new Mat(grayscaleMat.size(), CvType.CV_8U, new Scalar(0));
    Core.add(foregroundMat, backgroundMat, markersMat);
    //show markers:
    Bitmap markersBmp = Bitmap.createBitmap(grayscaleMat.cols(), grayscaleMat.rows(),Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(markersMat, markersBmp);
    imgView_markers.setImageBitmap(markersBmp);
    /*Watershed: Finale */

    //test watershed:
    Mat finalMat = markersMat;
    finalMat.convertTo(markersMat, CvType.CV_32S);
    Imgproc.watershed(originalPicMat, finalMat);

    //then return as CV_8U:
    finalMat.convertTo(finalMat, CvType.CV_8U);
    //test at final output:
    Bitmap testBmp = Bitmap.createBitmap(grayscaleMat.cols(), grayscaleMat.rows(),Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(finalMat, testBmp);
    imgView_finalOutput.setImageBitmap(testBmp);

Here是我的活动结果的屏幕截图(没有足够的信誉点发布截图)。

更新: 它现在正在工作,我现在可以标记叶子了。我现在唯一的问题是它以不同的颜色返回叶子。请参阅this照片。

最终照片返回不同的绿色和黄色变成蓝色。 问题是我不知道错误来自哪里。如果你们知道问题出在哪里,请告诉我。

0 个答案:

没有答案