MATLAB Answers

How do I make a different shape like a hexagon or triangle using image processing?

14 views (last 30 days)
Sakibur Rahman
Sakibur Rahman on 27 Nov 2019
Answered: Akira Agata on 28 Nov 2019
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

  0 Comments

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 27 Nov 2019

  3 Comments

Sakibur Rahman
Sakibur Rahman on 27 Nov 2019
Sorry but is there a simpler code or explanation? I just want to know how to create shape like a hexagon or triangle . This is the code my prof used but it makes a square, so I was wondering if I can adjust/modify the code to make a triangle or hexagon.
layer_1 = 255*ones(50, 50); % red layer; 100%turned on
layer_2 = 255*ones(50, 50); % green layer 100%turned on
layer_3 = 255*ones(50, 50); % blue layer 100%turned on
my_image(:,:,1) = layer_1; % red layer,applied to 3D matrix.
my_image(:,:,2) = layer_2; % green layer,applied to 3D matrix.
my_image(:,:,3) = layer_3; % blue layer,applied to 3D matrix.
imshow(my_image);
for row_index = 1:25
for col_index = 25:50
my_image(row_index,col_index,2) =0; % modify green layer values
my_image(row_index,col_index,3) =0; % modify blue layer values
end
end
imshow(my_image);
Image Analyst
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
Sakibur Rahman on 27 Nov 2019
thanks I appreciate it, but what about using the code my teacher gave, we're supposed to use that but im not sure how.

Sign in to comment.


Akira Agata
Akira Agata on 28 Nov 2019
If you want to create regulay polygon shape image, how about using nsidedpoly function?
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)
hexagon.png

  0 Comments

Sign in to comment.

Sign in to answer this question.