openCV中的Mat面向对象

本文通过一个简单的示例介绍了如何使用OpenCV进行图像读取、显示及灰度转换等基本操作,并展示了如何获取图像的基本属性。

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

这个部分就是对基础熟悉一下,熟悉一下openCV中各种API的使用额。。。

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

using namespace std;
using namespace cv;

int main(int argc, char** argv)
{
    Mat src;
    src = imread("E:/vcprojects/images/test1.JPG");
    if (src.empty())
    {
        printf("could not load image...");
        return -1;
    }
    namedWindow("input", CV_WINDOW_NORMAL);
    imshow("input", src);

    /*
    Mat dst;
    dst = Mat(src.size(), src.type());
    dst = Scalar(127, 0, 255);
    namedWindow("output", CV_WINDOW_NORMAL);
    imshow("output", dst);
    */

    //Mat dst = src.clone();
    Mat dst;
    //src.copyTo(dst);
    namedWindow("output", CV_WINDOW_NORMAL);

    cvtColor(src, dst, CV_BGR2GRAY);
    printf("input image channals:%d\n", src.channels());
    printf("output image channals:%d\n", dst.channels());

    int cols = dst.cols;
    int rows = dst.rows;
    printf("row:%d cols:%d\n", rows, cols);
    const uchar* firstRow = dst.ptr<uchar>(0);
    printf("first pixel value:%d\n", *firstRow);

    Mat M(3, 3, CV_8UC3, Scalar(0, 0, 255));
    //cout << "M=" << endl << M << endl;

    Mat m1;
    m1.create(src.size(), src.type());
    m1 = Scalar(0, 0, 255);

    Mat cscr;
    Mat kernel = (Mat_<float>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
    filter2D(src, cscr, -1, kernel);

    Mat m2 = Mat::zeros(src.size(), src.type());

    imshow("output", m2);
    waitKey(0);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值