I create a byte array
redPixels = (byte[])red.getPixels(slice); I want to sum the values and I try this long totalRed = 0; for (int j=0; j<redPixels.length; j++) totalRed += (byte)redPixels[j]; However because the bytes are signed I get a wrong total. How to I make the bytes fit into the range 0 - 255 so that me total is correct. Is there documentation about how ImageJ gets around signed types as I think this would be helpful. |
Hi,
yes there is!!! You need to do a dummy "AND" operation, see the Imagej plugin tutorial at http://mtd.fh-hagenberg.at/depot/imaging/imagej/tutorial171.pdf Exerpt: ------------------------------------ Java’s byte data type is signed and has values ranging from -128 to 127, while we would expect a 8 bit grayscale image to have values from 0 to 255. If we cast a byte variable to another type we have to make sure, that the sign bit is eliminated. This can be done using a binary AND operation (&): int pix = pixels[i] & 0xff; ... pixels[i] = (byte) pix; It’s the same with Java’s short data type, which is also signed and has values ranging from -32, 768 to 32, 767, while we would expect a 16 bit grayscale image to have values from 0 to 65, 535. If we cast a short variable to another type we have to make sure that the sign bit is eliminated. This can again be done using a binary AND operation: int pix = pixels[i] & 0xffff; ... pixels[i] = (short) pix; ------------------------------------ Hope this helps! JW |---------+---------------------------> | | Nikos Katsikanis| | | <nkatsikanis@HOT| | | MAIL.COM> | | | Gesendet von: | | | ImageJ Interest | | | Group | | | <[hidden email]| | | .GOV> | | | | | | | | | 03.08.2006 12:38| | | Bitte antworten | | | an ImageJ | | | Interest Group | |---------+---------------------------> >-------------------------------------------------------------------------------------------------------------------------------| | | | An: [hidden email] | | Kopie: (Blindkopie: Joachim Wesner/DEWET/LMSCentral/Leica) | | Thema: signed byte array problem | >-------------------------------------------------------------------------------------------------------------------------------| I create a byte array redPixels = (byte[])red.getPixels(slice); I want to sum the values and I try this long totalRed = 0; for (int j=0; j<redPixels.length; j++) totalRed += (byte)redPixels[j]; However because the bytes are signed I get a wrong total. How to I make the bytes fit into the range 0 - 255 so that me total is correct. Is there documentation about how ImageJ gets around signed types as I think this would be helpful. ______________________________________________________________________ This email has been scanned by the MessageLabs Email Security System. For more information please visit http://www.messagelabs.com/email ______________________________________________________________________ |
Free forum by Nabble | Edit this page |