Categorise and sort an histogram
Mostrar comentarios más antiguos
Hi All
I have an image (attached) that I would like to analyse - namely I would like to know the length in pixels of each of the labelled componnent in my image. I want to be able to separate isolated objects from the connected ones.
so far I have this piece of code that allows me to identify in my image the different objects and to label them.
clc
clear all
close all
%%Connectivity calculation pixels to pixels
% read image and display
A = imread('DFN_TEST_2.pgm');
figure(1)
imagesc (A)
colorbar
%%Initialise the colour of the regions
B = 255-A;
figure, imshow(B); title ('complemented image in grayscale')
%%Compute the cluster identification function
C = bwlabel(B);
% display the labelled image
figure, imshow(C), title('labelled image bw')
vislabels(C), title('each object labelled')
%%Extract objects by area
D = regionprops(C, 'Area');
E = [D.Area];
F = sort(E, 'descend');% Sort in descending order
%%Create histogram
G = histogram(C,[1:8]);
xlabel('Label of connected objects')
ylabel('Pixel length')
title('Length of connected objects')
I was able to produce this histogram:

Now I would like to: 1/ make each bar of the histogram corresponding to 1 object (cf the first (higest) bar is object 1, the second is object 2...)
2/ I want to reorder the bars from the longest pixel length to the shortest ( object 1 will be first, then object 8, object 5...) - I went to this page (https://nl.mathworks.com/help/matlab/creating_plots/control-categorical-histogram-display.html) but it seems that the DisplayOrder stuff is not working /I do not know how to make it work
NB I propose here an example with 8 object but usually I am working with thousands of objects...
Can you give me a hand concerning this issue ?
Thank you in advance
Respuestas (2)
Steven Lord
el 19 de Sept. de 2018
0 votos
The options listed on the documentation page to which you linked that allow you to control how a categorical histogram orders the bins won't work for your application for two reasons.
- Those name/value pairs were introduced in release R2017a while the Release for this Answer indicates you're using release R2016b.
- The data you're passing into histogram as the first input is not a categorical array.
In order to use 'DisplayOrder' with histogram you would need to upgrade to release R2017a or later and make your data categorical.
1 comentario
Giuseppe Degan Di Dieco
el 14 de Jun. de 2021
Dear Steven Lord,
thanks again for your help, your 'DisplayOrder' tip helped me in sorting histogram bins in ascending order.
Best.
Pierre-Olivier BRUNA
el 20 de Sept. de 2018
0 votos
Categorías
Más información sobre Data Distribution Plots en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!