life evader
life evader

Reputation: 988

Edge refinement OpenCV android

ok so I am a newbie to opencv, I have manage to perform a grabcut which seems "ok" to me except the edges are pretty out of shape, I want to get realistic edges, maybe blur the edges out to get a perfectly done image, another thing I have noticed is the colors tend to be too bright after the entire process I wanted to reduce the vibrancy to somewhat acceptable below is my code

private Bitmap backGrndErase()
{

    color = new Scalar(255, 0, 0, 255);
    dst = new Mat();
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.myshirt);
    Log.d(TAG, "bitmap: " + bitmap.getWidth() + "x" + bitmap.getHeight());


    bitmap = ResizeImage.getResizedBitmap(bitmap, calculatePercentage(40, bitmap.getWidth()), calculatePercentage(40, bitmap.getHeight()));



    bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
    Log.d(TAG, "bitmap 8888: " + bitmap.getWidth() + "x" + bitmap.getHeight());



    Mat img = new Mat();
    Utils.bitmapToMat(bitmap, img);


    Point p1 = new Point((img.cols()/10), (img.rows()/10));
    Point p2 = new Point((img.cols()-img.cols()/10), (img.rows()-img.rows()/10));
    Rect rect = new Rect(p1,p2);

    int border = 20;
    int border2 = border + border;
    Rect rect2 = new Rect( border, border, img.cols() - border2, img.rows()-border2);




    Mat mask = new Mat();
    debugger(""+mask.type());
    mask.setTo(new Scalar(125));
    Mat fgdModel = new Mat();
    fgdModel.setTo(new Scalar(255, 255, 255));
    Mat bgdModel = new Mat();
    bgdModel.setTo(new Scalar(255, 255, 255));

    Mat imgC3 = new Mat();
    Imgproc.cvtColor(img, imgC3, Imgproc.COLOR_RGBA2RGB);
    Log.d(TAG, "imgC3: " + imgC3);
    Log.d(TAG, "Grabcut begins");
    Imgproc.grabCut(imgC3, mask, rect2, bgdModel, fgdModel, 2, Imgproc.GC_INIT_WITH_RECT);
    Mat source = new Mat(1, 1, CvType.CV_8U, new Scalar(3.0));
    //Do Sth
    Core.compare(mask, source, mask, Core.CMP_EQ);
    //Do Sth
    Mat foreground = new Mat(img.size(), CvType.CV_8UC3, new Scalar(255, 255, 255));
    img.copyTo(foreground, mask);
    Imgproc.rectangle(img, p1, p2, color);

    Mat background = new Mat();
    try {
        background = Utils.loadResource(getApplicationContext(),
                R.drawable.blackcolor );
    } catch (IOException e) {

        e.printStackTrace();
    }
    Mat tmp = new Mat();
    Imgproc.resize(background, tmp, img.size());

    background = tmp;

    Mat tempMask = new Mat(foreground.size(), CvType.CV_8UC1, new Scalar(255, 255, 255));
    Imgproc.cvtColor(foreground, tempMask, 6/* COLOR_BGR2GRAY */);


    Mat vals = new Mat(1, 1, CvType.CV_8UC3, new Scalar(0.0));
    dst = new Mat();
    background.setTo(vals, tempMask);
    Imgproc.resize(foreground, tmp, mask.size());
    foreground = tmp;
    Core.add(background, foreground, dst, tempMask);
    Log.d(TAG, "Convert to Bitmap");


    //removing blackbaground started

     Mat tmp2 = new Mat();
     Mat alpha = new Mat();
     Imgproc.cvtColor(dst, tmp2, Imgproc.COLOR_BGR2GRAY);
     Imgproc.threshold(tmp2, alpha, 100, 255, Imgproc.THRESH_BINARY);


     List<Mat> rgb = new ArrayList<Mat>(3);
     Core.split(dst, rgb);


     List<Mat> rgba = new ArrayList<Mat>(4);
     rgba.add(rgb.get(0));
     rgba.add(rgb.get(1));
     rgba.add(rgb.get(2));
     rgba.add(alpha);
     Core.merge(rgba, dst);
     Bitmap output = Bitmap.createBitmap(dst.width(), dst.height(), Bitmap.Config.ARGB_8888);
     Utils.matToBitmap(dst, output);

    //removing back ended


    Utils.matToBitmap(dst, bitmap);



    //release MAT part
    img.release();
    imgC3.release();
    mask.release();
    fgdModel.release();
    bgdModel.release();
    alreadyRun = true;
    return  bitmap;

}

I have posted the same question on the opencv site but there seems to be low audience there compared to here, imagesmy output image, if you look you can see that my output is too vibrant and very bad edges

my input image

I tried it on another image and still got the same over vibrant effect

Upvotes: 5

Views: 1028

Answers (3)

Ranjithkumar
Ranjithkumar

Reputation: 18416

Try this code. It working for me

public Bitmap removeBackground(Bitmap bitmap) {
    //GrabCut part
    Mat img = new Mat();
    Utils.bitmapToMat(bitmap, img);

    int r = img.rows();
    int c = img.cols();
    Point p1 = new Point(c / 100, r / 100);
    Point p2 = new Point(c - c / 100, r - r / 100);
    Rect rect = new Rect(p1, p2);

    Mat mask = new Mat();
    Mat fgdModel = new Mat();
    Mat bgdModel = new Mat();

    Mat imgC3 = new Mat();
    Imgproc.cvtColor(img, imgC3, Imgproc.COLOR_RGBA2RGB);

    Imgproc.grabCut(imgC3, mask, rect, bgdModel, fgdModel, 5, Imgproc.
            GC_INIT_WITH_RECT);

    Mat source = new Mat(1, 1, CvType.CV_8U, new Scalar(3.0));
    Core.compare(mask, source/* GC_PR_FGD */, mask, Core.CMP_EQ);

    //This is important. You must use Scalar(255,255, 255,255), not Scalar(255,255,255)
    Mat foreground = new Mat(img.size(), CvType.CV_8UC3, new Scalar(255,
            255, 255,255));
    img.copyTo(foreground, mask);

    // convert matrix to output bitmap
    bitmap = Bitmap.createBitmap((int) foreground.size().width,
            (int) foreground.size().height,
            Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(foreground, bitmap);
    return bitmap;
}

Upvotes: 0

Wladimir Dlc R
Wladimir Dlc R

Reputation: 35

If you want to detect the edges or corners very precicely opencv has a set o subpixel resolution tools, and it could be a good idea to apply a gaussianFilter before the edge detection, that erase some undesirable noise

Upvotes: 0

sturkmen
sturkmen

Reputation: 3550

a trial code just to show some possibilities around your question ( assuming the background will be always white )

#include "opencv2/opencv.hpp"

using namespace cv;

int main(int argc, char** argv)
{
Mat src= imread( argv[1] );
Mat original = src.clone();

imshow("source", src);

src = src + Scalar(40,40,40); // to remove jpeg artifacts

Mat mask;
cvtColor( src, mask, COLOR_BGR2GRAY );

mask = mask < 255;

add(src,Scalar(0,60,20),src,mask);

Mat blackbg = Mat::zeros(src.size(),CV_8UC3);
original.copyTo( blackbg, mask);

imshow("mask", mask);
imshow("changed color", src);
imshow("original image with black background", blackbg);
waitKey();

return 0;
}

enter image description hereenter image description hereenter image description here

Upvotes: 8

Related Questions