OpenCV 3.0 入门之图片的读写与显示

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cylj102908/article/details/80615460

再一次回到了图像处理,OpenCV 已经如此的强大了。慢慢学习,然后从入门到放弃…
Life is short, show me the code…
读取图像文件 imread,图像显示 imshow,写图像文件imwrite
如此统一简洁的函数,好用好记。

#include<opencv2\highgui.hpp>
#include<opencv2\core.hpp>
#include<opencv2\opencv.hpp>
#include<string>
#include<iostream>

using namespace cv;
using namespace std;

const char SRC_IMG_WINNAME[] = "src img";
const char GRAY_IMG_WINNAME[] = "gray img";

int main(int argc, char** argv)
{
    string strFileName(".\\sources\\samples\\data\\HappyFish.jpg");//default img file name
    if (argc > 1)
    {
        strFileName = argv[1];
    }

    Mat img;
    img = imread(strFileName, IMREAD_COLOR);//read img file
    if (img.empty())                        //check whether read file ok
    {
        cout<<"failed to open or find img file, check file name!"<< endl;
        return -1;
    }
    //create an opencv window for display image
    //those window managed by opencv
    namedWindow(SRC_IMG_WINNAME, WINDOW_AUTOSIZE);

    imshow(SRC_IMG_WINNAME, img);               //update window content with image

    Mat grayImg;                                //gray image of srcImg
    cvtColor(img, grayImg, CV_BGR2GRAY);        //convert color image to gray image
    namedWindow(GRAY_IMG_WINNAME, WINDOW_AUTOSIZE);
    imshow(GRAY_IMG_WINNAME, grayImg);

    imwrite("grayHappyFish.jpg", grayImg);      //save the gray image on disk
    waitKey(0);                                 // Wait for a keystroke in the window, 0 means wait forever
    return 0;
}

最后添加运行的结果:
图像的读取、显示

展开阅读全文

没有更多推荐了,返回首页