【发布时间】:2018-10-09 03:26:59
【问题描述】:
public class Grabcut extends Activity {
ImageView iv;
Bitmap bitmap;
Canvas canvas;
Scalar color = new Scalar(255, 0, 0, 255);
Point tl, br;
int counter;
Bitmap bitmapResult, bitmapBackground;
Mat dst = new Mat();
final String pathToImage = Environment.getExternalStorageDirectory()+"/gcut.png";
public static final String TAG = "Grabcut demo";
static {
if (!OpenCVLoader.initDebug()) {
// Handle initialization error
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grabcut_main);
iv = (ImageView) this.findViewById(R.id.imageView);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.grabcut);
Log.d(TAG, "bitmap: " + bitmap.getWidth() + "x" + bitmap.getHeight());
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
Log.d(TAG, "bitmap 8888: " + bitmap.getWidth() + "x" + bitmap.getHeight());
//GrabCut part
Mat img = new Mat();
Utils.bitmapToMat(bitmap, img);
Log.d(TAG, "img: " + img);
int r = img.rows();
int c = img.cols();
Point p1 = new Point(c/5, r/5);
Point p2 = new Point(c-c/5, r-r/8);
Rect rect = new Rect(p1,p2);
//Rect rect = new Rect(50,30, 100,200);
Log.d(TAG, "rect: " + rect);
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, 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, mask, Core.CMP_EQ);
Mat foreground = new Mat(img.size(), CvType.CV_8UC3, new Scalar(255, 255, 255));
img.copyTo(foreground, mask);
Core.rectangle(img, p1, p2, color);
Mat background = new Mat();
try {
background = Utils.loadResource(getApplicationContext(),
R.drawable.wall2 );
} 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 */);
//Imgproc.threshold(tempMask, tempMask, 254, 255, 1 /* THRESH_BINARY_INV */);
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);
//convert to Bitmap
Log.d(TAG, "Convert to Bitmap");
Utils.matToBitmap(dst, bitmap);
iv.setBackgroundResource(R.drawable.wall2);
iv.setImageBitmap(bitmap);
//release MAT part
img.release();
imgC3.release();
mask.release();
fgdModel.release();
bgdModel.release();
}
public void debugger(String s){
Log.v("","########### "+s);
}
}
我已经按照上面的教程进行操作。但问题是我得到的输出图像比我的输入图像颜色更亮。为什么会这样以及如何解决? 我的输入图像是Here,输出图像是here。输出图像实际上是我的应用程序的屏幕截图,其中大的为输入图像,黑色背景的小图像为输出图像。
【问题讨论】:
-
提供minimal reproducible example,以及实际有用的示例输入图像(部分图像被其他东西遮挡的屏幕截图绝对不是)。
-
我不知道代码的哪一部分导致了问题,这就是为什么我给出了我使用过的整个代码的链接。对于输入图像图像,我已经更新了我的问题
-
是的,但最好先准确地展示你所拥有的东西。该页面包含多个 sn-ps 代码,因此现在选择正确的代码有点猜测(另外,我们更喜欢在问题中包含代码,因为外部资源可能会更改、移动或完全消失)。 |尝试做一些简单的调试 - 将所有中间的
Mats 保存到磁盘作为 PNG 图像(JPEG 是有损的并且会添加伪影)并仔细检查它们以发现它们开始偏离您的期望的地方。 -
感谢您的建议。我已经更新了我的问题。
标签: opencv image-processing opencv3.0 image-segmentation opencv4android