MATLAB >> Filling a solid polygon on an image

by Phil Au » Thu, 28 Feb 2008 22:14:24 GMT

Hi All,

I was very struggle in drawing a sold polygon on an image,
for example,

I = imread('image.jpg');

I want to draw a circle in an Image(I) position (3,3) with
a radius fill with colour [2 3 4], i know the coordinates
that locate at circumference.. so I get the line with the
the specific colour, the problem is how can I filled the
area with a specific colour??

I figure out that using a loop to decrease the radius to
get every pixel coordinates inside the area of the circle,
then I fill every pixel with the specific colour.. I think
of this way, but this is very time cosuming,

for example, if i have an 800 x 600 pixels, I want to draw
a circle in every pixel, it will loops for a very very long
time.. since every pixel it always calculate it surround

How about a ellispe and a stroke which is a line the every
pixel on that line is fill with a circle, that way, it take
a long time to do that..

I search for imfill, and fill, but it is using in a axes,
not in the image, how can I imply in the image???

Is there a better way for it??

Really want to help.. thanks

MATLAB >> Filling a solid polygon on an image

by Steven Lord » Thu, 28 Feb 2008 22:55:38 GMT

% Note: this is a truecolor image. It's a little different for indexed
% the first four lines remain the same; how you use rowindices
% and colindices in this case is left as an exercise for the reader
I = imread('ngc6543a.jpg');
[cols, rows] = meshgrid(1:size(I, 1), 1:size(I, 2));
distance = (cols-3).^2+(rows-3).^2;
[rowindices, colindices] = find(distance < 100); % 10 pixel radius

% Since I is in this case a uint8 matrix, I use 255 here. You might want
% to use ISINTEGER to check if your matrix is a *int* array and if it is,
% use INTMAX. For a double image, use numbers in the range [0, 1].
I(rowindices, colindices, :) = 255;

% If you want to change each page (R, G, and B) individually, use
% this commented code instead:
% I(rowindices, colindices, 1) = 0;
% I(rowindices, colindices, 2) = 0;
% I(rowindices, colindices, 3) = 255;



The idea is basically the same if you want to create other shapes.

Steve Lord

MATLAB >> Filling a solid polygon on an image

by Phil Au » Thu, 28 Feb 2008 23:55:24 GMT

<fq6i1a$4t5$ XXXX@XXXXX.COM >...

different for indexed
the reader
pixel radius
You might want
array and if it is,
range [0, 1].
individually, use
other shapes.

Thanks Steven, I kind of understand the concept, but just
wondering why I get a square.. In concept, it shd get a,..

MATLAB >> Filling a solid polygon on an image

by Steven Lord » Fri, 29 Feb 2008 00:55:47 GMT


When you're trying to draw a circle on a grid where the points are spaced 1
unit apart (like elements in an image matrix), that's often a coarse grid.
That's what you're doing here. If you reduce the spacing between the grid
elements, then you get a finer grid and a better approximation. Compare the
two approximate circles in the two figures created by the code below:

[x1, y1] = meshgrid(1:20, 1:20);
h1 = pcolor(x1, y1, double((x1-3).^2 + (y1-3).^2 < 100));

spacing = 0.5;
[x2, y2] = meshgrid(1:spacing:20, 1:spacing:20);
h2 = pcolor(x2, y2, double((x2-3).^2 + (y2-3).^2 < 100));

Now try changing the spacing variable and see what happens when you make the
grid spacing finer or coarser. [For really fine grid spacing, you may need
to set the EdgeColor property of the pcolor plot to 'none' so that you can
see the surface rather than just the edges of the elements.]

Steve Lord

MATLAB >> Filling a solid polygon on an image

by motoko Phil » Sat, 01 Mar 2008 03:10:03 GMT

points are spaced 1
often a coarse grid.
between the grid
approximation. Compare the
code below:
happens when you make the
spacing, you may need
to 'none' so that you can

Thanks Steve again,
Another interesting thing is that I think something is
wrong with the line
I(rowindices, colindices, :) = 255;

spacing = 1;
[cols, rows] = meshgrid(1:spacing:size(I, 1), 1:spacing:size
(I, 2));
distance = (cols-4).^2+(rows-4).^2;
[rowindices, colindices] = find(distance <= 2^2);

a part of the matrix distance:

18 13 10 9 10 13
13 8 5 4 5 8
10 5 2 1 2 5
9 4 1 0 1 4
10 5 2 1 2 5
13 8 5 4 5 8
18 13 10 9 10 13

I got the right rowindices and colindices with less than or
equal 4

I should get a slight rotate square, but I get a matrix I

0 0 0 0 0 0 0
0 255 255 255 255 255 0
0 255 255 255 255 255 0
0 255 255 255 255 255 0
0 255 255 255 255 255 0
0 255 255 255 255 255 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0

It is weird, even the size of the rowindices matrix has
only 13 elements, now I have 25..
can you help pls..

Similar Threads

1. Problem 3D imaging: making 3000 coordinates into a solid 3d image

I've got coordinates(3000 times x, y and z) of a bonepiece. 

I want to make it a solid bonepiece and I thought of connecting the coordinates(three at the time) wich are close together with trisurf or tetramesh. somehow, matlab doesnt find the coordinates which are closest to eachother. I tried to solve it with griddata but that doesnt work because it has a problem with seeing wich points are the top of the bone and which are the bottompoints of the bone because it searches for coordinates in x and y and it forgets z.

T = csvread('tal0r.csv');
x = T(2325:3164,1);                        
y = T(2325:3164,2);                        
z = T(2325:3164,3);

[XI,YI] = meshgrid(-22:1:23,-32:1:32);          
ZI = griddata(x,y,z,XI,YI);            
mesh(XI,YI,ZI), hold on
tri = delaunay(XI,YI);                          
trisurf(tri,XI,YI,ZI,'FaceColor','red')       in  'FaceColor','green','EdgeColor','black'
hold off

please help me!

2. ploting a filled polygon - MATLAB

3. poly2mask & fill not giving same output for same polygon

these should give me an exactly overlapping blue and white region, but when executed there are few white pixels appearing. Could you please explain?

4. Filling a polygon without border - MATLAB

5. drawing a filled polygon

How do i draw a filled polygon in 3d given 3 corners?
a triangle for example given the tree corners

6. filling OUTSIDE polygon - MATLAB

7. Rotate a set of fill polygons?


I am new to MATLAB (esp. graphics in MATLAB), and I am drawing a to-scale picture of a rocket in a building. I read in the dimensions from a text file, then draw the many parts of the rocket and the building by using the "fill" command many times. However, I initially draw the rocket facing upwards, and I would like to change its orientation/rotation with respect to the building. Can someone tell me how can I do that?

I have tried drawing the building on one axis, and then drawing the rocket on a new, hidden axis and changing the view and 'Position' of this new axis, but I cannot get the rocket in the exact position I would like. I am sure there is an easier way but I have read through help and cannot find it! (Maybe grouping through hggroup or hgtransform? I am not sure how to use these.)

Thank you in advance,

8. Problem 3D imaging: making 3000 coordinates into a solid 3d - MATLAB