本文参考Opencv官方文档建立一个简单的读入与显示图像文件,开启C++编程第一步:
//该头文件模式下,兼容C和C++
#include "cv.h"
#include "highgui.h"
using namespace cv;
using namespace std;
int main(int argc,char *argv[])
{
Mat src;
src=imread("lena.jpg",CV_LOAD_IMAGE_COLOR);
if (!src.data)
{
cout<<"Cound not read the file!"<<endl;
system("pause");
return -1;
}
namedWindow("src");
imshow("src",src);
waitKey(0);
return 0;
}
其中Mat内存是动态分配的;
Mat img=imread(filename); 默认是默认是三通道图像,灰度图像使用Mat img=imread(filename,0);
• CV_LOAD_IMAGE_UNCHANGED (<0) loads the image as is (including the alpha channel if present)
• CV_LOAD_IMAGE_GRAYSCALE ( 0) loads the image as an intensity one
• CV_LOAD_IMAGE_COLOR (>0) loads the image in the RGB format