We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › image remap
Page Index Toggle Pages: 1
image remap (Read 897 times)
image remap
Mar 16th, 2007, 7:22pm
 
hi all,

i have this problem:

i have 3 images: original.jpg, reference.jpg and lookup.jpg
reference.jpg and lookup.jpg are the same size.

i want to compare the color values in original.jpg with the color values in reference.jpg. if it finds the same color value in reference.jpg, it should remember the position of that color value in reference.jpg. then gets the same position in lookup.jpg and replace the color value of original.jpg to the color of lookup.jpg.

(original.jpg contains an image of an object with world-space normals, reference.jpg contains a sphere with world-space normals, lookup.jpg contains a chrome sphere)

i tried some code, but ended up in nested for() loops. the code is very slow and processing hangs.
does someone has a workaround?

here is my code:

PImage original;
PImage reference;
PImage lookup;

void setup()
{
 original = loadImage("teapot.jpg");
 reference = loadImage("reference.jpg");
 lookup = loadImage("lookup.jpg");
 size(360,270);
 noStroke();
 background(255);
 smooth();
}

void draw()
{
 for (int orig_y=0; orig_y<original.height; orig_y=orig_y+1) {
   for (int orig_x=0; orig_x<original.width; orig_x=orig_x+1) {
     int orig_loc = orig_x + orig_y*original.width;
     float orig_color = original.pixels[orig_loc];
     for (int ref_y=0; ref_y<reference.height; ref_y=ref_y+1) {
       for (int ref_x=0; ref_x<reference.width; ref_x=ref_x+1) {
         int ref_loc = ref_x + ref_y*reference.width;
         float ref_color = reference.pixels[ref_loc];
         float lookup_color = lookup.pixels[ref_loc];
         if ( orig_color == ref_color ) {
           orig_color = lookup.pixels[ref_loc];
         }
         image(original,0,0);
       }
     }
   }
 }
}
Re: image remap
Reply #1 - Mar 17th, 2007, 12:26pm
 
A few things.

The increment operator "++" is faster than x=x+1 or x+=1. Also, you seem to understand how to access pixels[] without understanding that to run through the whole thing, well, you just run through the whole thing. And you need to be aware of loadPixels() and updatePixels(). Select them in the code in the IDE and press Ctrl+Shft+F to look them up. You won't get very far in bitmap analysis without them.

I've cobbled together how your brief should work with some internally generated PImages.

Code:

PImage original;
PImage reference;
PImage lookup;
void setup(){
// size() ALWAYS GOES FIRST *stern wagging of finger
size(360,270);
original = randomPImage(360, 270);//loadImage("teapot.jpg");
reference = randomPImage(360, 270);//loadImage("reference.jpg");
lookup = greenPImage(360, 270);//loadImage("lookup.jpg");
noStroke();
background(255);
smooth();
}
// YOUR BRIEF:
// i want to compare the color values in original.jpg with the color values in reference.jpg.
// if it finds the same color value in reference.jpg, it should remember the position of that
// color value in reference.jpg. then gets the same position in lookup.jpg and replace the color
// value of original.jpg to the color of lookup.jpg
void draw(){
image(original, 0, 0);
}
void mousePressed(){
// This operation only needs to be performed once
for(int i = 0; i < original.pixels.length; i++){
if(original.pixels[i] == reference.pixels[i]){
original.pixels[i] = lookup.pixels[i];
}
}
original.updatePixels();
}

PImage randomPImage(int wide, int high){
PImage temp = new PImage(wide, high);
for(int i = 0; i < temp.pixels.length; i++){
temp.pixels[i] = color(random(100, 110));
}
return temp;
}
PImage greenPImage(int wide, int high){
PImage temp = new PImage(wide, high);
for(int i = 0; i < temp.pixels.length; i++){
temp.pixels[i] = color(0, 255, 0);
}
return temp;
}
Re: image remap
Reply #2 - Mar 17th, 2007, 4:20pm
 
hi st33d,

thank you for the fast reply.
just tried your code and found out that it only works, if the reference.jpg and the lookup.jpg contains the same object as in original image.
in my case, the pixel colors of original.jpg are the same as in reference.jpg but at different locations. so i have to look at every pixel color in original.jpg, find its corresponding color and position in reference.jpg, then replace the original pixel with the new color from lookup.jpg.

here is an illustration of that:
http://www.magmastream.de/image-remap.jpg
Re: image remap
Reply #3 - Mar 17th, 2007, 5:10pm
 
I'm doubtful that will actually work.

For starters you can't use jpgs. Even at zero compression you lose information. I would suggest saving them as pngs and importing them that way. Plus the shadows you're getting on the 3D object are likely not represented on your reference swatch.

I've put together a snippet that does what you want. On a lack of a match it's going to return white - white patches on your original will tell if your plan is flawed. Do tell if it works.

Code:

PImage original;
PImage reference;
PImage lookup;
void setup(){
size(360,270);
original = randomPImage(360, 270);//loadImage("teapot.jpg");
reference = greyRef(360, 270);//loadImage("reference.jpg");
lookup = greenLook(360, 270);//loadImage("lookup.jpg");
noStroke();
background(255);
smooth();
}
void draw(){
image(original, 0, 0);
}
void mousePressed(){
// This operation only needs to be performed once
for(int i = 0; i < original.pixels.length; i++){
original.pixels[i] = lookUp(i);
}
original.updatePixels();
}
int lookUp(int num){
for(int i = 0; i < reference.pixels.length; i++){
if(reference.pixels[i] == original.pixels[num]){
return lookup.pixels[i];
}
}
return(255);
}
PImage randomPImage(int wide, int high){
PImage temp = new PImage(wide, high);
for(int i = 0; i < temp.pixels.length; i++){
temp.pixels[i] = color(random(256));
}
return temp;
}
PImage greenLook(int wide, int high){
PImage temp = new PImage(wide, high);
for(int i = 0; i < temp.pixels.length; i++){
temp.pixels[i] = color(0, i % 256, 0);
}
return temp;
}
PImage greyRef(int wide, int high){
PImage temp = new PImage(wide, high);
for(int i = 0; i < temp.pixels.length; i++){
temp.pixels[i] = color(i % 256);
}
return temp;
}
Re: image remap
Reply #4 - Mar 17th, 2007, 7:43pm
 
thank you very much.
it works, but as you said there is a lack of match. i don´t get white colors but a "tolerence"-method or interpolation could help.
here is a result (~1min to compute):
http://www.magmastream.de/before-after.jpg
Page Index Toggle Pages: 1