Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to save only ROI pixels in a Mat using a binary mask?

I have the binary mask of an image, in a Mat object. I want to pass only the ROI (Region Of Interest) pixels (set to white in binary mask), without the background (that is set to black in the binary mask) to a colour-quantisation module. Question is how?

I have tried the [copyTo](http://docs.opencv.org/java/org/opencv/core/Mat.html#copyTo(org.opencv.core.Mat)) method, but it adds the ROI to a new Mat, which is by default set to black. The black pixels are then considered in the colour quantization module, unlike what is intended (those are not ROI pixels).

Bounded rectangles, as far as I understand (have not tried), have the disadvantage of potentially adding extra background pixels that are not needed, as well as finding reasonable boundary values, which is not preferred in this case.

Any idea how to save a Mat with only the pixels needed (set) from a binary mask? It's the pixels that matter, thus it's fine if the image is reshaped, provided only ROI pixels are stored. It's a common CV problem, but I am yet to find a solution that worked.

I'm using OpenCV on Android (Java API), but it's the technique that matters the most (hopefully, if possible, with OpenCV example code for better understanding), that should be cross-platform.

Thank you in advance.

How to save only ROI pixels in a Mat using a binary mask?

I have the binary mask of an image, in a Mat object. I want to pass only the ROI (Region Of Interest) pixels (set to white in binary mask), without the background (that is set to black in the binary mask) to a colour-quantisation module. Question is how?

I have tried the [copyTo](http://docs.opencv.org/java/org/opencv/core/Mat.html#copyTo(org.opencv.core.Mat)) method, but it adds the ROI to a new Mat, which is by default set to black. The black pixels are then considered in the colour quantization module, unlike what is intended (those are not ROI pixels).

Bounded rectangles, as far as I understand (have not tried), have the disadvantage of potentially adding extra background pixels that are not needed, as well as finding reasonable boundary values, which is not preferred in this case.

Any idea how to save a Mat with only the pixels needed (set) from a binary mask? It's the pixels that matter, thus it's fine if the image is reshaped, provided only ROI pixels are stored. It's a common CV problem, but I am yet to find a solution that worked.

I'm using OpenCV on Android (Java API), but it's the technique that matters the most (hopefully, if possible, with OpenCV example code for better understanding), that should be cross-platform.

Thank you in advance.