changing values of pixels in an image pixel by pixel (image in .jpeg format)

Hi everyone, I'm trying to replace all pixels with value 255 (on a grayscale image) to 249 without changing other grayscale values. The image is in .jpeg format and I would like to obtain image after changes back in jpeg format. Thanks for helping me in advance. regards

 Accepted Answer

im = imread('yourimage.jpg');
im(im == 255) = 249; %assume the image is indeed greyscale. No guarantee with jpg
imwrite(im, 'newimage.jpg');
Note that since you care about intensity values, using jpeg is not a good idea. jpeg is a lossy compression format. The pixel values stored in the file may be slightly different from the original one. Each time you save to jpg, you worsen the problem.
Also note that jpg does not know about grayscale images. The image will always be saved as RGB (with all three channels equal, but the compression may add colour)
Use png instead which can save true grayscale and has non-lossy compression.

7 Comments

Sorry I already tried this and its not working with jpeg :( Should I convert my images to png?
The code above does exactly what you asked, regardless of the image format. It changes pixels value of 255 to 249. So if "its not working", it's either because what you asked is not what you want, you're not using the same code, or you're not checking the result correctly.
In any case, we need to know in what way "its not working" to be able to help.
If you care about exact pixel intensity, you should use a non-lossy format such as png, indeed. It won't change anything about the working/not working state of the code, though.
Hi, Please find the code below. I used it for image processing. Kindly have a look and tell me the mistake(s). I want to add mask to the image and then change all pixels which are 255 to 249. thanks for your kind help.
img = imread('C:\Users\Kisvárday Zoltán\Documents\Lab_data\Data\UD\UD_055\exp9\after9_roi1x1.bmp');
mask = imread('C:\Users\Kisvárday Zoltán\Documents\Lab_data\Ud_55\after9_roi1x1mask.png');
comb = imadd(img,mask);
comb_x = comb;
comb_x(comb_x == 255) = 249;
subplot(1,4,1); imshow(img);
subplot(1,4,2); imshow(mask);
subplot(1,4,3); imshow(comb);
subplot(1,4,4); imshow(comb_x);
I'm assuming that both img and mask are of class uint8.
There's nothing wrong with your code in that it will produce a result. It may not be the result you want but I've no idea what it is you want. Your code does replace the pixels of comb_x which are 255 by 249.
I'll note though that it is very unusual to add a mask to an image. Is it really what you want to do? Bearing in mind that if the sum exceeds 255, it is truncated to 255 (i.e. 200 + 200 = 255, for images).
JPEG images are not always RGB. Try this code:
grayImage = imread('cameraman.tif');
imwrite(grayImage, 'deleteme.jpg');
recalledImage = imread('deleteme.jpg');
whos recalledImage
Name Size Bytes Class Attributes
recalledImage 256x256 65536 uint8
However, it is not advised to use them for image analysis if at all possible.
To combine an image with a mask, you multiply both, such that all values that are zero in the mask are set to zero in the masked image, and all values that are 1 in the mask are left as is in the masked image:
comb = img.*mask;
At least that is what I consider the usual way to combine an image with a mask.

Sign in to comment.

More Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!