Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- double __timp;
- void startTimer()
- {
- __timp = (double)getTickCount(); // Gฤseศte timpul curent [ms]
- }
- void stopTimer(const char* text = "")
- {
- __timp = ((double)getTickCount() - __timp) / getTickFrequency();
- printf(text);
- printf("Time = %.3f [ms]\n", __timp * 1000);
- }
- Mat medianFilterRGB(Mat src, const int size = 1)
- {
- Mat fin = src.clone();
- //src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- int height = src.rows;
- int width = src.cols;
- for(int y = 0; y < height; y++)
- for (int x = 0; x < width; x++)
- {
- vector<int> neighboursR, neighboursG, neighboursB;
- for(int dy = -size; dy <= size; dy++)
- for (int dx = -size; dx <= size; dx++)
- {
- int nx = x + dx;
- int ny = y + dy;
- if (nx < 0 || nx >= width) continue;
- if (ny < 0 || ny >= height) continue;
- neighboursR.push_back(src.at<Vec3b>(ny, nx)[2]);
- neighboursG.push_back(src.at<Vec3b>(ny, nx)[1]);
- neighboursB.push_back(src.at<Vec3b>(ny, nx)[0]);
- }
- sort(neighboursR.begin(), neighboursR.end());
- sort(neighboursG.begin(), neighboursG.end());
- sort(neighboursB.begin(), neighboursB.end());
- fin.at<Vec3b>(y, x) = Vec3b(neighboursB[neighboursB.size() / 2],
- neighboursG[neighboursG.size() / 2],
- neighboursR[neighboursR.size() / 2]);
- }
- return fin;
- }
- void filtrariMed()
- {
- char fname[MAX_PATH];
- while (openFileDlg(fname))
- {
- Mat src;
- src = imread(fname, CV_LOAD_IMAGE_COLOR);
- imshow("Original", src);
- int height = src.rows;
- int width = src.cols;
- startTimer();
- Mat res = medianFilterRGB(src, 2);
- stopTimer("Median");
- imshow("Median", res);
- waitKey(10);
- waitKey();
- destroyAllWindows();
- }
- }
- Mat gaussian2DFilter(Mat src, int size = 6)
- {
- Mat res = src.clone();
- double sigma = size/3.0;
- double norm = 1 / (2 * PI * sigma * sigma) * exp(-(0) / (2 * sigma * sigma));
- Mat gaussianFilter = Mat(size, size, CV_16SC1);
- for (int y = 0; y < size; y++)
- for (int x = 0; x < size; x++)
- {
- int dx = x - size / 2;
- int dy = y - size / 2;
- double val = 1 / (2 * PI * sigma * sigma) * exp(-(dx * dx + dy * dy) / (2 * sigma * sigma));
- gaussianFilter.at<short>(y, x) = (short)(255*val/norm);
- }
- convolve(res, src, gaussianFilter);
- return res;
- }
- Mat gaussian1D2Filter(Mat src, int size = 6)
- {
- Mat resp = src.clone();
- double sigma = size / 3.0;
- double norm = 1 / (2 * PI * sigma * sigma) * exp(-(0) / (2 * sigma * sigma));
- Mat gaussianFilterx = Mat(1, size, CV_16SC1);
- for (int x = 0; x < size; x++)
- {
- int dx = x - size / 2;
- int dy = 0;
- double val = 1 / (sqrt(2 * PI) * sigma) * exp(-(dx * dx + dy * dy) / (2 * sigma * sigma));
- gaussianFilterx.at<short>(0, x) = (short)(255 * val / norm);
- }
- convolve(resp, src, gaussianFilterx);
- Mat res = src.clone();
- Mat gaussianFiltery = Mat(size, 1, CV_16SC1);
- for (int y = 0; y < size; y++)
- {
- int dx = 0;
- int dy = y - size / 2;
- double val = 1 / (sqrt(2 * PI) * sigma * sigma) * exp(-(dx * dx + dy * dy) / (2 * sigma * sigma));
- gaussianFiltery.at<short>(y, 0) = (short)(255 * val / norm);
- }
- convolve(res, resp, gaussianFiltery);
- return res;
- }
- void filtrariGauss()
- {
- char fname[MAX_PATH];
- while (openFileDlg(fname))
- {
- Mat src;
- src = imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
- imshow("Original", src);
- int height = src.rows;
- int width = src.cols;
- int size = 7;
- //cin >> size;
- startTimer();
- Mat res2D = gaussian2DFilter(src, size);
- stopTimer("2D Gauss");
- imshow("2D Gauss", res2D);
- waitKey(10);
- startTimer();
- Mat res1D = gaussian1D2Filter(src, size);
- stopTimer("1D Gauss");
- imshow("1D Gauss", res1D);
- waitKey();
- destroyAllWindows();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement