【问题标题】:Is it possible to plot a graph into an image using matlab (or something else)?是否可以使用 matlab(或其他东西)将图形绘制成图像?
【发布时间】:2016-07-06 12:41:00
【问题描述】:

我有一个灰度的建筑物图片文件 (.png),我有一个脚本,该脚本可以生成一个网格图,其中包含有人在白天呆过的建筑物的哪个部分的颜色强度。建筑物被划分成一个 3x3 的网格,地块也相应地对应起来。如何将其绘制成灰度图像并对其进行着色以使其与 3x3 绘图相匹配?我很想遵循这种覆盖技术,但不断出错:http://blogs.mathworks.com/steve/2009/02/18/image-overlay-using-transparency/

我的网格绘图脚本:

Data = [1:1:9; 1 2 3 4 5 6 7 8 9; 1 2 3 4 5 6 7 8 9 ;1 2 3 4 5 6 7 8 9;1 2 3 4 5 6 7 8 9];

M = zeros(3,3);

 for ii = 2:size(Data,1)
      plot(ii-1)
      M(1:end) = Data(ii,:);
      imagesc(M)
      colormap jet
      shading flat %for an exact result
      % shading interp %for a smooth result 
figure
  end

有什么想法或者这不可能吗?我确实有图像处理工具箱,如果有其他程序更适合这个,我很乐意试一试。

编辑:

这是我尝试使用的代码,以使有关透明度的博客文章正常工作:

BaseImage = imread('buildinglayout.png'); 
imshow(BaseImage, 'InitialMag', 'fit');
GridPlot = imread('5.png'); %output of grid plot generating script
imshow(GridPlot,'InitialMag','fit')
M(1:end) = Data(ii,:);
h = imshow(M);
hold off
set(h,'AlphaData', GridPlot); 

我得到的错误,尽管我愿意打赌,不管这个错误如何,我根本没有正确地勾勒出覆盖:

Error using set
Bad property value found.
Object Name :  image
Property Name :  'AlphaData'.

Error in OverlayPlots (line 13)
set(h,'AlphaData', GridPlot);

【问题讨论】:

  • 你的错误是什么?也给我们这部分代码。
  • @obchardon 编辑了帖子以显示错误以及我尝试调整博客帖子代码。
  • AlphaData 应该与您的图像的 CData 大小相同,这就是 set 所抱怨的。
  • @RandomGuy 如何确保网格图的大小与建筑布局的大小相同?网格是 3x3 的矩形,但建筑物 png 是不规则形状。使用图像处理工具箱,我可以进行边缘检测并仅隔离建筑部分,并获得这些边界,但我如何直接绘制到该空间?
  • imfilll 或仅填充功能可以在这里工作吗? mathworks.com/help/matlab/ref/fill.html

标签: matlab image-processing plot


【解决方案1】:

不使用图形创建 GridPlot 有点棘手。
以下代码以您的图像大小创建颜色图。
我假设大小为 481 行和 585 列。

我创建了一个基于 Matlab“JET”颜色图的颜色图。
我使用了内循环迭代列,外循环迭代行。
每次内部迭代都会用地图中选定的颜色填充 GridPlot 的 1/9。

金属板代码:

%BuildGridPlot
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
width = 585;  %Number of columns.
height = 481; %Number of rows.

%Fill matrix GridPlot with zeros.
%GridPlot dimensions is: height x width x 3
GridPlot = uint8(zeros(height, width, 3));

w_div3 = width/3;
h_div3 = height/3;

%Create JET color map - each row index is [R, G, B] triple.
ColorMap = jet(9); %Create 9 triples.

%Convert color map from double to uint8.
ColorMap = uint8(round(ColorMap*255));

color_counter = 1;

%Fill 3x3 blocks with different colors.
for y = 0:2
    y0 = 1 + round(y*h_div3);
    y1 = round((y+1)*h_div3);
    y1 = min(y1, height);      %Limit y1 to height (just in case rounded up...).
    for x = 0:2
        x0 = 1 + round(x*w_div3);
        x1 = round((x+1)*w_div3);
        x1 = min(x1, width);   %Limit x1 to width (just in case rounded up...).

        %Fill rectangular area from row y0 to row y1, and from column x0 to
        %column x1 with [R, G, B] triple from ColorMap.
        %The row index of the [R, G, B] triple is color_counter.
        GridPlot(y0:y1, x0:x1, 1) = ColorMap(color_counter, 1); %Set pixels red color
        GridPlot(y0:y1, x0:x1, 2) = ColorMap(color_counter, 2); %Set pixels green color
        GridPlot(y0:y1, x0:x1, 3) = ColorMap(color_counter, 3); %Set pixels blue color

        color_counter = color_counter + 1;
    end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%GridPlot = imwrite('GridPlot.png');

%Load cameraman as an example for grayscale image.
BaseImage = imread('cameraman.tif');

%Resize BaseImage to [height, width] (simulate size of your image).
BaseImage = imresize(BaseImage, [height, width]);

%Verify 
if (ndims(BaseImage) == 2)
    %Duplicate BaseImage 3 times - convert to RGB.
    BaseImageRGB = cat(3, BaseImage, BaseImage, BaseImage);    
elseif (ndims(BaseImage) == 3)
    %BaseImage is already in RGB format - copy as is.
    BaseImageRGB = BaseImage;    
else
    error('Error: BaseImage is not a valid image');
end

alpha = 0.7; %Tranceparency coefficient.

%Take 70% of BaseImage and 30% of GridPlot image.
BlendedImage = alpha*double(BaseImageRGB) + (1-alpha)*double(GridPlot);

%Convert to uint8 with rounding.
BlendedImage = uint8(round(BlendedImage));

figure;
imshow(BlendedImage);

imwrite(BlendedImage, 'BlendedImage.png');

网格图:

混合图像:

【讨论】:

  • 有趣我试一试,如果图像不是正方形会怎样?它会适合图片的网格吗?
  • 网格图的创建是否从某个地方获取数据?还是应该用我的数据生成一堆网格图,将它们保存为图片,然后运行覆盖脚本?
【解决方案2】:

我不知道may的回答是否正是你的意思...... 最简单的解决方案(对我而言)是直接使用 RGB 矩阵。
在 Matlab 中,RGB 图像可以表示为 uint8 元素的 3 维数组。

  • RGB(:,:,1) 纯红色
  • RGB(:,:,2) 绿色素色
  • RGB(:,:,3) 蓝色纯色

灰度图像可以表示为 uint8 元素的一维数组。

将灰度图像转换为 RGB 很简单:将灰度矩阵复制 3 次。我用猫(3,我,我,我)。

混合两个 RGB 图像很简单:
BlendRGB = alpha*RGB1 + (1-alpha)*RGB2。
当 alpha 在 [0, 1] 范围内时。
(假设 RGB1、RGB2 的大小相同并且属于双精度类 - 不是 uint8)。

我使用 imresize() 使 GridPlot 与 BaseImage 具有相同的大小(相同的分辨率)。

在我的代码示例中,我将图形转换为 GridPlot,并留下了灰色边框和网格编号 - 我假设您已在 '5.png' imgae 中删除了它。

这是我的代码示例(基于您的原始代码):

Data = [1:1:9; 1 2 3 4 5 6 7 8 9; 1 2 3 4 5 6 7 8 9 ;1 2 3 4 5 6 7 8 9;1 2 3 4 5 6 7 8 9];

M = zeros(3,3);
h = figure;
for ii = 2:size(Data,1)
    plot(ii-1)
    M(1:end) = Data(ii,:);
    imagesc(M)
    colormap jet
    shading flat %for an exact result
    % shading interp %for a smooth result 
%figure
end

%Convert figure to RGB image.
GridPlot = frame2im(getframe(h));

%BaseImage = imread('buildinglayout.png');

%Load cameraman as an example for grayscale image.
BaseImage = imread('cameraman.tif');

%GridPlot = imread('5.png'); %output of grid plot generating script

%Resize GridPlot image to be the same dimensions as BaseImage.
GridPlot = imresize(GridPlot, [size(BaseImage, 1), size(BaseImage, 2)]);

%Verify 
if (ndims(BaseImage) == 2)
    %Duplicate BaseImage 3 times - convert to RGB.
    BaseImageRGB = cat(3, BaseImage, BaseImage, BaseImage);    
elseif (ndims(BaseImage) == 3)
    %BaseImage is already in RGB format - copy as is.
    BaseImageRGB = BaseImage;    
else
    error('Error: BaseImage is not a valid image');
end

alpha = 0.7; %Tranceparency coefficient.

%Take 70% of BaseImage and 30% of GridPlot image.
BlendedImage = alpha*double(BaseImageRGB) + (1-alpha)*double(GridPlot);

%Convert to uint8 with rounding.
BlendedImage = uint8(round(BlendedImage));

figure;
imshow(BlendedImage);

imwrite(BlendedImage, 'BlendedImage.png');

混合图像:

【讨论】:

  • 这对我来说是一个很好的开始,谢谢!!但我收到一个错误:使用 iptcheckmap 时出错(第 35 行)函数 IMRESIZE 预期输入编号 2 MAP 是有效的颜色图。有效的颜色图的值不能超出 [0,1] 范围。
  • imresize 可以使用:B = imresize(A, scale) 或 B = imresize(A, [numrows numcols])。
  • 如果我尝试使用 GridPlot= imresize(GridPlot, [481 585] 行,我在混合过程中进一步向下得到一个错误:数组尺寸必须与二进制数组操作匹配。绘图中的错误(第 36 行)BlendedImage = alpha*double(BaseImageRGB) + (1-alpha)*double(GridPlot);
  • 我修改了代码示例,使其更加宽容。
  • 效果很好!现在要弄清楚如何让它仅在我的图片区域内绘制。在您的情况下,我将如何仅在您照片中的那个人中进行绘图?
猜你喜欢
  • 2011-04-13
  • 2021-10-13
  • 1970-01-01
  • 2012-07-29
  • 2023-04-02
  • 1970-01-01
  • 2021-08-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多