我有一个问题,那该怎么做呢?!
我在matlab中读取了两个不同大小的图像,然后将它们转换为双倍,对它们进行操作,但问题是它们不是相同的大小,那么如何使它们与更大的图像相同,然后用0填充其他的空大小?
发布于 2013-02-19 01:49:56
假设你有两个矩阵:
a = 1 2 3
4 5 6
7 8 9
b = 1 2
3 4你可以这样做:
c = zeros(size(a)) %since a is bigger这将创建:
c = 0 0 0
0 0 0
0 0 0然后复制较小矩阵的内容(本例中为b):
c(1:size(b,1), 1:size(b,2)) = b;(size(b,1)返回行数,size(b,2)返回列数)
最终结果将是一个大小为a的矩阵,其中填充了b和0的值:
c = 1 2 0
3 4 0
0 0 0编辑:
image1=imread(image1Path);
image2=imread(image2Path);
image1= double(image1);
image2= double(image2);
%%%ASSUME image1 is bigger%%%
new_image = zeros(size(image1));
new_image(1:size(image2,1), 1:size(image2,2)) = image2;
%NOW new_image will be as you want.发布于 2013-02-19 02:32:55
你的问题有点模糊,但假设你有两个大小不同的矩阵A和B。现在,如果你总是想用零填充最小的维度,你可以这样做:
rs = max([size(A);size(B)]); % Find out what size you want
A(rs(1)+1,rs(2)+1) = 0; % Automatically pad the matrix to the desired corner (plus 1)
B(rs(1)+1,rs(2)+1) = 0; % Plus one is required to prevent loss of the value at (end,end)
A = A(1:end,1:end); %Now remove that one again
B = B(1:end,1:end);请注意,无论哪一个更大,以及一个更高,而另一个更宽,这都是有效的。当您喜欢使用if语句时,这可能更容易理解:
rs = max([size(A);size(B)]); % Find out what size you want
if any(size(A) < rs)
A(rs(1),rs(2)) = 0;
end
if any(size(B) < rs)
B(rs(1),rs(2)) = 0;
endhttps://stackoverflow.com/questions/14942091
复制相似问题