MATLAB: Change a single pixel value in a Grayscale JPEG image

change pixel valuegrayscale jpeg image

I have a grayscale jpeg image X. I want to change its pixel value. For example change pixel at (14,20) to 1,How can I do it. I tried the following but it doesn't WORK:
Xc=X; %Xc is image with a pixel value changed from X[X is original image]
X(14,20); %What is the value at 14,20
Xc(14,20)=1 %Change Pixel value at 14,20 to 1
When i view its values in Variable Editor,I don't get value changed at (14,20) in Xc image.
What are the Errors or suggestion?

Best Answer

Are you sure you are checking Xc in the variable editor, you might be checking X (original image). the above does work..
I have tried
X=imread('1.jpg') % My image was color so was MxNxP
Xc=X(:,:,1) % crateda grayscale image MxN
Xc(14,20) % here my output was ans=129
Xc(14,20)=1 % changed the value of (14,20) to 1
Xc(14,20) % here my output was ans=1
If I change a block of values in Xc to 1 and do the imshow(Xc) - gives me a black box..