1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| #include<iostream> #include<opencv2/opencv.hpp> #include"Salt.h"
using namespace cv; using namespace std;
void myfilter(int filter_size,Mat& img_input,Mat& img_output); int mysort(Mat& sortarray,int filter_size);
int main(){ Mat img,img_gray,img_output,img_output2; img = imread("/home/v/home.png"); if (img.empty()){ cout<<"could not load image"<<endl; return -1; } imshow("img",img); cvtColor(img,img_gray,COLOR_BGR2GRAY); Salt(img_gray,1000); imshow("img gray",img_gray);
int a=7; medianBlur(img_gray,img_output,a); imshow("medianBlur",img_output);
myfilter(a,img_gray,img_output2); imshow("myfilter",img_output2);
waitKey(0); return 0; }
void myfilter(int filter_size,Mat& img_input,Mat& img_output){ img_output = img_input.clone(); Mat sortarray(1,filter_size*filter_size,CV_8U); int k = (filter_size-1)/2;
for (int i=k;i<img_input.rows-k;i++){ for (int j=k;j<img_input.cols-k;j++){ int h = 0; for (int m=-k;m<=k;m++){ for (int n=-k;n<=k;n++){ sortarray.at<uchar>(h) = img_input.at<uchar>(i+m,j+n); h++; } } img_output.at<uchar>(i,j) = mysort(sortarray,filter_size*filter_size); } } }
int mysort(Mat& sortarray,int filter_size){
int exchange; int m; for (int i=1;i<=filter_size-1;i++){ for (int j=1;j<filter_size-1;j++){ if (sortarray.at<uchar>(j-1) > sortarray.at<uchar>(j)){ exchange = sortarray.at<uchar>(j-1); sortarray.at<uchar>(j-1) = sortarray.at<uchar>(j); sortarray.at<uchar>(j) = exchange; } } } return sortarray.at<uchar>((filter_size+1)/2); }
|