![](https://www.mathworks.com/matlabcentral/images/broken_image.png)
How to display image above threshold?
6 次查看(过去 30 天)
显示 更早的评论
I am trying to display an image (X) above the noise threshold (N), where X is a matrix and N is a scalar. I don't want regions lesser than the noise to be displayed, rather they should be rendered white.
How can this be achieved?
0 个评论
采纳的回答
Image Analyst
2013-7-12
Don't use cryptic variable names like X and N. Soon, when you have lots of variables, your code looks like a confusing alphabet soup of variables with no clue what each one is.
It's basically like Evan said except that you wanted to set the pixels to white, not zero. You can also do it by casting the thresholded image to integer and multiplying. Here's a full blown demo:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
button = menu('Use which demo image?', 'CameraMan', 'Moon', 'Cell', 'Coins', 'Pout');
if button == 1
baseFileName = 'cameraman.tif';
elseif button == 2
baseFileName = 'moon.tif';
elseif button == 3
baseFileName = 'cell.tif';
elseif button == 4
baseFileName = 'coins.png';
else
baseFileName = 'pout.tif';
end
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Now get some threshold for noise
meanGL = mean(grayImage(:))
sd = std(double(grayImage(:)))
noiseThreshold = meanGL + 1.1 * sd
% Threshold the image to find noise.
noisePixels = grayImage >= noiseThreshold;
% Display the noise pixels.
subplot(2, 2, 2);
imshow(noisePixels, []);
caption = sprintf('Pixels above Noise Threshold of %.2f', noiseThreshold);
title(caption, 'FontSize', fontSize);
% He says " I don't want regions lesser than the noise to be displayed, rather they should be rendered white."
% Find pixels less than the noise threshold
lessThanNoise = grayImage < noiseThreshold;
% Display the less than noise pixels.
subplot(2, 2, 3);
imshow(lessThanNoise, []);
title('Pixels less than the Noise', 'FontSize', fontSize);
% Set those pixels to white in the original image
outputImage = grayImage; % Initialize.
outputImage(lessThanNoise) = 255;
% Display the less than noise pixels.
subplot(2, 2, 4);
imshow(outputImage, []);
caption = sprintf('Pixels less than the Noise set to White\nMore than noise is unchanged (original)');
title(caption, 'FontSize', fontSize);
![](https://www.mathworks.com/matlabcentral/images/broken_image.png)
1 个评论
更多回答(3 个)
Venkatessh
2013-7-12
2 个评论
Evan
2013-7-12
Glad you've got it working!
In cases where two users submit answers that solve your problem, it's best practice to accept the best available answer so that other users who come across this thread will be directed to it first. So you made the right choice in selecting Image Analyst's tutorial.
shafaq nisar
2017-1-11
Can you refer any research paper in which this code is used?
1 个评论
Image Analyst
2017-1-11
No, there are probably too many of them and masking with a threshold is such a basic operation that you're unlikely to find anything. It would be like asking if I can refer you to any reference papers that use addition or subtraction.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!