OpenCV C++ Mat读取和显示图片 imread () 函数读入图片

本文介绍了如何在C++中使用OpenCV库函数imread读取图片文件,并通过imshow显示图片。代码示例展示了错误处理和窗口创建的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Mat img = imread(fileName);

地址\单或\\双都可以

是否加载成功

    if(image.empty()) //Check for invalid input
    {
        cout << "找不到图片请检查路径\n";
        return-1;
    }

是否有数据

    if (!image.data)
    {
        cout << "加载图片失败" << endl;
        //cout << "找不到图片请检查路径\n";
        return 0;
        //return-1;
    }
// opencvtest.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include<opencv2/highgui.hpp>
#include<opencv2/core.hpp>
#include<opencv2/imgcodecs.hpp>
using namespace cv;
using namespace std;

int main()
{
    Mat image;
    image = imread("D:\8968.jpg"); //read the file 地址也可以用D:\\8969.jpg
    //if(image.empty()) //Check for invalid input
    if (!image.data)
    {
        cout << "加载图片失败" << endl;
        //cout << "找不到图片请检查路径\n";
        return 0;
        //return-1;
    }
    namedWindow("Display window", WINDOW_AUTOSIZE); //Create a window for display.
    imshow("Display window", image); //Show our image inside it
    waitKey(0); //Wait for a keystroke in the window
    //std::cout << "Hello World!\n";
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值