How to compare the saved names of two images stored in different folders ?
2 次查看(过去 30 天)
显示 更早的评论
I have 12 input images in the inputImages folder with names 001_in.png, 002_in.png ..... 012_in.png. Also I have the ground truths of these images 001_GT.png, 002_GT.png ... 012_GT.png in another folder named GroundTruth. I want to read and display these 12 images and for each of these 12 images, I have to calculate the PSNR value with respect to the ground truth stored in the GroundTruth folder. How will I get the ground truth for a particular input image ? Here's my code. I have tried to retrieve the image names and compare them. But, I am getting name1 = * , name 2 = * , last= 1×0 empty char array.
location1='G:\MTech\InputImages\*.png';
ds1=imageDatastore(location1)
while(hasdata(ds1))
I=read(ds1);
figure,imshow(I),title('Input Image');
[filepath1,name1,ext1] = fileparts(location1)
underlineLocation1 = strfind(name1, '_')
first = name1(1:underlineLocation1-1) %Getting the image name upto underscore
location2='G:\MTech\GroundTruth\*.png';
ds2=imageDatastore(location2)
while(hasdata(ds2))
ref=read(ds2);
figure,imshow(ref),title('Ground Truth');
[filepath2,name2,ext2] = fileparts(location2)
underlineLocation2 = strfind(name2, '_')
last = name2(1:underlineLocation2-1)
if first==last
[peaksnr, snr] = psnr(I, ref);
fprintf('\n The Peak-SNR value of final output is %0.4f', peaksnr);
end
end
end
0 个评论
采纳的回答
Stephen23
2020-2-9
编辑:Stephen23
2020-2-9
I don't think imageDatastore particularly helps in this case. Something like this:
P1 = 'G:\MTech\InputImages';
P2 = 'G:\MTech\GroundTruth';
for k = 1:12
F1 = sprintf('%03u_in.png',k);
F2 = sprintf('%03u_GT.png',k);
im = imread(fullfile(P1,F1));
rf = imread(fullfile(P2,F2));
[peaksnr,snr] = psnr(im,rf);
fprintf('\n The Peak-SNR value of final output is %0.4f', peaksnr);
end
更多回答(1 个)
Thiago Henrique Gomes Lobato
2020-2-9
You have a couple of errors in your code. Firstly to compare strings you must use the strcmp function, so
if strcmp(first,last)
instead of
if first==last
Second you're using the "fileparts" function in your image folder, not in the image that was actually loaded. By read you should also get the info and, from the info, get your file name (check here ):
[ref,info]=read(ds2);
figure,imshow(ref),title('Ground Truth');
[filepath2,name2,ext2] = fileparts(info.Filename)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Convert Image Type 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!