创建在Matlab片对象位图 [英] Create a bitmap from patch object in Matlab

查看:169
本文介绍了创建在Matlab片对象位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组多边形顶点的在X,Y矩阵和他们的颜色的RGB值在另一个矩阵C。

I have a set of polygon vertices in in X, Y matrices and their colors are in RGB values in another matrix C.

然后我用填充()函数生成显示在Matlab的数字补丁对象。

I then use fill() function to generate patch objects that is displayed in a Matlab figure.

我要创建从该图中一个bmp对象。我的意思是一个bmp对象是x,像素有RGB值的y坐标。

I want to create a bmp object from this figure. What I mean by a bmp object is the x,y coordinates of the pixels and there RGB values.

如果我用print()函数,以'-dbmp'和一个文件名,MATLAB可以写的bmp到一个文件中。但后来我不得不读取imread()的文件来创建BMP对象。

If I use the print() function, with '-dbmp' and a file name, matlab can write the bmp to a file. But then I have to read the file with imread() to create the bmp object.

有没有办法不写,从文件中读取到创建BMP对象?

Is there a way to create the bmp object without writing and reading to from a file?

Becauswe我必须执行此操作很多次,写作和阅读,文件非常耗时,而且会降低我的硬盘的寿命也是我猜。

Becauswe I have to perform this operation many times and writing and reading to file is time consuming and will reduce the life time of my disk too I guess.

编辑:根据答案code编辑后

code after editing according to answer

N = 5;
Tri = 100;
res = 200; %200 pixles per inch
G = zeros(Tri,9,N);


X = 2*rand(Tri,3,N);
Y = 2*rand(Tri,3,N);
R = randi([0 255],Tri,N)/255;
G = randi([0 255],Tri,N)/255;
B = randi([0 255],Tri,N)/255;

for c1=1:N
   G(:,1:3,c1)= X(:,:,c1);
    G(:,4:6,c1)= Y(:,:,c1);
    G(:,7,c1)= R(:,c1);
    G(:,8,c1)= G(:,c1);
    G(:,9,c1)= B(:,c1);

end

for c2=1:N;
    h = figure('Visible','off');
    set(h, 'PaperUnits', 'inches', 'PaperPosition', [0 0 400 400]/res);
    for c3 =1:Tri
        h1 = fill(G(c3,1:3,c2), G(c3,4:6,c2), [G(c3,7,c2) G(c3,8,c2) G(c3,9,c2)]);
        set(h1,'EdgeColor','None');
        hold on;
    end
    %print(h,'-dbmp',['-r' num2str(res)],['file' num2str(c2)]);
    F = getframe(h);
    [a, b] = frame2im(F);

    Tmp_v1 =  a;
    Tmp_v1 = Tmp_v1(:);
    Norm_v1(c2) = norm(single(Tmp_v1));
end

感谢您。

推荐答案

如果您目前的数字开放,您可以尝试的 的getFrame 成语。一旦你有机会获得这一点,你可以通过查看该结构中的 CDATA 字段访问帧的图像数据。你有这个后,您可以使用 imwrite 命令(让我们希望你有它...)写的图像到文件。

If you have the current figure open, you can try the getframe idiom. Once you have access to this, you can access the frame's image data by looking at the cdata field in the structure. After you have this, you can use the imwrite command from MATLAB's image processing toolbox (let's hope you have it...) to write the image to file.

下面是一个例子:

x = 1 : 5;
y = 1 : 5;
plot(x,y); %// Plot a line
h = getframe;
img = h.cdata;
imwrite(img, 'testFrame.bmp');

这应该能够抓住什么是目前的数字里并保存到文件中。在这种情况下,这将是一条直线(X,Y)=(1,1)(X,Y)=(5 5),以1熊记住,这将不会保存图形或轴的标题一个斜坡。这只会抢什么框架内呈现。

This should be able to grab what is inside the current figure and save it to file. In this case, this will be a straight line from (x,y) = (1,1) to (x,y) = (5,5), with a slope of 1. Bear in mind that this won't save the title of the graph or the axes. This will only grab what is rendered inside the frame.

现在,我知道你真的是经过什么样,你想生成一串随机的多边形,然后提取只是核心形象,没有任何轴或刻度线等。你必须让你想要的数字填满整个窗口,没有任何灰色填充您还需要修改的地方要创建的图像你的循环关闭轴了。此外,当您正在生成的每个图像,你需要关闭蜱。这与不写任何刻度标记,以及蜱长度设置为0。换句话说组合完成的,你需要修改code,这样它看起来像这样。你会看到我通过看插入code中的 //%NEW 在code语句:

Now that I know what you're really after, you want to generate a bunch of random polygons, then extract just the core image, without any axes or tick marks and so on. You'll have to modify your for loop where you're creating the images so that you want the figure to fill the entire window without any gray padding You'll also want to turn off the axis too. Also, when you're generating each image, you'll need to turn off the ticks. This is done with a combination of not writing any tick labels, as well as setting the tick length to 0. In other words, you'll need to modify your code so that it looks like this. You'll see where I inserted code by seeing the %// NEW statements in your code:

for c2=1:N;
    h = figure('Visible','off');
    subplot('position', [0 0 1 1]); %// NEW
    axis off; %// NEW
    set(h, 'PaperUnits', 'inches', 'PaperPosition', [0 0 400 400]/res);
    for c3 =1:Tri
        h1 = fill(G(c3,1:3,c2), G(c3,4:6,c2), [G(c3,7,c2) G(c3,8,c2) G(c3,9,c2)]);
        set(h1,'EdgeColor','None');
        set(gca,'xcolor','w','ycolor','w','xtick',[],'ytick',[]) %// NEW
        set(gca,'Ticklength',[0 0]); %// NEW
        hold on;
    end
    %print(h,'-dbmp',['-r' num2str(res)],['file' num2str(c2)]);

    F = getframe(h);
    close all; %// NEW
    [a, b] = frame2im(F);
    a = imresize(a, [400 400], 'nearest'); %// NEW

    Tmp_v1 =  a;
    Tmp_v1 = Tmp_v1(:);
    Norm_v1(c2) = norm(single(Tmp_v1));
end


这仍然会告诉你被弹出了您创建的每个图像帧,但你应该能够在这一点上得到的只是纯粹的图像数据。需要注意的是图片尚未出来作为比稍大400×400 。这是由于这样的事实,一旦我们已经删除了边框和刻度线等,这一数字将弹力以填满整个身影。为了解决这个问题,我使用 imresize 并缩小图像回落到 400×400 按你想要的大小。


This will still show you the frames being popped up for each image you're creating, but you should be able to get just pure image data at this point. Note that the images are still coming out as a bit larger than 400 x 400. This is due to the fact that once we have removed the borders and the tick marks and so on, the figure will stretch to fill the entire figure. To get around this, I use imresize and shrink the images back down to 400 x 400 as per your desired size.

另外,还要注意的是每次生成一个新的形象的时候,一个新的数字催生。每次调用的getFrame 的时候,这个随机生成多边形图像图弹出,而的getFrame 采取的一个快照当前帧。没有办法prevent这种情况的发生,因为你将不能够把这个数字的快照。要解决这个问题的一种方法是你抢图像后关闭的身影。你可以做一个关闭所有; 每次调用的getFrame 之后。这种方式中,只有一个图被示出在任一时刻,但仍不会prevent从示出了图

Also, take note that every time you generate a new image, a new figure is spawned. Every time you call getframe, the figure with this randomly generated polygon image pops up, and getframe takes a snapshot of that current frame. There isn't a way to prevent this from happening, as you won't be able to take a snapshot of that figure. One way to get around this would be to close the figure after you grab the image. You can do a close all; after each call to getframe. That way, only one figure gets shown at any one time, but this still won't prevent the figure from showing up.

如果我找到一个解决这个问题,我会告诉你的!

If I do find a solution to this, I'll let you know!

这篇关于创建在Matlab片对象位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆