Faking less bits for color
in
Contributed Library Questions
•
1 year ago
I'm using openCV and P2D to to get a live image from my webcam, get the red / green / blue value of each pixel, and then floor the values to a multiple of a power of 2. For example, if I set a variable called bitsPerChannel to 2 then that means each pixel has 4 possible values for each color channel. The reason I'm doing this is to turn try to see if it would be feasible to turn the pixels into vector blobs live (too many colors means too many shapes). The way I'm flooring every pixel is:
int bitsPerChannel = 2;
import hypermedia.video.*;
OpenCV opencv;
void setup() {
size(640, 480, P2D);
opencv = new OpenCV(this);
opencv.capture(width, height);
if (bitsPerChannel < 1) bitsPerChannel = 1;
if (bitsPerChannel > 8) bitsPerChannel = 8;
}
void draw() {
loadPixels();
opencv.read();
image(opencv.image(), 0, 0);
for (int i = 0; i < width*height; i++) {
int[] channel = new int[3];
channel[0] = int(red(pixels[i]));
channel[1] = int(green(pixels[i]));
channel[2] = int(blue(pixels[i]));
for (int k = 0; k < 3; k++) {
channel[k] = int(map(channel[k], 0, 255, 0, pow(2, bitsPerChannel)));
channel[k] *= 256/pow(2, bitsPerChannel);
}
pixels[i] = color(channel[0], channel[1], channel[2]);
}
}
My question would be if there is a faster way to do this. I know using a smaller capture area would help, but I feel like this could be simplified / optimized somehow.
1