Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
imposing a colormap threshold

Subject: imposing a colormap threshold

From: Susan

Date: 18 Apr, 2010 09:31:04

Message: 1 of 8

Hi,

I wish to implement a threshold variance value above which all other values are zero and I wish to assign a colour to the remaining pixels (it is a rgb to grayscale image). I placed the 'if' loop within the following and it doesn't seem to work.

Any comments and/or suggestions I would very much appreciate!

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% variance of image grids
SV = zeros(height, width);

for y = 1:1:width
    for x = 1:1:height
        SV(x,y) = I2(x,y).^2;

% Apply Threshold
         if SV(x,y) > (0.1*(min(min(SV))+min(min(SV))))
             SV(x,y) = 0;
        else
             SV(x,y);
         end

    end
    hold on;
end
subplot(4,1,3);
imshow(SV, []), title('Var of Image'), colormap(hot);

Subject: imposing a colormap threshold

From: ImageAnalyst

Date: 18 Apr, 2010 18:57:15

Message: 2 of 8

I don't doubt that it doesn't work - lots of things wrong with it
(starting with min(min(SV)) always being zero since that's what you
initialized it to and squares are always zero or greater). Why don't
you just start over with my demo code, then we can talk more if you
need to.

% Demo to take the local mean, variance, and standard deviation
% of a gray scale image.
% userImage, if passed in, is used as the image.
% If userImage is not passed in, user is asked to use a demo image.
% Code written by ImageAnalyst
function local_variance(userImage)
% Clean up.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.

% Change the current folder to the folder of this m-file.
% (The line of code below is from Brett Shoelson of The Mathworks.)
% Don't use these lines if you're calling this from another m-file.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end

% Initialize.
fontSize = 20;
if nargin == 0
    % No image passed in on the command line.
    % Read in one of the standard MATLAB demo images
    % as our original gray scale image and display it.
    promptMessage = sprintf('Which image do you want to use.\nThe
coins or the cameraman?');
    button = questdlg(promptMessage, 'Select Image', 'Coins',
'Cameraman', 'Coins');
    if strcmp(button, 'Coins')
        grayImage = double(imread('coins.png')); % Cast to double.
    else
        grayImage = double(imread('cameraman.tif')); % Cast to double.
    end
else
    % Use the image array passed in on the command line.
    grayImage = double(userImage); % Cast to double.
end

% Start timing.
startTime = tic;

subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Image', 'FontSize', fontSize);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.

% Blur the image with a 5 by 5 averaging (box filter) window.
blurredImage = conv2(grayImage, ones(5,5)/25);
subplot(2, 2, 2);
imshow(blurredImage, []);
title('Blurred Image', 'FontSize', fontSize);

% Perform a variance filter.
% Output image is the variance of the input image in a 3 by 3 sliding
window.
VarianceFilterFunction = @(x) var(x(:));
varianceImage = nlfilter(grayImage, [3 3], VarianceFilterFunction);
% An alternate way of doing the variance filter is on the next line:
% varianceImage = reshape(std(im2col(originalImage,[3 3],'sliding')),
size(originalImage)-2);
subplot(2, 2, 3);
imshow(varianceImage, [])
title('Variance Image', 'FontSize', fontSize);

% Compute the square root of the variance image to get the standard
deviation.
standardDeviationImage = sqrt(varianceImage);
subplot(2, 2, 4);
imshow(standardDeviationImage, [])
title('Standard Deviation Image', 'FontSize', fontSize);
elapsedTime = toc(startTime);

message = sprintf('Done!\n\nElapsed time = %.2f seconds.',
elapsedTime);
msgbox(message);
return; % End of local_variance() function.

Subject: imposing a colormap threshold

From: Susan

Date: 19 Apr, 2010 11:04:06

Message: 3 of 8

Hi ImageAnalyst,

Thank you for your reply and your code - it is helpful regarding some nice things I could use. But, perhaps I was being a bit stingy with the initial code I displayed - I have already computed the std and variance of my desired image and these are displaying correctly. But, I wish to display only pixels which are above a threshold set by me. I have attampted this with the first code I showed, but my PC seems to be stuck in a loop and doesn't end.

Help, as I am quite the novice!

Subject: imposing a colormap threshold

From: Oliver Woodford

Date: 19 Apr, 2010 12:21:06

Message: 4 of 8

"Susan" wrote:
> I wish to implement a threshold variance value above which all other values are zero and I wish to assign a colour to the remaining pixels (it is a rgb to grayscale image). I placed the 'if' loop within the following and it doesn't seem to work.
>
> Any comments and/or suggestions I would very much appreciate!

Hi Susan.

The SC function (http://www.mathworks.com/matlabcentral/fileexchange/16233) can do this for you very easily.

HTH,
Oliver

Subject: imposing a colormap threshold

From: Cris Luengo

Date: 19 Apr, 2010 12:40:32

Message: 5 of 8

"Susan " <Cooma_kista@yahoo.co.uk> wrote in message <hqejgo$p3o$1@fred.mathworks.com>...
> Hi,
>
> I wish to implement a threshold variance value above which all other values are zero and I wish to assign a colour to the remaining pixels (it is a rgb to grayscale image). I placed the 'if' loop within the following and it doesn't seem to work.
>
> Any comments and/or suggestions I would very much appreciate!
>
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
>
> % variance of image grids
> SV = zeros(height, width);
>
> for y = 1:1:width
> for x = 1:1:height
> SV(x,y) = I2(x,y).^2;
>
> % Apply Threshold
> if SV(x,y) > (0.1*(min(min(SV))+min(min(SV))))
> SV(x,y) = 0;
> else
> SV(x,y);
> end
>
> end
> hold on;
> end
> subplot(4,1,3);
> imshow(SV, []), title('Var of Image'), colormap(hot);

Hi Susan,
To me it seems you are trying to do this with your code:
   SV = l2.^2;
   mask = SV > (0.1*min(SV(:)) + min(SV(:)));
   SV(mask) = 0;
   imshow(SV,[])
...etc
Though I'm not sure what this is supposed to do.

If you want to set all pixels in an image that are above the std to 0, you'd do this:
   s = std(img(:));
   img(img>s) = 0;

Maybe if you clarify your post we can help you better...

Subject: imposing a colormap threshold

From: Susan

Date: 20 Apr, 2010 13:38:05

Message: 6 of 8

"Oliver Woodford" <o.j.woodford.98@cantab.net> wrote in message <hqhhri$rp3$1@fred.mathworks.com>...
> "Susan" wrote:
> > I wish to implement a threshold variance value above which all other values are zero and I wish to assign a colour to the remaining pixels (it is a rgb to grayscale image). I placed the 'if' loop within the following and it doesn't seem to work.
> >
> > Any comments and/or suggestions I would very much appreciate!
>
> Hi Susan.
>
> The SC function (http://www.mathworks.com/matlabcentral/fileexchange/16233) can do this for you very easily.
>
> HTH,
> Oliver


Hi Oliver,

Could you please indicate the relevant section in sc.m?

Kind regards,
Susan

Subject: imposing a colormap threshold

From: Susan

Date: 20 Apr, 2010 13:45:20

Message: 7 of 8

"Cris Luengo" <cris.luengo@google.for.my.name.to.contact.me> wrote in message <hqhj00$hni$1@fred.mathworks.com>...
> "Susan " <Cooma_kista@yahoo.co.uk> wrote in message <hqejgo$p3o$1@fred.mathworks.com>...
> > Hi,
> >
> > I wish to implement a threshold variance value above which all other values are zero and I wish to assign a colour to the remaining pixels (it is a rgb to grayscale image). I placed the 'if' loop within the following and it doesn't seem to work.
> >
> > Any comments and/or suggestions I would very much appreciate!
> >
> > %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
> >
> > % variance of image grids
> > SV = zeros(height, width);
> >
> > for y = 1:1:width
> > for x = 1:1:height
> > SV(x,y) = I2(x,y).^2;
> >
> > % Apply Threshold
> > if SV(x,y) > (0.1*(min(min(SV))+min(min(SV))))
> > SV(x,y) = 0;
> > else
> > SV(x,y);
> > end
> >
> > end
> > hold on;
> > end
> > subplot(4,1,3);
> > imshow(SV, []), title('Var of Image'), colormap(hot);
>
> Hi Susan,
> To me it seems you are trying to do this with your code:
> SV = l2.^2;
> mask = SV > (0.1*min(SV(:)) + min(SV(:)));
> SV(mask) = 0;
> imshow(SV,[])
> ...etc
> Though I'm not sure what this is supposed to do.
>
> If you want to set all pixels in an image that are above the std to 0, you'd do this:
> s = std(img(:));
> img(img>s) = 0;
>
> Maybe if you clarify your post we can help you better...

Hi Cris,

Thank you for your reply. My intention is to use the threshold to signify areas of 'flow' that are below (or above) a certain threshold. I am processing an image and calculating the variance and from this flow signatures may be obtained.

Although I attempted the code you provided, I think it was stuck in a loop and wouldn't end. Currently I am at a stage where I have outputted both a standard deviation and a variance of the input image and all I wish to do is to create a cutoff for the pixel values (grayscale) of the variance image. Perhaps the ability to change the color of the remaining pixels would be good also, leaving the other pixels with (value<threshold) black.

Any tips?

Kind regards,
Susan

Subject: imposing a colormap threshold

From: Oliver Woodford

Date: 20 Apr, 2010 14:16:04

Message: 8 of 8

"Susan " wrote:
> Hi Oliver,
>
> Could you please indicate the relevant section in sc.m?
>
> Kind regards,
> Susan

Hi Susan

I suggest you just call sc in the right way, e.g.

sc(data, [0 thresh], 'jet', 'k', data>thresh);

To find out more about how to use it call
help sc
and to run the demo call
sc

Regards,
Oliver

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us