Ask Your Question
0

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

asked 2013-06-06 05:09:39 -0600

user1446598 gravatar image

updated 2020-11-07 11:58:26 -0600

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.

edit retag flag offensive close merge delete

Comments

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.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-06-06 06:35:01 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
0

answered 2013-06-09 14:09:30 -0600

user1446598 gravatar image

Thanks for the answers and comments, they have been useful.

The problem has been resolved by iterating over the image's pixels manually and keeping only those where the mask's bit is set to white.

I've tried this before but it didn't work. What I've noticed to be the reason, if not other factors as well:

  • The binary mask was at one point converted to RGB and then back to a grayscale image (before going through the method that selects ROI pixels). I think this might have been the reason why pixels had values near, but not exactly to, black and white intensity values. For eg. black was not 0, but was 16. I thus used pixels within a certain proximity to the actual expected white colour of 255.
  • I also had a logical error in the code where I iterated over the pixels of the single-channel mask, as opposed to the three-channel image. This was noticed upon receiving values only for the first channel after colour quantisation.

    The two above points seemed to help with more reasonable results. Perhaps there is a better way, and perhaps this method is not completely error-free, it hasn't been tested enough, but hope it would be useful for someone facing similar bugs.

    The Mat object that contained the ROI pixels is not suitable for display where the pixels are not ordered as in the original image. It merely provides a container for all the available colour pixels, which is convenient for the quantisation method.

edit flag offensive delete link more

Comments

Good that the problems are solved. I accepted your input as solution, so that your questions show solved.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-06-13 07:42:57 -0600 )edit
2

answered 2013-06-06 06:49:01 -0600

Ben gravatar image

I suppose that you wrote your quantization module yourself? In that case you could add your ROI mask as extra input argument and check for every pixel, if it should be taken into account for your quantization or not by looking up your ROI mask.

Otherwise there has to be some kind of definition in your quantization module, which pixels are to be processed (e.g. a key color or a 4th image channel or a float matrix with negative values for background pixels or ...).

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-06-06 05:09:39 -0600

Seen: 1,888 times

Last updated: Jun 09 '13