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
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.
What you suggesting will never work, since the binary mask has an unlimited shape and a matrix is always rectangular. You cannot simply initiate a part of a matrix with values and leave the rest of the values uninitialized.
What you shoud do is take a preselected value and put that at each position that you see as unwanted background, which could be zeros, and during processing you just ignore pixels with this 0 value.