基本思想:基本原理参考OpenCV手册,此处只记录一下如何使用;
#include<opencv2/opencv.hpp>
#include<iostream>
#include<vector>
using namespace std;
using namespace cv;
int main() {
Mat img = imread("/home/ubuntu/Downloads/a.jpeg");
cout << img.cols << " " << img.rows << endl;
Mat result;
cvtColor(img, result, CV_BGR2GRAY);
cout << result.channels() << endl;
imwrite("gray.jpg",result);
Mat gray0=result.clone();
for (int i = 0; i < gray0.rows; i++) {
for (int j = 0; j < gray0.cols; j++) {
gray0.ptr<uchar>(i, j)[0] = 255 - gray0.ptr<uchar>(i, j)[0];
}
}
imwrite("subtract.jpg", gray0);
//减法操作 似乎 相差帧的背景变化很少的时候 有用
double xmin;
double xmax;
Mat gray1=result.clone();
minMaxLoc(gray1,&xmin,&xmax,0,0);
//将原来的空间的像素点拉伸到0-255空间中, 使用的比例计算(z-0)/(灰度像素-xmin)=(255-0)/(xmax-xmin)
for (int i = 0; i < gray1.rows; i++) {
for (int j = 0; j < gray1.cols; j++) {
gray1.ptr<uchar>(i, j)[0]=(255-0)/(xmax-xmin)*(gray1.ptr<uchar>(i, j)[0]-xmin);
}
}
imwrite("explosion.jpg",gray1);
// 像素压缩在 [50:150]区间范围内
Mat gray2=result.clone();
minMaxLoc(gray2,&xmin,&xmax,0,0);
//将原来的空间的像素点压缩在50-150空间中,
for (int i = 0; i < gray2.rows; i++) {
for (int j = 0; j < gray2.cols; j++) {
gray2.ptr<uchar>(i, j)[0]=(150-50)/(xmax-xmin)*(gray2.ptr<uchar>(i, j)[0]-xmin);
}
}
imwrite("rar.jpg",gray2);
// 像素压缩在 [50:150]区间范围内
Mat gray3=result.clone();
for (int i = 0; i < gray2.rows; i++) {
for (int j = 0; j < gray2.cols; j++) {
gray3.ptr<uchar>(i, j)[0]=pow((gray3.ptr<uchar>(i, j)[0])/255.0,5.0)*255;
}
}
imshow("gray.jpg",result);
imshow("gama.jpg",gray3);
waitKey(0);
return 0;
}
标签:xmin,17,int,++,OpenCV,灰度,result,gray2,ptr From: https://blog.51cto.com/u_12504263/5718737