I want MATLAB to identify the overlapping area.

5 views (last 30 days)
I have a hexagonal grid. The following is its code:
l=10;
k=12;
C=rand(l,k);
xhex=[0 1 2 2 1 0];
yhex=[2 3 2 1 0 1];
for i=1:k
j=i-1;
for q=1:l
m=q-1;
patch((xhex+mod(q,2))+2*j,yhex+2*m,C(q,i))
hold on
end
end
axis equal
I want to place this grid on an image. Code for image is:
P=imread('design2image_pa.png');
imshow(P)
grayImage = imread('design2image_pa.png');
imshow(grayImage, [], 'XData', [0, 50], 'YData', [0, 77]);
axis('on', 'image');
The output I want is only the hexagons that overlap with the circle to appear. Can someone kindly help me out with this. Many thanks.

Answers (2)

Voss
Voss on 3 Jul 2022
Edited: Voss on 3 Jul 2022
I don't have the image file, but you mention a circle, so I've specified a circle and modified the hexagon grid code to create only those hexagons which have at least one vertex strictly inside the circle. Maybe this modified code can be a useful reference for you.
R_circle = 6;
C_circle = [15 11.5];
l=10;
k=12;
C=rand(l,k);
xhex=[0 1 2 2 1 0];
yhex=[2 3 2 1 0 1];
hold on
for i=1:k
j=i-1;
for q=1:l
m=q-1;
x = (xhex+mod(q,2))+2*j;
y = yhex+2*m;
if any((x-C_circle(1)).^2 + (y-C_circle(2)).^2 < R_circle^2)
patch(x,y,C(q,i));
end
end
end
axis equal
th = linspace(0,2*pi,200);
line(C_circle(1)+R_circle*cos(th),C_circle(2)+R_circle*sin(th), ...
'Color','k','LineWidth',2)
  1 Comment
Mariam Shahab
Mariam Shahab on 3 Jul 2022
Thank you for your help.
I have attached the image. Could you kindly guide me on what the code will be when using this image? Thanks in advance.

Sign in to comment.


Image Analyst
Image Analyst on 3 Jul 2022
If you have a color digital image, and a binary image of the circle, which you can get using poly2mask, then you can mask the image with the mask like this:
% Mask the image using bsxfun() function to multiply the mask by each channel individually. Works for gray scale as well as RGB Color images.
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, 'like', rgbImage));
Attach your 'design2image_pa.png' image if you want more help.
  4 Comments
Image Analyst
Image Analyst on 4 Jul 2022
That's not the higher goal. What's the use case? Is it your homework assignment? Or are you making some kind of plate or grate that will cover a hole or pipe in a nuclear power plant or some kind of machinery?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by