Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <opencv2/opencv.hpp>
- /*
- Note: I split off this piece of code into a separate function in order
- to simplify the reasoning about the code.
- This is probably not ideal for performance, but it's too early to worry about that now.
- */
- uint8_t filter_mean_at(cv::Mat const& image, int32_t x, int32_t y, int32_t framing)
- {
- std::cout << "Applying filter at (" << x << "," << y << ")\n";
- int32_t sum{ 0 };
- for (int32_t dx = 0; dx < framing; ++dx) {
- for (int32_t dy = 0; dy < framing; ++dy) {
- int32_t const xx = x + dx;
- int32_t const yy = y + dy;
- std::cout << " * Processing pixel at (" << xx << "," << yy << ")\n";
- sum += image.at<uint8_t>(xx, yy);
- }
- }
- int32_t mean = sum; // NB: Mistake intentionally left here.
- std::cout << " Result = " << mean << " (as uint8_t = "
- << static_cast<int32_t>(static_cast<uint8_t>(mean)) << ")\n";
- return mean;
- }
- cv::Mat filter_mean(cv::Mat image, int window_size)
- {
- cv::Mat result = image.clone();
- int32_t const framing = window_size / 2;
- for (int32_t x = 0; x < image.rows; ++x) {
- for (int32_t y = 0; y < image.cols; ++y) {
- result.at<uint8_t>(x, y) = filter_mean_at(image, x, y, framing);
- }
- }
- return result;
- }
- int main()
- {
- // Just some small simple 4x4 grayscale image to test this on...
- cv::Mat img = (cv::Mat1b(4, 4)
- << 0, 16, 32, 48
- , 64, 64, 64, 64
- , 128, 128, 128, 192
- , 255, 255, 255, 255
- );
- cv::Mat filtered_image = filter_mean(img, 3);
- std::cout << "Result:\n" << filtered_image << '\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement