05 儲存圖片

使用OpenCV 改變圖片的顏色成為灰色,並將資料儲存在檔案中,以及顯示在畫面上。

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>
#include <string.h>
#include <fstream>

using namespace cv;

int main( int argc, char** argv )
{
    char* imageName = argv[1];
    Mat image;
    image = imread( imageName, 1 );
    if( argc != 2 || !image.data )
    {
        printf( " No image data \n " );
        return -1;
    }
    Mat gray_image;
    cvtColor( image, gray_image, CV_BGR2GRAY );
   imwrite( "Gray_Image.jpg", gray_image );
    namedWindow( "Gray image", CV_WINDOW_AUTOSIZE );
    imshow( "Gray image", gray_image );
    waitKey(0);
    return 0; 
}

編譯時的指令:
$ g++ main.cpp -o main -I/opt/local/include -L/opt/local/lib -lopencv_core.2.4.10 -lopencv_highgui.2.4.10 -lopencv_imgproc.2.4.10

執行結果:
Screen Shot 2014-11-23 at 1.46.50 PM

OpenCV
CH01 簡介CH02 OpenCV APICH03 繪圖CH04 應用範例CH06 haarcascadesCH06 surf和siftCH07 OCRCH09 FaceCH5 輪廓(findContours、drawContours)