Reputation: 808
After hours and hours of searching, I'm relying on your expertise! Please help!
Basically, I'm trying to access the data from individual pixels in a greyscale image.
Here's a link to the image I'm talking about:- http://s14.postimg.org/ak092kza5/WORKS.jpg?noCache=1382913709 (it should be all black. I am aware I could just color it in, but that's not what I want to learn - I want to find out how I can remove the noise!)
public static void handlesinglepixel(int x, int y, int pixel) {
int alpha = (pixel >> 24) & 0xff;
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel) & 0xff;
// Deal with the pixel as necessary...
}
public static void handlepixels(Image img, int x, int y, int w, int h) {
int[] pixels = new int[w * h];
PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);
try {
pg.grabPixels();
} catch (InterruptedException e) {
System.err.println("interrupted waiting for pixels!");
return;
}
if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
System.err.println("image fetch aborted or errored");
return;
}
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
handlesinglepixel(x + i, y + j, pixels[j * w + i]);
}
}
}
So using this I can access the individual pixels. I am really happy! But...I now want to compare the adjacent pixels to see if they are abnormally lighter than the other pixel. I'm doing this because I want to remove noise from a pic. Any advice?
P.S I tried using RescaleOp and changed the brightness factor of all pixels, then multiplied them down again, but it just made the image unrecogniseable. I'm really stuck as to how to remove the noise!
I look forward to reading your responses.
Upvotes: 0
Views: 1104
Reputation: 19333
This depends greatly on the type of noise you are removing. If you're encountering a case where some neighbouring pixels are much brighter than the majority of pixels in a set region, you're generally doing some type of 2D filtering with a simple kernel.
We are missing some information from you, such as the type of noise. So I'll proceed the best I can with limited information on hand.
First, what constitutes "too bright"? Is "too bright" just being a few standard deviations higher than neighbouring pixels in intensity, or is it pixels that are basically black-or-white?
In the latter case, it is salt-pepper / impulse noise removal.
You basically do a 3x3 spatial filter, and select the mode or median value. Voila, no more overly bright pixels. However, this will effectively blur your image a little, so you might consider using adaptive filtering instead, so you only modify a pixel if it's brighter than the surrounding pixels, rather than modifying ALL pixels.
Lastly, we must consider that you simply want to remove pixels that are brighter than the surrounding pixels according to some statistical value, rather than the pixels just being potentially extreme outliers (ie: impulse/salt-pepper noise). This final approach is very expensive in terms of CPU time, but lets you handle local extrema.
http://www.mathsisfun.com/data/standard-deviation.html
If you were to combine this statistical method with an adaptive filtering technique (ie: detecting which pixels need to be modified, rather than blindly modifying ALL available pixels), you can remove the problem pixels without the overall image quality suffering by any noticeable amount.
Lastly, you should also consider looking into the LaPlace operator, with respect to image processing. It serves as a simple edge-detector, and might also steer you in the right direction.
http://homepages.inf.ed.ac.uk/rbf/HIPR2/log.htm
Good luck!
Upvotes: 2
Reputation: 713
If you just want to compare the brightness of two pixels, it might be an idea to use the HSV color model, where the V stands for Value (lightness, basically). You can convert between RGB and HSV with a relatively simple algorithm. According to the second link, the java.awt.Color
package already has these algorithms built in.
Upvotes: 0