Create an Tiff image reading pixel values of 16 bit signed image

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Create an Tiff image reading pixel values of 16 bit signed image

Muqeet Khan
I have a 16-bit signed image, i want to create a tiff image by reading the pixel values of the 16-bit signed image. For some reason i don't get a proper tiff image when doing it. Here are the steps.

ImagePlus createImage(ImagePlus imagePlus, ImageProcessor targetImage) {
   
    int pixelCount = imagePlus.getWidth() * imagePlus.getHeight();
    Short pixels[] = new Short[pixelCount]
    ImageProcessor iP = imagePlus.getProcessor();
    int count = 0;    

    for (int x = 0;x < imagePlus.getWidth(); x++) {
       
        for (int y = 0; y < imagePlus.getHeight; y++) {
            double pixelValue = iP.getPixelValue(x, y);
            pixels[count] = pixelValue;
            count ++;
        }
    }
   
    targetImage.setPixels(pixels);
    targetImage .resetMinAndMax();
    return new ImagePlus("Image-RXZ", targetImage);
}

Is this correct way of doing???
Reply | Threaded
Open this post in threaded view
|

Re: Create an Tiff image reading pixel values of 16 bit signed image

Muqeet Khan
Is it due to the reason that you use short and values greater than 32,767 turns out to be negative. How do i retain the original pixel value. Well i could use putPixelValue.... but my actual intention later is to take mean of couple of image pixels and generate an image. So i have to use setPixels(...) which expects me to use only short data type. Any idea how to retain the original pixel value in this scenario???
Muqeet Khan wrote
I have a 16-bit signed image, i want to create a tiff image by reading the pixel values of the 16-bit signed image. For some reason i don't get a proper tiff image when doing it. Here are the steps.

ImagePlus createImage(ImagePlus imagePlus, ImageProcessor targetImage) {
   
    int pixelCount = imagePlus.getWidth() * imagePlus.getHeight();
    Short pixels[] = new Short[pixelCount]
    ImageProcessor iP = imagePlus.getProcessor();
    int count = 0;    

    for (int x = 0;x < imagePlus.getWidth(); x++) {
       
        for (int y = 0; y < imagePlus.getHeight; y++) {
            double pixelValue = iP.getPixelValue(x, y);
            pixels[count] = pixelValue;
            count ++;
        }
    }
   
    targetImage.setPixels(pixels);
    targetImage .resetMinAndMax();
    return new ImagePlus("Image-RXZ", targetImage);
}

Is this correct way of doing???
Reply | Threaded
Open this post in threaded view
|

Re: Create an Tiff image reading pixel values of 16 bit signed image

Muqeet Khan
I know use FloatProcessor, i get the image but it does not look ok. Any idea what is going wrong here?
Muqeet Khan wrote
Is it due to the reason that you use short and values greater than 32,767 turns out to be negative. How do i retain the original pixel value. Well i could use putPixelValue.... but my actual intention later is to take mean of couple of image pixels and generate an image. So i have to use setPixels(...) which expects me to use only short data type. Any idea how to retain the original pixel value in this scenario???
Muqeet Khan wrote
I have a 16-bit signed image, i want to create a tiff image by reading the pixel values of the 16-bit signed image. For some reason i don't get a proper tiff image when doing it. Here are the steps.

ImagePlus createImage(ImagePlus imagePlus, ImageProcessor targetImage) {
   
    int pixelCount = imagePlus.getWidth() * imagePlus.getHeight();
    Short pixels[] = new Short[pixelCount]
    ImageProcessor iP = imagePlus.getProcessor();
    int count = 0;    

    for (int x = 0;x < imagePlus.getWidth(); x++) {
       
        for (int y = 0; y < imagePlus.getHeight; y++) {
            double pixelValue = iP.getPixelValue(x, y);
            pixels[count] = pixelValue;
            count ++;
        }
    }
   
    targetImage.setPixels(pixels);
    targetImage .resetMinAndMax();
    return new ImagePlus("Image-RXZ", targetImage);
}

Is this correct way of doing???
Reply | Threaded
Open this post in threaded view
|

Re: Create an Tiff image reading pixel values of 16 bit signed image

Michael Schmid
Hi Muqeet,

if you input data are signed, and you want to convert them to float,
simply use
  short[] inPixels = (short[])inImageProcessor.getPixels();
  float[] outPixels = new float[inPixels.length];
  for (int i=0; i<inPixels.length; i++)
    outPixels[i] = inPixels[i]; //conversion keeps sign

for unsigned data, you would need to disable the sign explicitly:
    outPixels[i] = (float)(inPixels[i]&0xffff);

If you want to keep a shortProcessor, simply do an exclusive or with
0x8000 on all pixels, i.e., flip the highest bit. You can then set the
value calibration of the ImagePlus if you care about the numeric values:

  Calibration cal = imp.getCalibration();
  double[] coeff = new double[2];
  coeff[0] = -32768.0;
  coeff[1] = 1.0;
  cal.setFunction(Calibration.STRAIGHT_LINE, coeff, "gray value");


Michael
________________________________________________________________________


On Sat, November 21, 2009 14:40, Muqeet Khan wrote:

> I know use FloatProcessor, i get the image but it does not look ok. Any
> idea
> what is going wrong here?
>
> Muqeet Khan wrote:
>>
>> Is it due to the reason that you use short and values greater than
>> 32,767
>> turns out to be negative. How do i retain the original pixel value. Well
>> i
>> could use putPixelValue.... but my actual intention later is to take
>> mean
>> of couple of image pixels and generate an image. So i have to use
>> setPixels(...) which expects me to use only short data type. Any idea
>> how
>> to retain the original pixel value in this scenario???
>>
>> Muqeet Khan wrote:
>>>
>>> I have a 16-bit signed image, i want to create a tiff image by reading
>>> the pixel values of the 16-bit signed image. For some reason i don't
>>> get
>>> a proper tiff image when doing it. Here are the steps.
>>>
>>> ImagePlus createImage(ImagePlus imagePlus, ImageProcessor targetImage)
>>> {
>>>
>>>     int pixelCount = imagePlus.getWidth() * imagePlus.getHeight();
>>>     Short pixels[] = new Short[pixelCount]
>>>     ImageProcessor iP = imagePlus.getProcessor();
>>>     int count = 0;
>>>
>>>     for (int x = 0;x < imagePlus.getWidth(); x++) {
>>>
>>>         for (int y = 0; y < imagePlus.getHeight; y++) {
>>>             double pixelValue = iP.getPixelValue(x, y);
>>>             pixels[count] = pixelValue;
>>>             count ++;
>>>         }
>>>     }
>>>
>>>     targetImage.setPixels(pixels);
>>>     targetImage .resetMinAndMax();
>>>     return new ImagePlus("Image-RXZ", targetImage);
>>> }
>>>
>>> Is this correct way of doing???
>>>
>>
>>
>
> --
> View this message in context:
> http://n2.nabble.com/Create-an-Tiff-image-reading-pixel-values-of-16-bit-signed-image-tp4038039p4042633.html
> Sent from the ImageJ mailing list archive at Nabble.com.
>
Reply | Threaded
Open this post in threaded view
|

Re: Create an Tiff image reading pixel values of 16 bit signed image

Muqeet Khan
Thanks Michael

I tried getting the FloatProcessor by converting 16 signed image to 32 bit (ImageProcessor#convertToFloat). And finally i create another 32 bit images setting the pixels using setPixels(float[]). Actually my main intention is to create an image with the mean of all the pixels from a list of images. Finally i do resetMinAndMax() on the generated image. Everything looks ok except the generated image. It does not look ok something wrong. Are these steps ok?

-Muqeet
 
Michael Schmid-3 wrote
Hi Muqeet,

if you input data are signed, and you want to convert them to float,
simply use
  short[] inPixels = (short[])inImageProcessor.getPixels();
  float[] outPixels = new float[inPixels.length];
  for (int i=0; i<inPixels.length; i++)
    outPixels[i] = inPixels[i]; //conversion keeps sign

for unsigned data, you would need to disable the sign explicitly:
    outPixels[i] = (float)(inPixels[i]&0xffff);

If you want to keep a shortProcessor, simply do an exclusive or with
0x8000 on all pixels, i.e., flip the highest bit. You can then set the
value calibration of the ImagePlus if you care about the numeric values:

  Calibration cal = imp.getCalibration();
  double[] coeff = new double[2];
  coeff[0] = -32768.0;
  coeff[1] = 1.0;
  cal.setFunction(Calibration.STRAIGHT_LINE, coeff, "gray value");


Michael
________________________________________________________________________


On Sat, November 21, 2009 14:40, Muqeet Khan wrote:
> I know use FloatProcessor, i get the image but it does not look ok. Any
> idea
> what is going wrong here?
>
> Muqeet Khan wrote:
>>
>> Is it due to the reason that you use short and values greater than
>> 32,767
>> turns out to be negative. How do i retain the original pixel value. Well
>> i
>> could use putPixelValue.... but my actual intention later is to take
>> mean
>> of couple of image pixels and generate an image. So i have to use
>> setPixels(...) which expects me to use only short data type. Any idea
>> how
>> to retain the original pixel value in this scenario???
>>
>> Muqeet Khan wrote:
>>>
>>> I have a 16-bit signed image, i want to create a tiff image by reading
>>> the pixel values of the 16-bit signed image. For some reason i don't
>>> get
>>> a proper tiff image when doing it. Here are the steps.
>>>
>>> ImagePlus createImage(ImagePlus imagePlus, ImageProcessor targetImage)
>>> {
>>>
>>>     int pixelCount = imagePlus.getWidth() * imagePlus.getHeight();
>>>     Short pixels[] = new Short[pixelCount]
>>>     ImageProcessor iP = imagePlus.getProcessor();
>>>     int count = 0;
>>>
>>>     for (int x = 0;x < imagePlus.getWidth(); x++) {
>>>
>>>         for (int y = 0; y < imagePlus.getHeight; y++) {
>>>             double pixelValue = iP.getPixelValue(x, y);
>>>             pixels[count] = pixelValue;
>>>             count ++;
>>>         }
>>>     }
>>>
>>>     targetImage.setPixels(pixels);
>>>     targetImage .resetMinAndMax();
>>>     return new ImagePlus("Image-RXZ", targetImage);
>>> }
>>>
>>> Is this correct way of doing???
>>>
>>
>>
>
> --
> View this message in context:
> http://n2.nabble.com/Create-an-Tiff-image-reading-pixel-values-of-16-bit-signed-image-tp4038039p4042633.html
> Sent from the ImageJ mailing list archive at Nabble.com.
>
Reply | Threaded
Open this post in threaded view
|

Re: Create an Tiff image reading pixel values of 16 bit signed image

Michael Schmid
Hi Muqeet,

sorry, a statement like "It does not look ok, something wrong" is not
clear enough. You have to describe the problem more exactly or post a
sample image to some web server and give the link.

Anyhow, if your original image is signed 16-bit,
ImageProcessor.convertToFloat applied to the 16-bit image will give you a
sensible result only in case you have flipped the highest bit (as I
suggested in my last mail) or if the ShortProcessor has a Calibration
Table that correctly translates the values to float. Otherwise, it will
only work with unsigned date.

Michael
_________________________________________________________________

On Sun, November 22, 2009 12:34, Muqeet Khan wrote:

> Thanks Michael
>
> I tried getting the FloatProcessor by converting 16 signed image to 32 bit
> (ImageProcessor#convertToFloat). And finally i create another 32 bit
> images
> setting the pixels using setPixels(float[]). Actually my main intention is
> to create an image with the mean of all the pixels from a list of images.
> Finally i do resetMinAndMax() on the generated image. Everything looks ok
> except the generated image. It does not look ok something wrong. Are these
> steps ok?
>
> -Muqeet
>
>
> Michael Schmid-3 wrote:
>>
>> Hi Muqeet,
>>
>> if you input data are signed, and you want to convert them to float,
>> simply use
>>   short[] inPixels = (short[])inImageProcessor.getPixels();
>>   float[] outPixels = new float[inPixels.length];
>>   for (int i=0; i<inPixels.length; i++)
>>     outPixels[i] = inPixels[i]; //conversion keeps sign
>>
>> for unsigned data, you would need to disable the sign explicitly:
>>     outPixels[i] = (float)(inPixels[i]&0xffff);
>>
>> If you want to keep a shortProcessor, simply do an exclusive or with
>> 0x8000 on all pixels, i.e., flip the highest bit. You can then set the
>> value calibration of the ImagePlus if you care about the numeric values:
>>
>>   Calibration cal = imp.getCalibration();
>>   double[] coeff = new double[2];
>>   coeff[0] = -32768.0;
>>   coeff[1] = 1.0;
>>   cal.setFunction(Calibration.STRAIGHT_LINE, coeff, "gray value");
>>
>>
>> Michael
>> ________________________________________________________________________
>>
>>
>> On Sat, November 21, 2009 14:40, Muqeet Khan wrote:
>>> I know use FloatProcessor, i get the image but it does not look ok. Any
>>> idea
>>> what is going wrong here?
>>>
>>> Muqeet Khan wrote:
>>>>
>>>> Is it due to the reason that you use short and values greater than
>>>> 32,767
>>>> turns out to be negative. How do i retain the original pixel value.
>>>> Well
>>>> i
>>>> could use putPixelValue.... but my actual intention later is to take
>>>> mean
>>>> of couple of image pixels and generate an image. So i have to use
>>>> setPixels(...) which expects me to use only short data type. Any idea
>>>> how
>>>> to retain the original pixel value in this scenario???
>>>>
>>>> Muqeet Khan wrote:
>>>>>
>>>>> I have a 16-bit signed image, i want to create a tiff image by
>>>>> reading
>>>>> the pixel values of the 16-bit signed image. For some reason i don't
>>>>> get
>>>>> a proper tiff image when doing it. Here are the steps.
>>>>>
>>>>> ImagePlus createImage(ImagePlus imagePlus, ImageProcessor
>>>>> targetImage)
>>>>> {
>>>>>
>>>>>     int pixelCount = imagePlus.getWidth() * imagePlus.getHeight();
>>>>>     Short pixels[] = new Short[pixelCount]
>>>>>     ImageProcessor iP = imagePlus.getProcessor();
>>>>>     int count = 0;
>>>>>
>>>>>     for (int x = 0;x < imagePlus.getWidth(); x++) {
>>>>>
>>>>>         for (int y = 0; y < imagePlus.getHeight; y++) {
>>>>>             double pixelValue = iP.getPixelValue(x, y);
>>>>>             pixels[count] = pixelValue;
>>>>>             count ++;
>>>>>         }
>>>>>     }
>>>>>
>>>>>     targetImage.setPixels(pixels);
>>>>>     targetImage .resetMinAndMax();
>>>>>     return new ImagePlus("Image-RXZ", targetImage);
>>>>> }
>>>>>
>>>>> Is this correct way of doing???
>>>>>
>>>>
>>>>
>>>
>>> --
>>> View this message in context:
>>> http://n2.nabble.com/Create-an-Tiff-image-reading-pixel-values-of-16-bit-signed-image-tp4038039p4042633.html
>>> Sent from the ImageJ mailing list archive at Nabble.com.
>>>
>>
>>
>
> --
> View this message in context:
> http://n2.nabble.com/Create-an-Tiff-image-reading-pixel-values-of-16-bit-signed-image-tp4038039p4045845.html
> Sent from the ImageJ mailing list archive at Nabble.com.
>
Reply | Threaded
Open this post in threaded view
|

Re: Create an Tiff image reading pixel values of 16 bit signed image

Muqeet Khan
Hi,

I have uploaded the tiff(meanImage_16_1.tif) file that gets generated with the following code.

ImageProcessor imageProcessor = imageArray[0].getProcessor();
int width = imageProcessor.getWidth();
int height = imageProcessor.getHeight();
int pixelCount = width * height;
float[] meanImagePixels = new float[pixelCount];
double[] totalValues = new double[pixelCount];
for(ImagePlus imagePlus: imageArray) {
    FloatProcessor floatProcessor = (FloatProcessor) imagePlus.getProcessor().convertToFloat();
    float[] inPixels = (float[]) floatProcessor.getPixels();
    for (int i = 0; i < inPixels.length; i++)
        totalValues[i] += (inPixels[i]);
}
 
for (int count = 0; count < pixelCount; count++) {
    meanImagePixels[count] = (float)  (totalValues[count]/imageArray.length);
}
                       
ImageProcessor finalMeanImage = new FloatProcessor(width, height, meanImagePixels, null);
finalMeanImage.resetMinAndMax();
ImagePlus imp = new ImagePlus("Mean Image", finalMeanImage);

-Muqeet Khan

Michael Schmid-3 wrote
Hi Muqeet,

sorry, a statement like "It does not look ok, something wrong" is not
clear enough. You have to describe the problem more exactly or post a
sample image to some web server and give the link.

Anyhow, if your original image is signed 16-bit,
ImageProcessor.convertToFloat applied to the 16-bit image will give you a
sensible result only in case you have flipped the highest bit (as I
suggested in my last mail) or if the ShortProcessor has a Calibration
Table that correctly translates the values to float. Otherwise, it will
only work with unsigned date.

Michael
_________________________________________________________________

On Sun, November 22, 2009 12:34, Muqeet Khan wrote:
> Thanks Michael
>
> I tried getting the FloatProcessor by converting 16 signed image to 32 bit
> (ImageProcessor#convertToFloat). And finally i create another 32 bit
> images
> setting the pixels using setPixels(float[]). Actually my main intention is
> to create an image with the mean of all the pixels from a list of images.
> Finally i do resetMinAndMax() on the generated image. Everything looks ok
> except the generated image. It does not look ok something wrong. Are these
> steps ok?
>
> -Muqeet
>
>
> Michael Schmid-3 wrote:
>>
>> Hi Muqeet,
>>
>> if you input data are signed, and you want to convert them to float,
>> simply use
>>   short[] inPixels = (short[])inImageProcessor.getPixels();
>>   float[] outPixels = new float[inPixels.length];
>>   for (int i=0; i<inPixels.length; i++)
>>     outPixels[i] = inPixels[i]; //conversion keeps sign
>>
>> for unsigned data, you would need to disable the sign explicitly:
>>     outPixels[i] = (float)(inPixels[i]&0xffff);
>>
>> If you want to keep a shortProcessor, simply do an exclusive or with
>> 0x8000 on all pixels, i.e., flip the highest bit. You can then set the
>> value calibration of the ImagePlus if you care about the numeric values:
>>
>>   Calibration cal = imp.getCalibration();
>>   double[] coeff = new double[2];
>>   coeff[0] = -32768.0;
>>   coeff[1] = 1.0;
>>   cal.setFunction(Calibration.STRAIGHT_LINE, coeff, "gray value");
>>
>>
>> Michael
>> ________________________________________________________________________
>>
>>
>> On Sat, November 21, 2009 14:40, Muqeet Khan wrote:
>>> I know use FloatProcessor, i get the image but it does not look ok. Any
>>> idea
>>> what is going wrong here?
>>>
>>> Muqeet Khan wrote:
>>>>
>>>> Is it due to the reason that you use short and values greater than
>>>> 32,767
>>>> turns out to be negative. How do i retain the original pixel value.
>>>> Well
>>>> i
>>>> could use putPixelValue.... but my actual intention later is to take
>>>> mean
>>>> of couple of image pixels and generate an image. So i have to use
>>>> setPixels(...) which expects me to use only short data type. Any idea
>>>> how
>>>> to retain the original pixel value in this scenario???
>>>>
>>>> Muqeet Khan wrote:
>>>>>
>>>>> I have a 16-bit signed image, i want to create a tiff image by
>>>>> reading
>>>>> the pixel values of the 16-bit signed image. For some reason i don't
>>>>> get
>>>>> a proper tiff image when doing it. Here are the steps.
>>>>>
>>>>> ImagePlus createImage(ImagePlus imagePlus, ImageProcessor
>>>>> targetImage)
>>>>> {
>>>>>
>>>>>     int pixelCount = imagePlus.getWidth() * imagePlus.getHeight();
>>>>>     Short pixels[] = new Short[pixelCount]
>>>>>     ImageProcessor iP = imagePlus.getProcessor();
>>>>>     int count = 0;
>>>>>
>>>>>     for (int x = 0;x < imagePlus.getWidth(); x++) {
>>>>>
>>>>>         for (int y = 0; y < imagePlus.getHeight; y++) {
>>>>>             double pixelValue = iP.getPixelValue(x, y);
>>>>>             pixels[count] = pixelValue;
>>>>>             count ++;
>>>>>         }
>>>>>     }
>>>>>
>>>>>     targetImage.setPixels(pixels);
>>>>>     targetImage .resetMinAndMax();
>>>>>     return new ImagePlus("Image-RXZ", targetImage);
>>>>> }
>>>>>
>>>>> Is this correct way of doing???
>>>>>
>>>>
>>>>
>>>
>>> --
>>> View this message in context:
>>> http://n2.nabble.com/Create-an-Tiff-image-reading-pixel-values-of-16-bit-signed-image-tp4038039p4042633.html
>>> Sent from the ImageJ mailing list archive at Nabble.com.
>>>
>>
>>
>
> --
> View this message in context:
> http://n2.nabble.com/Create-an-Tiff-image-reading-pixel-values-of-16-bit-signed-image-tp4038039p4045845.html
> Sent from the ImageJ mailing list archive at Nabble.com.
>
meanImage_16_1.tif