问题描述
我有一个灰度的建筑物(.png)的图片文件,我有一个脚本生成一个网格图,其颜色强度是某人在白天花费时间的部分。该建筑物分为3x3网格,因此该图对应。如何将此INTO绘制到灰度图像并将其着色以使其与3x3绘图匹配?我已经非常谨慎地遵循这种叠加技术但不断出现错误:
BlendedImage:
I have a picture file of a building (.png) that is in grayscale, and I have a script that generates a grid plot with color intensity of what section of the building someone spent time in during the day. The building is divided into a 3x3 grid and the plot corresponds accordingly. How can I plot this INTO the grayscale image and colorize it so that it matches the 3x3 plot? I have treid to follow this overlay technique but keep getting errors: http://blogs.mathworks.com/steve/2009/02/18/image-overlay-using-transparency/
My grid plot script:
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
Any ideas or is this just not possible? I do have the image processing toolbox and if there is another program better suited for this I am happy to give it a shot.
Edit:
Here is the code I am trying to work with to get the blog post on transparency working:
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);
And the error I get, though I am willing to bet I am not outlining the overlay properly at all regardless of this error:
Error using set
Bad property value found.
Object Name : image
Property Name : 'AlphaData'.
Error in OverlayPlots (line 13)
set(h,'AlphaData', GridPlot);
Creating the GridPlot, without using figure, is a little tricky.
The following code creates the color plot in the size of your image.
I assumed size is 481 rows and 585 columns.
I created a color map, based on Matlab "JET" color map.
I used an inner loop iterates columns, and outer loop iterates rows.
Each inner iteration fills 1/9 of GridPlot with selected color from the map.
Matalb code:
%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');
GridPlot:
BlendedImage:
这篇关于是否可以使用matlab(或其他)将图形绘制成图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!