Effacer les filtres
Effacer les filtres

png画像処理時の白飛びについて

11 vues (au cours des 30 derniers jours)
隆海
隆海 le 20 Oct 2023
現在png画像をマトラボで結合して一枚の画像にする作業を行なっています。
処理自体はエラーなく動いたものの処理後の画像が大きく白飛びしており、原型を留めていませんでした。
現在はマトラボのweb版で作業しており、画像は全てドライブにアップロードしたものを処理しています。
これはウェブ版で生じるトラブルなのか、処理上のトラブルなのか不明です。
大変お手数ですがお力添えをお願いいたします。
以下に使用したスクリプトと、白飛びした処理後の画像を添付します。
% フォルダのパスとファイル名の設定
folderPath = '/MATLAB Drive/Exp2321/2321trim/';
outputPath = '/MATLAB Drive/Exp2321/';
% 出力画像のサイズ設定 (A4サイズ)
pageWidth = 210; % A4幅 (mm)
pageHeight = 297; % A4高さ (mm)
% 画像の配置設定
numRows = 10; % 行数
numColumns = 2; % 列数
imageSpacingX = 15; % 横の画像間隔 (mm)
imageSpacingY = 8; % 縦の画像間隔 (mm)
leftMargin = 10; % 左の余白 (mm)
topMargin = 8; % 上の余白 (mm)
% フォントとフォントサイズの設定
fontSize = 12; % フォントサイズ
fontColor = [1, 1, 1]; % フォントの色 (白)
% ファイル番号の開始値
startNumber = 10;
fileExtension = '.png';
% 出力画像の初期設定
outputImage = ones(pageHeight, pageWidth, 3); % ホワイトバックグラウンド
% ファイルのループ
currentColumn = 1;
currentRow = 1;
for fileNumber = startNumber:5:885
% 画像読み込み
fileName = sprintf('NewImage_V_%d_cropped%s', fileNumber, fileExtension);
filePath = fullfile(folderPath, fileName);
img = imread(filePath);
% 画像を75%に縮小
img = imresize(img, 0.75);
% 画像配置
startX = leftMargin + (currentColumn - 1) * (imageSpacingX + size(img, 2));
startY = topMargin + (currentRow - 1) * (imageSpacingY + size(img, 1));
endX = startX + size(img, 2) - 1;
endY = startY + size(img, 1) - 1;
outputImage(startY:endY, startX:endX, :) = img;
% ファイル番号のテキスト
fileText = sprintf('Flame %d', fileNumber * 2);
textX = startX + size(img, 2) - 1;
textY = startY;
outputImage = insertText(outputImage, [textX, textY], fileText, 'FontSize', fontSize, 'BoxOpacity', 0, 'TextColor', fontColor);
% 列数と行数を更新
currentColumn = currentColumn + 1;
if currentColumn > numColumns
currentColumn = 1;
currentRow = currentRow + 1;
end
% 出力画像を保存
if currentRow > numRows
outputFileName = sprintf('OutputPage_%d.png', (fileNumber - 5) / 10); % 出力ファイル名
imwrite(outputImage, fullfile(outputPath, outputFileName));
% 新しい出力画像を初期化
outputImage = ones(pageHeight, pageWidth, 3);
% 行数と列数をリセット
currentRow = 1;
currentColumn = 1;
end
end
% 最終ページの保存
outputFileName = sprintf('OutputPage_%d.png', (fileNumber - 5) / 10);
imwrite(outputImage, fullfile(outputPath, outputFileName));

Réponses (1)

Atsushi Ueno
Atsushi Ueno le 20 Oct 2023
> 【解決策】どうすれば問題が解消するか ⇒ 下記のいずれか一方を実施する
im2double 関数を使ってuint8/16 型のイメージデータを double 型のイメージデータに変換する
img = im2double(img); % double 型のイメージデータに変換
もしくは、初めに初期化する outputImage の型を読み込むイメージデータの型に合わせる
outputImage = uint8(ones(size(img))*255); % ホワイトバックグラウンド
> 【原因】これはウェブ版で生じるトラブルなのか、処理上のトラブルなのか不明です。
これは処理上のトラブルで、ウェブ版か否かは無関係です。ones 関数で初期化した outputImage と imread 関数で読み込んだ画像データ img のデータ型が合致していないからです。double 型の outputImage に uint8 型の img を代入すると、1.0 ~ 255.0 の値を取る画素は全部真っ白( RGB なら真っ赤:真っ緑:真っ青の組み合わせ)になってしまいます。
【参考1】(一部)イメージデータ型の値と色の関係
  • double 型:(黒) 0.0 ~ 1.0 (白)
  • uint16 型:(黒) 0 ~ 65535 (白)
  • uint8 型:(黒) 0 ~ 255 (白)
img = imread('peppers.png');
class(img) % RGBのPNGファイルなら uint8 または uint16 型になるはず
ans = 'uint8'
outputImage = ones(size(img)); % ホワイトバックグラウンド
class(outputImage) % ones 関数で初期化した行列のデータ型はdouble
ans = 'double'
outputImage(:,:,:) = img; % インデックスを指定して代入する
class(outputImage) % データ型はdouble 型のまま変わらないが、値は0-255になったので真っ白になる
ans = 'double'

Catégories

En savoir plus sur イメージ dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!