Ask Your Question
1

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

asked 2014-02-04 10:21:32 -0600

Astrgan gravatar image

updated 2015-12-24 01:51:15 -0600

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;

}

edit retag flag offensive close merge delete

Comments

See the answer here

Haris gravatar imageHaris ( 2014-02-05 00:35:46 -0600 )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 ( 2014-02-05 03:25:35 -0600 )edit
1

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

Mat gray = inputFrame.gray();
berak gravatar imageberak ( 2014-02-05 03:58:47 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
3

answered 2014-02-05 03:26:38 -0600

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...

edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-02-04 10:21:32 -0600

Seen: 2,284 times

Last updated: Feb 05 '14