Measuring average intensity of pixels for multiple images.

6 次查看(过去 30 天)
I am trying to read multiple similar images located in a folder and find the average intensity of pixels of the images. Following is the code I wrote initially which works fine:
Location = 'D:\\dummy location';
B = dir(fullfile(sprintf(Location),sprintf('BLACK'), '*.tif'));
B_0 = numel(B);
for k = 1:B_0
F = fullfile(sprintf(Location),sprintf('BLACK'),B(k).name);
I{k} = imread(F);
end
B1 = cat(3,I{:});
black = mean(B1,3);
However, upon getting suggested to use vectorization to make the process faster, I wrote the following code:
Location = 'D:\\dummy location';
B = dir(fullfile(sprintf(Location),sprintf('BLACK'), '*.tif'));
B_0 = numel(B);
k = 1:B_0;
F = fullfile(sprintf(Location),sprintf('BLACK'),B(k).name);
I{k} = imread(F);
B1 = cat(3,I{:});
black = mean(B1,3);
I am getting the following error:
Error using dir
Pathname 'D:\dummy location\BLACK\1.tif\10.tif\100.tif\101.tif\102.tif\103.tif\104.tif\105.tif\106.tif\107.tif\108.tif\109.tif\11.tif\110.tif\111.tif\112.tif\113.tif\114.tif\115.tif\116.tif\117.tif\118.tif\119.tif\12.tif\120.tif\121.tif\122.tif\123.tif\124.tif\125.tif\126.tif\127.tif\128.tif\129.tif\13.tif\130.tif\131.tif\132.tif\133.tif\134.tif\135.tif\136.tif\137.tif\138.tif\139.tif\14.tif\140.tif\141.tif\142.tif\143.tif\144.tif\145.tif\146.tif\147.tif\148.tif\149.tif\15.tif\150.tif\151.tif\152.tif\153.tif\154.tif\155.tif\156.tif\157.tif\158.tif\159.tif\16.tif\160.tif\161.tif\162.tif\163.tif\164.tif\165.tif\166.tif\167.tif\168.tif\169.tif\17.tif\170.tif\171.tif\172.tif\173.tif\174.tif\175.tif\176.tif\177.tif\178.tif\179.tif\18.tif\180.tif\181.tif\182.tif\183.tif\184.tif\185.tif\186.tif\187.tif\188.tif\189.tif\19.tif\190.tif\191.tif\192.tif\193.tif\194.tif\195.tif\196.tif\197.tif\198.tif\199.tif\2.tif\20.tif\200.tif\201.tif\202.tif\203.tif\204.tif\205.tif\206.tif\207.tif\208.tif\209.tif\21.tif\210.tif\211.tif\212.tif\213.tif\214.tif\215.tif\216.tif\217.tif\218.tif\219.tif\22.tif\220.tif\221.tif\222.tif\223.tif\224.tif\225.tif\226.tif\227.tif\228.tif\229.tif\23.tif\230.tif\231.tif\232.tif\233.tif\234.tif\235.tif\236.tif\237.tif\238.tif\239.tif\24.tif\240.tif\241.tif\242.tif\243.tif\244.tif\245.tif\246.tif\247.tif\248.tif\249.tif\25.tif\250.tif\251.tif\252.tif\253.tif\254.tif\255.tif\256.tif\257.tif\258.tif\259.tif\26.tif\260.tif\261.tif\262.tif\263.tif\264.tif\265.tif\266.tif\267.tif\268.tif\269.tif\27.tif\270.tif\271.tif\272.tif\273.tif\274.tif\275.tif\276.tif\277.tif\278.tif\279.tif\28.tif\280.tif\281.tif\282.tif\283.tif\284.tif\285.tif\286.tif\287.tif\288.tif\289.tif\29.tif\290.tif\291.tif\292.tif\293.tif\294.tif\295.tif\296.tif\297.tif\298.tif\299.tif\3.tif\30.tif\300.tif\301.tif\302.tif\303.tif\304.tif\305.tif\306.tif\307.tif\308.tif\309.tif\31.tif\310.tif\311.tif\312.tif\313.tif\314.tif\315.tif\316.tif\317.tif\318.tif\319.tif\32.tif\320.tif\321.tif\322.tif\323.tif\324.tif\325.tif\326.tif\327.tif\328.tif\329.tif\33.tif\330.tif\331.tif\332.tif\333.tif\334.tif\335.tif\336.tif\337.tif\338.tif\339.tif\34.tif\340.tif\341.tif\342.tif\343.tif\344.tif\345.tif\346.tif\347.tif\348.tif\349.tif\35.tif\350.tif\351.tif\352.tif\353.tif\354.tif\355.tif\356.tif\357.tif\358.tif\359.tif\36.tif\360.tif\361.tif\362.tif\363.tif\364.tif\365.tif\366.tif\367.tif\368.tif\369.tif\37.tif\370.tif\371.tif\372.tif\373.tif\374.tif\375.tif\376.tif\377.tif\378.tif\379.tif\38.tif\380.tif\381.tif\382.tif\383.tif\384.tif\385.tif\386.tif\387.tif\388.tif\389.tif\39.tif\390.tif\391.tif\392.tif\393.tif\394.tif\395.tif\396.tif\397.tif\398.tif\399.tif\4.tif\40.tif\400.tif\401.tif\402.tif\403.tif\404.tif\405.tif\406.tif\407.tif\408.tif\409.tif\41.tif\410.tif\411.tif\412.tif\413.tif\414.tif\415.tif\416.tif\417.tif\418.tif\419.tif\42.tif\420.tif\421.tif\422.tif\423.tif\424.tif\425.tif\426.tif\427.tif\428.tif\429.tif\43.tif\430.tif\431.tif\432.tif\433.tif\434.tif\435.tif\436.tif\437.tif\438.tif\439.tif\44.tif\440.tif\441.tif\442.tif\443.tif\444.tif\445.tif\446.tif\447.tif\448.tif\449.tif\45.tif\450.tif\451.tif\452.tif\453.tif\454.tif\455.tif\456.tif\457.tif\458.tif\459.tif\46.tif\460.tif\461.tif\462.tif\463.tif\464.tif\465.tif\466.tif\467.tif\468.tif\469.tif\47.tif\470.tif\471.tif\472.tif\473.tif\474.tif\475.tif\476.tif\477.tif\478.tif\479.tif\48.tif\480.tif\481.tif\482.tif\483.tif\484.tif\485.tif\486.tif\487.tif\488.tif\489.tif\49.tif\490.tif\491.tif\492.tif\493.tif\494.tif\495.tif\496.tif\497.tif\498.tif\499.tif\5.tif\50.tif\500.tif\51.tif\52.tif\53.tif\54.tif\55.tif\56.tif\57.tif\58.tif\59.tif\6.tif\60.tif\61.tif\62.tif\63.tif\64.tif\65.tif\66.tif\67.tif\68.tif\69.tif\7.tif\70.tif\71.tif\72.tif\73.tif\74.tif\75.tif\76.tif\77.tif\78.tif\79.tif\8.tif\80.tif\81.tif\82.tif\83.tif\84.tif\85.tif\86.tif\87.tif\88.tif\89.tif\9.tif\90.tif\91.tif\92.tif\93.tif\94.tif\95.tif\96.tif\97.tif\98.tif\99.tif'
is too long.
Error in imread>get_full_filename (line 560)
if ~isempty(dir(filename))
Error in imread (line 371)
fullname = get_full_filename(filename);
Error in avg_new (line 12)
I{k} = imread(F);
Can anyone please help me correcting this error to get the same result as my first code?
  2 个评论
DGM
DGM 2024-8-26
编辑:DGM 2024-8-26
Your first code is fine. There isn't a way to feed imread() a wildcard or an array of filenames, so vectorization doesn't make sense here.
I'm not sure why you're doing it that particular way. If you're assuming that all the images are single-channel, and they have the same page geometry, then you can do this a number of ways. Consider the three examples
In this example, all the images are loaded into a cell array and averaged
% i'm using my own test files (1521 files, 286x384 px)
Location = 'sources/faces';
B = dir(fullfile(Location,'*.pgm'));
B_0 = numel(B);
tic
I = cell(B_0,1); % preallocate
for k = 1:B_0
F = fullfile(Location,B(k).name);
I{k} = imread(F);
end
meanvalues = cast(mean(cat(3,I{:}),3),class(I{1})); % average and recast
toc
In this example, all the images are loaded as a volumetric image and then averaged all at once.
% i'm using my own test files (1521 files, 286x384 px)
Location = 'sources/faces';
B = dir(fullfile(Location,'*.pgm'));
B_0 = numel(B);
tic
for k = 1:B_0
F = fullfile(Location,B(k).name);
I = imread(F);
if k == 1
meanvalues = zeros([size(I,1:2),B_0],class(I));
end
meanvalues(:,:,k) = I; % store all images as pages in a volume
end
meanvalues = cast(mean(meanvalues,3),class(I)); % calculate pagewise mean of volume
toc
In this example, only one image is loaded at a time, incrementally accumulating a sum prior to division.
% i'm using my own test files (1521 files, 286x384 px)
Location = 'sources/faces';
B = dir(fullfile(Location,'*.pgm'));
B_0 = numel(B);
tic
for k = 1:B_0
F = fullfile(Location,B(k).name);
I = imread(F);
if k == 1
meanvalues = double(I); % allocate
else
meanvalues = meanvalues + double(I); % accumulate
end
end
meanvalues = cast(meanvalues/B_0,class(I)); % divide and recast
toc
The first examples might seem faster because all the arithmetic is done only once. For a small set of images, it might be faster, but as the total volume of data increases, the time spent allocating memory will slow it down. The potential use of cell arrays may or may not improve the time-cost of that memory allocation in certain cases.
In my own test, the first two examples are slightly slower than the incremental method in the second example, but that depends on the total size of the data, your amount of memory, etc. The cell array method at least succinct.

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2024-8-26
There is absolutely no need to store all your images in a cell array or as a 3-D image.
See the FAQ for code on how to process a sequence of images:
Also see this FAQ entry on cell arrays: What is a cell array

更多回答(0 个)

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by