imread function + cvtColor () function

The image is loaded (with cv :: imread)

  • imread function is to load an image file into a Mat object that the first parameter represents the image file name
  • The second parameter indicates what type of loaded images, support for common values ​​of three parameters

  1. IMREAD_UNCHANGED (<0) represents the original load, without any change
  2. IMREAD_GRAYSCALE (0) represents the original gray-scale image as come loading
  3. IMREAD_COLOR (> 0) represents the original RGB image is loaded as come

Note: OpenCV support JPG, PNG, TIFF and other common image file formats loaded

Display images (cv :: namedWindos and cv :: imshow)

  • namedWindos OpenCV function is to create a window, it is automatically created by the release of OpenCV with, you do not need to take to destroy it.
  • Common usage namedWindow ( "Window Title", WINDOW_AUTOSIZE)
  • WINDOW_AUTOSIZE automatically the image size, display size according to the window, the window size can not be artificially changed
  • WINDOW_NORMAL, when integrated with the QT will use, it allows you to modify the window size.
  • imshow images up to a specified window according to the window name is displayed, the first parameter is the name of the window, the second parameter is an object Mat

Save the image (cv :: imwrite)

  • Save the image file to the specified directory path
  • Only 8-bit, 16-bit PNG, JPG, Tiff file format and a single-channel or three-channel image BGR can only be saved in this way
  • PNG format saved time can be saved alpha channel pictures
  • You can specify compression parameters

  1. The first argument: will save another name for the image, you can define your own name.
  2. The second argument: you want to save the image name, is already existing image.
  3. Third parameter: the parameter set for the image format generally omitted, do not write

opencv provides cvtColor () function to implement these functions. First look at cvtColor function definition:

C ++: void cvtColor (InputArray the src, outputArray DST, int code, int dstCn = 0);
. 1
of parameters:
InputArray the src: an input image that is to be the original image color space conversion may be Mat class.
OutputArray DST: an output image. i.e. after the color space conversion store images may Mat type
int code:. transcoded or identification, i.e. where it is determined to convert what format images into what format images detail later will
int dstCn = 0:. target image number of channels, if the value is 0, and by the src code decision

code show as below:

//首先是导入相关函数(类似于包java import....)
#include<opencv2\opencv.hpp>
#include<iostream>
#include<math.h>

//域名控件(类似java导入包后,无需在每个对象中添加包名例如:本来要写cv::Mat 添加域名空间后,只需写Mat)
using namespace cv;

//主函数
int main(int argc, char** argv)
{
    //创建一个Mat对象并在里面写入图片数据(图片即存储像素点的矩阵数组)
    Mat srcImage = imread("E:\\VS2015Opencv\\vs2015\\project\\picture\\01.jpg");
    //判断Mat是否有数据。无,打印消息,退出,有就继续(C和java都差不多)
    if (srcImage.empty()) {
        printf("couldn't load image");
        //暂停
        getchar();
        //返回-1,退出
        return -1;
    }
    //显示原图像
    namedWindow("原图像", WINDOW_AUTOSIZE);
    imshow("原图像", srcImage);

    //将图像转换为灰度图,采用CV_前缀
    Mat grayImage;
    cvtColor(srcImage, grayImage, CV_BGR2GRAY);     //将图像转换为灰度图
    namedWindow("灰度图", WINDOW_AUTOSIZE);
    imshow("灰度图", grayImage);

    //将图像转换为HSV,采用COLOR_前缀
    Mat HSVImage;
    cvtColor(srcImage, HSVImage, COLOR_BGR2HSV);    //将图像转换为HSV图
    namedWindow("HSV", WINDOW_AUTOSIZE);
    imshow("HSV", HSVImage);

    waitKey(0);

    return 0;
}

 



Guess you like

Origin www.cnblogs.com/fcfc940503/p/11244607.html