How to make this be a loop?

조회 수: 1 (최근 30일)
Jiajie Zhang
Jiajie Zhang 2018년 11월 29일
댓글: Yixn Chen 2020년 11월 2일
Hi, I want to make the function deal with a lot of images,
but I got errors when trying to add the loop.
The original one is:
imx = 'Images\20\IMG_4596.JPG',
Noisex = NoiseExtractFromImage(imx,2);
Noisex = WienerInDFT(Noisex,std2(Noisex));
% The optimal detector (see publication "Large Scale Test of Sensor Fingerprint Camera Identification")
Ix = double(rgb2gray(imread(imx)));
C = crosscorr(Noisex,Ix.*Fingerprint);
detection = PCE(C)
And I add a loop like this
file_path = 'Images\20\';
img_path_list = dir(strcat(file_path,'*.jpg'));
img_num = length(img_path_list);
if img_num > 0
for j = 1:img_num
image_name = img_path_list(j).name;
I = imread(strcat(file_path,image_name));
fprintf('%d %d %s\n',j,img_num,strcat(file_path,image_name));
imx(j).name = strcat(file_path,image_name);
Noisex{j} = NoiseExtractFromImage(imx(j).name,2);
Noisex(j) = WienerInDFT(Noisex(j),std2(Noisex(j)));
% The optimal detector (see publication "Large Scale Test of Sensor Fingerprint Camera Identification")
Ix(j) = double(rgb2gray(imread(imx(j).name)));
C(j) = crosscorr(Noisex(j),Ix(j).*Fingerprint);
detection(j) = PCE(C(j))
end
end
But I get an error :
Unable to perform assignment because brace indexing is not supported for variables of this type.
Error in Example (line 39)
Noisex{j} = NoiseExtractFromImage(imx(j).name,2);
Does anyone know how to make it right?
I'm new to matlab,
Thank you so much!
  댓글 수: 1
Yixn Chen
Yixn Chen 2020년 11월 2일
Hey did you ever fix this? If yes do you mind sharing how :D

댓글을 달려면 로그인하십시오.

답변 (1개)

Jan
Jan 2018년 11월 29일
Start with simplifying your code:
file_path = 'Images\20\';
img_path_list = dir(fullfile(file_path,'*.jpg')); % Safer than STRCAT
img_num = length(img_path_list);
for j = 1:img_num
image_name = img_path_list(j).name;
image_file = fullfile(file_path,image_name);
I = imread(image_file);
fprintf('%d %d %s\n', j, img_num, image_file);
Noisex{j} = NoiseExtractFromImage(image_file, 2);
Noisex(j) = WienerInDFT(Noisex(j), std2(Noisex(j)));
% The optimal detector (see publication "Large Scale Test of Sensor Fingerprint Camera Identification")
Ix(j) = double(rgb2gray(I));
C(j) = crosscorr(Noisex(j),Ix(j) .* Fingerprint);
detection(j) = PCE(C(j))
end
Now this looks strange:
Noisex{j} = NoiseExtractFromImage(image_file, 2);
Noisex(j) = WienerInDFT(Noisex(j), std2(Noisex(j)));
You use curly and round parentheses for the same variable. NoiseExtractFromImage and WienerInDFT do not belong to Matlab's toolbox functions, so I cannot guess, what the outputs are. Please check this.
double(rgb2gray(I)) replies a matrix. Then assignign the output to the scalar Ix(j) should fail. Do you want a cell array? Then use curly braces: Ix{j} .
Prefer to pre-allocate the results before the loop. This is more efficient and the types of the variables are well defined.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by