- Loading the Image: The image is loaded using imread.
- Threshold Value: A threshold value is defined.
- Initialize Mask: A mask is initialized to detect high-value pixels.
- Processing Each Channel: Each color channel (R, G, B) is processed independently to detect pixels with values higher than the threshold.
- Combining Masks: Threshold masks for each channel are combined to form a single mask.
- Region Filling: The regionfill function is used to fill the high-value regions in each color channel, effectively removing the spots.
- Save or Display: The cleaned image is saved and displayed.
detecting (unwanted) pixels and removing them
4 views (last 30 days)
Show older comments
Hi everyone,
Based on this image,
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/170615/image.png)
I want to detect some pixels that have larger value than other pixels and delete them, basically the ones you see at the boundary of the mask as well as the flow artifacts in the image itself.
Any help is highly appreciated!
Cheers
0 Comments
Answers (1)
Nihal
on 18 Jul 2024
If you want to remove all the spots detected by the threshold mask from the image, you can modify the code to ensure that all pixels identified by the threshold mask are removed (set to zero). Below is the updated MATLAB code to achieve this:
% Load the image
image = imread('image.png');
% Define the threshold value
threshold_value = 200; % Adjust this value based on your image
% Initialize a mask for high-value pixels
mask = false(size(image, 1), size(image, 2));
% Process each color channel independently
for channel = 1:size(image, 3)
% Detect pixels with values higher than the threshold in the current channel
thresholded_image = image(:, :, channel) > threshold_value;
% Combine the threshold masks for each channel
mask = mask | thresholded_image;
end
% Use the mask to fill the high-value regions in the original image
cleaned_image = image;
for channel = 1:size(image, 3)
channel_data = cleaned_image(:, :, channel);
channel_data = regionfill(channel_data, mask); % Fill the regions
cleaned_image(:, :, channel) = channel_data;
end
% Save or display the cleaned image
imwrite(cleaned_image, 'cleaned_image.png');
imshow(cleaned_image)
Explanation:
By using regionfill, the high-value regions detected by the threshold mask are filled with interpolated values, resulting in a more natural-looking image.
Feel free to adjust the threshold_value and other parameters based on your specific image and requirements.
2 Comments
DGM
on 18 Jul 2024
Edited: DGM
on 18 Jul 2024
It really doesn't make much sense to do RGB segmentation in this context, since the data is not RGB. What's shown is a screenshot of a pseudocolor representation of what is likely only 2D data. We shouldn't be reprocessing screenshots.
The concept of using a mask and regionfill() would still potentially work though. The only difference is how we choose to make the mask -- be that a matter of absolute thresholds, or a matter of local statistics. Since OP isn't here to clarify what they want, it's fair to say you have the latitude to make that decision yourself.
DGM
on 18 Jul 2024
For example:
% a cropped screenshot
inpict = imread('image.png');
inpict = imcrop(inpict,[75.51 29.51 476.98 476.98]);
% back-estimate the original data in uint8-scale
% getting the exact map is critical for estimation,
% but the original image is from R2014b-R2017a
% when the 'parula' map was different
CT = ccmap('parula14',256); % MIMT
Z = rgb2ind(inpict,CT,'nodither');
% find regions of high variance
mk = rangefilt(Z,ones(5)) > 50;
% apply an aggressive adaptive median noise removal filter
Zf = amedfilt(Z,5,1); % MIMT
% combine the two images using the mask
Zout = Z;
Zout(mk) = Zf(mk);
% rescale to [0 360] float
Zout = 360*im2double(Zout);
% show the result in pseudocolor
% this uses whatever version of parula() is present
% the exact map doesn't really matter for visualization
imagesc(Zout)
colormap(parula(256))
colorbar
clim([0 360])
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1737011/image.png)
That takes care of most of all the speckles, but without touching the lower-contrast discrete speckles which constitute the subtle interior textures.
This example uses MIMT tools ccmap() and amedfilt(). Again, we shouldn't even need ccmap() here, since we shouldn't need to back-estimate Z data from a screenshot. Depending on the scale of our actual data, the preparation and thresholds will likely need to be different.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!