问题
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 "Image<Gray, Byte>
"
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 "Image<Gray,Byte>
". This did not help. I also tried to scale the image using a simple method. This too did not help.
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):
...
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 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.
The ScaleImage is below. This was not really necessary. As the 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;
}
Would anybody be able to suggest what could be going wrong and why the intensities are lost in the above operations? Thanks.
Edit: fixed the formatting issue... conversion was from Image<Gray, float>
to Image<Gray, Byte>
Edit 12-Jan: I further dug into OpenCV code and as I understand, when you save an image of type Image<Gray,float>
to JPEG, imwrite()
first converts the image to an 8-bit image image.convertTo( )temp, CV_8U );
and writes to the file. When the same operation is performed with Convert<Gray,Byte>()
, the intensities are not the same. So, It is not clear what is the difference between the two.
来源:https://stackoverflow.com/questions/27793886/emgu-image-conversion-from-imagegray-float-to-imagegray-byte-results-in-inte