How to display image above threshold?

21 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

Image Analyst
Image Analyst il 12 Lug 2013
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);
  1 Commento
Evan
Evan il 12 Lug 2013
Modificato: Evan il 12 Lug 2013
"It's basically like Evan said except that you wanted to set the pixels to white, not zero."
Oops! I completely glazed over that part. Habit, I guess. Thanks, I've modified my answer.

Accedi per commentare.

Più risposte (3)

Evan
Evan il 12 Lug 2013
Modificato: Evan il 12 Lug 2013
mask = rawImage > N; %simple thresholding--adjust for your needs
maskedImage = rawImage; %copy image
maskedImage(~mask) = 255; %set all pixels that don't pass threshold to zero
imagesc(maskedImage) %view masked image

Venkatessh
Venkatessh il 12 Lug 2013
Would like to accept both the solution? But made the same problem like Evan did. Initialized values lesser than threshold to zero instead of 255.
Anyway, thanks a ton
  2 Commenti
Evan
Evan il 12 Lug 2013
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.
Image Analyst
Image Analyst il 12 Lug 2013
You can "vote" for both Answers but only "Accept" one.

Accedi per commentare.


shafaq nisar
shafaq nisar il 11 Gen 2017
Can you refer any research paper in which this code is used?
  1 Commento
Image Analyst
Image Analyst il 11 Gen 2017
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.

Accedi per commentare.

Categorie

Scopri di più su Image Processing Toolbox in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by