13

イメージを MATLAB に読み込み、その上に四角形を描画して、イメージを保存したいと考えています。

また、私は MATLAB を学習しているところです。お手柔らかにお願いします。簡単なように思えますが、私にはできないようです。

im = imread('image.tif');
imshow(im);
rectangle('Position', [100, 100, 10, 10]);
imwrite(im, 'image2.tif');

画像に四角形が表示されますが、保存された画像には四角形が表示されません。画像を保存して長方形を表示するにはどうすればよいですか?

FWIW、私はすでに試しましsaveas()たが、それは私に巨大なイメージを与えます. saveas()保存した画像を使用して正しいサイズにする方法はありますか?

4

8 に答える 8

19

保存された画像に四角形が表示されない理由はim、画像データを格納する変数を変更していないためです。四角形は、プロットされたイメージの上に表示される単なるプロット オブジェクトです。画像データ自体を変更する必要があります。

通常、MATLAB に読み込まれたイメージは、N x M x 3 の行列 (つまり、各ピクセルの RGB (赤、緑、青) 値を持つ N x M のピクセル イメージ) として読み込まれます。通常、画像データは uint8 データ型であるため、RGB 値の範囲は 0 ~ 255 です。特定のピクセルの RGB 値を変更する場合は、次のようにします。

im = imread('test.jpg');  % Load a jpeg image
im(1,1,1) = 255;  % Change the red value for the first pixel
im(1,1,2) = 0;    % Change the green value for the first pixel
im(1,1,3) = 0;    % Change the blue value for the first pixel
imwrite(im,'new.jpeg');  % Save modified image

一度に複数のピクセル (つまり、長方形の領域) を変更するにはさまざまな方法があります。これには、多次元配列にインデックスを付ける方法を調べる必要があります。さまざまなタイプの画像が MATLAB に読み込まれる方法 (つまり、 truecolorとindexed ) の詳細については、 imreadのドキュメントを確認してください

于 2009-02-22T19:23:55.170 に答える
13

一番上の質問に、matlabによって提供される非常に簡単な解決策があります:

% you so far

im = imread('image.tif');
imshow(im);
rectangle('Position', [100, 100, 10, 10]);

% now you use "getframe" and "frame2im"

f = getframe(gca);
im = frame2im(f);

imwrite(im,'image2.tif');

画像に長方形を描いて保存しようとすると、それは私にとって非常にうまくいきました。それを使い続けたい場合は、追加するだけです

imread('image2.tif');

そしてそれで働き続けます:)

よろしく、ローラ

于 2012-11-06T13:48:12.580 に答える
10

There's actually a bug at The MathWorks site about this issue. Too bad they don't spell out a real answer (as, IMHO, holding up a ruler to your monitor is not a real solution).

Using the print command, you must manually change the -r parameter until the size of the saved image matches the size of the input image. The -r parameter specifies the DPI of the saved image. Since most screens have different DPIs, there's no one-size-fits-all solution.

im = imread('image.tif');
f = figure, imshow(im, 'Border', 'tight');
rectangle('Position', [100, 100, 10, 10]);
print(f, '-r80', '-dtiff', 'image2.tif');

Use the code above, tweak the -r parameter until it looks right, and voilà!

于 2009-02-22T21:55:53.217 に答える
7

jacobkoの回答に 続きます。Figure のpaperpositionプロパティとpaperunitsプロパティ、および axis unitsプロパティとpositionプロパティを設定すると、通常、解像度を微調整しなくても目的の結果が得られます。そう、

>> im = imread('image.tif');
>> f = figure, imshow(im);
>> r=rectangle('Position',[100, 100,10,10]);
>> set(r,'edgecolor','b') % change the color of the rectangle to blue
>> set(f,'units','centimeters','position',[1 1 2.5 2.5]) % set the screen size and position
>> set(f,'paperunits','centimeters','paperposition',[1 1 2.5 2.5]) % set size and position for printing
>> set(gca,'units','normalized','position',[0 0 1 1]) % make sure axis fills entire figure
>> print(f, '-r80','-dtiff','image2.tif')

出力イメージ image2.tif は、軸の周りの境界線なしで、80dpi の解像度で 2.5cm x 2.5cm になります。

于 2009-02-23T17:33:13.947 に答える
3

im を保存する場合は、まずその値を変更する必要があります。私は長方形関数に慣れていませんが、次のことができます(力ずく):

im = imread('image.tif');
im(100:110,100)=0;
im(100:110,110)=0;
im(100,100:110)=0;
im(110,100:110)=0;
imshow(im);
imwrite(im, 'image2.tif');

上記のコードはグレースケール画像用であることに注意してください。画像が RGB 画像の場合は、次の操作を行う必要があります。

 im(100:110,100,:)=0;
 ....
于 2009-02-22T19:23:45.453 に答える
2

getframeFigure ウィンドウから変更されたイメージを取得するために使用できる場合があります。getframeによって返される構造体の cdata フィールドと colormap フィールドをimwrite、それぞれイメージとそのカラーマップとして渡すことができると思います。

于 2009-02-22T20:01:19.090 に答える
1
close all; clear; clc;

r = 240 ; c = 320;

fig = figure('Visible', 'off');
imshow( zeros(r,c) );
hold on;
plot([c-fix(c/2),c-fix(c/2)],[r-fix(r/2),r-fix(r/2)],'r*', 'MarkerSize', 10 );

% Sets position and size of figure on the screen
set(fig, 'Units', 'pixels', 'position', [100 100 c r] ); 

% Sets axes to fill the figure space
set(gca, 'Units', 'pixels', 'position', [0 0 c+1 r+1 ]);

% Sets print properties; Looks like 1 pixel = (3/4)th of a point
set(fig, 'paperunits', 'points', 'papersize', [fix((c-1)*(3/4))+1 fix((r-1)*(3/4))+1]);
set(fig, 'paperunits', 'normalized', 'paperposition', [0 0 1 1]);

print( fig, sprintf('-r%d', ceil(72*(4/3))), '-dpng', 'image.png' ); 


im = imread( 'image.png');
figure; imshow(im);
于 2009-05-29T22:40:06.470 に答える