Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.