002 图像色彩空间转换

2019-07-24  本文已影响0人  几时见得清梦

C++

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int artc, char** argv) {
    Mat src = imread("D:/vcprojects/images/test.png");
    if (src.empty()) {
        printf("could not load image...\n");
        return -1;
    }
    namedWindow("input", WINDOW_AUTOSIZE);
    imshow("input", src);

    Mat gray;
    //C++中cvtColor接受三个参数:(输入图像,输出图像,转换前后的色彩空间)
    cvtColor(src, gray, COLOR_BGR2GRAY);
    
    //imwrite接受两个参数:(保存路径,图像内存对象)
    //若保存路径不存在,imwrite不会有任何提示
    imwrite("D:/gray.png", gray);

    waitKey(0);
    return 0;
}

Python

import cv2 as cv

src = cv.imread("D:/vcprojects/images/test.png")
cv.namedWindow("input", cv.WINDOW_AUTOSIZE)
cv.imshow("input", src)

gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
cv.imshow("gray", gray)

cv.waitKey(0)
cv.destroyAllWindows()
上一篇下一篇

猜你喜欢

热点阅读