How to find and replace extreme pixels in image for loop

Hi, I have a set of the gray image dataset, and some of the images have extreme bright or dark pixels (e.g., for example, if I use imhist(fig), the histogram peak around the dark pixels from 0-10 or bright from 240-250) that I wish to replace values with. I'm wondering if there is a way to replace those extreme values to the specific pixel value (e.g. set threshold) either via the loop or on an individual image in order to recenter the histogram of images from the dataset. For example, I would like to replace pixel value from 0-10 by the values 11 and from 240-250 by the value 239. Here is my attempt that I am not sure how to improve/fix in order to reach the outcome. The outcome image I got is a blank white image, no error message. Any feedback or solution is appreciated. Thanks
image = im2double(imread('Face_23.jpg'));
image = image(:,:);
% allocate space for thresholded image
image_thresholded = zeros(size(image));
% loop over all rows and columns
for ii=1:size(image,1)
for jj=1:size(image,2)
% get pixel value
pixel=image(ii,jj);
% check pixel value and assign new value
if pixel<10
new_pixel=11;
elseif pixel>240
new_pixel=239;
else
new_pixel = pixel;
end
% save new pixel value in thresholded image
image_thresholded(ii,jj)=new_pixel;
end
end
% display result
figure()
subplot(1,2,1)
imshow(image,[])
title('original image')
subplot(1,2,2)
imshow(image_thresholded,[])
title('thresholded image')

 Accepted Answer

Jan
Jan on 19 Nov 2018
Edited: Jan on 19 Nov 2018
By the way: you define the folder, but do not use it. It is possible, that this is the only problem, although this is unlikely:
img_folder = '/Users/MATLAB/Faces';
img = im2double(imread(fullfile(img_folder, 'Face_23.jpg')));
More likely is, that after im2double the range of img is [0, 1], such that all pixel values are smaller than 10. Use im2uint8 instead, or the limits 10/255, or 240/255 respectively.
img = im2double(imread(fullfile(img_folder, 'Face_23.jpg')));
img(img < 10 / 255) = 11 / 255;
img(img > 240 / 255) = 239 / 255;
Another hint: "image" is a built-in Matlab functions. Avoid to use it as name of a variable, because this can cause confusions.
Do you see, that the method of "logical indexing" in img(img < 10 / 255) is much neater than the loop over the pixel indices?

3 Comments

Hi Jan, it works nicely and I'd learned something, thanks a lot. Would you mind walk me through the logical indexing part, please? Why we divide 10 with 255 here "(img < 10 / 255)", if we divided by 255 (255 here refers to 8-bit gray image right?) why the replaced value ("11") is divided by 22 instead of the value I set as threshold ( the value of "11" in this line). Same puzzle for the second line that 240 is divided by 355 "img(img > 240 / 355)" and "= 239/255". I wish to understand this neat code to learn from it. Sorry to ask, can I loop through all of my images to thresholded images with this code? Cheers!
@Yanagawa: My fault. Dividing by 22 abd 355 has been a typo. Of course both must be 255 to scale the range of [0,255] to [0,1] . (This is fixed in my answer now.)
Thanks a lot for your update and explnation.

Sign in to comment.

More Answers (1)

img_folder= '/Users/MATLAB/Faces';
image = im2double(imread('Face_23.jpg'));
A=image;
A(A<10)=11;
A(A>240)=239;
image_thresholded=A;
% display result
figure()
subplot(1,2,1)
imshow(image,[])
title('original image')
subplot(1,2,2)
imshow(image_thresholded,[])
title('thresholded image')

4 Comments

Emma
Emma on 19 Nov 2018
Edited: Emma on 19 Nov 2018
thanks for your suggestion. I'd tried this way but I got the same outcome from your code as my code.I expect to see only the darkness and brightness of pixels have been modified. Hence, I assigned the threshold values as below in the code:
(if pixel<10 new_pixel=11;
elseif pixel>240
new_pixel=239,...
So I assume I won't get a thresholded image without anything (white). Or do you expect the thresholded image shown as the image above as the outcome? cheers!
What does the original image look like?
The original grayscale image is a face with a scene background.
Starting here:
image = im2double(imread('Face_23.jpg'));
replace image with grayImage, since image is a built-in function. You'll also need to multiply by 255 if you want to use threshold values in the range 0-255 since im2double rescales the image into the range 0-1.
grayImage = 255 * im2double(imread('Face_23.jpg'));

Sign in to comment.

Asked:

on 19 Nov 2018

Commented:

on 20 Nov 2018

Community Treasure Hunt

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

Start Hunting!