14 views (last 30 days)

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

Image Analyst
on 27 Nov 2019

See my attached demos.

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.

Sign in to comment.

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)

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.