Quantcast
Viewing all articles
Browse latest Browse all 4929

Set White Pixels to Transparent

@eightlines wrote:

I've been looking into the simpleColorKey example which turns purple pixels to black. In my case I'd like to turn white pixels to transparent (I'm using a B&W JPG with no grey tones). The end result doesn't work as expected, and I can see the color change, but no transparency. Am I missing something simple?

include "ofApp.h"

void ofApp::setup(){
ofEnableAlphaBlending();
ofBackground(0, 0, 255);

keyed.setImageType(OF_IMAGE_COLOR_ALPHA);

source.load("image1.jpg");
ofPixelsRef pixels = source.getPixels();
for (uint y = 0; y < source.getHeight(); y ++) {
    for (uint x = 0; x < source.getWidth(); x ++) {
        ofColor color = source.getColor(x, y);
        int threshold = 150;
        if (color.r > threshold && color.g > threshold && color.b > threshold) {
            pixels.setColor(x, y, ofColor(255, 0, 0, 0));
        }
    }
}
keyed.setFromPixels(pixels);

}

void ofApp::draw(){
keyed.draw(0, 0);
}

Posts: 3

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 4929

Trending Articles