实验4.2
#include <bits/stdc++.h> #include "opencv2/core.hpp" #include "opencv2/imgproc.hpp" #include "opencv2/video.hpp" #include "opencv2/objdetect.hpp" #include "opencv2/imgcodecs.hpp" #include "opencv2/highgui.hpp" #include "opencv2/ml.hpp" #include <ctime> #define inf 2333333333333333 #define N 100010 #define p(a) putchar(a) #define For(i,a,b) for(int i=a;i<=b;++i) typedef unsigned char uchar; //by war //2020.10.8 using namespace std; using namespace cv; int alpha_slider=1,m,cnt; char TrackbarName[50]; Mat image,image0,image1,new_image,boxFilterDst; double Sum[3][2010][2010]; double sigma,alpha,alpha_slider_max=100,eps=1e-7; time_t t0, t1, t2; void in(int &x){ int y=1;char c=getchar();x=0; while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();} while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();} x*=y; } void o(int x){ if(x<0){p('-');x=-x;} if(x>9)o(x/10); p(x%10+'0'); } void on_trackbar(int, void*){ alpha = (double) alpha_slider/(double) 10.0; if(1.0/6.0 - alpha>eps) alpha=1.0/6.0; sigma=alpha; m=6.0*sigma-1; m+=(m%2==0); t0 = clock(); boxFilter(image, boxFilterDst, -1, cv::Size(m, m)); t1 = clock(); copyMakeBorder(image, image0, m/2, m/2, m/2, m/2, BORDER_REFLECT); For(i,0,image0.rows-1)//处理二维前缀和 For(j,0,image0.cols-1) For(k,0,2){ Sum[k][i][j]=(i>0?Sum[k][i-1][j]:0)+(j>0?Sum[k][i][j-1]:0)-((i>0 && j>0)?Sum[k][i-1][j-1]:0)+image0.at<Vec3b>(i,j)[k]; } new_image = Mat::zeros(image.size(), image.type()); For(i,0,image.rows-1) For(j,0,image.cols-1) For(k,0,2){ auto temp = Sum[k][i+m-1][j+m-1]- (i>0?Sum[k][i-1][j+m-1]:0)- (j>0?Sum[k][i+m-1][j-1]:0) + ((i>0 && j>0)?Sum[k][i-1][j-1]:0); new_image.at<Vec3b>(i,j)[k]=temp/(m*m); } t2 = clock(); cout<<"时间差为"<<(double)(t2-2.0*t1+t0)/CLOCKS_PER_SEC<<endl; imshow("Contrast", new_image); } signed main(){ image = imread("/Users/war/Downloads/nk.jpg"); namedWindow("Contrast", 1); sprintf(TrackbarName, "Contrast -> %lf", alpha_slider_max); createTrackbar(TrackbarName, "Contrast", &alpha_slider, alpha_slider_max, on_trackbar); on_trackbar( alpha_slider, 0 ); waitKey(0); return 0; }