Ask Your Question
1

How to convert ROI in gray (monochrome) on android?

asked Feb 4 '14

Astrgan gravatar image

updated Dec 24 '15

Hello. How to convert ROI in gray (monochrome) on android?

I try that, but it does not work:

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

      Mat image = inputFrame.rgba();
      Rect roi = new Rect(300, 50, 50, 10);
      Imgproc.cvtColor(image.submat(roi), image.submat(roi), Imgproc.COLOR_RGBA2GRAY);
      return image;
}

and

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

   Mat image = inputFrame.rgba();
   Rect roi = new Rect(300, 50, 50, 10);
   Mat sub =image.submat(roi);
   Imgproc.cvtColor(sub, sub, Imgproc.COLOR_RGBA2GRAY);
   sub.copyTo(image.submat(roi));
   return image;

}

Preview: (hide)

Comments

See the answer here

Haris gravatar imageHaris (Feb 5 '14)edit
1

Actually this link might disappear in the future. To help future searches for this problem, I copied the solution here!

StevenPuttemans gravatar imageStevenPuttemans (Feb 5 '14)edit
1

sidenote, you can query a grayscale image from CvCameraViewFrame already:

Mat gray = inputFrame.gray();
berak gravatar imageberak (Feb 5 '14)edit

1 answer

Sort by » oldest newest most voted
3

answered Feb 5 '14

The the problem in above code is that you are trying to copy single channel Mat (GRAY) to multi channel Mat (RGBA).

So you need to convert GRAY to RGBA before copying back to original RGBA image.

Change your code to

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
   Mat image = inputFrame.rgba();
   Rect roi = new Rect(300, 50, 50, 10);
   Mat sub =image.submat(roi); 
   Imgproc.cvtColor(sub, sub, Imgproc.COLOR_RGBA2GRAY);
   Imgproc.cvtColor(sub, sub, Imgproc.COLOR_GRAY2RGBA);
   sub.copyTo(image.submat(roi));
   return image;
}

Copied from stackoverflow to provide a solution if the topic would disappear...

Preview: (hide)

Question Tools

Stats

Asked: Feb 4 '14

Seen: 2,298 times

Last updated: Feb 05 '14