【发布时间】:2016-06-23 12:22:54
【问题描述】:
我不能链接超过 2 个的 url,所以我将我的图片发布到这个博客。 请在这里查看我的问题。 http://blog.naver.com/mail1001/220650041897
我想知道如何使用Android opencv使图像的白色部分,即白纸,透明。
我研究过url(我写在博客上),它使黑色背景透明,我认为“阿尔法通道”与它有关。
我认为它会在我制作 Alpha 通道时起作用,方法是让我想要制作透明黑色的部分和另一部分为白色,并将此 Alpha 通道合并到原始 RGB 通道。
所以我做了两个实验。
1) 我将纸张部分设为黑色,将书写部分设为白色以制作 Alpha 通道。并将其合并到 RGB 通道中。
(请看博客。实验一的alpha通道图片)
我以为字迹应该一样,背景应该是透明的,但是背景只变成了白色,有点透明。
(请看博客。实验一的结果图)
2) 这一次,纸的部分是白色的,书写的部分是黑色的。但这一次只有文字变成了透明。
(请看博客。实验2的alpha通道图片和结果图片)
在第二个实验中,我的意思是让透明变成透明,但在第一个实验中效果不一样。
我做错了哪一部分?有什么概念我理解错了吗?
这是我测试的来源。
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<Mat> rgb = new ArrayList<Mat>(3);
Core.split(src, rgb);
// Create the final result by merging three single channel and alpha(BGRA order)
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);
// 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;
}
感谢您的好意回答。
我试过了,但它与实验 1 的结果图片相同。 T_T
编辑代码
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<Mat> bgra = new ArrayList<Mat>(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;
}
【问题讨论】: