基本思想:基本原理參考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;
}