Hi. I am trying to manipulate histogram data. Firstly I create a histogram and want to replace say the first 20 bins with a value of zero
%Histogram
maxval=max(IM(:));
[counts,x] = imhist(IM,maxval+1);
bar(x,counts,'b','EdgeColor','b');
%Replace first 20 bins with zero
p=20;
for i=1:p
counts(p)=0;
end
for some reason this is only deleting the bin with p=0 ??
Next I want to supress higher bins, that is remove both x and counts from the histogram data so I can output it to excel, so say i want to delete bins totally from 20,000 to 65536. Not sure how to do this.

 Akzeptierte Antwort

Guillaume
Guillaume am 28 Nov. 2014

1 Stimme

p is a constant in your code, equal to 20, so "for some reason this is only deleting the bin with p=0" shouldn't be what happens. What should happen is that only bin 20 is set to 0. If you'd use the loop counter i instead of p, then you'd set bins 1:20 to 0.
counts(i) = 0; %not p
However, there's really no need for a loop. I think you need to go back to the basics of matlab an learn about array and matrix manipulation. The loop is equivalent to:
counts(1:20) = 0;
Similarly to completely remove all the bins above 20000:
counts(20000:end) = [];

3 Kommentare

Jason
Jason am 28 Nov. 2014
Thankyou, yes I will readup.
Not quite. Look at his statement:
[counts,x] = imhist(IM,maxval+1);
He's not using 65536 bins. So counts may have only 4000 bins, or 37000 bins, or whatever, and gray level 20000 won't lie at element 20000 of counts. See attached demo for an illustration of that.
You can either examine x to find out what bin 20,000 occurs at (which won't be at 20000), or, easier though slightly more wasteful on memory (but you have plenty of that) is to just don't give a second argument to imhist() and it will automatically use 65536 bins for a 16 bit image. Then you can set counts(20000:end) = []; like Guillaume said.
Guillaume
Guillaume am 28 Nov. 2014
Bearbeitet: Guillaume am 28 Nov. 2014
Oh, yes, for some reason I assumed that Jason wanted to delete bins numbers 20000 and up, but you're right he probably meant bins of intensity 20000 and up. In that case,
counts(x>=20000) = [];
Or as you said, use 655336 bins.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Gefragt:

am 28 Nov. 2014

Bearbeitet:

am 28 Nov. 2014

Community Treasure Hunt

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

Start Hunting!

Translated by