Loading ...
Sorry, an error occurred while loading the content.

Re: ASCOM Camera Driver: Displaying Image in C#/.NET (WPF Appl.)

Expand Messages
  • Hartmut
    Alan, do not copy element by element, used Array.Copy instead. The display pixel size should be 8 and you need to transform the 16 bit image data to this
    Message 1 of 7 , May 12, 2012
    View Source
    • 0 Attachment
      Alan,

      do not copy element by element, used Array.Copy instead.

      The display pixel size should be 8 and you need to transform the 16 bit image data to this format.

      Useful steps:

      1. Create a display image buffer from camera image buffer by resampling

      2. Render the display buffer to a .net bitmap 
          determine the black and white points of your image using
          background computation and histogram (this is the most difficult work)
          calculate the intensityscale = 256.0 / (white - black)
          set the pixel values
              (x,y) = (int) ((display buffer(x,y) - black) * intensityscale)
          set the bitmap palette with values from 0 to 255

      3. Show the bitmap in your control

      4. When you change the black and/or white point thereafter, repeat with 2.

      I"m using this frequently. A typical 8Mpix frame uses < 1 sec. on a i3 CPU with 2.93 GHz. The code is written in VBExpress.

      Hartmut


      --- In ASCOM-Talk@yahoogroups.com, "Alan Orr" <alanrorr1@...> wrote:
      >
      > Hi,
      >
      > BitmapSource class in .NET is used. Ultimately it is associated with an image control for display.
      >
      > I have two questions if anyone can help:
      > First, wondering, why the image is coming up hashed when displayed? In other words, the pixels are not correctly aligned vertically from row to row (the alignment looks slanted). Is the rawstride wrong? The original data is 16 bit from the "QHY8 CCD Camera" driver. The code that loads it is as follows. Note that this is hack prototype code to understand the concepts.
      > //Source Array
      > int[,] imgArray = (int[,])C.ImageArray;
      > //Destination Array - conversion is needed since destination array is not of same type or number of bits per pixel.
      > UInt16[] img2Array = new UInt16[C.CameraXSize * C.CameraYSize+1];
      > //Load destination with pixel data from source
      > int iXPosition = 0;
      > int iYPosition = 0;
      > long i = 0;
      > while (i < (C.CameraXSize * C.CameraYSize))
      > {
      >
      > iXPosition = 0;
      > while (iXPosition < C.CameraXSize)
      > {
      > img2Array[i] = (UInt16)(imgArray[iYPosition, iXPosition]);
      > i++;
      > iXPosition++;
      > }
      > iYPosition++;
      > }
      > PixelFormat pf = PixelFormats.Gray16;
      > int width = C.CameraXSize;
      > int height = C.CameraYSize;
      > int rawStride = (width * pf.BitsPerPixel + 7) / 8;
      > //Create bitmap for display
      > BitmapSource bitmap = BitmapSource.Create(width, height,
      > 96, 96, pf, null,
      > img2Array, rawStride);
      > //image1 is the image on the screen
      > //Associate bitmap to image1 for display
      > image1.Source = bitmap;
      >
      > Second, any ideas for loading/associating the image data faster to improve performance? The above performance is definitely a hack taking 90 seconds (plus) in debug mode for the above loop.
      >
      > I am just starting on this .NET / C# adventure so learning what is available. I used to develop with Visual C++ (image processing, algorithms, etc.).
      >
      > Thanks for any/all help,
      >
      > Alan
      >
    • Alan Orr
      Chris and Hartmut, Thank you very much for the good information! Stupid question-where is the cameratest sample? Am I missing a link to a developer page? I
      Message 2 of 7 , May 14, 2012
      View Source
      • 0 Attachment
        Chris and Hartmut,

        Thank you very much for the good information! Stupid question-where is the cameratest sample? Am I missing a link to a developer page? I also searched under the platform 6 installed files.

        Thanks and best,

        Alan

        --- In ASCOM-Talk@yahoogroups.com, "Alan Orr" <alanrorr1@...> wrote:
        >
        > Hi,
        >
        > BitmapSource class in .NET is used. Ultimately it is associated with an image control for display.
        >
        > I have two questions if anyone can help:
        > First, wondering, why the image is coming up hashed when displayed? In other words, the pixels are not correctly aligned vertically from row to row (the alignment looks slanted). Is the rawstride wrong? The original data is 16 bit from the "QHY8 CCD Camera" driver. The code that loads it is as follows. Note that this is hack prototype code to understand the concepts.
        > //Source Array
        > int[,] imgArray = (int[,])C.ImageArray;
        > //Destination Array - conversion is needed since destination array is not of same type or number of bits per pixel.
        > UInt16[] img2Array = new UInt16[C.CameraXSize * C.CameraYSize+1];
        > //Load destination with pixel data from source
        > int iXPosition = 0;
        > int iYPosition = 0;
        > long i = 0;
        > while (i < (C.CameraXSize * C.CameraYSize))
        > {
        >
        > iXPosition = 0;
        > while (iXPosition < C.CameraXSize)
        > {
        > img2Array[i] = (UInt16)(imgArray[iYPosition, iXPosition]);
        > i++;
        > iXPosition++;
        > }
        > iYPosition++;
        > }
        > PixelFormat pf = PixelFormats.Gray16;
        > int width = C.CameraXSize;
        > int height = C.CameraYSize;
        > int rawStride = (width * pf.BitsPerPixel + 7) / 8;
        > //Create bitmap for display
        > BitmapSource bitmap = BitmapSource.Create(width, height,
        > 96, 96, pf, null,
        > img2Array, rawStride);
        > //image1 is the image on the screen
        > //Associate bitmap to image1 for display
        > image1.Source = bitmap;
        >
        > Second, any ideas for loading/associating the image data faster to improve performance? The above performance is definitely a hack taking 90 seconds (plus) in debug mode for the above loop.
        >
        > I am just starting on this .NET / C# adventure so learning what is available. I used to develop with Visual C++ (image processing, algorithms, etc.).
        >
        > Thanks for any/all help,
        >
        > Alan
        >
      • Chris
        The camera test source code is part of the platform sources available from here http://sourceforge.net/projects/ascom/ This link should get you straight to
        Message 3 of 7 , May 15, 2012
        View Source
        • 0 Attachment
          The camera test source code is part of the platform sources available from here http://sourceforge.net/projects/ascom/

          This link should get you straight to what you need,
          http://ascom.svn.sourceforge.net/viewvc/ascom/trunk/TestApps/CameraTest/
          You can download a snapshot or browse the project code online.

          Chris

          --- In ASCOM-Talk@yahoogroups.com, "Alan Orr" <alanrorr1@...> wrote:
          >
          > Chris and Hartmut,
          >
          > Thank you very much for the good information! Stupid question-where is the cameratest sample? Am I missing a link to a developer page? I also searched under the platform 6 installed files.
          >
          > Thanks and best,
          >
          > Alan
          >
          > --- In ASCOM-Talk@yahoogroups.com, "Alan Orr" <alanrorr1@> wrote:
          > >
          > > Hi,
          > >
          > > BitmapSource class in .NET is used. Ultimately it is associated with an image control for display.
          > >
          > > I have two questions if anyone can help:
          > > First, wondering, why the image is coming up hashed when displayed? In other words, the pixels are not correctly aligned vertically from row to row (the alignment looks slanted). Is the rawstride wrong? The original data is 16 bit from the "QHY8 CCD Camera" driver. The code that loads it is as follows. Note that this is hack prototype code to understand the concepts.
          > > //Source Array
          > > int[,] imgArray = (int[,])C.ImageArray;
          > > //Destination Array - conversion is needed since destination array is not of same type or number of bits per pixel.
          > > UInt16[] img2Array = new UInt16[C.CameraXSize * C.CameraYSize+1];
          > > //Load destination with pixel data from source
          > > int iXPosition = 0;
          > > int iYPosition = 0;
          > > long i = 0;
          > > while (i < (C.CameraXSize * C.CameraYSize))
          > > {
          > >
          > > iXPosition = 0;
          > > while (iXPosition < C.CameraXSize)
          > > {
          > > img2Array[i] = (UInt16)(imgArray[iYPosition, iXPosition]);
          > > i++;
          > > iXPosition++;
          > > }
          > > iYPosition++;
          > > }
          > > PixelFormat pf = PixelFormats.Gray16;
          > > int width = C.CameraXSize;
          > > int height = C.CameraYSize;
          > > int rawStride = (width * pf.BitsPerPixel + 7) / 8;
          > > //Create bitmap for display
          > > BitmapSource bitmap = BitmapSource.Create(width, height,
          > > 96, 96, pf, null,
          > > img2Array, rawStride);
          > > //image1 is the image on the screen
          > > //Associate bitmap to image1 for display
          > > image1.Source = bitmap;
          > >
          > > Second, any ideas for loading/associating the image data faster to improve performance? The above performance is definitely a hack taking 90 seconds (plus) in debug mode for the above loop.
          > >
          > > I am just starting on this .NET / C# adventure so learning what is available. I used to develop with Visual C++ (image processing, algorithms, etc.).
          > >
          > > Thanks for any/all help,
          > >
          > > Alan
          > >
          >
        • Alan Orr
          Thanks to all for all of the great comments! I have been looking further at the array.copy plus looking at cameratest. For cameratest, unfortunately, not all
          Message 4 of 7 , May 17, 2012
          View Source
          • 0 Attachment
            Thanks to all for all of the great comments! I have been looking further at the array.copy plus looking at cameratest.

            For cameratest, unfortunately, not all ASCOM drivers work the same. I am getting the same/similar hashed effect in cameratest as I was seeing in my application. I will keep playing with it and see how it works compared to another ASCOM driver for the QHY8 if I can find one. I am getting closer.

            Thanks again!!!!!!!!

            Alan

            --- In ASCOM-Talk@yahoogroups.com, "Chris" <chris_group_mail@...> wrote:
            >
            > The camera test source code is part of the platform sources available from here http://sourceforge.net/projects/ascom/
            >
            > This link should get you straight to what you need,
            > http://ascom.svn.sourceforge.net/viewvc/ascom/trunk/TestApps/CameraTest/
            > You can download a snapshot or browse the project code online.
            >
            > Chris
            >
            > --- In ASCOM-Talk@yahoogroups.com, "Alan Orr" <alanrorr1@> wrote:
            > >
            > > Chris and Hartmut,
            > >
            > > Thank you very much for the good information! Stupid question-where is the cameratest sample? Am I missing a link to a developer page? I also searched under the platform 6 installed files.
            > >
            > > Thanks and best,
            > >
            > > Alan
            > >
            > > --- In ASCOM-Talk@yahoogroups.com, "Alan Orr" <alanrorr1@> wrote:
            > > >
            > > > Hi,
            > > >
            > > > BitmapSource class in .NET is used. Ultimately it is associated with an image control for display.
            > > >
            > > > I have two questions if anyone can help:
            > > > First, wondering, why the image is coming up hashed when displayed? In other words, the pixels are not correctly aligned vertically from row to row (the alignment looks slanted). Is the rawstride wrong? The original data is 16 bit from the "QHY8 CCD Camera" driver. The code that loads it is as follows. Note that this is hack prototype code to understand the concepts.
            > > > //Source Array
            > > > int[,] imgArray = (int[,])C.ImageArray;
            > > > //Destination Array - conversion is needed since destination array is not of same type or number of bits per pixel.
            > > > UInt16[] img2Array = new UInt16[C.CameraXSize * C.CameraYSize+1];
            > > > //Load destination with pixel data from source
            > > > int iXPosition = 0;
            > > > int iYPosition = 0;
            > > > long i = 0;
            > > > while (i < (C.CameraXSize * C.CameraYSize))
            > > > {
            > > >
            > > > iXPosition = 0;
            > > > while (iXPosition < C.CameraXSize)
            > > > {
            > > > img2Array[i] = (UInt16)(imgArray[iYPosition, iXPosition]);
            > > > i++;
            > > > iXPosition++;
            > > > }
            > > > iYPosition++;
            > > > }
            > > > PixelFormat pf = PixelFormats.Gray16;
            > > > int width = C.CameraXSize;
            > > > int height = C.CameraYSize;
            > > > int rawStride = (width * pf.BitsPerPixel + 7) / 8;
            > > > //Create bitmap for display
            > > > BitmapSource bitmap = BitmapSource.Create(width, height,
            > > > 96, 96, pf, null,
            > > > img2Array, rawStride);
            > > > //image1 is the image on the screen
            > > > //Associate bitmap to image1 for display
            > > > image1.Source = bitmap;
            > > >
            > > > Second, any ideas for loading/associating the image data faster to improve performance? The above performance is definitely a hack taking 90 seconds (plus) in debug mode for the above loop.
            > > >
            > > > I am just starting on this .NET / C# adventure so learning what is available. I used to develop with Visual C++ (image processing, algorithms, etc.).
            > > >
            > > > Thanks for any/all help,
            > > >
            > > > Alan
            > > >
            > >
            >
          • Alan Orr
            Hartmut, Thanks for information! I am impressed with the
            Message 5 of 7 , May 17, 2012
            View Source
            • 0 Attachment
              Hartmut,

              Thanks for information! I am impressed with the <1 second. I hope it is not bad form to ask, but if you happen to have any code you are willing/can share, it would be great to see how you do it. I am most interested in your step 1 and the portion of step 2 "Render the display buffer to a .net bitmap". Anything though that you would have would be appreciated. FYI: VB sounds fine and I would not adversely criticize your code.

              Best and Thanks Again,

              Alan

              --- In ASCOM-Talk@yahoogroups.com, "Hartmut" <hvb356@...> wrote:
              >
              > Alan,
              >
              > do not copy element by element, used Array.Copy instead.
              >
              > The display pixel size should be 8 and you need to transform the 16 bit image data to this format.
              >
              > Useful steps:
              >
              > 1. Create a display image buffer from camera image buffer by resampling
              >
              > 2. Render the display buffer to a .net bitmap 
              >     determine the black and white points of your image using
              >     background computation and histogram (this is the most difficult work)
              >     calculate the intensityscale = 256.0 / (white - black)
              >     set the pixel values
              >         (x,y) = (int) ((display buffer(x,y) - black) * intensityscale)
              >     set the bitmap palette with values from 0 to 255
              >
              > 3. Show the bitmap in your control
              >
              > 4. When you change the black and/or white point thereafter, repeat with 2.
              >
              > I"m using this frequently. A typical 8Mpix frame uses < 1 sec. on a i3 CPU with 2.93 GHz. The code is written in VBExpress.
              >
              > Hartmut
              >
              >
              > --- In ASCOM-Talk@yahoogroups.com, "Alan Orr" <alanrorr1@> wrote:
              > >
              > > Hi,
              > >
              > > BitmapSource class in .NET is used. Ultimately it is associated with an image control for display.
              > >
              > > I have two questions if anyone can help:
              > > First, wondering, why the image is coming up hashed when displayed? In other words, the pixels are not correctly aligned vertically from row to row (the alignment looks slanted). Is the rawstride wrong? The original data is 16 bit from the "QHY8 CCD Camera" driver. The code that loads it is as follows. Note that this is hack prototype code to understand the concepts.
              > > //Source Array
              > > int[,] imgArray = (int[,])C.ImageArray;
              > > //Destination Array - conversion is needed since destination array is not of same type or number of bits per pixel.
              > > UInt16[] img2Array = new UInt16[C.CameraXSize * C.CameraYSize+1];
              > > //Load destination with pixel data from source
              > > int iXPosition = 0;
              > > int iYPosition = 0;
              > > long i = 0;
              > > while (i < (C.CameraXSize * C.CameraYSize))
              > > {
              > >
              > > iXPosition = 0;
              > > while (iXPosition < C.CameraXSize)
              > > {
              > > img2Array[i] = (UInt16)(imgArray[iYPosition, iXPosition]);
              > > i++;
              > > iXPosition++;
              > > }
              > > iYPosition++;
              > > }
              > > PixelFormat pf = PixelFormats.Gray16;
              > > int width = C.CameraXSize;
              > > int height = C.CameraYSize;
              > > int rawStride = (width * pf.BitsPerPixel + 7) / 8;
              > > //Create bitmap for display
              > > BitmapSource bitmap = BitmapSource.Create(width, height,
              > > 96, 96, pf, null,
              > > img2Array, rawStride);
              > > //image1 is the image on the screen
              > > //Associate bitmap to image1 for display
              > > image1.Source = bitmap;
              > >
              > > Second, any ideas for loading/associating the image data faster to improve performance? The above performance is definitely a hack taking 90 seconds (plus) in debug mode for the above loop.
              > >
              > > I am just starting on this .NET / C# adventure so learning what is available. I used to develop with Visual C++ (image processing, algorithms, etc.).
              > >
              > > Thanks for any/all help,
              > >
              > > Alan
              > >
              >
            Your message has been successfully submitted and would be delivered to recipients shortly.