知识点
(1)读取指定路径下的图像
(2)显示读取的图像
相关API(C++版本)
(1)图像载入:imread()
Mat imread (const string& filename, intflags=1)
第一个参数:const string&类型的filename(文件名),填入图片的路径名。Win下,支持的文件类型有.bmp,.dib,.jpg,.png等;
第二个参数,int类型的flags,为载入标识,指定一个加载图像的颜色类型。
对标识符相应的解释
标识符 | 等价取值 | 意义 |
CV_LOAD_IMAGE_COLOR | 1 | 转换到彩色图像在返回,默认为1 |
CV_LOAD_IMAGE_GRAYSCALE | 0 | 将图像转为灰度图像再返回 |
CV_LOAD_IMAGE_ANYDEPTH | 2 | 等价取值为2,如果图像的深度为16位或者32位,就返回相应的图像深度,否则,就转换为8位再返回 |
(2)显示图像:imshow()
void imshow (const string& winname, InputArray mat);
第一个参数,const string&类型的winname,填写需要显示的窗口的标识符;
第二个参数,InputArray类型的mat,需要显示的图像;
C++示例程序
#include<opencv2\opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main()
{
Mat src = imread("D:/opencv3.1.0/images/Messi.jpg");//读取图像
if (src.empty()) //如果src为空
{
cout << "image loading error" << endl;
return -1;
}
imshow("input image", src);//输出图像
waitKey(0);//表示阻塞等待用户键盘输入,用户按键盘任意键就会停止阻塞,继续执行
return 0;
}
运行结果:
扫描二维码关注公众号,回复:
4603594 查看本文章
python示例程序
import cv2 as cv
src = cv.imread("D:/opencv3.1.0/images/Messi.jpg") #读取图像
cv.imshow("input image",src) #显示图像
cv.waitKey(0)
cv.destroyAllWindows()#避免跨语言导致的内存泄漏
运行结果:
★finished by songpl, 2018.12.2