Opencv_tutorials--core MODULE. The CORE functionality--interoperability with OpenCV 1

Source: Internet
Author: User
Tags color gamut

2.10and theOpenCVInteroperability ofGoal

For OpenCV 's development team, it is important to continuously upgrade the OpenCV Library. We keep thinking about the ways you can reduce your work process while maintaining the flexibility of your library. The new C + + interface is what we developed for this purpose. However, backward compatibility is still important. We don't want to break the code that you wrote down with the earlier OpenCV Library. Therefore, we have added some functions to protect this matter. In the following tutorial, you will learn:

1, compared to the same use of the first version of the OpenCV Library, the second version of what has changed.

2, how to add some Gaussian noise in the image.

3, what is the lookup table, why use them.

Overview

Your first step in making a change is to learn some new data structures about the image:mat-the Basic image Container here instead of the old Cvmat and Impimage. Using the new equation looks much easier. You just have to remember a few new things.

OpenCV 2 accepts adaptations. All functions are no longer crammed into a single library. We have a number of modules, each of which includes some tasks related to data structures and functions. With this method, you don't have to call this library if you only need a portion of the OpenCV. This means that you need to introduce the header files that you need. For example:

#Include <opencv2/core/core.hpp>

#include <opencv3/imgproc/imgproc.hpp>

#include <opencv2/highgui/highgui.hpp>

Everything related to OpenCV is in the north to the CV namespace (namespace) to prevent name collisions with other libraries ' data structures and functions. So you need to Add a CV to anything from the OpenCV library: keyword or directly add this line of code directly containing all:

using namespace CV;

Because the function is already in the namespace, there is no need to add CV prefix. All the new compatible functions do not have this, they follow the hump naming method. This means that the first letter of the name is a lowercase letter (unless it is itself a name (names, etc.) like canny copymakeborder

Now, remember that you need to connect all the modules you use to your application. In windows environment, you need to use the dll added again into all binary paths. More in-depth information if you use windows how to build applications with opencv  inside the microsoft visual studio Span style= "font-family: ' Times New Roman ';" >linux using  opencv with eclipse ( CDT)

Now, to convert the Mat object, you can use the iplimage or cvmat operator. However , the pointers normally used in the C interface no longer exist here. in the C + + interface, we use the Mat object more. This object can be converted to iplimage and cvmat types at no cost, simply by assigning a value . For example:

Mat I;

Iplimager pi=i;

Cvmat mi=i;

Now if you want to switch to pointers, it's a bit of a hassle. The compiler no longer automatically determines what you want but you need to explicitly specify your purpose. That is, call the iplimage and cvmat operators and get their pointers. For the pointer to, we use the & tag:

Mat I;

Iplimage *pi=&i,operator iplimage ();

Cvmat *mi= &i.opeartor Cvmat ();

The biggest disadvantage of the C interface is that you make your own memory management. You need to know when you can refactor a memory that is no longer in use and make sure you do it after the program runs, or you may have a memory leak. To solve This problem in OpenCV, a series of smart pointers are introduced here. When the object is no longer in use, it is automatically destroyed. Use this specially-declared pointer Ptr:

Ptr<iplimage> pii=&i.operator iplimage ();

The transformation from the data structure of C to the Mat is done by passing them into the internal constructor. For example:

Mat K (PiL), L;

L=mat (PI);

About Learning

Now you have basically completed the example of mixing C interfaces and C + + interfaces. You will also find the source code in the OpenCV instance folder. To help you see the difference: one is a mix of C and C + + and the other is pure C + +. If you define demo_mixed_api_use You will no longer use the first one. The program separates the color plane, then makes some changes on it, and finally merges them together.

#include <stdio.h>

#include <iostream>

#include <opencv2/core/core.hpp>

#include <opencv2/imgproc/imgproc.hpp>

#include <opencv2/highgui/highgui.hpp>

Using namespace CV;

Using namespace Std;

#define Demo_mixed_api_use

int main (int argc,char**argv)

{

Const char* imagename=argc>1?argv[1]: "Lena.jpg";
}

#ifdef Demo_mixed_api_use

Ptr<iplimage> ipli=cvloadimage (imagename);

If (Ipli.empty ())

{

cerr<< "An not load image" <<imagename<<endl;

Return-1;
}

Mat I (IplI);

#else

Mat I=imread (imagename);

If (I.empty ())

{

cerr<< "Can not load image" <<imagename<<endl;

Return-1;
}

#endif

Here you can see that using the new structure, we no longer have pointer problems, although old functions are used, but eventually the results are converted to Mat Objects

Mat I_YUV;

Cvtcolor (I,I_YUV,COLOR_BGR2YCRCB);

Vector<mat> planes;

Split (I_yuv,planes);

Because we want to mix the luminance components in the image, we first convert the default RGB Color System to the YUV color gamut and then separate it into different color planes. Here's the program separation: In the first example use three ( [] operator in C , iterator, read independent element) One of the main image traversal algorithms to process each plane. The second change is that we add Gaussian noise to the image and fuse their channels according to some equations.

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_case<uchar> (v*v/255);
}

for (int y=0;y<i_yuv.rows;y++)

{

uchar* uptr=planes[1].ptr<ujchar> (y);

for (int x=0;x<i_yuv.cols;x++)

{

Uptr[x]=saturate_cast<uchar> ((UPTR[X]-128/2) +128);

Uchar&vxy=planes[2].at<uchar> (Y,X);

Vxy=saturate_cast<uchar> ((Vxy-128)/2+128);
}
}

Here you can see that we traverse the pixels in the entire image in three ways (iterators,C pointers, and independent element entry methods). You can get more in-depth descriptions from how to scan images,lookup tables and time measurement with OpenCV tutorials. It is easy to convert old function names. Simply Remove the CV prefix and use the new Mat data structure. Here is the use case for a function that increases the weight:

Mat Noisyi (I.size (),. CV_8U);

Randn (Noisyi,scalar::all (+), Scalar::all (20));

Gaussianblur (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

Iplimage Cv_planes_0=planes[0],cv_noise=noisyi;

Cvaddweighted (&CV_LANES_0,CONTRAST_GAIN,&CV_NOISE,1,-128+BRIGHTNESS_GAIN,&CV_PLANES_0);

#else

Addweighted (Planes[0],contrast_gain,noisyi,1,-128+brightness_gain,planes[0]);

#endif

Const double color_scale=0.5;

Planes[1].convertto (Planes[1],planes[1].type (), color_scale,128* (1-color_scale));

Planes[2]=mat_<uchar> (planes[2]*color_scale+128* (1-color_scale));

Planes[0]=planes[0].mul (planes[0],1./255);

You may see that the type of the planar variable is Mat. However, converting from Mat to iplimage is easy and can be done automatically using an assignment operation.

Merge (PLANES,I_YUV);

Cvcolor (I_YUV,I,CV_YCRCB2BGR);

Namedwindow ("Image with Grain", window_autosize);

#ifdef Demo_mixed_api_use

Cvshowimage ("Image with grain". IplI);

#else

Imshow ("Image with Grain", I);

The new imshow Highgui function accepts Two kinds of data structures, Mat and iplimage. Compile and run the program, if the first image below is your input image, you may go to the second or third chapter of the image as output.

Opencv_tutorials--core MODULE. The CORE functionality--interoperability with OpenCV 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.