实验4.1

#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"
#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;
double sigma,alpha,alpha_slider_max=100,eps=1e-7;
double kel[N];
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);
    copyMakeBorder(image, image0, m/2, m/2, m/2, m/2, BORDER_REFLECT);
    //imshow("Contrast", image0);
    image1 = image0;
    double sum=0;
    For(i,1,m){
        kel[i]=exp(1.0/(2.0*sigma*sigma)*(i-(m+1)/2)*(i-(m+1)/2)*(-1.0));
        sum+=kel[i];
    }
    For(i,1,m) kel[i]/=sum;
    For(i,0,image.rows-1)//先处理列方向
        For(j,0,image.cols-1)
            For(k,0,2){
                cnt=0;sum=0;
                For(t,j,j+m-1){
                    sum+=image0.at<Vec3b>(i+m/2,t)[k]*kel[++cnt];
                }
                image1.at<Vec3b>(i+m/2,j+m/2)[k]=sum;
            }
    new_image = Mat::zeros(image.size(), image.type());
    For(i,0,image.rows-1)//再处理行方向
        For(j,0,image.cols-1)
            For(k,0,2){
                cnt=0;sum=0;
                For(t,i,i+m){
                    sum+=image1.at<Vec3b>(t,j+m/2)[k]*kel[++cnt];
                }
                new_image.at<Vec3b>(i,j)[k]=sum;
            }
    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;
}

原文地址:https://www.cnblogs.com/war1111/p/13781660.html