实验2.2
#include <bits/stdc++.h> #define inf 2333333333333333 #define N 1000010 #define p(a) putchar(a) #define For(i,a,b) for(int i=a;i<=b;++i) typedef unsigned char uchar; //by war //2020.9.24 using namespace std; using namespace cv; int T; Mat image0,image1,new_image; 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'); } int dis(int i,int j){ int sum=0; For(k,0,2){ sum+=(image0.at<Vec3b>(i,j)[k]-image1.at<Vec3b>(i,j)[k])*(image0.at<Vec3b>(i,j)[k]-image1.at<Vec3b>(i,j)[k]); } return sum; } signed main(){ in(T); image0 = imread("/Users/war/Desktop/视觉实验/bgs-data/02_bg.jpg"); image1 = imread("/Users/war/Desktop/视觉实验/bgs-data/02.jpg"); new_image = Mat::zeros(image0.size(), image0.type()); For(i,0,image0.rows-1) For(j,0,image0.cols-1){ if(dis(i,j)>T) For(k,0,2) new_image.at<Vec3b>(i, j)[k] = saturate_cast<uchar>(0); else For(k,0,2) new_image.at<Vec3b>(i, j)[k] = saturate_cast<uchar>(255); } imshow("background", new_image); waitKey(0); return 0; }