OpenCV基本操作

来源:互联网 发布:sql server复制表结构 编辑:程序博客网 时间:2024/04/27 11:21

详细请见opencv_tutorials.pdf,版本2.4.8
显示一副图片

#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <iostream>using namespace cv;using namespace std;int main( int argc, char** argv ){    Mat image;    const char* s="board.jpg";    image = imread(s, IMREAD_COLOR); // Read the file    image = imread(s, CV_LOAD_IMAGE_COLOR); // Read the file    if(! image.data ) // Check for invalid input    {        cout << "Could not open or find the image" << std::endl ;        return -1;    }    namedWindow( "Display window", WINDOW_AUTOSIZE ); // Create a window for display.    imshow( "Display window", image ); // Show our image inside it.    waitKey(0); // Wait for a keystroke in the window    return 0;}

这里写图片描述

生成图片

#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <iostream>//下边两个找不到请参考:http://blog.sina.com.cn/s/blog_488611e30102v6rg.html*/#include<cv.h>#include<highgui.h>using namespace cv;using namespace std;int main(){    char* s = "board.jpg";    Mat image;    image = imread(s, CV_LOAD_IMAGE_COLOR);/*imread has BGR default channel order*/    Mat gray_image;    /*Now we are going to convert our image from BGR to Grayscale format.*/    cvtColor(image, gray_image, CV_BGR2GRAY);    imwrite("./boardGray.jpg", gray_image); //第三个为默认参数    namedWindow(s, CV_WINDOW_AUTOSIZE);    namedWindow("./boardGray.jpg", CV_WINDOW_AUTOSIZE);    imshow(s, image);    imshow("./boardGray.jpg", gray_image);    waitKey(0); //一直等待    return 0;}

这里写图片描述

关于Mat类
C++中为Mat:会自动进行内存管理,而在C中,
C structure called IplImage: manual memory management,所以不推荐,除非是某些不支持C++的嵌入式设备.
Mat is basically a class with two data parts: the matrix header (containing information such as the size of the matrix,the method used for storing, at which address is the matrix stored, and so on) and a pointer to the matrix containing
the pixel values (taking any dimensionality depending on the method chosen for storing) .
注意:the copy operators will only copy the headers and the pointer to the large matrix, not the data itself.
下边是浅拷贝,只是复制了被复制的Mat的头,而真正的图像矩阵只是复制了他的指针.

Mat A, C; // creates just the header partsA = imread(argv[1], CV_LOAD_IMAGE_COLOR); // here we’ll know the method used (allocate matrix)Mat B(A); // Use the copy constructor 复制 调用拷贝构造函数C = A; // Assignment operator  赋值

So if you want to copy the whole Mat, you should operate like this:

Mat F = A.clone();Mat G;A.copyTo(G);

关于图片的存储方式:
• RGB is the most common as our eyes use something similar, our display systems also compose colors using
these.
• The HSV and HLS decompose colors into their hue, saturation and value/luminance components, which is a more natural way for us to describe colors. You might, for example, dismiss the last component, making your algorithm less sensible to the light conditions of the input image.
• YCrCb is used by the popular JPEG image format.
• CIE L*a*b* is a perceptually uniform color space, which comes handy if you need to measure the distance of a given color to another color.

创建Mat:

int main(){       Mat M(2, 2, CV_8UC3, Scalar(0, 0, 225));    /*对于CV_8UC3的解释:    CV_[The number of bits per item][Signed or Unsigned][Type Prefix]C[The channel number]*/    cout<<"M="<<endl<<" "<<M<<endl;    return 0;}

这里写图片描述

对于高于二维的,如:

int main(){    int sz[3] = {2,2,2}; //2*2*2的三维矩阵    Mat L(3,sz, CV_8UC(1), Scalar::all(0));//看相应构造函数    //cout<<"L="<<endl<<" "<<L<<endl;  //错误:三维矩阵在这里是不能可视化的    return 0;}

还可用Creat函数创建
M.create(4,4, CV_8UC(2));
也可使用Matlab风格的代码,这里不多介绍.
矩阵的显示风格可使用format函数进行设置,如:
cout << “R (python) = ” << endl << format(R,”python”) << endl << endl;使用python风格
cout << “R (numpy) = ” << endl << format(R,”numpy” ) << endl << endl;numpy矩阵库的风格
对点,数组也有相应的操作.

如何知道程序执行的时间?
OpenCV offers two simple functions to achieve this getTickCount()
and getTickFrequency(). The first returns the number of ticks of your systems CPU from a certain event (like since
you booted your system). The second returns how many times your CPU emits a tick during a second. So to measure
in seconds the number of time elapsed between two operations is easy as:

double t = (double)getTickCount();// do something ...t = ((double)getTickCount() - t)/getTickFrequency();cout << "Times passed in seconds: " << t << endl;

How the image matrix is stored in the memory?
gray scale image:
这里写图片描述

multichannel images:
RGB color system(BGR instead of RGB)
这里写图片描述

如何遍历图片?
有三种方法:
1.The efficient way
2.The iterator (safe) method(较慢)
3.On-the-fly address calculation with reference returning(不推荐)

使用查找表快速的减小颜色空间:
in image processing it is quite common that you want to replace all of a given image value to some other value OpenCV has a function that makes the modification without the need from you to write the scanning of the image. We use the LUT() function of the core module.

Mat lookUpTable(1, 256, CV_8U);//生成查找表 uchar* p = lookUpTable.data;for( int i = 0; i < 256; ++i)p[i] = table[i];LUT(I, lookUpTable, J);//使用上面的查找表操作我们的图片.I is our input image and J the output one
0 0