How to sort images into rows?

4 views (last 30 days)
Mitchell
Mitchell on 26 Jun 2013
I want to make a loop that displays images into the groups they are clustered in. Each row should include just the pictures in the group. This is the clusters they are in. For example: Picture 1 is in group 5. clust =
5
5
5
4
1
5
5
5
5
5
5
5
5
5
3
5
5
5
5
2
Also below is how I read in the images: for i=1:20 %Reading in the images count=count+1; picnum=num2str(count); im= imread(strcat('/Users/mitchellelmer/Documents/Pictures/20pics/', picnum, '.bmp'));

Answers (1)

Image Analyst
Image Analyst on 26 Jun 2013
Use hist() on that vector to find out how many 1's you have, how many 2's you have, ..., how many 5's you have.
[counts, values] = hist(vector);
Then use calculate the number of rows and columns:
rows = length(values); % 5 for this example
columns = max(counts); % 16 for this example, which happens for group 5.
Then you need to call subplot with that number of rows and columns:
subplot(rows, columns, location);
You need to start location at 1 for each cluster and every time you read in an image that belongs to a cluster, increment location. So for cluster (group) #5, location will start at rows*5 and go to rows*5+columns.
So all images from group 1 go into the first row, from group 2 into the second row, etc.
  2 Comments
Mitchell
Mitchell on 26 Jun 2013
[N,X] = hist(...) also returns the position of the bin centers in X.
I think that would not return the correct numbers.
Image Analyst
Image Analyst on 26 Jun 2013
Perhaps I needed to go into more detail for you to figure it out. Try this:
categories = [5
5
5
4
1
5
5
5
5
5
5
5
5
5
3
5
5
5
5
2]
numberOfBins = max(categories);
counts = histc(categories, 1:numberOfBins)
rows = length(counts)
columns = max(counts)
location = ones(rows, 1); % Next location in each row.
for k = 1 : length(categories)
% Read in image
grayImage = imread('cameraman.tif'); % Use the same image for demo
% Figure out what category it belongs to.
thisCategory = categories(k)
% Determine what row and column it should be displayed in.
columnForThisImage = location(thisCategory)
rowForThisImage = categories(k)
% Get this into a location number that subplot likes.
thisLocation = columns * (rowForThisImage - 1) + columnForThisImage
% Specify the destination location.
subplot(rows, columns, thisLocation);
% Display the image.
imshow(grayImage);
drawnow;
% Increment what column the next image in this category should be placed into.
location(thisCategory) = location(thisCategory) + 1;
end
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!