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 Pixel Position
Page Index Toggle Pages: 1
Image Pixel Position (Read 1417 times)
Image Pixel Position
Apr 6th, 2010, 2:21pm
 
Hi All,

I have one image in which I have simply modified a pixel value to yellow. I save that image onto the hard disk and load the modified (new image with the yellow pixel) again to see that the pixel location is different that was expected or saved (the pixel location is determined by the mouse click). Following is the code excerpt

Code:

if (mousePressed)
{
ellipse(mouseX,mouseY,2,2);

/*
* This part is if you want to change the color of the                 pixel under the mouse click
*/


int pixelColorYellow = color(254, 255, 0);

img.set(mouseX,mouseY, pixelColorYellow);
img.updatePixels();

img.get().save(fileName.substring(0, fileName.length()-4).concat("_Modified.jpg")); // This does save the file on the disk

}

if (key =='L')
{

imgModified = loadImage(fileName.substring(0, fileName.length()-4).concat("_Modified.jpg"));
image(imgModified,screenWidth-472,100,412,412);
}



Any help or insight would be great!

Thanks!
Re: Image Pixel Position
Reply #1 - Apr 7th, 2010, 5:49am
 
Let me guess: as suggested by your last line, you don't draw the image in 0, 0 coordinates.
So, instead of setting the pixel at mouseX, mouseY, you must set it at mouseX - imageOffsetX, mouseY - imageOffsetY.
Re: Image Pixel Position
Reply #2 - Apr 7th, 2010, 2:48pm
 
Thanks PhiLho. I didn't mention the other part of the code where after changing the pixel he has to PImage.save the imageg by hitting the key "S".

The Image is being saved onto the filesystem after being modified by the img.set() and then img.save() but when a loadImage is performed to load the same saved image, it shows the pixel but on a different location.

Am attaching the code with the missing part.

Thanks again, appreciate your input Smiley
Code:

void draw()
{
if (mousePressed)
{
ellipse(mouseX,mouseY,2,2);

/*
* This part is if you want to change the color of the     pixel under the mouse click
*/


int pixelColorYellow = color(254, 255, 0);

img.set(mouseX,mouseY, pixelColorYellow);
img.updatePixels();

img.get().save(fileName.substring(0, fileName.length()-4).concat("_Modified.jpg")); // This does save the file on the disk

}



if (key =='L')
{

imgModified = loadImage(fileName.substring(0, fileName.length()-4).concat("_Modified.jpg"));
image(imgModified,screenWidth-472,100,412,412);
}

if (key=='S' || key=='s')
{
if (fileName != "")
{

// Saving the modified image to be loaded for Simulation
// If the original Medical Image has been modified then save it with a new name for Simulation
if (img.isModified())
{
img.get().save(fileName.substring(0, fileName.length()-4).concat("_Modified.jpg"));
}
}
else
{
System.out.println("Cannot save without an image being loaded first!");
}
}
}

Re: Image Pixel Position
Reply #3 - Apr 8th, 2010, 1:21am
 
Please, re-read my answer. If I am missing something, just let me know.
Re: Image Pixel Position
Reply #4 - Apr 8th, 2010, 1:07pm
 
Kindly pardon my ignorance, can you guide me through in determining the ImageOffset in my case?

What I understand from your post is that when a users mouse clicks the actual pixel (or mouse click) is not on the image itself rather at some offset. Right?

Thanks

Re: Image Pixel Position
Reply #5 - Apr 9th, 2010, 1:17am
 
If you draw the image to alter at coordinates 0, 0 of the sketch area, then mouseX, mouseY are coordinates on this area and on the image.
If you draw it, say at 100, 100, then mouseX at 250 points to the pixel at x=150 in the image.
Re: Image Pixel Position
Reply #6 - Apr 9th, 2010, 5:45pm
 
Hi PhiLho,

Thanks a bunch, appreciate your help.

I got it right, I guess I was considering the whole screen resolution. Adjusted the imageoffset and voila!

Thanks again Smiley
Page Index Toggle Pages: 1