我有问题,怎么办?!
我在matlab中读取了两个不同大小的图像,然后我将它们转换为加倍以对它们进行操作,但问题是它们的大小不同,所以如何使它们与更大的图像相同,然后将其他空大小填充为零?
我有问题,怎么办?!
我在matlab中读取了两个不同大小的图像,然后我将它们转换为加倍以对它们进行操作,但问题是它们的大小不同,所以如何使它们与更大的图像相同,然后将其他空大小填充为零?
假设您有两个矩阵:
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.
您的问题有点模糊,但假设您有两个矩阵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;
end