Zain ul abdeen
Zain ul abdeen

Reputation: 155

OpenCV Android: Define and save ROI in new Mat

I am trying to slice (crop) part of my image to another so it can be worked on separately. I have found contours and now trying to save every contour in new Mat but it is giving error

Mat crop;
Imgproc.findContours(m, contours, new Mat() ,Imgproc.RETR_EXTERNAL , Imgproc.CHAIN_APPROX_SIMPLE);
for(int i=0; i <contours.size();i++)
{
    Rect rect = Imgproc.boundingRect(contours.get(i));
    crop = m.submat(rect);
}

Utils.matToBitmap(crop, bm);
ImageView iv = (ImageView) findViewById(R.id.imageView1);
iv.setImageBitmap(bm);

Here m is my Mat where image is saved

Error: Error

Upvotes: 0

Views: 1480

Answers (1)

Pedro Oliveira
Pedro Oliveira

Reputation: 20500

What I always do in this situation is to create a new mat using the constructor with a rect:

Mat cropped = new Mat(mOriginal, boudingRect);

Edit:

Your bitmap should also have the same size:

bm = Bitmap.createBitmap(crop.size().width,crop.size().height, Bitmap.Config.ARGB_8888);

Upvotes: 4

Related Questions