作成した画像が元画像​とサイズが異なる問題​の解決方法

27 次查看(过去 30 天)
yoshiki
yoshiki 2024-6-24,9:05
评论: yoshiki 2024-6-25,7:47
以下のコードを使ってしきい値を設定した画像(しきい値画像)の作成を行っているのですが,元画像のサイズが4000*6000なのに対し,作成した画像をfigureから保存すると667*1000になってしまいます.元画像と同サイズでしきい値画像を作成したいのですが,解決方法があればご教授して頂けると幸いです.
RGB = imread('image.jpg');
[BW,maskedRGBImage] = createMask(RGB);
imshow(maskedRGBImage,'Border','tight')
function [BW,maskedRGBImage] = createMask(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
%
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the colorThresholder app. The colorspace and
% range for each channel of the colorspace were set within the app. The
% segmentation mask is returned in BW, and a composite of the mask and
% original RGB images is returned in maskedRGBImage.
% Auto-generated by colorThresholder app on 24-May-2024
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2lab(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.000;
channel1Max = 99.781;
% Define thresholds for channel 2 based on histogram settings
channel2Min = -26.783;
channel2Max = 32.161;
%channel2Max = -1.000;
% Define thresholds for channel 3 based on histogram settings
%channel3Min = -16.864;
channel3Min = 0.000;
channel3Max = 56.688;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end

采纳的回答

Atsushi Ueno
Atsushi Ueno 2024-6-24,14:21
imwrite関数を使えば問題を回避できます。
RGB = imread('football.jpg');
maskedRGBImage = RGB; % createMask()の入出力サイズは同一なので省略
imwrite(maskedRGBImage, 'maskedRGBImage.jpg'); % しきい値画像をファイルに書き出す
clear maskedRGBImage
ImageFromFile = imread('maskedRGBImage.jpg'); % 同じファイルから読み込む
size(RGB) == size(ImageFromFile) % 元画像とファイルから読み込んだしきい値画像は同サイズか?
ans = 1x3 logical array
1 1 1
  1 个评论
yoshiki
yoshiki 2024-6-25,7:47
ありがとうございます.参考にさせていただきます.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Image Processing and Computer Vision 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!