内接对象旋转的圆的结果-Matlab [英] Inscribe circle of an object rotated results - Matlab
问题描述
I modified a code based on the shown in https://www.mathworks.com/matlabcentral/answers/377838-please-how-can-i-find-the-center-and-the-radius-of-the-inscribed-circle-of-a-set-of-points in order to find the inscribe circle but I do not understand why the image is rotated. Why and how can I solve it?
代码:
url='https://i.pcmag.com/imagery/reviews/00uaCVfzQ4Gsuhmh85WvT3x-4.fit_scale.size_1028x578.v_1569481686.jpg';
Image = rgb2gray(imread(url));
Image = imcomplement(Image);
fontSize = 10;
% determine contours
BW = imbinarize(Image);
BW = imfill(BW,'holes');
[B,L] = bwboundaries(BW,'noholes');
k = 1;
b = B{k};
y = b(:,2);
x = b(:,1);
subplot(2, 2, 1);
plot(x, y, 'b.-', 'MarkerSize', 3);
grid on;
title('Original Points', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% Make data into a 1000x1000 image.
xMin = min(x)
xMax = max(x)
yMin = min(y)
yMax = max(y)
scalingFactor = 1000 / min([xMax-xMin, yMax-yMin])
x2 = (x - xMin) * scalingFactor + 1;
y2 = (y - yMin) * scalingFactor + 1;
mask = poly2mask(x2, y2, ceil(max(y2)), ceil(max(x2)));
% Display the image.
p2 = subplot(2, 2, 2);
imshow(mask);
axis(p2, 'on', 'xy');
title('Mask Image', 'FontSize', fontSize);
% Compute the Euclidean Distance Transform
edtImage = bwdist(~mask);
% Display the image.
p3 = subplot(2, 2, 3);
imshow(edtImage, []);
axis(p3, 'on', 'xy');
% Find the max
radius = max(edtImage(:))
% Find the center
[yCenter, xCenter] = find(edtImage == radius)
% Display circles over edt image.
viscircles(p3, [xCenter, yCenter], radius,'Color','g');
% Display polygon over image also.
hold on;
plot(x2, y2, 'r.-', 'MarkerSize', 3, 'LineWidth', 2);
title('Euclidean Distance Transform with Circle on it', 'FontSize', fontSize);
% Display the plot again.
subplot(2, 2, 4);
plot(x, y, 'b.-', 'MarkerSize', 3);
grid on;
% Show the circle on it.
hold on;
% Scale and shift the center back to the original coordinates.
xCenter = (xCenter - 1)/ scalingFactor + xMin
yCenter = (yCenter - 1)/ scalingFactor + yMin
radius = radius / scalingFactor
rectangle('Position',[xCenter-radius, yCenter-radius, 2*radius, 2*radius],'Curvature',[1,1]);
title('Original Points with Inscribed Circle', 'FontSize', fontSize);
原始图片:
输出图像
推荐答案
[B,L] = bwboundaries(BW,...)
在B
中返回行和列值(
[B,L] = bwboundaries(BW,...)
returns in B
the row and column values (documentation). That is, the first column of B{k}
is y
, the second one is x
.
按如下所示更改这段代码后:
After changing this bit of code as follows:
y = b(:,1);
x = b(:,2);
您会注意到图像颠倒了!这是因为在图像中y轴向下增加(y
是矩阵中的行号),而在绘图中y轴增加(数学坐标系).
you will notice that the image is upside down! That is because in an image the y-axis increases down (y
is the row number in the matrix), whereas in the plot the y-axis increases up (mathematical coordinate system).
在其中使用imshow
的轴会自动设置为正确的坐标系,但是随后执行axis(p3, 'on', 'xy');
并再次将其上下颠倒即可.而是使用
The axes where you use imshow
in are automatically set to the right coordinate system, but then you do axis(p3, 'on', 'xy');
, turning it upside down again. Instead, use
axis(p1, 'on', 'image');
在不使用imshow
的轴(即左上角和右下角的轴)上.
on the axes where you don't use imshow
(i.e. the top-left and bottom-right ones).
这篇关于内接对象旋转的圆的结果-Matlab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!