博客
关于我
opencv4-图像操作
阅读量:791 次
发布时间:2023-02-23

本文共 1223 字,大约阅读时间需要 4 分钟。

Mat src = imread("E:\vs2015\opencvstudy\2.jpg", 1);

if (!src.data) {
cout << "could not load image!" << endl;
return -1;
}

// 灰度化处理

Mat gray_src;
cvtColor(src, gray_src, CV_BGR2GRAY);

// 取反处理

Mat gray_src_invert = Mat::zeros(gray_src.size(), gray_src.type());
for (int row = 0; row < gray_src.rows; row++) {
for (int col = 0; col < gray_src.cols; col++) {
int gray_pixel = gray_src.at
(row, col);
gray_src_invert.at
(row, col) = 255 - gray_pixel;
}
}

// 3通道取反处理

Mat dst, dst2;
dst.create(src.size(), src.type());
dst2.create(src.size(), src.type());

for (int row = 0; row < src.rows; row++) {

for (int col = 0; col < src.cols; col++) {
if (src.channels() == 1) {
int gray_pixel = gray_src.at
(row, col);
gray_src_invert.at
(row, col) = 255 - gray_pixel;
} else if (src.channels() == 3) {
Vec3b pixel = src.at
(row, col);
dst.at
(row, col) = {255 - pixel[0], 255 - pixel[1], 255 - pixel[2]};
dst2.at
(row, col) = {pixel[2], max(pixel[1], pixel[0])};
}
}
}

// 位操作取反

Mat bitwise_not_dst;
bitwise_not(src, bitwise_not_dst);

// ROI截取

Rect r(300, 300, 1000, 400);
Mat smallImg = gray_src(r);

cout << "gray_image height:" << gray_src.rows << endl;

cout << "gray_image width:" << gray_src.cols << endl;

imshow("ROI截取图像", smallImg);

转载地址:http://mmsfk.baihongyu.com/

你可能感兴趣的文章