Hello imagej users,
I am trying to count and locate the peaks in a picture.for this purpose I use the following macro wich is an adaptation of the FindMaxPixelLocation macro. for peak identification I just compare the values of consecutives points
(curve[i+1]) && (curve[i+1]>curve[i+2])
curve[i] being the pixel value at location i of the loop. but as result I get 39 peaks instead of 4 (see the attached profile)
I would like to know what I am doing wrong and if anyone has another easier way to solve this problem
thanks in advance
regards
Paul Rostand
showStatus("Finging pixel with largest value...");
max = 0; peak=0; width = getWidth(); height = getHeight(); for (y=0; y<height; y++) { if (y%20==0) showProgress(y, height); for (x=0; x<50; x++) { value = getPixel(x,y); if (value>max) { max = value; xmax = x; ymax = y; } } } print("A pixel with the max value ("+max+") is located at "+xmax+","+ymax); curve=newArray(width); threshold = 0.9*max; // define the threshold used to count the peaks for (y=0; y<height; y++) {
if (y%20==0) showProgress(y, height); for (x=0; x<50; x++) { value = getPixel(x,y); if (value>threshold) { for (i=0;i<48;i++) { curve[i]=value; if((curve[i]<curve[i+1]) && (curve[i+1]>curve[i+2])){ peak=peak+1; } } } } } print("the number of peaks is "+peak+");
|
Dear Paul,
There is a Process>Find Maxima command in ImageJ that works quite well to detect peaks in images. Did you try to use it ? Christophe On Thu, Apr 28, 2011 at 11:19, paul_rostand <[hidden email]> wrote: > Hello imagej users, > > I am trying to count and locate the peaks in a picture.for this purpose I > use > the following macro wich is an adaptation of the FindMaxPixelLocation > macro. for > peak identification I just compare the values of consecutives points > (curve[i+1]) && (curve[i+1]>curve[i+2]) > > curve[i] being the pixel value at location i of the loop. but as result I > get 39 > peaks instead of 4 (see the attached profile) > > I would like to know what I am doing wrong and if anyone has another easier > way > to solve this problem > thanks in advance > regards > > Paul Rostand > > showStatus("Finging pixel with largest value..."); > max = 0; > peak=0; > width = getWidth(); > height = getHeight(); > for (y=0; y<height; y++) { > if (y%20==0) showProgress(y, height); > for (x=0; x<50; x++) { > value = getPixel(x,y); > if (value>max) { > max = value; > xmax = x; > ymax = y; > } > } > } > print("A pixel with the max value ("+max+") is located at "+xmax+","+ymax); > > curve=newArray(width); > threshold = 0.9*max; // define the threshold used to > count > the peaks > > for (y=0; y<height; y++) { > if (y%20==0) showProgress(y, height); > for (x=0; x<50; x++) { > value = getPixel(x,y); > if (value>threshold) { > for (i=0;i<48;i++) { > curve[i]=value; > if((curve[i]<curve[i+1]) && (curve[i+1]>curve[i+2])){ > peak=peak+1; > } > } > } > } > } > print("the number of peaks is "+peak+"); > > -- > View this message in context: > http://imagej.588099.n2.nabble.com/how-to-count-the-number-of-peaks-in-a-dynamic-profile-tp6312558p6312558.html > Sent from the ImageJ mailing list archive at Nabble.com. > |
In reply to this post by paul_rostand
Hello Paul,
maybe I'm wrong, but I think the problem is that your detection is too sensitive. I would take the values given by the plot profile and filter them with a 1D median or mean filter to get rid of the "noisy peaks". On the other hand you could try to enlarge your comparison of the left and the right neighbour to the 2 left and the 2 right neighbours. There you only get the real big maxima. Greeting Fred On 28.04.2011 11:19, paul_rostand wrote: > Hello imagej users, > > I am trying to count and locate the peaks in a picture.for this purpose I use > the following macro wich is an adaptation of the FindMaxPixelLocation macro. for > peak identification I just compare the values of consecutives points > (curve[i+1])&& (curve[i+1]>curve[i+2]) > > curve[i] being the pixel value at location i of the loop. but as result I get 39 > peaks instead of 4 (see the attached profile) > > I would like to know what I am doing wrong and if anyone has another easier way > to solve this problem > thanks in advance > regards > > Paul Rostand > > showStatus("Finging pixel with largest value..."); > max = 0; > peak=0; > width = getWidth(); > height = getHeight(); > for (y=0; y<height; y++) { > if (y%20==0) showProgress(y, height); > for (x=0; x<50; x++) { > value = getPixel(x,y); > if (value>max) { > max = value; > xmax = x; > ymax = y; > } > } > } > print("A pixel with the max value ("+max+") is located at "+xmax+","+ymax); > > curve=newArray(width); > threshold = 0.9*max; // define the threshold used to count > the peaks > > for (y=0; y<height; y++) { > if (y%20==0) showProgress(y, height); > for (x=0; x<50; x++) { > value = getPixel(x,y); > if (value>threshold) { > for (i=0;i<48;i++) { > curve[i]=value; > if((curve[i]<curve[i+1])&& (curve[i+1]>curve[i+2])){ > peak=peak+1; > } > } > } > } > } > print("the number of peaks is "+peak+"); > > -- > View this message in context: http://imagej.588099.n2.nabble.com/how-to-count-the-number-of-peaks-in-a-dynamic-profile-tp6312558p6312558.html > Sent from the ImageJ mailing list archive at Nabble.com. -- Frederik Sündermann, Dipl.-Biol. Department of Neurobiology University of Osnabrück Barbarastraße 11 49076 Osnabrück Tel: +49 541 969-2846 |
In reply to this post by lechristophe
thank you Fred and Christophe,
<<<fred ; increace the step of iteration just increase the amount of peaks
<<<Christophe : the problem with Process>Find Maxima ist that i have a file with more than 1000pictures with and without holes in the middle and all with different noise tolerance. and I want to use the fact that the hole is always in the middle of 2 maximas as criteria to sort my pictures before stacking.
regards
De : lechristophe [via ImageJ] <[hidden email]> À : paul_rostand <[hidden email]> Envoyé le : Jeu 28 avril 2011, 12h 12min 01s Objet : Re: how to count the number of peaks in a dynamic profile Dear Paul, There is a Process>Find Maxima command in ImageJ that works quite well to detect peaks in images. Did you try to use it ? Christophe On Thu, Apr 28, 2011 at 11:19, paul_rostand <[hidden email]> wrote: > Hello imagej users, > > I am trying to count and locate the peaks in a picture.for this purpose I > use > the following macro wich is an adaptation of the FindMaxPixelLocation > macro. for > peak identification I just compare the values of consecutives points > (curve[i+1]) && (curve[i+1]>curve[i+2]) > > curve[i] being the pixel value at location i of the loop. but as result I > get 39 > peaks instead of 4 (see the attached profile) > > I would like to know what I am doing wrong and if anyone has another easier > way > to solve this problem > thanks in advance > regards > > Paul Rostand > > showStatus("Finging pixel with largest value..."); > max = 0; > peak=0; > width = getWidth(); > height = getHeight(); > for (y=0; y<height; y++) { > if (y%20==0) showProgress(y, height); > for (x=0; x<50; x++) { > value = getPixel(x,y); > if (value>max) { > max = value; > xmax = x; > ymax = y; > } > } > } > print("A pixel with the max value ("+max+") is located at "+xmax+","+ymax); > > curve=newArray(width); > threshold = 0.9*max; // define the threshold used to > count > the peaks > > for (y=0; y<height; y++) { > if (y%20==0) showProgress(y, height); > for (x=0; x<50; x++) { > value = getPixel(x,y); > if (value>threshold) { > for (i=0;i<48;i++) { > curve[i]=value; > if((curve[i]<curve[i+1]) && (curve[i+1]>curve[i+2])){ > peak=peak+1; > } > } > } > } > } > print("the number of peaks is "+peak+"); > > -- > View this message in context: > http://imagej.588099.n2.nabble.com/how-to-count-the-number-of-peaks-in-a-dynamic-profile-tp6312558p6312558.html > Sent from the ImageJ mailing list archive at Nabble.com. > If you reply to this email, your message will be added to the discussion below: http://imagej.588099.n2.nabble.com/how-to-count-the-number-of-peaks-in-a-dynamic-profile-tp6312558p6312709.html |
In reply to this post by paul_rostand
Hi Paul,
for finding whether a point is a maximum it is not enough to check whether a profile along the x direction has a maximum. It must be also a maximum in the y direction (and in the diagonal directions, when considering 8-connected pixels). Also, you may have a maximum with several pixels of the same value, which would be lost in your approach. So, I'd recommend ImageStatistics for finding the overall maximum (this is much faster than the macro approach) and Process>Find Maxima for finding local maxima. "Find Maxima" also has an option to limit maximum finding to values above the lower threshold of a thresholded image - just threshold your image between 90% of the overall maximum and the full maximum. Michael ________________________________________________________________ On 28 Apr 2011, at 11:19, paul_rostand wrote: > Hello imagej users, > > I am trying to count and locate the peaks in a picture.for this > purpose I use > the following macro wich is an adaptation of the > FindMaxPixelLocation macro. for > peak identification I just compare the values of consecutives points > (curve[i+1]) && (curve[i+1]>curve[i+2]) > > curve[i] being the pixel value at location i of the loop. but as > result I get 39 > peaks instead of 4 (see the attached profile) > > I would like to know what I am doing wrong and if anyone has > another easier way > to solve this problem > thanks in advance > regards > > Paul Rostand > > showStatus("Finging pixel with largest value..."); > max = 0; > peak=0; > width = getWidth(); > height = getHeight(); > for (y=0; y<height; y++) { > if (y%20==0) showProgress(y, height); > for (x=0; x<50; x++) { > value = getPixel(x,y); > if (value>max) { > max = value; > xmax = x; > ymax = y; > } > } > } > print("A pixel with the max value ("+max+") is located at "+xmax > +","+ymax); > > curve=newArray(width); > threshold = 0.9*max; // define the threshold > used to count > the peaks > > for (y=0; y<height; y++) { > if (y%20==0) showProgress(y, height); > for (x=0; x<50; x++) { > value = getPixel(x,y); > if (value>threshold) { > for (i=0;i<48;i++) { > curve[i]=value; > if((curve[i]<curve[i+1]) && (curve[i+1]>curve[i+2])){ > peak=peak+1; > } > } > } > } > } > print("the number of peaks is "+peak+"); > > -- > View this message in context: http://imagej.588099.n2.nabble.com/ > how-to-count-the-number-of-peaks-in-a-dynamic-profile- > tp6312558p6312558.html > Sent from the ImageJ mailing list archive at Nabble.com. |
On Thursday 28 Apr 2011, Michael Schmid wrote:
> for finding whether a point is a maximum it is not enough to check > whether a profile along the x direction has a maximum. It must be > also a maximum in the y direction (and in the diagonal directions, > when considering 8-connected pixels). > Also, you may have a maximum with several pixels of the same value, > which would be lost in your approach. In addition to Michael's comments, let me say that this sort of problem has been resolved using mathematical morphology (in particular greyscale reconstruction) long time ago and there are formal ways of doing it. What I guess you are looking for is called "regional maxima" and a related procedure "h-domes". Reading about this will save you lots of time in getting the algorithms right. The papers by Luc Vincent (many of those are online) will cover this in detail and with examples. Pierre Soille's book (Morphological image analysis) is also very good on this. Several of these procedures have been implemented in the Morphology plugin collection available in my page. Cheers Gabriel |
Free forum by Nabble | Edit this page |