需要說明的是在OpenCV3中已經將imread()和imwrite()函數轉移到imgcodecs模塊中,因此讀寫圖像時,需要包含imgcodecs.hpp頭文件,但是highgui.hpp頭文件中已經包含了該頭文件,因此不用再顯式包含了。
#include <iostream>
#include <string>
using namespace std;
// OpenCV includes
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main(int argc, const char** argv)
{
// Read images
Mat color = imread("../images/eating.jpg");
Mat gray = imread("../images/eating.jpg", 0);
// Write images
imwrite("gray.jpg", gray);
// Get same pixel with opencv function
int myRow = color.cols - 1;
int myCol = color.rows - 1;
Vec3b pixel = color.at<Vec3b>(myRow, myCol);
cout << "Pixel Value (B, G, R): ("
<< (int)pixel[0] << ", "
<< (int)pixel[1] << ", "
<< (int)pixel[2] << ")" << endl;
// Show images
imshow("Color Image", color);
imshow("Gray Image", gray);
// Wait for any key
waitKey(0);
return 0;
}
顯示的圖片效果如下:
OpenCV官方教程中文版(For Python) PDF http://www.linuxidc.com/Linux/2015-08/121400.htm
Ubuntu Linux下安裝OpenCV2.4.1所需包 http://www.linuxidc.com/Linux/2012-08/68184.htm
Ubuntu 12.04 安裝 OpenCV2.4.2 http://www.linuxidc.com/Linux/2012-09/70158.htm
CentOS下OpenCV無法讀取視頻文件 http://www.linuxidc.com/Linux/2011-07/39295.htm
Ubuntu 12.04下安裝OpenCV 2.4.5總結 http://www.linuxidc.com/Linux/2013-06/86704.htm
Ubuntu 10.04中安裝OpenCv2.1九步曲 http://www.linuxidc.com/Linux/2010-09/28678.htm
基於QT和OpenCV的人臉識別系統 http://www.linuxidc.com/Linux/2011-11/47806.htm
[翻譯]Ubuntu 14.04, 13.10 下安裝 OpenCV 2.4.9 http://www.linuxidc.com/Linux/2014-12/110045.htm
OpenCV的詳細介紹:請點這裡
OpenCV的下載地址:請點這裡