实验10.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) //by war //2020.11.19 using namespace std; using namespace cv; Mat image0,image1,Ix,Iy,Ix_2,Iy_2,IxIy,out,dst_norm, dst_norm_scaled; double alpha = 0.05; 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'); } signed main(){ int ksize = 5; int scale = 1; int delta = 0; int ddepth = CV_16S; image0 = imread("/Users/war/Downloads/nk.jpg",0); image1 = imread("/Users/war/Downloads/nk.jpg"); Sobel(image0, Ix, ddepth, 1, 0, ksize, scale, delta, BORDER_DEFAULT); Sobel(image0, Iy, ddepth, 1, 0, ksize, scale, delta, BORDER_DEFAULT); IxIy = Mat::zeros(Ix.rows, Ix.cols, Ix.type()); For(i,0,Ix.rows-1) For(j,0,Iy.cols-1) For(k,0,2){ IxIy.at<Vec3b>(i,j)[k] = Ix.at<Vec3b>(i,j)[k]*Iy.at<Vec3b>(i,j)[k]; } Ix_2 = Mat::zeros(Ix.rows, Ix.cols, Ix.type()); For(i,0,Ix.rows-1) For(j,0,Iy.cols-1) For(k,0,2){ Ix_2.at<Vec3b>(i,j)[k] = Ix.at<Vec3b>(i,j)[k]*Ix.at<Vec3b>(i,j)[k]; } Iy_2 = Mat::zeros(Ix.rows, Ix.cols, Ix.type()); For(i,0,Ix.rows-1) For(j,0,Iy.cols-1) For(k,0,2){ Iy_2.at<Vec3b>(i,j)[k] = Iy.at<Vec3b>(i,j)[k]*Iy.at<Vec3b>(i,j)[k]; } GaussianBlur(Ix_2,Ix_2,Size(5,5),1); GaussianBlur(Iy_2,Iy_2,Size(5,5),1); GaussianBlur(IxIy,IxIy,Size(5,5),1); out = Mat::zeros(Ix.rows, Ix.cols, Ix.type()); For(i,0,Ix.rows-1) For(j,0,Iy.cols-1) For(k,0,2){ out.at<Vec3b>(i,j)[k] = Ix_2.at<Vec3b>(i,j)[k]*Iy_2.at<Vec3b>(i,j)[k]-IxIy.at<Vec3b>(i,j)[k]*IxIy.at<Vec3b>(i,j)[k]- alpha*((Ix_2.at<Vec3b>(i,j)[k]+Iy_2.at<Vec3b>(i,j)[k])*(Ix_2.at<Vec3b>(i,j)[k]+Iy_2.at<Vec3b>(i,j)[k])); } threshold(out, out, 200, 255, THRESH_BINARY); imshow("out1",out); // For(i,0,Ix.rows-1) // For(j,0,Iy.cols-1) // if((int) out.at<Vec3b>(i,j)[0] > 240 && (int) out.at<Vec3b>(i,j)[1] > 240 && (int) out.at<Vec3b>(i,j)[2] > 240){ // circle( image1, Point(j,i),0.0001, Scalar(0,0,255), 3, LINE_AA ); // } // imshow("out",image1); waitKey(); return 0; }
#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) //by war //2020.11.19 using namespace std; using namespace cv; int w=20; Mat image0,image1,Ix,Iy,Ix_2,Iy_2,IxIy,out,mask; bool vis[2000][2000]; double alpha = 0.05; 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'); } signed main(){ image1 = imread("/Users/war/Downloads/nk.jpg"); cvtColor(image1, image0, COLOR_BGR2GRAY); Sobel(image0, Ix, CV_64FC1, 0, 1, 3); Sobel(image0, Iy, CV_64FC1, 1, 0, 3); IxIy = Ix.mul(Iy); Ix_2 = Ix.mul(Ix); Iy_2 = Iy.mul(Iy); GaussianBlur(Ix_2,Ix_2,Size(3,3),0,0); GaussianBlur(Iy_2,Iy_2,Size(3,3),0,0); GaussianBlur(IxIy,IxIy,Size(3,3),0,0); Mat R(Ix_2.size(), CV_64FC1); R = Ix_2.mul(Iy_2) - IxIy.mul(IxIy) - 0.05 * (Ix_2 + Iy_2).mul(Ix_2 + Iy_2); Point maxLoc; double Max = 0; R = R.reshape(1,0); For(i,0,R.rows-1-w) For(j,0,R.cols-1-w){ Rect r1(i, j, w, w); mask = R(r1); minMaxLoc(mask,NULL,&Max,NULL,&maxLoc); if(maxLoc.x==0 && maxLoc.y==0) vis[i][j]=1; } For(i,0,image1.rows-1) For(j,0,image1.cols-1){ if(vis[i][j]) circle(image1, Point(j,i), 2, Scalar(0, 0, 255), -1); } imshow("hybjs",image1); waitKey(); return 0; }