|
On Sep 24, 7:05 pm, "Chuck37 " <chuck3...@yahooremovethis.com> wrote:
> What I wanted is quite simple to explain. Say I have XY data in a matrix. I, for example, want to color all regions with values less than 5. Contour(XY,[5 5]) outlines those regions. Contourf will color the regions > 5 a color and the region I care about white.
>
> The answer I found is to use contourc to get the contour data, then run a while loop on that data (it indicates the closed contour regions in the contour matrix, see help contourc) to pull out and run PATCH on each individual closed contour. It works well.
>
> Thanks everyone for the help.
-------------------------------------------------------------------
That's not the way I'd do it. First of all, patch covers the pixels
with a patch of color in the overlay above the image. It does not
actually change the matrix to green - which may be okay, if that's
what you want. But drawing potentially hundreds or thousands of
patches all over the place doesn't seem very efficient. A better way
to do what you just said is to simply display the array with image, or
imagesc, or imshow, and use a color map to pseudocolor all pixels less
than some level to be green. Like this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 14;
% Change the current folder to the folder of this m-file.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
% Read in a standard MATLAB gray scale demo image.
folder = 'C:\Program Files\MATLAB\R2010a\toolbox\images\imdemos';
baseFileName = 'cameraman.tif';
fullFileName = fullfile(folder, baseFileName);
grayImage = imread(fullFileName);
% create the colormap
grayRamp = linspace(0,1,256);
cmap = [grayRamp; grayRamp; grayRamp]';
rowsToMakeGreen = 50;
cmap (1:rowsToMakeGreen, :) = repmat([0 1 0], [rowsToMakeGreen 1]); %
0 - 49 graylevels will be green.
% Display the original gray scale image.
imshow(grayImage, cmap);
title('Original Grayscale Image', 'FontSize', fontSize);
set(gcf, 'Position', get(0,'Screensize')); % Enlarge figure to full
screen.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
Displaying with a colormap does not change matrix values, only how
they're displayed. If you actually want to change the matrix (pixel)
values you'll have to create an rgb image and then assign the pixels
to green. Run this example. I changed the value for green to "less
than 50" so you could see some green on the image, but you could use 5
if you want. (You may have to join some lines if the newsreader
splits them into two, which it will do for long lines.)
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 14;
% Change the current folder to the folder of this m-file.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
% Read in a standard MATLAB gray scale demo image.
folder = 'C:\Program Files\MATLAB\R2010a\toolbox\images\imdemos';
baseFileName = 'cameraman.tif';
fullFileName = fullfile(folder, baseFileName);
grayImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(2, 1, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
set(gcf, 'Position', get(0,'Screensize')); % Enlarge figure to full
screen.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Find pixels less than, say 50, and color them green.
darkPixels = grayImage < 50;
% Initialize a new color image.
redPlane = grayImage;
greenPlane = grayImage;
bluePlane = grayImage;
% make them green.
redPlane(darkPixels) = 0;
greenPlane(darkPixels) = 255;
bluePlane(darkPixels) = 0;
% Build up the rgb color image from the individual color planes.
rgbImage = cat(3, redPlane, greenPlane, bluePlane); % Initialize
subplot(2, 1, 2);
imshow(rgbImage, []);
title('Color Image with pixels less than 50 as green', 'FontSize',
fontSize);
It's a lot of code, but it's there just to be explicit and make you
understand. It's a tutorial - don't be afraid of it. Just go through
line by line and you'll understand.
|