I want MATLAB to identify the overlapping area.

조회 수: 1 (최근 30일)
Mariam Shahab
Mariam Shahab 2022년 7월 3일
댓글: Image Analyst 2022년 7월 4일
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.

답변 (2개)

Voss
Voss 2022년 7월 3일
편집: Voss 2022년 7월 3일
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
Mariam Shahab
Mariam Shahab 2022년 7월 3일
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.

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


Image Analyst
Image Analyst 2022년 7월 3일
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
Mariam Shahab
Mariam Shahab 2022년 7월 4일
Well, the goal is that the program is able to give me only those colored regions where it identified an overlap with the circle (or any figure). The figure size has to be smaller than the colored region.
Image Analyst
Image Analyst 2022년 7월 4일
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?

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

Community Treasure Hunt

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

Start Hunting!

Translated by