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.