Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h> // Windows.h must be included before GdiPlus.h
- #include <GdiPlus.h>
- #include <stdlib.h> // подключаем qsort
- #include <iostream> // подключаем cout
- #include <chrono>
- #include <vector>
- #include <omp.h>
- #pragma comment(lib, "gdiplus.lib")
- ULONG_PTR m_gdiplusToken; // class member
- #include "imageformats.hpp"
- #include "imageio.hpp"
- using namespace std;
- int hexcolor(ColorBytePixel pixel) {
- return (pixel.r << 16) | (pixel.g << 8) | pixel.b;
- }
- ColorBytePixel rgbcolor(int hex) {
- ColorBytePixel p(((hex) & 0xFF), ((hex >> 8) & 0xFF), ((hex >> 16) & 0xFF));
- return p;
- }
- void medianFilter(const int n, const char* input, const char* output, bool ompEnabled) {
- ColorByteImage image = ImageIO::FileToColorByteImage(input);
- ColorByteImage res(image.Width(), image.Height());
- int offset = (n + 1) / 2;
- auto start = chrono::steady_clock::now();
- #pragma omp parallel shared(offset, res) if (ompEnabled)
- {
- for (int i = 0; i < res.Width(); i++) {
- #pragma omp for schedule(static, 50) nowait
- for (int j = 0; j < res.Height(); j++) {
- int* matrix = new int[n * n];
- for (int y = i - offset, l = 0; y < i + offset - 1; y++) {
- int i_normalize = y < 0 ? 0 : (y > image.Width() - 1 ? image.Width() - 1 : y);
- for (int x = j - offset; x < j + offset - 1; x++, l++) {
- int j_normalize = x < 0 ? 0 : (x > image.Height() - 1 ? image.Height() - 1 : x);
- matrix[l] = hexcolor(image(i_normalize, j_normalize));
- }
- }
- qsort(matrix, n * n, sizeof(int), [](const void* a, const void* b) {
- return (*(int*)a - *(int*)b);
- });
- #pragma omp critical
- res(i, j) = rgbcolor(matrix[(n + 1) / 2]);
- }
- }
- }
- auto end = std::chrono::steady_clock::now();
- auto elapsed = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
- cout << "Size - " << image.Width() << "x" << image.Height() << " px" << endl;
- cout << "Elapsed time is - " << elapsed.count() << " microseconds" << endl;
- ImageIO::ImageToFile(res, output);
- }
- int main(int argc, char* argv[])
- {
- const int n = 11;
- vector<pair<const char*, const char*>> images{
- {"input-1386x903.jpg", "output-1386x903.jpg"},
- {"input-693x452.jpg", "output-693x452.jpg"},
- {"input-347x226.jpg", "output-347x226.jpg"},
- {"input-174x113.jpg", "output-174x113.jpg"}
- };
- // InitInstance
- Gdiplus::GdiplusStartupInput gdiplusStartupInput;
- Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
- // Количество потоков
- omp_set_num_threads(5);
- for (auto image : images) {
- medianFilter(n, image.first, image.second, true);
- }
- // ExitInstance
- Gdiplus::GdiplusShutdown(m_gdiplusToken);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement