How to find and replace extreme pixels in image for loop
Show older comments
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
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
Matt J
on 19 Nov 2018
What does the original image look like?
Emma
on 19 Nov 2018
Image Analyst
on 19 Nov 2018
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'));
Categories
Find more on Image Thresholding in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!