\tutorial_code\core\interoperability_with_OpenCV_1

浏览这篇的前提是

你以前用过OpenCV



没用过的怎么办??


就直接用c++的表達方式嘛!!








【9】 interoperability_with_OpenCV_1

\OpenCV2.3.1\opencv\samples\cpp\tutorial_code\core\interoperability_with_OpenCV_1


那个switching_to_the_new_interface和这个例子是一样的。 = =





======================================================
新的图像数据结构: Mat - 基本图像容器 ,


它取代了旧的 CvMat 和 IplImage 。转换到新函数非常容易,

你仅需记住几条新的原则。


》》》》》》》》》》》》》》》
OpenCV 2 接受按需定制。

所有函数不再装入一个单一的库中。我们会提供许多模块,

使用时,你仅需要包含用到的头文件,比如:

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>


加上以下指令:

using namespace cv; // 新的C++接口API都在此名字空间中,需要导入。


因为所有库中函数都已在此名字空间中,所以无需加 cv 作为前缀。



据此所有新的C++兼容函数都无此前缀,并且遵循驼峰命名准则。

也就是第一个字母为小写(除非是单个单词作为函数名,如 Canny)并且后续单词首字母大写(如 copyMakeBorder ).



》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
接下来,请记住你需要将所有用到的模块链接到你的程序中。

如果你在Windows下开发且用到了 动态链接库(DLL) ,你还需要将OpenCV对应动态链接库的路径加入程序执行路径中。关于Windows下开发的更多信息请阅读 How to build applications with OpenCV inside the Microsoft Visual Studio ;对于Linux用户,可参考 Using OpenCV with Eclipse (plugin CDT) 中的实例及说明。




====================================
你可以使用 IplImage 或 CvMat 操作符来转换 Mat 对象。

在C接口中,你习惯于使用指针,但此处将不再需要。
在C++接口中,我们大多数情况下都是用 Mat 对象。
此对象可通过简单的赋值操作转换为 IplImage 和 CvMat 。

示例如下:

Mat I;

IplImage pI = I;
CvMat mI = I;


获取指针

我们可以用 & 符号获取其指针如下:

Mat I;
IplImage* pI = &I.operator IplImage();
CvMat* mI = &I.operator CvMat();


===================================================
OpenCV引进了一种智能指针。它将自动释放不再使用的对象。使用时,指针将被声明为 Ptr 模板的特化:

Ptr<IplImage> piI = &I.operator IplImage();


将C接口的数据结构转换为 Mat 时,可将其作为构造函数的参数传入,例如:

Mat K(piL), L;
L = Mat(pI);

===================================================


》》》》》》》》》》》》》
从旧的函数名转换新版本非常容易,仅需要删除 cv 前缀





实例学习。。

今天是第一次贴完整代码哎。。
眼花缭乱 = =

贴的是用新方法的写法


//各种头 = =

#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;




int main( int argc, char** argv )
{

    const char* imagename = argc > 1 ? argv[1] : "lena.jpg";
//默认用 暴露狂 lena 姐



    Mat I = imread(imagename);
 //== MATLAB式的打开图像方法

    if( I.empty() ) // same as if( !I.data )
    {
        cerr << "Can not load image " << imagename << endl;
        return -1;
    }

    

    // 转换到YUV色彩空间,值得注意的是,不用申请内存空间哟!

    Mat I_YUV;
    cvtColor(I, I_YUV, CV_BGR2YCrCb);


    vector<Mat> planes; // 使用 STL 的 vector 结构存储多个 Mat
    split(I_YUV, planes); // 分离Y U V通道


    // see a blurred and noisy version of this processing
  
    Mat noisyI(I.size(), CV_8U);

   // 创建一个同样大小的矩阵名曰noisy
    
    // 填充随机值

    randn(noisyI, Scalar::all(128), Scalar::all(20));
    
    // 进行高斯模糊, kernel size is 3x3 and both sigma's are set to 0.5

    GaussianBlur(noisyI, noisyI, Size(3, 3), 0.5, 0.5);


    const double brightness_gain = 0;
    const double contrast_gain = 1.7;

    addWeighted(planes[0], contrast_gain, noisyI, 1, -128 + brightness_gain, planes[0]);

    
    const 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);//量化,设置元素取值范围的一种方法


    
    merge(planes, I_YUV); // now merge the results back
    cvtColor(I_YUV, I, CV_YCrCb2BGR); // and produce the output RGB image

    
    namedWindow("image with grain", CV_WINDOW_AUTOSIZE); // use this to create images


    imshow("image with grain", I);
// ==新的方法,MATLAB style

    waitKey();

    // ==Tip: No memory freeing is required!
    // All the memory will be automatically released by the Vector<>, Mat and Ptr<> destructor.
    
return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值