Group images based on high or low contrast
4 views (last 30 days)
Show older comments
Alex McConville
on 9 Nov 2021
Edited: Image Analyst
on 10 Nov 2021
Hello. I have a collection of images that i need to group into two categories, high or low constrast. I am current using k-means clustering to write the image name, image index, edge, entropy, and contrast. I would like to be able to write the contrast of the images sorted into high or low constrast to the file. Here is my current code:
files = dir('*.jpg'); % specigy the extension of your image file
C=zeros(20,2);
for i = 1:numel(files)
filename = files(i).name;
image = imread(filename);
test_Img=rgb2gray(image);
C(i,1)=sum(sum(edge(test_Img,'canny')));
C(i,2)=entropy(test_Img);
C(i,3)=max(test_Img(:)) - min(test_Img(:));
end
[idx, centers] = kmeans(C,5);
filename = 'Contrast.xlsx';
for j=1:length(idx)
name=files(j).name;
writematrix(name,filename,'Sheet',1,'Range',strcat('A',num2str(j)));
writematrix(idx(j),filename,'Sheet',1,'Range',strcat('B',num2str(j)));
writematrix(C(j,1),filename,'Sheet',1,'Range',strcat('C',num2str(j)));
writematrix(C(j,2),filename,'Sheet',1,'Range',strcat('D',num2str(j)));
writematrix(C(j,3),filename,'Sheet',1,'Range',strcat('E',num2str(j)));
end
Thank you!
0 Comments
Accepted Answer
Image Analyst
on 10 Nov 2021
Edited: Image Analyst
on 10 Nov 2021
Alex:
You are doing this:
C=zeros(20,2);
for i = 1:numel(files)
while you should be doing this:
numFiles = numel(files);
C = zeros(numFiles, 3);
for i = 1 : numFiles
You should preallocate because you are using three different metrics to characterize contrast and put it in to your contrast matrix C.
Below is improved code that will compute your 3 different contrast metrics and classify them into 5 classes and write the filename and 2 data values out to worksheets in the workbook. Each class has its own worksheet.
% Demo by Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
markerSize = 40;
% Get a list of all JPG image files.
files = dir('*.jpg'); % Specify the extension of your image file
numFiles = numel(files)
% Preallocate arrays.
C = zeros(numFiles, 3);
ca = cell(numFiles, 4);
% Loop over files computing 3 different contrast metrics.
for k = 1 : numFiles
fullFileName = fullfile(files(k).folder, files(k).name);
fprintf('Processing file %d of %d : "%s".\n', k, numFiles, fullFileName);
test_Img = imread(fullFileName);
if ndims(test_Img) == 3
test_Img=rgb2gray(test_Img);
end
% Compute 3 different contrast metrics.
C(k, 1) = sum(sum(edge(test_Img, 'canny')));
C(k, 2) = entropy(test_Img);
C(k, 3) = max(test_Img(:)) - min(test_Img(:));
% Put into cell array for worksheeet.
ca{k, 1} = files(k).name;
ca{k, 2} = C(k, 1);
ca{k, 3} = C(k, 2);
ca{k, 4} = C(k, 3);
end
ca
% Force it into 5 clusters.
numClusters = 5;
[assignedClassIndex, centers] = kmeans(C, numClusters);
% Write out each class to a different worksheet
fullExcelFileName = fullfile(pwd, 'Contrast.xlsx')
for k = 1 : numClusters
% Get rows of all data that has been assigned this class number by kmeans.
thisClass = assignedClassIndex == k;
% Extract only those rows from the cell array.
thisCellArray = ca(thisClass, :);
% Get the sheetname for this class;
sheetName = sprintf('Class %d', k);
% Write this class to its own sheet in the workbook.
fprintf('Writing sheet "%s" to workbook "%s".', sheetName, fullExcelFileName);
writecell(thisCellArray, fullExcelFileName, 'Sheet', sheetName, 'Range', 'A1');
end
fprintf('Done processing %d files!\n', numFiles);
% Open the workbook in Excel if on a PC.
if ispc
winopen(fullExcelFileName);
end
0 Comments
More Answers (1)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!