#include <iostream> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/opencv.hpp> using namespace std; using namespace cv; int main() { Mat src, gray_src; src = imread("/Users/apple/Desktop/test3.png", IMREAD_COLOR); if (src.empty()) { // if (!src.data()) cout << "could not load image..." << endl; return -1; } namedWindow("test opencv setup", CV_WINDOW_AUTOSIZE); imshow("test opencv setup", src); cvtColor(src, gray_src, CV_BGR2GRAY); namedWindow("output", CV_WINDOW_AUTOSIZE); imshow("output", gray_src); int height = gray_src.rows; int width = gray_src.cols; // 单通道 for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { int gray = gray_src.at<uchar>(i, j); gray_src.at<uchar>(i, j) = 255 - gray; } } imshow("invert gray", gray_src); Mat dst; dst.create(src.size(), src.type()); height = src.rows; width = src.cols; int nc = src.channels(); imshow("dst", dst); Mat new_gray; new_gray.create(src.size(), CV_8UC1); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (nc == 1) { int gray = gray_src.at<uchar>(i, j); gray_src.at<uchar>(i, j) = 255 - gray; } else if (nc == 3) { // 用 Vec3b 来读取三通道的像素点 // Vec3b对应的是顺序是 blue green red 的uchar类型数据 // Vec3f对应三通道的float类型 dst.at<Vec3b>(i, j)[0] = 255 - src.at<Vec3b>(i, j)[0]; dst.at<Vec3b>(i, j)[1] = 255 - src.at<Vec3b>(i, j)[1]; dst.at<Vec3b>(i, j)[2] = 255 - src.at<Vec3b>(i, j)[2]; new_gray.at<uchar>(i, j) = min(src.at<Vec3b>(i, j)[0], min(src.at<Vec3b>(i, j)[1], src.at<Vec3b>(i, j)[2])); } } } //bitwise_not(dst, dst); // 位操作 同上 imshow("3 channels invert", dst); // 把CV_8UC1 转换成 CV_32F1 src.convertTo(dst, CV_32FC1); imshow("new gray", new_gray); waitKey(0); return 0; }
对象图像像素的简单操作
猜你喜欢
转载自blog.csdn.net/ringggr_h/article/details/79948135
今日推荐
周排行