How to make this be a loop?

1 view (last 30 days)
Jiajie Zhang
Jiajie Zhang on 29 Nov 2018
Commented: Yixn Chen on 2 Nov 2020
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 Comment
Yixn Chen
Yixn Chen on 2 Nov 2020
Hey did you ever fix this? If yes do you mind sharing how :D

Sign in to comment.

Answers (1)

Jan
Jan on 29 Nov 2018
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.

Categories

Find more on MATLAB Support Package for USB Webcams in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!