url = 'https://www.cs.toronto.edu/~kriz/cifar-10-matlab.tar.gz';
downloadFolder = fullfile(tempdir, 'cifar10');
filename = fullfile(downloadFolder, 'cifar-10-matlab.tar.gz');
untar(url, downloadFolder);
load(fullfile(downloadFolder, 'cifar-10-batches-mat', 'test_batch.mat'));
selectedIndices = randperm(size(data, 1), numImages);
brisqueScores = zeros(numImages, 1);
piqeScores = zeros(numImages, 1);
img = reshape(data(selectedIndices(i), :), 32, 32, 3);
brisqueScores(i) = brisque(img);
piqeScores(i) = piqe(img);
minBrisque = min(brisqueScores);
maxBrisque = max(brisqueScores);
medianBrisque = median(brisqueScores);
minPiqe = min(piqeScores);
maxPiqe = max(piqeScores);
medianPiqe = median(piqeScores);
mkdir(outputFolder, 'BelowMinBrisque');
mkdir(outputFolder, 'AboveMaxBrisque');
mkdir(outputFolder, 'BetweenMinMaxBrisque');
mkdir(outputFolder, 'BelowMinPiqe');
mkdir(outputFolder, 'AboveMaxPiqe');
mkdir(outputFolder, 'BetweenMinMaxPiqe');
img = reshape(data(selectedIndices(i), :), 32, 32, 3);
imgName = sprintf('image%d.jpg', i);
if brisqueScores(i) < minBrisque
imwrite(img, fullfile(outputFolder, 'BelowMinBrisque', imgName));
elseif brisqueScores(i) > maxBrisque
imwrite(img, fullfile(outputFolder, 'AboveMaxBrisque', imgName));
imwrite(img, fullfile(outputFolder, 'BetweenMinMaxBrisque', imgName));
if piqeScores(i) < minPiqe
imwrite(img, fullfile(outputFolder, 'BelowMinPiqe', imgName));
elseif piqeScores(i) > maxPiqe
imwrite(img, fullfile(outputFolder, 'AboveMaxPiqe', imgName));
imwrite(img, fullfile(outputFolder, 'BetweenMinMaxPiqe', imgName));