OpenCV读取图片、判断读取是否成功、显示图片的代码。
虽然很简单,但是经常用到,懒得每次去写,记在这里,需要时直接来复制粘帖。
先上C++的代码。
- #include <opencv2/opencv.hpp>
- #include <iostream>
- using namespace cv;
- int main()
- {
- Mat src_image = imread("F:/material/images/2022/2022-06/01_woman.jpg");
- if (src_image.empty())
- {
- std::cout << "Error: Could not load image" << std::endl;
- return 0;
- }
- imshow("Source Image", src_image);
- waitKey();
- return(0);
- }
复制代码 再上Python的代码:
- import cv2 as cv
- import sys
- src_image = cv.imread('F:/material/images/2022/2022-06/01_woman.jpg')
- if src_image is None:
- print('Error: Could not load image')
- sys.exit()
- cv.imshow('Source Image', src_image)
- cv.waitKey(0)
- cv.destroyAllWindows()
复制代码 |