C++
#include <opencv2/opencv.hpp> //包含头文件
#include <iostream>
using namespace cv; //声明opencv命名空间
using namespace std; //C++的一些容器
int main(int artc, char** argv) { //定义main函数
// Mat src = imread("D:/vcprojects/images/test.png"); //读取为BGR三通道彩色图像
Mat src = imread("D:/vcprojects/images/test.png", IMREAD_GRAYSCALE); //读取为灰度图像
if (src.empty()) { //检查图像是否为空,若为空,则返回-1,程序结束。
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++中智能指针会自动回收内存,所以不用销毁窗口。
网友评论