Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing black background and make transparent from grabcut output in python open cv

I have been trying to remove the black background from the grabcut output using python opencv.

import numpy as np
import cv2

img = cv2.imread(r'myfile_1.png')
mask = np.zeros(img.shape[:2],np.uint8)

bgdModel = np.zeros((1,65),np.float64)
fgdModel = np.zeros((1,65),np.float64)

rect = (1,1,665,344)
cv2.grabCut(img,mask,rect,bgdModel,fgdModel,5,cv2.GC_INIT_WITH_RECT)

mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8')
img = img*mask2[:,:,np.newaxis]

cv2.imshow('img',img)
cv2.imwrite('img.png',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Above code I had written to save the grabcut output. Please suggest, How I can remove the black background and make it transparent?

enter image description here

enter image description here

like image 263
Srikanth Avatar asked Nov 10 '16 12:11

Srikanth


People also ask

How do I make a black background transparent in OpenCV?

Step 1: First of all, import the library OpenCV. Step 2: Now, import the image from your computer. Step 3: Then, read the image in OpenCV. Step 4: Then, convert the image background to gray image background.

How do I convert black to white in OpenCV?

Converting an image to black and white with OpenCV can be done with a simple binary thresholding operation. We start with a gray scale image and we define a threshold value. Then, for each pixel of the gray scale image, if its value is lesser than the threshold, then we assign to it the value 0 (black).


2 Answers

I have achieved this by using the following snippet.

import cv2
file_name = "grab.png"

src = cv2.imread(file_name, 1)
tmp = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
_,alpha = cv2.threshold(tmp,0,255,cv2.THRESH_BINARY)
b, g, r = cv2.split(src)
rgba = [b,g,r, alpha]
dst = cv2.merge(rgba,4)
cv2.imwrite("test.png", dst)

enter image description here

like image 122
Srikanth Avatar answered Sep 19 '22 18:09

Srikanth


This is java code. After use grabcut, the result background is transparent.

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;
}
like image 35
QKT Avatar answered Sep 19 '22 18:09

QKT