在 Matlab 中使用轴坐标生成图像而不绘制它

发布于 2024-12-09 11:11:39 字数 460 浏览 0 评论 0原文

我有一张充满椭圆形物体的图像。我需要为每个对象设计一个最适合对象本身的椭圆。我找到了一个代码,可以帮助我在图像上绘制椭圆 这里

我更改了最后部分,将 xy 保存在 3D 矩阵中(一个维度用于 x,另一个维度用于 y code>,第三个表示对象的数量)。因为此代码位于 for 循环中,所以我不想在其顶部生成图形省略号,请保存它并使用 imread 上传以将其传递给其余部分的代码。

有没有办法将这个 3D 矩阵转换为黑白图像,其中在图像中的正确位置充满了拟合椭圆?

I have an image full of objects of the shape of an ellipse. I need to design an ellipse for each object that is the best fit for the object itself. I have found a code that helps me to plot the ellipses on the image here.

I have changed the final part saving x and y in a 3D matrix (one dimension for x, the other for y, and the 3rd for the number of objects). Because this code is in a for loop, I do not want to generate the figure plot ellipses on top of it, save it and upload with imread to pass it to the rest of the code.

Is there a way to convert this 3D matrix in a bw image full of the fitting ellipses in the correct position in the image?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长梦不多时 2024-12-16 11:11:39

由于使用 imshow 显示图像后的 hold on 语句,因此在现有图形的顶部绘制了椭圆。因此,而不是这样:

imshow(bw)
hold on

只需使用 figure 语句创建一个新图形:

figure

[Edit]

好的,首先,仅存储 (x, y)< /code> 只给你椭圆中心。要绘制椭圆,您还需要存储其长轴/短轴尺寸(ab)及其方向角(theta) 。

我会简单地重用您已有的循环,但将每个坐标的黑白图像像素设置为 1 来替换 plot

% get image dimensions
dim = size(bw);

% preallocate a blank bw image
target = false(dim);

% for each ellipse
for k = 1:length(s)

    % this part remains the same:
    xbar = s(k).Centroid(1);
    ybar = s(k).Centroid(2);

    a = s(k).MajorAxisLength/2;
    b = s(k).MinorAxisLength/2;

    theta = pi*s(k).Orientation/180;
    R = [ cos(theta)   sin(theta)
         -sin(theta)   cos(theta)];

    xy = [a*cosphi; b*sinphi];
    xy = R*xy;

    x = xy(1,:) + xbar;
    y = xy(2,:) + ybar;

    % ----------
    % but replace plot(x,y) with this:

    % limit to image dimensions (1:256)
    x(x<1) = 1; x(x>dim(1))=dim(1);
    y(y<1) = 1; y(y>dim(2))=dim(2);    

    % set those pixels to 1
    target(sub2ind(dim, round(x),round(y))) = 1;

end

imshow(target);

现在,有一半在图像边界之外的椭圆。这就是为什么它们的 x,y 坐标需要限制为 (1:256);否则你会得到超出范围的错误。您仍然需要重新考虑是否应该完全删除这些省略号,或者像这里那样部分绘制。

Ellipses are drawn on top of the existing figure because of the hold on statement after the image is shown using imshow. So, instead of this:

imshow(bw)
hold on

Simply create a new figure using the figure statement:

figure

[Edit]

Ok, first of all, storing only (x, y) gives you ellipse centers only. To draw an ellipse, you will also need to store its majos/minor axis size (a, b) and its orientation angle (theta).

I would simply reuse the loop that you already have, but replace plot with simply setting a bw image pixel to 1 for each coordinate:

% get image dimensions
dim = size(bw);

% preallocate a blank bw image
target = false(dim);

% for each ellipse
for k = 1:length(s)

    % this part remains the same:
    xbar = s(k).Centroid(1);
    ybar = s(k).Centroid(2);

    a = s(k).MajorAxisLength/2;
    b = s(k).MinorAxisLength/2;

    theta = pi*s(k).Orientation/180;
    R = [ cos(theta)   sin(theta)
         -sin(theta)   cos(theta)];

    xy = [a*cosphi; b*sinphi];
    xy = R*xy;

    x = xy(1,:) + xbar;
    y = xy(2,:) + ybar;

    % ----------
    % but replace plot(x,y) with this:

    % limit to image dimensions (1:256)
    x(x<1) = 1; x(x>dim(1))=dim(1);
    y(y<1) = 1; y(y>dim(2))=dim(2);    

    % set those pixels to 1
    target(sub2ind(dim, round(x),round(y))) = 1;

end

imshow(target);

Right now, there are ellipses which are half outside the image boundaries. That's why their x,y coordinates need to be limited to (1:256); otherwise you get an out-of-range error. You still need to rethink whether these ellipses should be removed entirely, or drawn partially as done here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文