輝度の数字の行列をグ​レースケール画像に変​換する方法

12 views (last 30 days)
suzuka iwaki
suzuka iwaki on 16 Jan 2024
Commented: Akira Agata on 21 Jan 2024
[-255,255]の範囲で輝度の行列にしたとき、その行列を画像に変換するコードを教えていただきたいです。また、画像に変換するときに、輝度の行列に負の値が含まれていても画像への変換が可能であるか教えていただきたいです。
  1 Comment
Akira Agata
Akira Agata on 16 Jan 2024
mat2gray 関数を使うと簡単にグレースケール画像に変換することができます。

Sign in to comment.

Answers (1)

Atsushi Ueno
Atsushi Ueno on 20 Jan 2024
Edited: Atsushi Ueno on 20 Jan 2024
@Akira Agataさんの仰る mat2gray 関数を使ってみました
% 輝度の数字の行列サンプル:[0,255](int8) イメージを2倍して255減算
oldG = int16(im2gray(imread('peppers.png'))) .* 2 - 255;
% @Akira Agataさんの仰る mat2gray 関数に突っ込んだ
newG = mat2gray(oldG,[-255,255]);
class(newG) % 出力は double 型に変換された
ans = 'double'
tiledlayout(3,1)
ax1 = nexttile; plot(oldG(:)); % [-255,255](int16型) はイメージデータではない
ax2 = nexttile; plot(newG(:)); % [0,1](double型) はイメージデータの一つ
ax3 = nexttile; montage({oldG,newG}); % 前者はimshowでは表示できない。後者はできる
  1 Comment
Akira Agata
Akira Agata on 21 Jan 2024
mat2gray 関数の第二入力引数は出力値の範囲です。このため [-255, 255] と入力してしまうとその範囲で出力してしまいます。
この引数のデフォルト設定は [0, 1] ですので、第二入力引数を指定せずに適用するとグレースケール画像になります。もし uint8 型の画像に変換したい場合には、mat2gray 適用後に im2uint8 で変換してください。
% 輝度の数字の行列サンプル:[0, 255](int8) イメージを2倍して255減算
oldG = int16(im2gray(imread('peppers.png'))).*2 - 255;
% mat2gray関数を適用 (デフォルト設定: [0, 1](double) イメージとして出力
newG = mat2gray(oldG);
% 結果を確認
figure
imshow(newG)

Sign in to comment.

Categories

Find more on イメージ in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!