OpenCV笔记:图像简记

1.图像读写

功能:加载,显示,保存

接口定义:
Mat imread( const string& filename, int flags=1 );
void imshow(const string& winname, InputArray mat);
bool imwrite( const string& filename, InputArray img,
              const vector<int>& params=vector<int>());
Mat imdecode( InputArray buf, int flags );
Mat imdecode( InputArray buf, int flags, Mat* dst );
bool imencode( const string& ext, InputArray img,
               CV_OUT vector<uchar>& buf,
               const vector<int>& params=vector<int>());
测试代码:
Mat lena_bmp = imread("../lena.bmp");
imshow("Image Show", lena_bmp);
try {
    imwrite("../test.png", lena_bmp, params);
}
catch (std::runtime_error &err) {
    fprintf(stderr, "error: %s\n", err.what());
    return;
}

2.像素遍历

at方法:

测试代码:
Mat test_bmp(nrows, ncols, CV_8UC4);
for (int i = 0; i < test_bmp.rows; i++)
    for (int j = 0; j < test_bmp.cols; j++) {
        Vec4b &rgba = test_bmp.at<Vec4b>(i, j);
        rgba[0] = UCHAR_MAX;
        rgba[1] = rgba[2] = rgba[3] = saturate_cast<uchar>(pixel);
    }

指针

测试代码:
Mat test_bmp(nrows, ncols, CV_8UC1);
for (int i = 0; i < test_bmp.rows; ++i) {
    uchar *p = test_bmp.ptr<uchar>(i);
    for (int j = 0; j < test_bmp; ++j) {
        p[j] = saturate_cast<uchar>(pixel);
    }
}

Mat_<\T>

测试代码:
Mat test_bmp(nrows, ncols, CV_8UC3);
Mat_<Vec3b> test_mat = test_bmp;
for (int i = 0; i < test_bmp.rows; ++i) {
    for (int j = 0; j < test_bmp.cols; ++j) {
        test_mat(i, j)[0] = 255;
        test_mat(i, j)[1] = 255;
        test_mat(i, j)[2] = 255;
    }
}

迭代器

测试代码:
Mat test_bmp(nrows, ncols, CV_8UC3);
MatIterator_<Vec3b> it = test_bmp.begin<Vec3b>();
while (it != test_bmp.end<Vec3b>()) {
    (*it)[0] = (*it)[1] = (*it)[2] = 0;
    ++it;
}

3.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值