我使用以下C#EmguCV代码保存jpeg图像:
Emgu.CV.Image<Gray, byte> image
...
image.Save("imageName.jpg");
但是图像以极低的质量存储(每8x8像素1个颜色的正方形)。
当我保存bmp时,一切正常:
Emgu.CV.Image<Gray, byte> image
...
image.Save("imageName.bmp");
如何在使用Emgu.Cv.Image.Save
时提高jpeg的质量,还是应该调用其他功能?
为什么默认质量如此之低?
试图在EmguCV forum上询问,但无法访问。
答案 0 :(得分:8)
EMGU只有image.Save(filename)因此你必须使用保存图像的.Net方法。此代码源自here。我将代码分开以便于此代码打开文件然后尝试保存它。这是您对 saveJpeg(SaveFile.FileName,img.ToBitmap(),100); 感兴趣的功能。基于函数saveJpeg(字符串路径,Bitmap img,长质量)。
open.Filter = "Image Files (*.tif; *.dcm; *.jpg; *.jpeg; *.bmp)|*.tif; *.dcm; *.jpg; *.jpeg; *.bmp";
if (open.ShowDialog() == DialogResult.OK)
{
Image<Bgr, Byte> img = new Image<Bgr, Byte>(open.FileName);
SaveFileDialog SaveFile = new SaveFileDialog();
if (SaveFile.ShowDialog() == DialogResult.OK)
{
saveJpeg(SaveFile.FileName, img.ToBitmap(), 100);
}
}
现在,为了获得该函数的代码,您可以将以下内容复制并粘贴到项目中,不要忘记代码顶部的using语句。
using System.Drawing.Imaging;
private void saveJpeg(string path, Bitmap img, long quality)
{
// Encoder parameter for image quality
EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
// Jpeg image codec
ImageCodecInfo jpegCodec = this.getEncoderInfo("image/jpeg");
if (jpegCodec == null)
return;
EncoderParameters encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = qualityParam;
img.Save(path, jpegCodec, encoderParams);
}
private ImageCodecInfo getEncoderInfo(string mimeType)
{
// Get image codecs for all image formats
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
// Find the correct image codec
for (int i = 0; i < codecs.Length; i++)
if (codecs[i].MimeType == mimeType)
return codecs[i];
return null;
}
如果你遇到困难,这是EMGU最好的方法让我知道。
希望这有助于,
克里斯
答案 1 :(得分:4)
如果有人对此感兴趣,请以Chris的答案为基础,这是一个简单的延伸;
using System;
using System.Linq;
using System.Drawing.Imaging;
using Emgu.CV.Structure;
public static class EmguImageSave
{
/// <summary>
/// Save a jpeg image with a specified quality
/// </summary>
/// <param name="path">Name of the file to which to save the image</param>
/// <param name="quality">Byte that specifies JPEG Quality of the image encoder</param>
public static void Save( this Emgu.CV.Image<Bgr, Byte> img, string filename, double quality )
{
var encoderParams = new EncoderParameters( 1 );
encoderParams.Param[0] = new EncoderParameter(
System.Drawing.Imaging.Encoder.Quality,
(long)quality
);
var jpegCodec = (from codec in ImageCodecInfo.GetImageEncoders()
where codec.MimeType == "image/jpeg"
select codec).Single();
img.Bitmap.Save( filename, jpegCodec, encoderParams );
}
}
用法;
var fname = 0;
while( true )
{
try
{
var img = capture.QueryFrame();
img.Save( String.Format( @"c:\rec\{0}.jpg", ++fname), 100 );
} catch( Exception ) { break; }
}
答案 2 :(得分:1)
以下是我对Emgu CV的处理方式:
CvInvoke.Imwrite(path, cvImage, new KeyValuePair<ImwriteFlags, int> (ImwriteFlags.JpegQuality, quality));
答案 3 :(得分:-1)
试试这个。为什么不使用旧的和好的OpenCV http://opencv.itseez.com doc?
int quality = 100; // 0 - low, 100 - best
image.Save("imageName.bmp", CV_IMWRITE_JPEG_QUALITY, quality);