0

問題があるのでどうしたらいいですか?!

matlabでサイズの異なる2つの画像を読み取り、それらを2倍にして操作しますが、サイズが同じではないという問題があるため、大きい画像と同じにして、他の空のサイズをゼロで埋めるにはどうすればよいですか? ?

4

2 に答える 2

2

2つの行列があるとしましょう:

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で満たされたサイズのマトリックスになります。b0

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-18T17:49:56.087 に答える
0

質問は少しあいまいですが、2つの行列が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
于 2013-02-18T18:32:54.990 に答える