Converting Emgu images from <Gray, float> image to <Gray, Byte> image results in loss of intensity?

We shade a gray image of the Image type 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 resulting image is converted to bitmap OR " Image<Gray, Byte>

" and saved as JPEG, the intensity is reduced and the sharpening effect is lost. I suspected converting to Bitmap might cause this problem. So, I saved some intermediate images and also converted the image to " Image<Gray,Byte>

". It did not help. I also tried to scale the image with a simple method. It didn't help either.

The above behavior is also true when we do Laplace and subtract the resulting image from the original image. Illustrations below (code has been modified for simplicity):

...

Image<Gray, Byte> sharpenedImage = Sharpen(filter, originalprocessedImage);
ProcessedImage = sharpenedImage.ToBitmap(); // Or ProcessedImage.Bitmap;
ProcessedImage.Save("ProcessedImage.jpg");  // results in intensity loss

...

public Image<Gray, Byte> Sharpen(Image<Gray, Byte> inputFrame)
{
    ConvolutionKernelF Sharpen1Kernel = new ConvolutionKernelF (new float[,] { { -1,-1,-1 }, { -1, 8,-1 }, { -1,-1,-1 } });
    Image<Gray, float> newFloatImage = inputFrame.Convert<Gray, float>();
    Image<Gray, float> newConvolutedImage = newFloatImage.Convolution(Sharpen1Kernel);    
    Image<Gray, float> convolutedScaledShiftedImage = newFloatImage.AddWeighted(newConvolutedImage, 1.0, 1.0, 0);

    // added for testing
    convolutedScaledShiftedImage .Save("ConvolutedScaledShiftedImage .jpg");

    //Now try to scale and save:
    Image<Gray, float> scaledImageFloat = convolutedScaledAddedImage.Clone();
    Image<Gray, float> scaledImageFloat2 = ScaleImage(scaledImageFloat);

    // added for testing
    scaledImageFloat.Save("ScaledImage.jpg");

    // added for testing
    scaledImageFloat2.Convert<Gray,Byte>().Save("ScaledImage-8Bits.jpg");

    // both of these return the images of lower intensity
    return scaledImageFloat2.Convert<Gray,Byte>();
    return convolutedScaledShiftedImage.Convert<gray,Byte>();
}

      

While ConvolutedScaledShifteImage.jpeg is brighter and with more contrast, "ScaledImage.jpeg" and "ScaledImage-8Bits.jpeg" have lost intensity levels compared to ConvolutedScaledShifteImage.jpeg. The same is true for ProcessedImage.jpeg.

ScaleImage below. It wasn't really necessary. Since Convert was losing intensity, I tried to do the conversion and check:

Image<Gray, float> ScaleImage(Image<Gray, float> inputImage)
{
    double[] minValue;
    double[] maxValue;
    Point[] minLocation;
    Point[] maxLocation;

    Image<Gray, float> 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<Gray, float> scaledImage1 = scaledImage.ConvertScale<float>(1.0, Math.Abs(minValue[0]));
    Image<Gray, float> scaledImage2 = scaledImage1.ConvertScale<float>(scaleFactor * 255, 0);

    return scaledImage2;
}

      

Can anyone guess what might go wrong and why intensities are lost in the above operations? Thank.

Edit: Fixed formatting issue ... conversion was from Image<Gray, float>

toImage<Gray, Byte>

Edit Jan 12: I dug even more into the OpenCV code, and as far as I understand, when saving an image of a type Image<Gray,float>

in JPEG format, it imwrite()

first converts the image to an 8-bit image image.convertTo( )temp, CV_8U );

and writes to a file. When the same operation is performed with the aid Convert<Gray,Byte>()

, the intensities do not match. So, it is not clear what is the difference between the two.

+3


source to share





All Articles