Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void gaussianFilter(const char* input_filename, const char* output_filename, int radius)
- {
- std::vector<unsigned char> input_image;
- unsigned int width, height;
- // загрузка входного изображения
- unsigned error = lodepng::decode(input_image, width, height, input_filename);
- if (error)
- std::cout << "decoder error " << error << ": " << lodepng_error_text(error) << std::endl;
- // создание копии входного изображения
- std::vector<unsigned char> output_image(input_image.size());
- // настройка параметров фильтра
- float sigma = radius / 3.0f;
- int size = radius * 2 + 1;
- std::vector<float> kernel(size * size);
- float sum = 0.0f;
- for (int y = -radius; y <= radius; y++)
- {
- for (int x = -radius; x <= radius; x++)
- {
- float value = std::exp(-(x * x + y * y) / (2.0f * sigma * sigma));
- kernel[(y + radius) * size + (x + radius)] = value;
- sum += value;
- }
- }
- for (int i = 0; i < size * size; i++)
- {
- kernel[i] /= sum;
- }
- // фильтрация изображения
- for (unsigned int y = radius; y < height - radius; y++)
- {
- for (unsigned int x = radius; x < width - radius; x++)
- {
- for (unsigned int c = 0; c < 4; c++)
- {
- float accumulator = 0.0f;
- for (int ky = -radius; ky <= radius; ky++)
- {
- for (int kx = -radius; kx <= radius; kx++)
- {
- float value = kernel[(ky + radius) * size + (kx + radius)];
- unsigned int index = ((y + ky) * width + (x + kx)) * 4 + c;
- accumulator += input_image[index] * value;
- }
- }
- unsigned int index = (y * width + x) * 4 + c;
- output_image[index] = (unsigned char)accumulator;
- }
- }
- }
- // сохранение выходного изображения
- error = lodepng::encode(output_filename, output_image, width, height);
- if (error)
- std::cout << "encoder error " << error << ": " << lodepng_error_text(error) << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement