EmguCV - 保存图像时如何指定jpeg质量? [英] EmguCV - how to specify jpeg quality when saving image?

查看:773
本文介绍了EmguCV - 保存图像时如何指定jpeg质量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下C# EmguCV 代码保存jpeg图像:

I save jpeg image using the following C# EmguCV code:

Emgu.CV.Image<Gray, byte> image
...
image.Save("imageName.jpg");

但图像以极低的质量存储(每8x8像素1个颜色的方块)。

But image is stored in extremally low quality (1 color square per 8x8 pixels).

当我保存bmp时,一切正常:

When I save bmp all are ok:

Emgu.CV.Image<Gray, byte> image
...
image.Save("imageName.bmp");

如何在使用 Emgu.Cv.Image.Save <时提高jpeg质量/ code>还是应该调用其他函数?
为什么默认质量如此之低?

How to increase jpeg quality when using Emgu.Cv.Image.Save or should I call other function? Why is default quality so low?

试图在 EmguCV论坛上询问,但它无法访问。

Tried to ask on EmguCV forum, but it is unreachable.

推荐答案

EMGU只有image.Save(filename)因此你必须使用.Net方法保存图片。此代码源自此处。我将代码分开以便于此代码打开文件然后尝试保存它。这是您对 saveJpeg(SaveFile.FileName,img.ToBitmap(),100); 感兴趣的功能。基于函数saveJpeg(字符串路径,Bitmap img,长质量)。

EMGU only has image.Save(filename) therefore you have to use the .Net method of saving the image. This code is derived from here. I separated the code for ease this code opens a file then attempts to save it. This is the function you interested in saveJpeg(SaveFile.FileName, img.ToBitmap(), 100);. Based on the function saveJpeg(string path, Bitmap img, long quality).

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语句。

Now to get the code for that function comes from the following you can copy and paste this into your project don't forget the using statement at the top of your code.

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的最佳方法让我知道。

This is the best method for EMGU if you get stuck let me know.

希望这有帮助,

Chris

这篇关于EmguCV - 保存图像时如何指定jpeg质量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆