Login  Register

Re: copy number of pixels with a predefined color in clipboard

Posted by Wayne Rasband on Jan 27, 2006; 6:08pm
URL: http://imagej.273.s1.nabble.com/copy-number-of-pixels-with-a-predefined-color-in-clipboard-tp3703923p3703924.html

> I am using Mac OS9 and I want to know how to get the
> number of pixels in an RGB image with a predefined color
> [like (255,255,255)] and copy this number in the clipboard?

You can count the number of pixels with a predefined color using a
macro like the one below but there is currently no easy way to
programmatically copy text to the clipboard.

-wayne

   red = 255;
   green = 255;
   blue = 255;
   if (bitDepth!=24)
       exit("RGB Image required");
   count = getCount(red, green, blue);
   print("Count for "+red+","+green+","+blue+" = "+ count);
   print("Area fraction = "+count*100/(getWidth*getHeight) + "%");
   exit;

   function getCount(red, green, blue) {
       color = red<<16 + green<<8 + blue;
       w= getHeight;
       h = getHeight;
       count = 0;
       for (y=0; y<getHeight; y++) {
           for (x=0; x<getWidth; x++) {
               if (getPixel(x, y)&0xffffff==color)
                   count++;
           }
           if (y%10==0) showProgress(y, h);
       }
       return count;
   }