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

相关文章

golang获取当前时间、时间戳和时间字符串及它们之间的相互转换方法

《golang获取当前时间、时间戳和时间字符串及它们之间的相互转换方法》:本文主要介绍golang获取当前时间、时间戳和时间字符串及它们之间的相互转换,本文通过实例代码给大家介绍的非常详细,感兴趣... 目录1、获取当前时间2、获取当前时间戳3、获取当前时间的字符串格式4、它们之间的相互转化上篇文章给大家介

浅谈配置MMCV环境,解决报错,版本不匹配问题

《浅谈配置MMCV环境,解决报错,版本不匹配问题》:本文主要介绍浅谈配置MMCV环境,解决报错,版本不匹配问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录配置MMCV环境,解决报错,版本不匹配错误示例正确示例总结配置MMCV环境,解决报错,版本不匹配在col

Linux卸载自带jdk并安装新jdk版本的图文教程

《Linux卸载自带jdk并安装新jdk版本的图文教程》在Linux系统中,有时需要卸载预装的OpenJDK并安装特定版本的JDK,例如JDK1.8,所以本文给大家详细介绍了Linux卸载自带jdk并... 目录Ⅰ、卸载自带jdkⅡ、安装新版jdkⅠ、卸载自带jdk1、输入命令查看旧jdkrpm -qa

opencv图像处理之指纹验证的实现

《opencv图像处理之指纹验证的实现》本文主要介绍了opencv图像处理之指纹验证的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录一、简介二、具体案例实现1. 图像显示函数2. 指纹验证函数3. 主函数4、运行结果三、总结一、

Tomcat版本与Java版本的关系及说明

《Tomcat版本与Java版本的关系及说明》:本文主要介绍Tomcat版本与Java版本的关系及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Tomcat版本与Java版本的关系Tomcat历史版本对应的Java版本Tomcat支持哪些版本的pythonJ

python+opencv处理颜色之将目标颜色转换实例代码

《python+opencv处理颜色之将目标颜色转换实例代码》OpenCV是一个的跨平台计算机视觉库,可以运行在Linux、Windows和MacOS操作系统上,:本文主要介绍python+ope... 目录下面是代码+ 效果 + 解释转HSV: 关于颜色总是要转HSV的掩膜再标注总结 目标:将红色的部分滤

Vue中组件之间传值的六种方式(完整版)

《Vue中组件之间传值的六种方式(完整版)》组件是vue.js最强大的功能之一,而组件实例的作用域是相互独立的,这就意味着不同组件之间的数据无法相互引用,针对不同的使用场景,如何选择行之有效的通信方式... 目录前言方法一、props/$emit1.父组件向子组件传值2.子组件向父组件传值(通过事件形式)方

MySQL中慢SQL优化的不同方式介绍

《MySQL中慢SQL优化的不同方式介绍》慢SQL的优化,主要从两个方面考虑,SQL语句本身的优化,以及数据库设计的优化,下面小编就来给大家介绍一下有哪些方式可以优化慢SQL吧... 目录避免不必要的列分页优化索引优化JOIN 的优化排序优化UNION 优化慢 SQL 的优化,主要从两个方面考虑,SQL 语

IDEA中Git版本回退的两种实现方案

《IDEA中Git版本回退的两种实现方案》作为开发者,代码版本回退是日常高频操作,IntelliJIDEA集成了强大的Git工具链,但面对reset和revert两种核心回退方案,许多开发者仍存在选择... 目录一、版本回退前置知识二、Reset方案:整体改写历史1、IDEA图形化操作(推荐)1.1、查看提

JDK多版本共存并自由切换的操作指南(本文为JDK8和JDK17)

《JDK多版本共存并自由切换的操作指南(本文为JDK8和JDK17)》本文介绍了如何在Windows系统上配置多版本JDK(以JDK8和JDK17为例),并通过图文结合的方式给大家讲解了详细步骤,具有... 目录第一步 下载安装JDK第二步 配置环境变量第三步 切换JDK版本并验证可能遇到的问题前提:公司常