Ask Your Question
1

Is there cvSetImageROI function in OpenCV on Android?

asked 2012-08-17 00:23:30 -0600

this post is marked as community wiki

This post is a wiki. Anyone with karma >50 is welcome to improve it.

Hi, I can't find the cvSetImageROI like function in Android OpenCV 2.4.2 SDK. How can I set a ROI on android application? Thank you in advance.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
4

answered 2012-08-17 03:35:03 -0600

Rui Marques gravatar image

Image ROI in OpenCV4Android:

Mat roi = original.submat( arguments );

  • arguments can be a Rect object, can be (int rowStart, int rowEnd, int colStart, int colEnd) and (Range rangeStart, Range rangeEnd).
edit flag offensive delete link more

Comments

Rui, thank you. I think this one works. I will try the method and post result here today.

HawkWang gravatar imageHawkWang ( 2012-08-19 20:18:54 -0600 )edit

I've just tried your method and it works. Thank you. But here comes an interesting question.

Suppose I am processing the preview data in onPreviewFrame, with a parameter "data" contains the preview data. Now I want to only change the left-top 1/4 of the preview image to gray while keep the other part of the image as colorful one. That means I have to set ROI to left-top 1/4 of the image and call cvtColor function. Previously, I tried: Imgproc.cvtColor(original_data, mRgba, Imgproc.COLOR_YUV420sp2RGB, 4);

Mat graySubmat = mRgba.submat(0,0,width/2,height/2);

Imgproc.cvtColor(graySubmat, mRgbaGray, Imgproc.COLOR_GRAY2RGBA, 4);

mRgbaGray.copyTo(graySubmat);

But exception happens to such code: error: (-215) scn == 1 && (dcn == 3 || dcn == 4) in function void cv::cvtColor

HawkWang gravatar imageHawkWang ( 2012-08-19 21:11:52 -0600 )edit

I think you should use another method to convert to gray. Long story short, cvtColor is more appropriate to convert full images instead of ROI's. Because cvtColor in that case wil convert a 4 channel image to a 1 channel and i think it is not possible to do that for a roi only.

Rui Marques gravatar imageRui Marques ( 2012-08-20 03:59:23 -0600 )edit

Yes, Rui. I agree with you. But because I am beginner of OpenCV I don't know what is the proper method. So I use a workaround: 1. change the data parameter of onPreviewFrame from yuv to rgb, then use matToBitmap() function to convert it to a bitmap. 2. change the ROI data to a gray bitmap (COLOR_GRAY2RGBA, and then matToBitmap()). 3. draw the gray bitmap onto the first bitmap I get 4. draw the bitmap on canvas. Although this new method works, I still want to know whether there is a better method: only process the ROI of original data and convert new data to a bitmap, and draw the bitmap on canvas. Do you have any idea?

HawkWang gravatar imageHawkWang ( 2012-08-20 07:43:20 -0600 )edit

You should open a new question explaining your 'new' problem so more people see it.

Rui Marques gravatar imageRui Marques ( 2012-08-20 12:18:41 -0600 )edit

OK Rui, I will do that

HawkWang gravatar imageHawkWang ( 2012-08-20 23:59:08 -0600 )edit

By the way, when you find that one answer is correct you should mark that answer as the correct one. It informs other people and gives reputation to whom answered :)

Rui Marques gravatar imageRui Marques ( 2012-08-31 04:21:31 -0600 )edit
0

answered 2012-08-17 03:09:12 -0600

this post is marked as community wiki

This post is a wiki. Anyone with karma >50 is welcome to improve it.

I Have yet to use OpenCV on android. And I'm not sure if it has a built in function. Anyway, I think this should work:

Mat normalImage;
Rect roi = new Rect(x, y, width, height);
Mat cropped = new Mat(normalImage, roi);

And if you want it as function then this might work:

private Mat croppMat(Mat normalImage, Rect roi)
{
    Mat cropped = new Mat(normalImage, roi);
    return cropped;
}

The usage of function I guess would be something like this:

myROIMat = croppMat(uncroppedImage, new Rect(x,y,width,height));
edit flag offensive delete link more

Comments

Thank you XCoder, but I am not sure your method works or not because I don't know wether the croppMat function is a "copy" method or a "pointing" method. to set the ROI we should use "pointing" method. Anyway, I will try your method later and I will post a response here today.

HawkWang gravatar imageHawkWang ( 2012-08-19 20:18:09 -0600 )edit

XCoder, I've just tried your method and it works. Thank you. But here comes an interesting question. Suppose I am processing the preview data in onPreviewFrame, with a parameter "data" contains the preview data. Now I want to only change the left-top 1/4 of the preview image to gray while keep the other part of the image as colorful one. That means I have to set ROI to left-top 1/4 of the image and call cvtColor function. Previously, I tried: Imgproc.cvtColor(original_data, mRgba, Imgproc.COLOR_YUV420sp2RGB, 4);

Mat graySubmat = croppMat(0,0,width/2,height/2); //Set ROI to left-top 1/4 image Mat mRgbaGray;

Imgproc.cvtColor(graySubmat, mRgbaGray, Imgproc.COLOR_GRAY2RGBA, 4);

mRgbaGray.copyTo(graySubmat);

But exception happens to such code. Idea?

HawkWang gravatar imageHawkWang ( 2012-08-19 21:09:16 -0600 )edit

I have never tried gray scaling only ROI. Not really sure how it's done, but I've merged matrices together. You can extract your ROI's and do whatever you want with them, and then put them back together.

XCoder gravatar imageXCoder ( 2012-08-23 06:30:30 -0600 )edit

XCoder, can you kindly let me know how to merge matrices together and extract required ROI ?

Sagar123 gravatar imageSagar123 ( 2017-03-26 22:34:29 -0600 )edit

Question Tools

Stats

Asked: 2012-08-17 00:23:30 -0600

Seen: 4,979 times

Last updated: Aug 17 '12