Ask Your Question
3

How to directly process ROI of preview data and display the processed data on screen?

asked 2012-08-21 00:06:00 -0600

HawkWang gravatar image

updated 2012-09-30 04:12:48 -0600

Kirill Kornyakov gravatar image

I am processing the frame data in onPreviewFrame() function. I would like to change the left-top 1/4 part of the preview data to gray while keep the other part as colorful image. Because I am a beginner of OpenCV I don't know what is the best 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 method works, I still want to know whether there is a better method: can I directly process the ROI of original data and convert new whole data to a bitmap, and draw the bitmap on canvas. Is there any way I can do it?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
3

answered 2012-09-30 19:37:36 -0600

karlphillip gravatar image

Another approach:

// Assume "box" is a cvRect variable with the ROI dimensions

// Allocate space for a single-channel ROI (to store the grayscale ROI)
IplImage* gray_roi = cvCreateImage(cvSize(box.width, box.height), IPL_DEPTH_8U, 1);

// Allocate space for a temporary 3-channel image
IplImage* rgb_roi = cvCreateImage(cvSize(box.width, box.height), IPL_DEPTH_8U, 3);

// vid_frame is the original image. Let's set the ROI and perform a grayscale conversion.
cvSetImageROI(vid_frame, box);
cvCvtColor(vid_frame, gray_roi, CV_BGR2GRAY);

// Copy the processed image back to the original image
cvCvtColor(gray_roi, rgb_roi, CV_GRAY2BGR);
cvCopy(rgb_roi, vid_frame, NULL);

// Now reset the ROI so cvShowImage displays the full image
cvResetImageROI(vid_frame);
cvShowImage("Example", vid_frame);
edit flag offensive delete link more

Comments

Hi karlphillip, you do an excellent work on SO, and I welcome you here. But with this said, the question is clearly for Android's opencv, so your solution is confusing for a beginner.

Rui Marques gravatar imageRui Marques ( 2012-10-03 16:35:15 -0600 )edit

Hi Rui, thanks. You might be right, but since the OpenCV docs are available, it's merely a matter of translating one language to another. I trust that the user that asked the question can read the docs :D

karlphillip gravatar imagekarlphillip ( 2012-10-03 17:26:21 -0600 )edit

I have presented an alternative solution, see if you agree with it ;)

Rui Marques gravatar imageRui Marques ( 2012-10-03 17:41:45 -0600 )edit
1

answered 2012-10-03 17:40:29 -0600

Rui Marques gravatar image

updated 2012-10-03 17:42:56 -0600

This solution is inspired by the Sepia filter that you can find in an OpenCV4Android Sample "Image Manipulations".

It might be a bit slow, because it does more processing than it should for a grayscale conversion, but it does not need to make image copies like other solutions.

// Because you mentioned changing parameter from yuv to rgba
// you don't need to change it, you can convert it like this:
Imgproc.cvtColor(mYuv, mRgba, Imgproc.COLOR_YUV420sp2RGB, 3);

// define some rectangle to be the roi
Rec roi = new Rect( arguments );

// This kernel tries to apply this formula:
// gray pixel = (red + green + blue) / 3 =>  each channel has a weight of 1/3 = 0.333%
mGrayKernel = new Mat(3, 3, CvType.CV_32F);
mGrayKernel.put(0, 0, /* R */0.333f, 0.333f, 0.333f);
mGrayKernel.put(1, 0, /* G */0.333f, 0.333f, 0.333f);
mGrayKernel.put(2, 0, /* B */0.333f, 0.333f, 0.333f);

Core.transform(mRgba.submat(roi), mRgba.submat(roi), mGrayKernel);

Let us know if you have any problem with this, or if you prefer @karlphillip's solution in Java.

edit flag offensive delete link more

Comments

+1 I think this solution might be a little slower than the other one, but it's interesting.

karlphillip gravatar imagekarlphillip ( 2012-10-03 18:57:59 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2012-08-21 00:06:00 -0600

Seen: 1,369 times

Last updated: Oct 03 '12