필터 지우기
필터 지우기

How to retrieve images with the same name from multiple folders?

조회 수: 1 (최근 30일)
Sivaramakrishnan Rajaraman
Sivaramakrishnan Rajaraman 2018년 7월 11일
댓글: Sivaramakrishnan Rajaraman 2018년 7월 13일
I have nine different folders, each containing hundreds of images. I suspect there are a few images with the same names that are present in two or more folders. How do I retrieve only those images that are present in these nine different folders?

답변 (1개)

Walter Roberson
Walter Roberson 2018년 7월 12일
Untested.
projectdir = '/Users/siv/PhD/part11';
dinfo = dir(projectdir);
dinfo(~[dinfo.isfolder]) = []; %remove non-folders
dinfo( ismember({dinfo.name}, {'.', '..'}) ) = []; %delete . and ..
subdirnames = {dinfo.name};
fqsubdirnames = fullfile(projectdir, subdirnames);
numsubs = length(subdirnames);
subinfo = cell(numsubs, 1);
for K = 1 : numsubs
thisinfo = dir(fqsubdirnames{K});
thisinfo( [thisinfo.isdir] ) = []; %remove folders. This includes . and ..
subinfo{K} = {thisinfo.name};
end
allnames = horzcat(subinfo{:});
numents = cellfun(@length, subinfo);
group_breakpoints = cumsum([1; numents(:)]) .' ;
[unames, ~, uidx] = unique(allnames);
counts = accumarray(uidx, 1);
dupidx = find(counts > 1);
if isempty(dupidx)
fprintf('No duplicate names!\n');
else
fprintf('Some duplicates found!\n\n');
for K = 1 : length(dupidx)
orig_idx = find(uidx == dupidx(K));
[~, folderidx] = histc(orig_idx, group_breakpoints);
fprintf('file "%s" found in the following folders:\n', unames{dupidx(K)} );
fprintf(' %s\n', subdirnames{folderidx} );
end
fprintf('\n');
end
  댓글 수: 5
Image Analyst
Image Analyst 2018년 7월 13일
Images can have the same name and not be duplicates so you might want to check the dates, CRC, etc. Just construct the output folder name, full file name, and then call imwrite():
Something like
outputFolder = '/Users/siv/PhD/part11/Duplicate Images' % Wherever you want.
baseFileName = sprintf('duplicate_%s', originalBaseFileName);
fullFileName = fullfile(outputFolder, baseFileName);
imwrite(theImage, fullFileName);
Obviously make needed variable name changes as appropriate.
Sivaramakrishnan Rajaraman
Sivaramakrishnan Rajaraman 2018년 7월 13일
I'm running your main code that prints the "unames{dupidx(K)}" and the sub directory names "subdirnames{folderidx}". In that context, if I'm to use to above code into the last for loop of your code, what should be the originalBaseFileName?

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

카테고리

Help CenterFile Exchange에서 Image Processing and Computer Vision에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by