How to make white part of the image transparent by using Android opencv
<p>I can't link url more than 2 so I post my pictures to this blog.
please see my problems here.
<a href="http://blog.naver.com/mail1001/220650041897" rel="nofollow">http://blog.naver.com/mail1001/220650041897</a></p>

<p>I want to know how to make white part of the image, which is white paper with writing on it, transparent by using Android opencv.</p>

<p>I have studied through url(I wrote it on blog) which makes the black background transparent, and I think "Alpha Channel" has something to do with it.</p>

<p>I think it will work as I make Alpha Channel by making the part I want to make transparent black and the other part white, and merge this Alpha Channel to the original RGB Channel.</p>

<p>So I made two experiments.</p>

<p>1) I made the papaer part black and the writing part white to make the Alpha Channel. And merged it to RGB Channel.</p>

<p>(Please see blog. Experiment 1's alpha channel picture)</p>

<p>I thought the writing should be the same and the background should be transparent, but the background only turned to white and little transparent.</p>

<p>(Please see blog. Experiment 1's result picture)</p>

<p>2) This time, the paper part is white and the writing part is black. But this time only writing turned to transparent.</p>

<p>(Please see blog. Experiment 2's alpha channel picture and result picture)</p>

<p>In the second experiment, what I meant to make transparent turned transparent but it did not work the same in the first experiment.</p>

<p>Which part am I doing wrong? Is there any concept I understand wrongly?</p>

<p>This is source what I tested.</p>

<pre><code>Bitmap test(Bitmap image) {
// convert image to matrix
Mat src = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC4);
Utils.bitmapToMat(image, src);

// init new matrices
Mat dst = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC4);
Mat tmp = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC4);
Mat alpha = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC4);

// convert image to grayscale
Imgproc.cvtColor(src, tmp, Imgproc.COLOR_BGR2GRAY);

// threshold the image to create alpha channel with complete transparency in black background region and zero transparency in foreground object region.
Imgproc.threshold(tmp, alpha, 100, 255, Imgproc.THRESH_BINARY_INV);
//Imgproc.threshold(tmp, alpha, 100, 255, Imgproc.THRESH_BINARY);

// split the original image into three single channel.
List&lt;Mat&gt; rgb = new ArrayList&lt;Mat&gt;(3);
Core.split(src, rgb);

// Create the final result by merging three single channel and alpha(BGRA order)
List&lt;Mat&gt; rgba = new ArrayList&lt;Mat&gt;(4);
rgba.add(rgb.get(0));
rgba.add(rgb.get(1));
rgba.add(rgb.get(2));
rgba.add(alpha);
Core.merge(rgba, dst);

// convert matrix to output bitmap
Bitmap output = Bitmap.createBitmap(image.getWidth(), image.getHeight(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(dst, output);
//Utils.matToBitmap(alpha, output);
return output;
}
</code></pre>

<hr>

<p>Thank you for your kindness answer.</p>

<p>I tried it but it was same Experiment 1's result picture. T_T</p>

<p>Edit code</p>

<pre><code>Bitmap makeBackgroundWhite(Bitmap image) {
    // convert image to matrix
    Mat src = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC4);
    Utils.bitmapToMat(image, src);

    // init new matrices
    Mat dst = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC4);
    Mat tmp = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8U);
    Mat alpha = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8U);

    // convert image to grayscale
    Imgproc.cvtColor(src, tmp, Imgproc.COLOR_BGR2GRAY);

    // threshold the image to create alpha channel with complete transparency in black background region and zero transparency in foreground object region.
    Imgproc.threshold(tmp, alpha, 100, 255, Imgproc.THRESH_BINARY_INV);

    // split the original image into three single channel.
    List&lt;Mat&gt; bgra = new ArrayList&lt;Mat&gt;(4);
    Core.split(src, bgra);

    // Create the final result by merging three single channel and alpha(BGRA order)
    bgra.remove(3);
    bgra.add(alpha);
    Core.merge(bgra, dst);

    // convert matrix to output bitmap
    Bitmap output = Bitmap.createBitmap(image.getWidth(), image.getHeight(), Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(dst, output);

    return output;
}
</code></pre>