detecting (unwanted) pixels and removing them

4 次查看(过去 30 天)
Hi everyone,
Based on this image,
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

回答(1 个)

Nihal
Nihal 2024-7-18
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:
  1. Loading the Image: The image is loaded using imread.
  2. Threshold Value: A threshold value is defined.
  3. Initialize Mask: A mask is initialized to detect high-value pixels.
  4. Processing Each Channel: Each color channel (R, G, B) is processed independently to detect pixels with values higher than the threshold.
  5. Combining Masks: Threshold masks for each channel are combined to form a single mask.
  6. Region Filling: The regionfill function is used to fill the high-value regions in each color channel, effectively removing the spots.
  7. Save or Display: The cleaned image is saved and displayed.
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 个评论
DGM
DGM 2024-7-18
编辑:DGM 2024-7-18
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
DGM 2024-7-18
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])
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.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by