There are many ways to get image / pixel data into and out of opencv objects. Perhaps start with the simplest:
- create n PImages, one for each channel, and fill them black
- in the loop of your nearestpixel function
- depending on result, write the results to each pixel as a white dot into the correct mask image
Now you have n masks.
- load each mask into opencv with
opencv = new OpenCV(this, img);
- close holes with
opencv.dilate(); opencv.erode();
– you can also experiment with blur
Now you have n masks with closed holes.
For basic examples of moving color and grayscale pixels in and out of opencv object, see:
There are probably much faster ways of running your custom threshhold directly on the OpenCV buffer – I haven’t worked with it recently, so I’m not sure – but this is a great quick starting point to see if dilate+erode actually does what you want. If not, you may not need opencv at all, so no need to optimize.