title: Emgu image conversion from Image<Gray,float> to Image<Gray,Byte> results in intensity loss?
body: <p>We are performing image sharpening of a gray scale image of type Image by subtracting the Laplacian of the image from the original image. The result, if saved as a JPEG, has well defined edges and contrast. However, if the resultant image is converted to Bitmap OR "<code>Image&lt;Gray, Byte&gt;</code>"
  and saved as JPEG, the intensity is reduced and the sharpening effect is lost. I suspected that converting to Bitmap may be causing this problem. So, I saved some of the intermediate images and also converted the image to "<code>Image&lt;Gray,Byte&gt;</code>". This did not help. I also tried to scale the image using a simple method. This too did not help.</p>
  
  <p>The above behaviour is also true when we perform Laplace and subtract the resultant image from the original image. Illustrations are below (code has been modified for simplicity):</p>
  
  <pre><code>...
  
  Image&lt;Gray, Byte&gt; sharpenedImage = Sharpen(filter, originalprocessedImage);
  ProcessedImage = sharpenedImage.ToBitmap(); // Or ProcessedImage.Bitmap;
  ProcessedImage.Save("ProcessedImage.jpg");  // results in intensity loss
  
  ...
  
  public Image&lt;Gray, Byte&gt; Sharpen(Image&lt;Gray, Byte&gt; inputFrame)
  {
      ConvolutionKernelF Sharpen1Kernel = new ConvolutionKernelF (new float[,] { { -1,-1,-1 }, { -1, 8,-1 }, { -1,-1,-1 } });
      Image&lt;Gray, float&gt; newFloatImage = inputFrame.Convert&lt;Gray, float&gt;();
      Image&lt;Gray, float&gt; newConvolutedImage = newFloatImage.Convolution(Sharpen1Kernel);    
      Image&lt;Gray, float&gt; convolutedScaledShiftedImage = newFloatImage.AddWeighted(newConvolutedImage, 1.0, 1.0, 0);
  
      // added for testing
      convolutedScaledShiftedImage .Save("ConvolutedScaledShiftedImage .jpg");
  
      //Now try to scale and save:
      Image&lt;Gray, float&gt; scaledImageFloat = convolutedScaledAddedImage.Clone();
      Image&lt;Gray, float&gt; scaledImageFloat2 = ScaleImage(scaledImageFloat);
  
      // added for testing
      scaledImageFloat.Save("ScaledImage.jpg");
  
      // added for testing
      scaledImageFloat2.Convert&lt;Gray,Byte&gt;().Save("ScaledImage-8Bits.jpg");
  
      // both of these return the images of lower intensity
      return scaledImageFloat2.Convert&lt;Gray,Byte&gt;();
      return convolutedScaledShiftedImage.Convert&lt;gray,Byte&gt;();
  }
  </code></pre>
  
  <p>While the ConvolutedScaledShifteImage.jpeg is brighter and with better contrast, "ScaledImage.jpeg" and "ScaledImage-8Bits.jpeg" have lost the intensity levels as compared to ConvolutedScaledShifteImage.jpeg. The same is true for ProcessedImage.jpeg.</p>
  
  <p>The ScaleImage is below. This was not really necessary. As the Convert was losing intensity, I tried to do the conversion and check:</p>
  
  <pre><code>Image&lt;Gray, float&gt; ScaleImage(Image&lt;Gray, float&gt; inputImage)
  {
      double[] minValue;
      double[] maxValue;
      Point[] minLocation;
      Point[] maxLocation;
  
      Image&lt;Gray, float&gt; scaledImage = inputImage.Clone();
  
      scaledImage.MinMax(out minValue, out maxValue, out minLocation, out maxLocation);
  
      double midValue = (minValue[0] + maxValue[0] ) / 2;
      double rangeValue = (maxValue[0]) - (minValue[0]);
      double scaleFactor = 1 / rangeValue;
      double shiftFactor = midValue;
  
      Image&lt;Gray, float&gt; scaledImage1 = scaledImage.ConvertScale&lt;float&gt;(1.0, Math.Abs(minValue[0]));
      Image&lt;Gray, float&gt; scaledImage2 = scaledImage1.ConvertScale&lt;float&gt;(scaleFactor * 255, 0);
  
      return scaledImage2;
  }
  </code></pre>
  
  <p>Would anybody be able to suggest what could be going wrong and why the intensities are lost in the above operations? Thanks.</p>
  
  <p>Edit: fixed the formatting issue... conversion was from <code>Image&lt;Gray, float&gt;</code> to <code>Image&lt;Gray, Byte&gt;</code></p>
  
  <p>Edit 12-Jan: I further dug into OpenCV code and as I understand, when you save an image of type <code>Image&lt;Gray,float&gt;</code> to JPEG, <code>imwrite()</code> first converts the image to an 8-bit image <code>image.convertTo( )temp, CV_8U );</code> and writes to the file. When the same operation is performed with <code>Convert&lt;Gray,Byte&gt;()</code>, the intensities are not the same. So, It is not clear what is the difference between the two.</p>