OpenCV读取图片、判断读取是否成功、显示图片的代码(C++代码和Python代码)

OpenCV读取图片、判断读取是否成功、显示图片的代码。
虽然很简单,但是经常用到,懒得每次去写,记在这里,需要时直接来复制粘帖。
先上C++的代码。

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

using namespace cv;

int main()
{
    
    
	Mat image = imread("F:/material/images/2022/2022-06/01_woman.jpg");
	if (image.empty())
	{
    
    
		std::cout << "Error: Could not load image" << std::endl;
		return 0;
	}

	imshow("Source Image", image);

	waitKey();
	return(0);
}

再上Python的代码:

import cv2 as cv
import sys

image = cv.imread('F:/material/images/2022/2022-06/01_woman.jpg')
if image is None:
    print('Error: Could not load image')
    sys.exit()

cv.imshow('Source Image', image)

cv.waitKey(0)
cv.destroyAllWindows()

猜你喜欢

转载自blog.csdn.net/wenhao_ir/article/details/125293928