1
我正在使用OpenCV的分水嶺算法編寫一個樣本。我能夠創建蒙版圖像,但是蒙版具有白色背景而不是透明背景。是否有辦法去除這個白色背景並使其透明?我使用的代碼被提供如下:從流域掩模中刪除白色背景圖像
Mat threeChannel = new Mat();
Imgproc.cvtColor(croppedImageMat, threeChannel, Imgproc.COLOR_BGR2GRAY);
Imgproc.threshold(threeChannel, threeChannel, 100, 255, Imgproc.THRESH_BINARY | Imgproc.THRESH_OTSU);
Mat fg = new Mat(croppedImageMat.size(), CvType.CV_8U);
Imgproc.erode(threeChannel, fg, new Mat(), new Point(-1, -1), 12);
Mat bg = new Mat(croppedImageMat.size(), CvType.CV_8U);
Imgproc.dilate(threeChannel, bg, new Mat(), new Point(-1, -1), 12);
Imgproc.threshold(bg, bg, 1, 128, Imgproc.THRESH_BINARY_INV);
Mat markers = new Mat(croppedImageMat.size(), CvType.CV_8U, new Scalar(0));
Core.add(fg, bg, markers);
WatershedSegmenter segmenter = new WatershedSegmenter();
segmenter.setMarkers(markers);
Mat result = segmenter.process(croppedImageMat);
Bitmap resultBmp = Bitmap.createBitmap(result.cols(), result.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(result, resultBmp);
Constants.writeBitmapToFile(resultBmp);
public class WatershedSegmenter {
public Mat markers = new Mat();
public void setMarkers(Mat markerImage) {
markerImage.convertTo(markers, CvType.CV_32S);
}
public Mat process(Mat image) {
Imgproc.watershed(image, markers);
markers.convertTo(markers, CvType.CV_8U);
return markers;
}
}
我打算由源圖像和掩模繪製到屏幕外的畫布創建使用Android的PorterDuff模式的圖像。請讓我知道如果我在這裏做錯了什麼。