直接将绘图保存到图像,而无需在MATLAB中使用getframe

时间:2014-06-12 12:22:04

标签: matlab image-processing matlab-figure

我有以下代码使用bwboundaries()检索图像的边界。

问题是我需要将生成的绘图线直接保存到图像而不先绘制它。目前我正在使用getframe()功能将绘图保存到图像。然而,这需要花费大量的处理时间,因为我必须每次为每个输入图像绘制边界图像。

以下是我目前使用的代码:

 %GET 8-NEIGHBOURHOOD BOUNDARIES
    [B,L,N] = bwboundaries(z,'noholes');

 %PLOT THE BOUNDARY FIGURE FOR EACH WORD
    f = figure;
    set(f, 'Visible', 'off');
    hold on;
    for k=1:length(B),
        boundary = B{k};
        if(k > N)
            plot(boundary(:,2),boundary(:,1),'g','LineWidth',2);
        else
            plot(boundary(:,2),boundary(:,1),'r','LineWidth',2);
        end
    end
    hold off;
    set(gca, 'visible', 'off');
    F = getframe(gcf);
    [X, Map] = frame2im(F);
    close(gcf);

 %CONVERT EACH BOUNDARY PLOT TO AN IMAGE
    if isempty(Map)
        rgb = X;
    else
        rgb = ind2rgb(X,Map);
    end

我有一个想法,因为每一行都是在循环中绘制的,我可以开始将每一行附加到图像矩阵。但我不知道如何实现这一点。 如果我没有弄错的话,每行的X和Y坐标是boundary(:,2),boundary(:,1)


看看Fletch的回复,我在代码中尝试了以下修改:

 %GET 8-NEIGHBOURHOOD BOUNDARIES
    [B,L,N] = bwboundaries(z,'noholes');

    rgb = ones(100, 100, 3);

    %PLOT THE BOUNDARY FIGURE FOR EACH WORD
    for k=1:length(B),
        boundary = B{k};
        if(k > N)
            rgb(boundary(:,2), boundary(:,1), :) = [0 0 0];
        else
            rgb(boundary(:,2), boundary(:,1), :) = [0 0 0];
        end
    end

然而,这给了我以下错误:

Subscripted assignment dimension mismatch.

Error in random (line 107)
            rgb(boundary(:,2), boundary(:,1), :) = [0 0 0];

B是一个单元格数组,例如B {2},具有以下数据:
2931 168
2931 169
2931 170
2931 171
2931 172
2931 173
2931 174
2931 175
2931 176
2931 177
2931 176
2931 175
2931 174
2931 173
2931 172
2931 171
2931 170
2931 169
2931 168

2 个答案:

答案 0 :(得分:0)

如果您拥有计算机视觉系统工具箱,则可以使用insertShape将线条直接绘制到图像中。

答案 1 :(得分:0)

据我所知,您正在尝试使用线坐标绘制图像。考虑:

  1. 你有X和Y坐标向量线(我假设坐标是整数)。
  2. 图像只是分配给2D矩阵的一组RBG矢量。
  3. 所以,你应该做的是制作一个空白的m-n-3矩阵,并改变图像矩阵上每个X-Y点的RBG值,该值对应于线X-Y集合中的一个点。像这样:

    %// First, set the size of your image. (you'll need to customize this)
    [imageSizeX, imageSizeY, ~] = size(yourInputImage);
    
    %// Next, build a matrix corresponding to an all-white image. 
    imageMat = ones(imageSizeX, imageSizeY, 3);
    
    %// Then, "black out" each pixel which corresponds to a point on your line.
    imageMat(boundary(:,2), boundary(:,1), :) = [0 0 0];
    
    %// At this point imageMat IS and image, and you can do anything with it that you would do with an image
    %// Finally, save the matrix as an image. This step is optional.
    imwrite(imageMat, 'yourImage.png');
    

    这是解决问题的一种相对有效(而且非常聪明,我认为)的方法。如果您需要更多解释,或者它不能按预期方式工作,请告诉我。我会尽力帮忙。祝你好运!


    编辑:试试这个。请注意,您需要提供yourInputImage的实际变量名称。这应解决维度不匹配问题。如果您收到index exceeds matrix dimensions错误,flip boundary(i,2)boundary(i,1)

    [B,L,N] = bwboundaries(z,'noholes');
    [imageSizeX, imageSizeY, ~] = size(yourInputImage);
    rgb = ones(imageSizeX, imageSizeY, 3);
    
    %PLOT THE BOUNDARY FIGURE FOR EACH WORD
    for k=1:length(B),
        boundary = B{k};
        if(k > N)
            for(ii= 1:size(boundary, 2) )
                rgb(boundary(ii,2), boundary(ii,1), :) = [0 1 0];
            end
        else
            for(ii= 1:size(boundary, 2) )
                rgb(boundary(ii,2), boundary(ii,1), :) = [1 0 0];
            end
        end
    end