Re: Value of each pixel inside a ROI

Posted by William O'Connell on
URL: http://imagej.273.s1.nabble.com/new-Bio-Formats-tp3701686p3701697.html

Hi Nas,
For questions like this, the searchable ImageJ archives are useful:
https://list.nih.gov/archives/imagej.html

Here's the answer wayne gave a few days ago for a related question:
<snip>
Get the ROI's mask and only process pixels that correspond to non-zero
pixels in the mask. Here is an example:

public void run(ImageProcessor ip) {
    byte[] pixels = (byte[])ip.getPixels();
    int width = ip.getWidth();
    Rectangle r = ip.getRoi();
    ImageProcessor mask = ip.getMask();
    for (int y=r.y; y<(r.y+r.height); y++) {
       for (int x=r.x; x<(r.x+r.width); x++) {
          if (mask==null || mask.getPixel(x-r.x,y-r.y)!=0) {
            ...DO What YOU WISH TO DO
          }    
       }
    }
}
<snap>


Notice that the example assumes the data is 8-bit. For 16-bit data use 'short', for 32-bit data use 'float', for RGB use 'int'.
Here's another way, slower,  but perhaps more transparent.
<snip>
    byte[] pixels = (byte[])ip.getPixels();
    int width = ip.getWidth();

      Roi roi = Imp.getRoi();
      Rectangle rect = roi.getBounds();
      rx = rect.x; ry = rect.y; w = rect.width; h = rect.height;
        for(int y=ry; y<ry+h; y++) {
           for(int x=rx; x<rx+w; x++) {
                if(roi.contains(x, y)) {
                    byte pixVal = pixels[y*width + x];
                 ...DO What YOU WISH TO DO WITH pixVal
                }            
           }
        }      
<snap>
 -------------- Original message ----------------------
From: Nas <[hidden email]>

> Dear all,
>
> I would like to get a list of the values for each
> pixel inside a ROI. how could I do that. I just know
> about the list that you can get through
> Analyze-->distribustion bye setting bins which is not
> what I'm looking for.
>
> Thanks for your help,
> Nas
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com