I want MATLAB to identify the overlapping area.
조회 수: 1 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
답변 (2개)
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)
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
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!