03 改變圖片的顏色

使用OpenCV 2 改變圖片的顏色成為灰色
更多顏色的調整, 請在 這裡http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html

#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 )
{
<%%KEEPWHITESPACE%%>    char* imageName = argv[1];

<%%KEEPWHITESPACE%%>    Mat image;
<%%KEEPWHITESPACE%%>    image = imread( imageName, 1 );

<%%KEEPWHITESPACE%%>    if( argc != 2 || !image.data )
<%%KEEPWHITESPACE%%>    {
<%%KEEPWHITESPACE%%>        printf( " No image data \n " );
<%%KEEPWHITESPACE%%>        return -1;
<%%KEEPWHITESPACE%%>    }

<%%KEEPWHITESPACE%%>    Mat gray_image; //
<%%KEEPWHITESPACE%%>    cvtColor( image, gray_image, CV_BGR2GRAY );
<%%KEEPWHITESPACE%%>    namedWindow( "Gray image", CV_WINDOW_AUTOSIZE );
<%%KEEPWHITESPACE%%>    imshow( "Gray image", gray_image );
<%%KEEPWHITESPACE%%>    waitKey(0);
<%%KEEPWHITESPACE%%>    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.21.18 PM
 

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


using namespace cv ;
using namespace std ;
int main() {
    cv::Mat image = cv::imread("/Users/powenko/Desktop/1.png");
    if(image.data!=NULL){
        cv::namedWindow("Image") ; cv::imshow("Image", image) ;
        
        Mat gray_image;
        cv::cvtColor( image, gray_image,CV_BGR2GRAY);
                 //CV_BGR2GRAY );
        
        cv::imshow("CV_BGR2GRAY", gray_image) ;
        cv::waitKey() ;
    }
    return 0;
}

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