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

조회 수: 23 (최근 30일)
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

답변 (2개)

Akira Agata
Akira Agata 2019년 11월 28일
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

Image Analyst
Image Analyst 2019년 11월 27일
  댓글 수: 9
murat murat
murat murat 2021년 11월 24일
I am using matlab 2014 so that is the problem, :.(
thank you very much
Image Analyst
Image Analyst 2021년 11월 24일
Then just try getting rid of that option and see if it still works.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by