How do I make a different shape like a hexagon or triangle using image processing?
Show older comments
Hi, I have a Labtest in 2 hours and just found out that we need to know how to make different shapes such as a hexagon using image processing which I’m not familiar with. We were given this code which just makes a square. Any help would be appreciated.
layer_1 = 255*ones(50, 50);
layer_2 = 255*ones(50, 50);
layer_3 = 255*ones(50, 50);
my_image(:,:,1) = layer_1; % applied to 3D matrix. my_image(:,:,2) = layer_2; % applied to 3D matrix. my_image(:,:,3) = layer_3;
imshow(my_image);
for row_index = 1:25 for col_index = 25:50 my_image(row_index,col_index,1)= 0; my_image(row_index,col_index,3) = 0; end end
imshow(my_image);
My friend also gave me this code but it also makes a square. for i=1:100 for j=1:100 mat(j,i,1)=255; mat(j,i,2)=0; mat(j,i,3)=0; end end
imshow(mat)
Also pls bare my shitty editing skills, I’m not sure how to insert code since I’m using my iPad rn.
Thanks
if true
for i=1:100
for j=1:100
mat(j,i,1)=255;
mat(j,i,2)=0;
mat(j,i,3)=0;
end
end
imshow(mat)
end
if true
layer_1 = 255*ones(50, 50);
layer_2 = 255*ones(50, 50);
layer_3 = 255*ones(50, 50);
my_image(:,:,1) = layer_1; % applied to 3D matrix. my_image(:,:,2) = layer_2; % applied to 3D matrix. my_image(:,:,3) = layer_3;
imshow(my_image);
for row_index = 1:25 for col_index = 25:50 my_image(row_index,col_index,1)= 0; my_image(row_index,col_index,3) = 0; end end
imshow(my_image); end
Answers (2)
Akira Agata
on 28 Nov 2019
The folloing is an example:
% Create hexagon polyshape
pgon = nsidedpoly(6,'Center',[50,50],'Radius',30);
% Generate 100x100 binary image with hexagon
[xGrid,yGrid] = meshgrid(1:100,1:100);
BW = isinterior(pgon,xGrid(:),yGrid(:));
BW = reshape(BW,size(xGrid));
% Visualize
figure
imshow(BW)

1 Comment
HG
on 9 Jun 2021
how can I create a triangle or rectangle with curve edge ? and then show the matrix with imagesc ?
Image Analyst
on 27 Nov 2019
0 votes
See my attached demos.
9 Comments
Sakibur Rahman
on 27 Nov 2019
Image Analyst
on 27 Nov 2019
You saw the code in demo #2:
% Make a triangle on it.
triangleXCoordinates = [360 420 480];
triangleYCoordinates = [350 252 350];
traiangleBinaryImage = poly2mask(triangleXCoordinates, triangleYCoordinates, rows, columns);
% Burn it into the gray scale image.
grayImage(traiangleBinaryImage) = 255;
What's complicated about that?
And for the more general case with any number of vertices that you specify, demo #1 has this code:
%----------------------------------------------------------------------------------------------------------------------------------
% Create a single polygon with the specified number of sides in a binary image of the specified number of rows and columns.
% centroidToVertexDistance is the distance from the centroid to each vertex.
% If centroidToVertexDistance is a length 2 vector, then this indicated the minimum and maximum size range and
% it will create a random size polygon between the min and max distance.
function binaryImage = CreatePolygon(numSides, centroidToVertexDistance, rows, columns)
try
% Get the range for the size from the center to the vertices.
if length(centroidToVertexDistance) > 1
% Random size between a min and max distance.
minDistance = centroidToVertexDistance(1);
maxDistance = centroidToVertexDistance(2);
else
% All the same size.
minDistance = centroidToVertexDistance;
maxDistance = centroidToVertexDistance;
end
thisDistance = (maxDistance - minDistance) * rand(1) + minDistance;
% Create a polygon around the origin
for v = 1 : numSides
angle = v * 360 / numSides;
x(v) = thisDistance * cosd(angle);
y(v) = thisDistance * sind(angle);
end
% Make last point the same as the first
x(end+1) = x(1);
y(end+1) = y(1);
% plot(x, y, 'b*-', 'LineWidth', 2);
% grid on;
% axis image;
% Rotate the coordinates by a random angle between 0 and 360
angleToRotate = 360 * rand(1);
rotationMatrix = [cosd(angleToRotate), sind(angleToRotate); -sind(angleToRotate), cosd(angleToRotate)];
% Do the actual rotation
xy = [x', y']; % Make a numSides*2 matrix;
xyRotated = xy * rotationMatrix; % A numSides*2 matrix times a 2*2 = a numSides*2 matrix.
x = xyRotated(:, 1); % Extract out the x as a numSides*2 matrix.
y = xyRotated(:, 2); % Extract out the y as a numSides*2 matrix.
% Get a random center location between centroidToVertexDistance and (columns - centroidToVertexDistance).
% This will ensure it's always in the image.
xCenter = thisDistance + (columns - 2 * thisDistance) * rand(1);
% Get a random center location between centroidToVertexDistance and (rows - centroidToVertexDistance).
% This will ensure it's always in the image.
yCenter = thisDistance + (rows - 2 * thisDistance) * rand(1);
% Translate the image so that the center is at (xCenter, yCenter) rather than at (0,0).
x = x + xCenter;
y = y + yCenter;
binaryImage = poly2mask(x, y, rows, columns);
catch ME
errorMessage = sprintf('Error in function %s() at line %d.\n\nError Message:\n%s', ...
ME.stack(1).name, ME.stack(1).line, ME.message);
fprintf(1, '%s\n', errorMessage);
uiwait(warndlg(errorMessage));
end
It's not that bad. Half the lines are comments. Most of my programs are thousands of lines long. Don't be scared of a function that's a dozen or two lines long. If you want, you could shorten it by taking out the code that rotates the shape by a random angle.
Sakibur Rahman
on 27 Nov 2019
murat murat
on 22 Nov 2021
dear sir I run the code But there are some errors,like:
Error in ifunc parse_input[] at line 131
minpeak prominence is not a recognized parameter
and
Error in shape_recognition_demo1 at line 54
index exceeds matrix dimention
could you help me to solve the problem ?
Image Analyst
on 22 Nov 2021
@murat murat attach your code. I need to see how you changed it because I just downloaded and ran the code I attahced and it ran fine. For example I had
[peakValues, peakIndexes] = findpeaks(distances, 'MinPeakProminence', minPeakHeight);
while you might have changed it to
[peakValues, peakIndexes] = findpeaks(distances, 'Min Peak Prominence', minPeakHeight);
murat murat
on 24 Nov 2021
I did not do any changes, I only downloaded and run demo2 is working good but 1 is not .
I attached the errors and code
thank you very much
Image Analyst
on 24 Nov 2021
@murat murat I just downloaded both and both ran fine. The only thing I can guess is that you have a really old version of MATLAB where the findpeaks() function does not have the 'MinPeakProminence' option. What version do you have? And if you look up the help for findpeaks() does it have the MinPeakProminence option?
murat murat
on 24 Nov 2021
I am using matlab 2014 so that is the problem, :.(
thank you very much
Image Analyst
on 24 Nov 2021
Then just try getting rid of that option and see if it still works.
Categories
Find more on Detection in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!