opencv不同版本之间的互操作性

2024-04-07 07:18

本文主要是介绍opencv不同版本之间的互操作性,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

下图1,为配置截图;下图2为含有#define DEMO_MIXED_API_USE的运行结果,下图3为不含有#define DEMO_MIXED_API_USE的运行结果。实现代码如下所示:

#include <stdio.h>
#include <iostream>#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>using namespace cv;  // The new C++ interface API is inside this namespace. Import it.
using namespace std;void help( char* progName)
{cout << endl << progName<< " shows how to use cv::Mat and IplImages together (converting back and forth)." << endl<< "Also contains example for image read, spliting the planes, merging back and "  << endl<< " color conversion, plus iterating through pixels. "                            << endl<< "Usage:" << endl<< progName << " [image-name Default: lena.jpg]"                           << endl << endl;
}// comment out the define to use only the latest C++ API
//包含C和C++应用程序接口
#define DEMO_MIXED_API_USEint main( int argc, char** argv )
{help(argv[0]);const char* imagename = argc > 1 ? argv[1] : "lena.jpg";#ifdef DEMO_MIXED_API_USE//包含C和C++应用程序接口Ptr<IplImage> IplI = cvLoadImage(imagename); // Ptr<T> is safe ref-counting pointer classif(IplI.empty()){cerr << "Can not load image " <<  imagename << endl;return -1;}Mat I(IplI); //转换为Mat类型,只复制指针,不复制图像
#else//纯C++应用程序接口Mat I = imread(imagename);// the newer cvLoadImage alternative, MATLAB-style functionif( I.empty() )           // same as if( !I.data ){cerr << "Can not load image " <<  imagename << endl;return -1;}
#endif///自动转换图像至YUV彩色空间Mat I_YUV;cvtColor(I, I_YUV, CV_BGR2YCrCb);vector<Mat> planes;   //应用标准模板库矢量存储多Mat对象 split(I_YUV, planes); //把图像分割为独立的Y,U,V彩色平面#if 1 // change it to 0 if you want to see a blurred and noisy version of this processing Mat scanning// Method 1. process Y plane using an iterator递归器MatIterator_<uchar> it = planes[0].begin<uchar>(), it_end = planes[0].end<uchar>();for(; it != it_end; ++it){double v = *it * 1.7 + rand()%21 - 10;*it = saturate_cast<uchar>(v*v/255);}for( int y = 0; y < I_YUV.rows; y++ ){// Method 2. process the first chroma plane using pre-stored row pointer.uchar* Uptr = planes[1].ptr<uchar>(y);for( int x = 0; x < I_YUV.cols; x++ ){Uptr[x] = saturate_cast<uchar>((Uptr[x]-128)/2 + 128);// Method 3. process the second chroma plane using individual element accessuchar& Vxy = planes[2].at<uchar>(y, x);Vxy =        saturate_cast<uchar>((Vxy-128)/2 + 128);}}#elseMat noisyI(I.size(), CV_8U);           // Create a matrix of the specified size and type// Fills the matrix with normally distributed random values (around number with deviation off).// There is also randu() for uniformly distributed random number generationrandn(noisyI, Scalar::all(128), Scalar::all(20));// blur the noisyI a bit, kernel size is 3x3 and both sigma's are set to 0.5GaussianBlur(noisyI, noisyI, Size(3, 3), 0.5, 0.5);const double brightness_gain = 0;const double contrast_gain = 1.7;#ifdef DEMO_MIXED_API_USE// To pass the new matrices to the functions that only work with IplImage or CvMat do:// step 1) Convert the headers (tip: data will not be copied).// step 2) call the function   (tip: to pass a pointer do not forget unary "&" to form pointers)IplImage cv_planes_0 = planes[0], cv_noise = noisyI;cvAddWeighted(&cv_planes_0, contrast_gain, &cv_noise, 1, -128 + brightness_gain, &cv_planes_0);
#elseaddWeighted(planes[0], contrast_gain, noisyI, 1, -128 + brightness_gain, planes[0]);
#endifconst double color_scale = 0.5;// Mat::convertTo() replaces cvConvertScale.// One must explicitly specify the output matrix type (we keep it intact - planes[1].type())planes[1].convertTo(planes[1], planes[1].type(), color_scale, 128*(1-color_scale));// alternative form of cv::convertScale if we know the datatype at compile time ("uchar" here).// This expression will not create any temporary arrays ( so should be almost as fast as above)planes[2] = Mat_<uchar>(planes[2]*color_scale + 128*(1-color_scale));// Mat::mul replaces cvMul(). Again, no temporary arrays are created in case of simple expressions.planes[0] = planes[0].mul(planes[0], 1./255);
#endifmerge(planes, I_YUV);                // now merge the results backcvtColor(I_YUV, I, CV_YCrCb2BGR);  // and produce the output RGB imagenamedWindow("image with grain", CV_WINDOW_AUTOSIZE);   // use this to create images#ifdef DEMO_MIXED_API_USE// this is to demonstrate that I and IplI really share the data - the result of the above// processing is stored in I and thus in IplI too.cvShowImage("image with grain", IplI);
#elseimshow("image with grain", I); // the new MATLAB style function show
#endifwaitKey();// Tip: No memory freeing is required!//      All the memory will be automatically released by the Vector<>, Mat and Ptr<> destructor.return 0;
}

图1:


图2:

图3:


                                    

这篇关于opencv不同版本之间的互操作性的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/881969

相关文章

Python一次性将指定版本所有包上传PyPI镜像解决方案

《Python一次性将指定版本所有包上传PyPI镜像解决方案》本文主要介绍了一个安全、完整、可离线部署的解决方案,用于一次性准备指定Python版本的所有包,然后导出到内网环境,感兴趣的小伙伴可以跟随... 目录为什么需要这个方案完整解决方案1. 项目目录结构2. 创建智能下载脚本3. 创建包清单生成脚本4

SpringBoot实现不同接口指定上传文件大小的具体步骤

《SpringBoot实现不同接口指定上传文件大小的具体步骤》:本文主要介绍在SpringBoot中通过自定义注解、AOP拦截和配置文件实现不同接口上传文件大小限制的方法,强调需设置全局阈值远大于... 目录一  springboot实现不同接口指定文件大小1.1 思路说明1.2 工程启动说明二 具体实施2

Ubuntu如何升级Python版本

《Ubuntu如何升级Python版本》Ubuntu22.04Docker中,安装Python3.11后,使用update-alternatives设置为默认版本,最后用python3-V验证... 目China编程录问题描述前提环境解决方法总结问题描述Ubuntu22.04系统自带python3.10,想升级

Python Flask实现定时任务的不同方法详解

《PythonFlask实现定时任务的不同方法详解》在Flask中实现定时任务,最常用的方法是使用APScheduler库,本文将提供一个完整的解决方案,有需要的小伙伴可以跟随小编一起学习一下... 目录完js整实现方案代码解释1. 依赖安装2. 核心组件3. 任务类型4. 任务管理5. 持久化存储生产环境

更改linux系统的默认Python版本方式

《更改linux系统的默认Python版本方式》通过删除原Python软链接并创建指向python3.6的新链接,可切换系统默认Python版本,需注意版本冲突、环境混乱及维护问题,建议使用pyenv... 目录更改系统的默认python版本软链接软链接的特点创建软链接的命令使用场景注意事项总结更改系统的默

Linux升级或者切换python版本实现方式

《Linux升级或者切换python版本实现方式》本文介绍在Ubuntu/Debian系统升级Python至3.11或更高版本的方法,通过查看版本列表并选择新版本进行全局修改,需注意自动与手动模式的选... 目录升级系统python版本 (适用于全局修改)对于Ubuntu/Debian系统安装后,验证Pyt

Java中数组与栈和堆之间的关系说明

《Java中数组与栈和堆之间的关系说明》文章讲解了Java数组的初始化方式、内存存储机制、引用传递特性及遍历、排序、拷贝技巧,强调引用数据类型方法调用时形参可能修改实参,但需注意引用指向单一对象的特性... 目录Java中数组与栈和堆的关系遍历数组接下来是一些编程小技巧总结Java中数组与栈和堆的关系关于

MySQL 升级到8.4版本的完整流程及操作方法

《MySQL升级到8.4版本的完整流程及操作方法》本文详细说明了MySQL升级至8.4的完整流程,涵盖升级前准备(备份、兼容性检查)、支持路径(原地、逻辑导出、复制)、关键变更(空间索引、保留关键字... 目录一、升级前准备 (3.1 Before You Begin)二、升级路径 (3.2 Upgrade

Nginx进行平滑升级的实战指南(不中断服务版本更新)

《Nginx进行平滑升级的实战指南(不中断服务版本更新)》Nginx的平滑升级(也称为热升级)是一种在不停止服务的情况下更新Nginx版本或添加模块的方法,这种升级方式确保了服务的高可用性,避免了因升... 目录一.下载并编译新版Nginx1.下载解压2.编译二.替换可执行文件,并平滑升级1.替换可执行文件

在Java中实现线程之间的数据共享的几种方式总结

《在Java中实现线程之间的数据共享的几种方式总结》在Java中实现线程间数据共享是并发编程的核心需求,但需要谨慎处理同步问题以避免竞态条件,本文通过代码示例给大家介绍了几种主要实现方式及其最佳实践,... 目录1. 共享变量与同步机制2. 轻量级通信机制3. 线程安全容器4. 线程局部变量(ThreadL