Hi
I’m struggling with ofPixels. accessing pixels and change r,g,b information. my source is following.
ofImage image;
ofPixels pixs;
void ofApp::setup() {
ofSetFrameRate(60);
ofSetWindowTitle("openFrameworks");
ofBackground(239);
ofSetLineWidth(2);
ofEnableDepthTest();
image.load("angelinaDanilova.jpg");
ofSetWindowShape(image.getWidth(), image.getHeight());
}
//--------------------------------------------------------------
void ofApp::update() {
pixs = image.getPixelsRef();
for (ui i = 0; i < pixs.getHeight(); i++)
{
for (ui j = 0; j < pixs.getWidth(); j++)
{
ui idx = i * image.getWidth() + j;
ofColor pix = pixs[idx];
// pix.r = pix.r;//(pix.r / 255) * 255;
// pix.g = pix.g;//(pix.g / 255) * 255;
// pix.b = pix.b;//(pix.b / 255) * 255;
pixs.setColor(idx, pix); //<-- pixs.setColor(i, j,pix), pis.setColor(j, i, pix)?
}
}
}
//--------------------------------------------------------------
void ofApp::draw() {
ofTexture data;
data.loadData(pixs);
data.draw(0, 0);
}
//--------------------------------------------------------------
int main() {
ofSetupOpenGL(720, 720, OF_WINDOW);
ofRunApp(new ofApp());
}
following image is result.
her head is missing…
I tried with pix.setColor(i, j, pixs), pix.setColor(j, i, pixs)… but failed.
I need help. thanks.
ofpixel
November 23, 2019, 2:07pm
#2
Make it simple
What happens if you only do
pixs = image.getPixels();
Without the rest below (cause it is at the moment redundant).
Maybe try with specifying the format. GL_RGBA or GL_RGB in both loads.
Just out of curiosity. Add this in the setup.
ofDisableAlphaBlending();
ofDisableArbTex();
Hi, @ofpixel . I tried with your solution. but the result is same.
your second suggestion that use format GL_RGBA. I tried with ofFbo but failed.
following is my source.
#include "ofApp.h"
using ui = unsigned int;
ofImage image;
ofPixels pixs;
void ofApp::setup() {
ofDisableArbTex();
ofDisableAlphaBlending();
ofSetFrameRate(60);
ofSetWindowTitle("openFrameworks");
ofBackground(239);
image.load("angelinaDanilova.jpg");
ofSetWindowShape(image.getHeight(), image.getWidth());
}
//--------------------------------------------------------------
void ofApp::update() {
pixs = image.getPixels();
for (ui i = 0; i < pixs.getHeight(); i++)
{
for (ui j = 0; j < pixs.getWidth(); j++)
{
ui idx = i * image.getWidth() + j;
ofColor pix = pixs[idx];
pix.r = pix.r;//(pix.r / 255) * 255;
pix.g = pix.g;//(pix.g / 255) * 255;
pix.b = pix.b;//(pix.b / 255) * 255;
pixs.setColor(idx, pix);
}
}
}
//--------------------------------------------------------------
void ofApp::draw() {
ofFbo fbo;
fbo.allocate(image.getWidth(), image.getHeight(), GL_RGBA);
fbo.begin();
ofTexture data;
data.loadData(pixs);
data.draw(0, 0);
fbo.end();
fbo.draw(0, 0);
}
//--------------------------------------------------------------
int main() {
ofSetupOpenGL(720, 720, OF_WINDOW);
ofRunApp(new ofApp());
}
but the result is same… sorry…
solved.
I was trying to write a code that manages ofPixels. but I found similar article at this site. I could same operator with image object. and now I know that what was problem. the problem was idx
(variable) that my cord calculate. it does not matched coordinate (x, y) of Image.
I achieved writing more readable code. following is my source. and also result.
#include "ofApp.h"
using ui = unsigned int;
ofImage image;
void ofApp::setup() {
ofSetFrameRate(60);
ofSetWindowTitle("openFrameworks");
ofBackground(239);
image.load("angelinaDanilova.jpg");
ofSetWindowShape(image.getHeight(), image.getWidth());
}
//--------------------------------------------------------------
void ofApp::update() {
for (ui i = 0; i < image.getHeight(); i++)
{
for (ui j = 0; j < image.getWidth(); j++)
{
ofColor col = image.getColor(j, i);
col.r = round(((double)col.r)/ 255) * 255;
col.g = round(((double)col.g)/ 255) * 255;
col.b = round(((double)col.b)/ 255) * 255;
image.setColor(j, i, col);
}
}
image.update();
}
//--------------------------------------------------------------
void ofApp::draw() {
image.draw(0, 0);
}
//--------------------------------------------------------------
int main() {
ofSetupOpenGL(720, 720, OF_WINDOW);
ofRunApp(new ofApp());
}