码迷,mamicode.com
首页 > 编程语言 > 详细

opencv c++:图像的读取,显示,写入,颜色空间转换

时间:2020-01-28 17:19:36      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:main   printf   nes   image   art   img   com   read   windows   

读取、显示

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");
    Mat src = imread("D:/vcprojects/images/test.png", IMREAD_GRAYSCALE);

    if (src.empty()) {
        printf("could not load image...\n");
        return -1;
    }
    namedWindow("input", WINDOW_AUTOSIZE);
    imshow("input", src);

    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)
cv.waitKey(0)
cv.destroyAllWindows()

色彩空间转换

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;
    cvtColor(src, gray, COLOR_BGR2GRAY);
    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()

写入文件

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;
    cvtColor(src, gray, COLOR_BGR2GRAY);
    imwrite("D:/gray.png", gray);

    waitKey(0);
    return 0;
}

创建一个图像

Mat基本结构

技术图片

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", CV_WINDOW_AUTOSIZE);
    imshow("input", src);

    // 创建方法 - 克隆
    Mat m1 = src.clone();

    // 复制
    Mat m2;
    src.copyTo(m2);

    // 赋值法
    Mat m3 = src;

    // 创建空白图像
    Mat m4 = Mat::zeros(src.size(), src.type());
    Mat m5 = Mat::zeros(Size(512, 512), CV_8UC3);
    Mat m6 = Mat::ones(Size(512, 512), CV_8UC3);

    Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0,
        -1, 5, -1,
        0, -1, 0);

    waitKey(0);
    return 0;
}

Python

import cv2 as cv
import numpy as np

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

# 克隆图像
m1 = np.copy(src)

# 赋值
m2 = src
src[100:200,200:300,:] = 255
cv.imshow("m2",m2)

m3 = np.zeros(src.shape, src.dtype)
cv.imshow("m3", m3)

m4 = np.zeros([512,512], np.uint8)
# m4[:,:] =127 try to give gray value 127
cv.imshow("m4", m4)

m5 = np.ones(shape=[512,512,3], dtype=np.uint8)
m5[:,:,0] = 255
cv.imshow("m5", m5)

cv.waitKey(0)
cv.destroyAllWindows()

opencv c++:图像的读取,显示,写入,颜色空间转换

标签:main   printf   nes   image   art   img   com   read   windows   

原文地址:https://www.cnblogs.com/wbyixx/p/12238403.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!