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:
Fill contour with specified color?

Subject: Fill contour with specified color?

From: Chuck37

Date: 24 May, 2011 21:43:03

Message: 1 of 12

I have a seemingly simple problem. I have a data matrix and I want to generate a contour at a single value, which will be closed in my case, and fill it a color. The things that complicate it are that (a) I'm overlaying on a pcolor plot with its own colormap, and (b) the contour may have holes in it, along with a large number of closed contours.

I can generate the contour data and then work through it myself using patch, but this doesn't take account of the holes (e.g. a donut would be filled in with only the outline of the hole). Can anyone think of how to pull this off?

Subject: Fill contour with specified color?

From: ImageAnalyst

Date: 24 May, 2011 22:39:47

Message: 2 of 12

Yes. Why can't you just threshold the data image, then use that
logical image as a mask to assign a value to the image that
corresponds to the color you want?

If you don't understand, then post your data image somewhere and tell
me what contour value you want filled.

Subject: Fill contour with specified color?

From: Chuck37

Date: 24 May, 2011 23:10:19

Message: 3 of 12

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <9b51b4cd-dd0b-4c81-b5dd-e4c499083d62@s9g2000yqm.googlegroups.com>...
> Yes. Why can't you just threshold the data image, then use that
> logical image as a mask to assign a value to the image that
> corresponds to the color you want?
>
> If you don't understand, then post your data image somewhere and tell
> me what contour value you want filled.

I know what you mean. The reason I don't want to do that is just because the result will look blocky compared to a smooth contour. My underlying data is slightly coarse.

Subject: Fill contour with specified color?

From: ImageAnalyst

Date: 25 May, 2011 01:12:36

Message: 4 of 12

How about using the 'Fill' 'on' option? If that doesn't work, then
get the coordinates of the contours from the function (it's one of the
output args) and then use patch() to fill just the one(s) that you
want filled.

Subject: Fill contour with specified color?

From: Chuck37

Date: 25 May, 2011 02:02:04

Message: 5 of 12

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <504a9335-8f4f-47e3-bb81-63545c15cf04@y19g2000yqk.googlegroups.com>...
> How about using the 'Fill' 'on' option? If that doesn't work, then
> get the coordinates of the contours from the function (it's one of the
> output args) and then use patch() to fill just the one(s) that you
> want filled.

How would I do that with something donut shaped for example? I'd get two circles, but treating them separately wouldn't fill it properly.

Subject: Fill contour with specified color?

From: ImageAnalyst

Date: 25 May, 2011 02:05:27

Message: 6 of 12

I'm not sure what you mean. Perhaps you can explain it on this demo:

Z = peaks(20);
[C,h] = contour(interp2(Z,4),'Fill', 'on');
text_handle = clabel(C,h);
set(text_handle,'BackgroundColor',[1 1 .6],...
    'Edgecolor',[.7 .7 .7])

% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')

Subject: Fill contour with specified color?

From: Chuck37

Date: 27 May, 2011 21:27:04

Message: 7 of 12

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <b5b64ea1-08a3-4cf9-bc4f-9e8828455f9d@j31g2000yqe.googlegroups.com>...
> I'm not sure what you mean. Perhaps you can explain it on this demo:
>
> Z = peaks(20);
> [C,h] = contour(interp2(Z,4),'Fill', 'on');
> text_handle = clabel(C,h);
> set(text_handle,'BackgroundColor',[1 1 .6],...
> 'Edgecolor',[.7 .7 .7])
>
> % Enlarge figure to full screen.
> set(gcf, 'Position', get(0,'Screensize'));
> set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')

In my case I have a background image (pcolor) I am rendering in black and white. I want to fill regions of the image that meet a certain criteria with the color red (ideally with alpha<1 so the image can still be seen). If I use contourf, the colormap of the pcolor is used for the fill, so everything is black and white and nearly impossible to see. If I could get the filled areas to be a set color, contourf would work. Thanks.

Subject: Fill contour with specified color?

From: ImageAnalyst

Date: 27 May, 2011 22:01:36

Message: 8 of 12

I'm still a bit confused. Can you use the patch() command? If not,
either ask the Mathworks to help you or post some code to generate
some sample data (like I did), and post an image somewhere of what you
want to achieve - mock something up in Photoshop if you have to.
Because I don't know what else to suggest and I'm about to bail out.

Subject: Fill contour with specified color?

From: Chuck37

Date: 27 May, 2011 23:08:04

Message: 9 of 12

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <a3fa42d3-3196-4f51-91c5-30c1dfabd339@r20g2000yqd.googlegroups.com>...
> I'm still a bit confused. Can you use the patch() command? If not,
> either ask the Mathworks to help you or post some code to generate
> some sample data (like I did), and post an image somewhere of what you
> want to achieve - mock something up in Photoshop if you have to.
> Because I don't know what else to suggest and I'm about to bail out.

I'm sorry, my work is on a machine not connected to the internet and is not something I can share in public. As an example, say I have some data representing the height of waves emanating from where a drop of water landed in a pond. The waves come out in rings. I could use contour to display places where the waves are above some height, but I want to color those regions red. I also want to display this data on top of different data, perhaps a pcolor plot of the nominal water depth to the bottom of the pond. Since the wave locations of interest are in rings, patch doesn't seem to work. I'd get, for example, two circles from my contour data - once I color in the outer circle, the inner circle is also colored, which I don't want, I just want the ring.

The end result I want would be a pcolor (or image) plot in grayscale of the water depth with transparent red rings overlaid on top representing the locations of wave height greater than some value. Does that make sense?

The best, but very kludgy, idea I've had is to make my own composite image with a custom colormap. This involves bumping up the red regions by some large value to get them into the red part of a two part colormap which is half grayscale and half "redscale". A downside of this is that it doesn't allow for any outlining, which makes the data easier to look at.

Thanks for the help.

Subject: Fill contour with specified color?

From: ImageAnalyst

Date: 28 May, 2011 01:25:22

Message: 10 of 12

Well good luck with that. I deal with images and find them much
easier than dealing with contours and patches. I don't know why
you're using pcolor. I absolutely hate it. I can never get past the
fact that it displays one less square in each direction than there are
pixels, so I never really know what the colored squares represent.
It's like the pixels are on the black gridlines, and the squares are
inbetween the pixels but they're pseudocolored and I have no idea what
the underlying value is that gets colored. Totally confusing.
imagesc() is somewhat better, except for the fact that it picks some
bizarre random colormap as default. image() is okay but it's
limited. So my favorite is imshow. If you didn't need those contour
lines, and were just dealing with images it would be a snap. I could
maybe help with that but you don't seem to want to share your data and
don't want to use any sample data such as I gave earlier, so you're on
your own. But it sounds like you have some kludgy idea that works for
you so that's all that matters I guess.

Subject: Fill contour with specified color?

From: Chuck37

Date: 31 May, 2011 15:48:04

Message: 11 of 12

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <88dedc51-f5e5-4f0b-8001-8aad612fadbd@r20g2000yqd.googlegroups.com>...
> Well good luck with that. I deal with images and find them much
> easier than dealing with contours and patches. I don't know why
> you're using pcolor. I absolutely hate it. I can never get past the
> fact that it displays one less square in each direction than there are
> pixels, so I never really know what the colored squares represent.
> It's like the pixels are on the black gridlines, and the squares are
> inbetween the pixels but they're pseudocolored and I have no idea what
> the underlying value is that gets colored. Totally confusing.
> imagesc() is somewhat better, except for the fact that it picks some
> bizarre random colormap as default. image() is okay but it's
> limited. So my favorite is imshow. If you didn't need those contour
> lines, and were just dealing with images it would be a snap. I could
> maybe help with that but you don't seem to want to share your data and
> don't want to use any sample data such as I gave earlier, so you're on
> your own. But it sounds like you have some kludgy idea that works for
> you so that's all that matters I guess.

The solution I have is a real kludge and I don't like it. I'm not trying to make it difficult for you to help, I'm on a machine that does not have matlab. I'm trying to make the problem as simple as possible to explain. Try this: I have two 2D matrices, one is to be the base image or pcolor or whatever, in grayscale. The other one is to be thresholded at some level and all values below the threshold are to be colored red and plotted on top of the first image with semi-transparency so the first image can still be partially seen through.

Manually parsing a contour of the second image and plotting with patch works, except when there are holes in the contour. Contourf doesn't work because it takes on the colormap of the grayscale image.

Subject: Fill contour with specified color?

From: ImageAnalyst

Date: 31 May, 2011 17:15:10

Message: 12 of 12

How about doing it this way:

% IMPORTANT: The newsreader may break long lines into multiple lines.
% Be sure to join any long lines that got split into multiple single
lines.
% These can be found by the red lines on the left side of your
% text editor, which indicate syntax errors, or else just run the
% code and it will stop at the split lines with an error.

% Clean up and initialization
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 = 15;

% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.tif';
fullFileName = fullfile(folder, baseFileName);
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% 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.', 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);
% 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, 'Position', get(0,'Screensize'));
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')

% Let's compute and display the histogram.
[pixelCount grayLevels] = imhist(grayImage);
subplot(2, 2, 2);
bar(pixelCount);
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.

% Get a thresholded, binary image.
binaryImage = grayImage < 80;
% Display the binary image.
subplot(2, 2, 3);
imshow(binaryImage, []);
title('Binary "Mask" Image', 'FontSize', fontSize);

% Average it with the image and put into the red channel
averageImage = (256 * single(binaryImage) + single(grayImage))/2;
redChannel = grayImage; % Initialize to same as gray.
% Make it reddish just where the mask is.
redChannel(binaryImage) = averageImage(binaryImage);
% Create the RGB image.
coloredImage = cat(3, redChannel, grayImage, grayImage);
% Display the binary image.
subplot(2, 2, 4);
imshow(coloredImage, []);
title('Colored Image', 'FontSize', fontSize);

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