i am in my final year Engg.i wish to implement sketch generation algorithm with a little modification for my final year project.For this i wish to access and modify the pixel intensity values(not in terms of RGB but in terms in numerical number as presented in Matlab).Though the algorithm works fine in matlab,but due to the requirement of the project,i intend to do it in Java. Going through the internet and various Java forums hasnt helped me.
The Matlab code allows me to access pixel using following snippets
The pixel intensity value at 120,234 is given by "A(120,234)" where A is the name of the image under consideration.
Similarly i would like to access the pixel intensity values of the image in Java and modify them with the algorithm.
i would be very glad if someone helps me out.
Thanks in advance
2 Answers
Since you have access to matlab, I'd suggest digging into their code, assuming their image stuff is written in Matlab, which I think it is, and see how they convert RGB into intensity. Are they using HSL (Hue-Saturation-Luminance)? Or some other color conversion. Knowing that, you can find Java code to convert, for instance, RGB into HSL.
Edit:
As per the comments on this question, I think this code will work. Its not complete as I didn't copy and re-write all the manipulation, but it should give you the idea.
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class Convolution {
public static void main( String[] args ) throws Exception {
File inputFile = new File("apple.jpg");
BufferedImage bufferedImage = ImageIO.read(inputFile);
int w = bufferedImage.getWidth();
int h = bufferedImage.getHeight();
System.out.println("w=" + w + ", h=" + h);
// Get Pixels
int[] image = new int[w * h];
bufferedImage.getRGB(0, 0, w, h, image, 0, w);
// Convert to simple grayscale
for ( int y = 0; y < h; y++ ) {
for ( int x = 0; x < w; x++ ) {
int idx = ( y * w ) + x;
int p = image[idx];
int r = p & 0x00FF0000 >> 16;
int g = p & 0x0000FF >> 8;
int b = p & 0x000000FF;
image[idx] = (int) ( ( r + g + b ) / 3.0 );
}
}
int convolutionSize = 3;
int[][] convolution = { { 0, -1, 0 }, { -1, 4, -1 }, { 0, -1, 0 } };
int[] newImage = new int[w * h];
// Apply the convolution to the whole image, note that we start at
// 1 instead 0 zero to avoid out-of-bounds access
for ( int y = 1; y + 1 < h; y++ ) {
for ( int x = 1; x + 1 < w; x++ ) {
int idx = ( y * w ) + x;
// Apply the convolution
for ( int cy = 0; cy < convolutionSize; cy++ ) {
for ( int cx = 0; cx < convolutionSize; cx++ ) {
int cIdx = ( ( ( y - 1 ) + cy ) * w )
+ ( ( x - 1 ) + cx );
newImage[idx] += convolution[cy][cx] * image[cIdx];
}
}
// pixel value rounding
if ( newImage[idx] < 0 ) {
newImage[idx] = -newImage[idx];
} else {
newImage[idx] = 0;
}
if ( newImage[idx] > 0 ) {
newImage[idx] = 120 - newImage[idx];
} else {
newImage[idx] = 255;
}
}
}
// Convert to "proper" grayscale
for ( int y = 0; y < h; y++ ) {
for ( int x = 0; x < w; x++ ) {
int idx = ( y * w ) + x;
int p = newImage[idx];
newImage[idx] = 0xFF000000 | ( p << 16 ) | ( p << 8 ) | p;
}
}
// Set the image to have the new values;
bufferedImage.setRGB(0, 0, w, h, newImage, 0, w);
// Write the new image as a PNG to avoid lossey compression,
// and its eaiser than trying to display an image in Java.
ImageIO.write(bufferedImage, "png", new File("new_apple.png"));
}
}
Edit:
Modified the code to work as expected. Its not fast, but it works.
Before:
After:

- 1,160
- 1
- 7
- 22
-
1Some code: http://www.f4.fhtw-berlin.de/~barthel/ImageJ/ColorInspector//HTMLHelp/farbraumJava.htm – Chris Dennett Feb 08 '11 at 17:56
-
Thanks for the suggestion.i did so as u mentioned,but there seems no proper mention of Matlabs way of obtaining the intensity values. In java,the values which i get is a six to eight digit number and that too in negative number.i have tried various classes in java like Raster and Color classes but they werent useful enough. – jack1689 Feb 09 '11 at 17:23
-
Is this a color or grayscale image? What format is it in, JPEG, GIF, ...? Could you give the code you are using to load image in matlab. I assume you are doing `A = imread(filename)` and then manipulating the matrix `A` directly. Is this correct? – troutinator Feb 09 '11 at 19:37
-
The image which i would be using can be both.(color as well as grey scale). The format can be JPEG or TIFF.Yes as you mentioned, i would be doing the above operations.The Matlab code is as follows. http://pastebin.com/zLdXaUPu. thanks for the interest . – jack1689 Feb 10 '11 at 13:34
-
It looks like when you read an image with `imread` in MatLab it is simply returning the value of the bits representing the pixel. Its not doing any color conversions. So `A(i,j)` returns a the R,G,B channels of that pixel. See my modified question with some code that I think will work. – troutinator Feb 10 '11 at 15:31
-
I cleaned up the code a bit and finished the translation. This produces an output, but I'm not sure what its supposed to look like. – troutinator Feb 14 '11 at 14:33
-
thank you very much for the effort.i tried using the code now. but it was giving an "ArrayIndexOutOfBounds exception". i tried to avoid it by using the try catch statements and looked at the output. The ouptut is not the one i wanted. Basically u wanted an image which would be a sketch i.e a black and white image.The statement which we have used ... " 0 * J[((y - 1) * h) + (x - 1)] - 1 * J[((y) * h) +..........;" is the convolution of the image I with a Laplacian operator[0 -1 0 ;-1 4 -1; 0 -1 0] in matlab as written in java; – jack1689 Feb 14 '11 at 17:22
-
the image which i would prefer is as follows in the link http://img251.imageshack.us/i/grand1.jpg/ the image which was obtained via java is as follows http://img510.imageshack.us/i/newgrand.png/ this is because the image is first converted into its corresponding sketch acc to the requirement of the project. i tried finding the value of the image pixel intensity values. which i expected to be in the range of 0-255 but the intensity values were huge. – jack1689 Feb 14 '11 at 17:24
-
Thats because they aren't intesenty values. An image is stored as a series of pixels, each made up of three colors Red, Green, Blue. In many cases each color is allotted 8-bits, giving it a range of 0-255. However, due to the inner working of computers all three colors are stored in a single integer. So, depending on the implementation you have an itneger which is 4-bytes wide, 3 of which are for the color of the pixel. This is why in HTML colors are written in hex, eg yellow is 0xFFFF00 which is R=255, G=255, B=0. In decimal it would be 65,280. – troutinator Feb 15 '11 at 17:38
-
I may give it another shot after some more thought if I have a chance. Now that you explained that its the convolution you are after a different approach might be in order. – troutinator Feb 15 '11 at 17:41
-
thank you very much for your support and help. i have understood the code in java thanks to ur help. – jack1689 Feb 16 '11 at 18:34
-
Don't forget to accept the answer if it answered your questions. – troutinator Feb 18 '11 at 23:14
It seems to me that what you want to use is a bufferedImage, from which you can get a writableRaster which you can use to read/write pixel values.
See also this tutorial on a few ways to interact with a bufferedImage.

- 74,690
- 10
- 137
- 177
-
i tried to use bufferedImages and Raster classes.but they werent useful enough with the scope of the project.thanks for the help though :) – jack1689 Feb 09 '11 at 17:26