ROI画素値

6 views (last 30 days)
ナッティング
ナッティング on 13 Sep 2023
Commented: Atsushi Ueno on 3 Feb 2024
%ROI内の画素値を取得
img_gray = im2gray(img)
roi_pixels = double(img(roi_x:roi_x+roi_height-1,roi_y:roi_y+roi_width-1));
roi_pixels(img) = img_gray(img(roi_x:roi_x+roi_height-1,roi_y:roi_y+roi_width-1));
%平均と標準偏差を計算
mean_value = mean(roi_pixels(:));
std_deviation = std(roi_pixels(:));
%結果をデータに格納
output_data(i,1) = i; %ROI番号
output_data(i,2) = mean_value %平均
output_data(i,3) = std_deviation %標準偏差
end
%エクセルファイルにデータを書き込む
writematrix(output_data,'解析データ2.xlsx','Sheet',2,'range','A2:C21','writemode','overwrite');
writematrix(roi_pixels(img),'解析データ2.xlsx','Sheet',1,'A1:T80');
初心者のものです。
ROIを20個組んでまして、その各ROIの画素値をエクセルファイルに記入したいと考えています。
これで実行すると「配列インデックスは正の整数または logical 値でなければなりません。」とエラーが出てしまいます。
画素値をそのままエクセルファイルに記入する方法はありませんでしょうか。

Answers (1)

COVAO
COVAO on 31 Jan 2024
このエラーは、roi_pixels(img)で配列のインデックスに画像を入力しているのが原因ではないかと推測されます。
以下は画像を読み込み、ROIの画素値をExcelに書き込む例です。
(コードの作成に生成AIを利用しています。)
% Load the image
I = imread('cameraman.tif');
% Display the original image
imshow(I);
% Define the ROI (e.g., x=80, y=45, width=80, height=100)
roi_x = 80;
roi_y = 45;
roi_width = 80;
roi_height = 100;
% Extract pixel data within the ROI
roi_pixels = I(roi_y:(roi_y + roi_height - 1), roi_x:(roi_x + roi_width - 1));
% Display the ROI
imshow(roi_pixels);
% Write the pixel data of the ROI to an Excel file
writematrix(roi_pixels,'roi_pixels.xlsx', 'Sheet',1);
  1 Comment
Atsushi Ueno
Atsushi Ueno on 3 Feb 2024
下記のようにしたかったのではないでしょうか。
img = imread('peppers.png'); % 動作させる為のサンプルデータ
roi_x = 100; roi_y = 100; roi_height = 4; roi_width = 5; % 動作させる為のサンプルデータ
roi_pixels = im2gray(img(roi_x:roi_x+roi_height-1, roi_y:roi_y+roi_width-1, :))
roi_pixels = 4×5
47 46 47 47 44 47 45 46 46 46 50 48 47 46 46 48 46 47 48 48

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!