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

这篇具有很好参考价值的文章主要介绍了使用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文章来源地址https://www.toymoban.com/news/detail-443319.html

到了这里,关于使用Opencv对图像进行压缩和解压缩的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Linux中_使用tar_gzip_zip_rar_命令_打包和解包_压缩和解压

    1.3.1、到当前目录下 1.3.2、到指定目录下 3.1.1、压缩当前目录: 3.1.2、压缩指定目录: 3.2.1、到当前目录 3.2.2、到指定目录 4.2.1、压缩文件本身: 4.2.2、递归压缩,将指定目录下所有文件和子目录一并压缩:

    2024年02月04日
    浏览(51)
  • 【OpenCV实现图像:使用OpenCV进行图像处理之透视变换】

    透视变换(Perspective Transformation)是一种图像处理中常用的变换手段,它用于将图像从一个视角映射到另一个视角,常被称为投影映射。透视变换可以用于矫正图像中的透视畸变,使得图像中的物体在新的视平面上呈现更加规则的形状。 透视变换通常涉及到寻找图像中的特定

    2024年02月03日
    浏览(48)
  • 使用openCV进行图像处理

    使用 openCV进行图像处理,又名:学习计算机视觉理论,做 demo(第3 天) 目录 2.1 图像模糊 2.1.1 均值滤波 2.1.2 中值滤波 2.1.3 高斯滤波 2.1.4 案例实现 2.2 图像锐化 2.2.1 图像锐化简介 2.2.2 案例实现 3.1 OpenCV绘图 3.1.1 使用OpenCV绘制各种图形 3.1.2 案例实现 3.2 图像的几何变换 3.2.1 几

    2024年01月22日
    浏览(45)
  • 【OpenCV实现图像:使用OpenCV进行物体轮廓排序】

    在图像处理中,经常需要进行与物体轮廓相关的操作,比如计算目标轮廓的周长、面积等。为了获取目标轮廓的信息,通常使用OpenCV的findContours函数。然而,一旦获得轮廓信息后,可能会发现轮廓的顺序是无序的,如下图左侧所示: 在这个图中,每个轮廓都被找到,但它们的

    2024年02月03日
    浏览(29)
  • Ubuntu下的RAR文件压缩和解压缩方法

    在Ubuntu系统中,默认情况下是无法直接处理RAR文件的,因为RAR是一种闭源的压缩格式。然而,我们可以通过安装一些工具来使Ubuntu支持RAR文件的压缩和解压缩操作。本文将介绍如何在Ubuntu系统上实现RAR文件的压缩和解压缩。 步骤1:安装unrar和rar工具 要在Ubuntu上进行RAR文件的

    2024年02月04日
    浏览(26)
  • 如何使用OpenCV库进行图像检测

    import cv2 # 加载Haar级联分类器 face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + \\\'haarcascade_frontalface_default.xml\\\') # 读取输入图像 img = cv2.imread(\\\'input_image.jpg\\\') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 使用Haar级联分类器进行人脸检测 faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors

    2024年02月16日
    浏览(32)
  • 使用opencv对图像进行透视变换

    一.什么是透视变换 透视变换就是透视变换(Perspective Transformation)是指利用透视中心、像点、目标点三点共线的条件,按透视旋转定律使承影面(透视面)绕迹线(透视轴)旋转某一角度,破坏原有的投影光线束,仍能保持承影面上投影几何图形不变的变换。简单的来说就是

    2024年02月08日
    浏览(36)
  • 入门:使用 OpenCV 进行图像处理

    介绍 图像处理是计算机视觉的一个分支,它使用各种算法来处理和分析数字图像。它涉及使用数学或统计操作来为许多应用修改图像,包括但不限于医学和卫星图像以及数字摄影。本文探讨了图像处理的基础知识和该领域中使用的一些技术。 目录 图像处理基础 图像处理的应

    2024年02月08日
    浏览(48)
  • 谈谈如何使用 opencv 进行图像识别

    原文由hakaboom发表于TesterHome社区,点击原文链接可与作者直接交流。 从18年开始,我接触了叉叉助手(平台已经被请喝茶了),通过图色识别,用来给常玩的游戏写挂机脚本,写了也有两三年.也算是我转行当游戏测试的理由. 去年11月,也是用了这身技术,混进了外包,薪资还不错,属于是

    2024年02月10日
    浏览(42)
  • Java使用OpenCV进行图像操作

    OpenCV(开源计算机视觉库)是在BSD(开源协议)许可下发布的。它是一个高度优化的库,专注于实时应用程序。它具有C ++,Python和Java接口,支持Windows,Linux,Mac OS,iOS和Android。 OpenCV是一个开源的计算机视觉库,它提供了一系列丰富的图像处理和计算机视觉算法,包括图像读取

    2024年02月09日
    浏览(31)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包