使用Opencv对图像进行压缩和解压缩

1.关于压缩的必要性

  最近在一个项目中需要将工业相机采集到的图像通过jsonrpc进行传输,一开始没进行压缩,传输的速度很慢,相机分辨率是2592×1944,单通道,这么一算一次要传输的数据量大小是2592×1944×1=5,038,848字节,500多万的字节,通过opencv自带imencodeimdecode两个函数,将图像进行二进制编码,实测经过.jpg格式压缩后的数据量在2万多字节,确实相差很多倍。

2.API介绍

2.1 imencode()

/** @brief Encodes an image into a memory buffer.

The function imencode compresses the image and stores it in the memory buffer that is resized to fit the
result. See cv::imwrite for the list of supported formats and flags description.

@param ext File extension that defines the output format.
@param img Image to be written.
@param buf Output buffer resized to fit the compressed image.
@param params Format-specific parameters. See cv::imwrite and cv::ImwriteFlags.
*/
CV_EXPORTS_W bool imencode( const String& ext, InputArray img,
                            CV_OUT std::vector<uchar>& buf,
                            const std::vector<int>& params = std::vector<int>());

参数介绍

  • const String& ext:ext文件扩展名,定义输出格式
  • InputArray img:需要被编码的图像
  • CV_OUT std::vector& buf:输出的缓冲区,调整大小以适应压缩的图像
  • onst std::vector& params = std::vector()):被编码的格式和压缩率,类型是vector
    prams目前支持以下参数:
    JPEG,它的压缩率范围(cv_imwrite_jpeg_quality)从0到100(越大越好)。默认值是95。100为没有压缩。
    对于WEBP来说,它的压缩范围(cv_imwrite_webp_quality)从1到100(越大越好)。默认情况下(不含任何参数)和质量在100以上,则使用无损压缩。
    png,可以压缩级别(cv_imwrite_png_compression)从0到9。更高的值意味着更小的尺寸和更长的压缩时间。默认值是3。
    PPM、PGM、或PBM,它可以是一个二进制格式的标志(cv_imwrite_pxm_binary),0或1。默认值是1。

2.2 imdecode()

/** @brief Reads an image from a buffer in memory.

The function imdecode reads an image from the specified buffer in the memory. If the buffer is too short or
contains invalid data, the function returns an empty matrix ( Mat::data==NULL ).

See cv::imread for the list of supported formats and flags description.

@note In the case of color images, the decoded images will have the channels stored in **B G R** order.
@param buf Input array or vector of bytes.
@param flags The same flags as in cv::imread, see cv::ImreadModes.
*/
CV_EXPORTS_W Mat imdecode( InputArray buf, int flags );

/** @overload
@param buf
@param flags
@param dst The optional output placeholder for the decoded matrix. It can save the image
reallocations when the function is called repeatedly for images of the same size.
*/
CV_EXPORTS Mat imdecode( InputArray buf, int flags, Mat* dst);

参数介绍

  • InputArray buf:输入解压的buf
  • int flags:
    网上介绍的是:
    CV_LOAD_IMOSE_COLOR-如果设置,始终将图像转换为彩色图像;
    CV_LOAD_IMAGE_GRAYSCALE如果设置,始终将图像转换为灰度图像。
    dst -解码矩阵的可选输出占位符。不填则是NULL。
    实测int flags设为0时表示始终将图像转换为灰度图像,
    int flags设为1时表示始终将图像转换为彩色图像。

3.接口介绍

提供两个压缩图像和解压缩图像的接口函数,可根据需求修改。

#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <vector>
#include <string>
#include <memory>

/**
 * @brief    图像信息结构体
 * @param	 int height, width         图像像素高度,宽度
 * @param	 std::vector<uint8_t> data 图像数据信息
 * @param	 int channels              图像通道数
 */
struct ImageInfo
{
    int height, width;
    std::vector<uint8_t> data;
    int channels;
};


ImageInfo getZipImageInfo(ImageInfo image_info, int view_width, int view_height,
                          ImageType image_type)
{
    ImageInfo dst_image_info{};
    std::vector<uint8_t> image_buffer;

    cv::Mat *getImage = new cv::Mat();
    cv::Mat src;

    if (image_info.channels == 1) {
        *getImage = cv::Mat(image_info.height, image_info.width, CV_8UC1, (image_info.data).data());
    } else if (image_info.channels == 3) {
        *getImage = cv::Mat(image_info.height, image_info.width, CV_8UC3, (image_info.data).data());
    } else {
        std::cout << "Error!" << std::endl;
        return dst_image_info;
    }
    (*getImage).copyTo(src);
    (*getImage).release();

    cv::Mat dst;
    if (src.channels() == 1) {
        dst = cv::Mat(view_height, view_width, CV_8UC1); 
    } else if (src.channels() == 3) {
        dst = cv::Mat(view_height, view_width, CV_8UC3);
    } else {
        std::cout << "Error!" << std::endl;
        return dst_image_info;
    }

    cv::resize(src, dst, cv::Size(view_width, view_height));

    std::vector<int> para;
    para.resize(3, 0);
    para[0] = cv::IMWRITE_JPEG_QUALITY;
    para[1] = 80;
    // 压缩图像并将其存储在内存缓冲区中
    if (image_type == ZIP_JPG) {
        cv::imencode(".jpg", dst, image_buffer, para);
    } else if (image_type == ZIP_BMP) {
        cv::imencode(".bmp", dst, image_buffer, para);
    } else {
        std::cout << "The compressed image format is wrong!" << std::endl;
        return dst_image_info;
    }

    dst_image_info.height = 480;
    dst_image_info.width = 640;
    dst_image_info.data = image_buffer;
    dst_image_info.channels = image_info.channels;

    return dst_image_info;
}

ImageInfo getUnzipImageInfo(ImageInfo image_info)
{
    cv::Mat image;
    ImageInfo dst_image_info{};
    if (image_info.channels == 1) {
        image = cv::imdecode(image_info.data, 0);
        dst_image_info.channels = 1;
    } else if (image_info.channels == 3) {
        image = cv::imdecode(image_info.data, 1);
        dst_image_info.channels = 3;
    } else {
        std::cout << "Error!" << std::endl;
        return dst_image_info;
    }

    dst_image_info.height = image.rows;
    dst_image_info.width = image.cols;
    dst_image_info.data.clear();
    int buf_size = dst_image_info.height * dst_image_info.width;
    dst_image_info.data =
        std::vector<uint8_t>(image.data, image.data + buf_size);

    return dst_image_info;
}

参考:
https://blog.csdn.net/qq_37406130/article/details/78820176
https://zhangpy.blog.csdn.net/article/details/69789263?spm=1001.2014.3001.5502

  • 7
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

boss-dog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值