OpenCV C++版图像去雾技术对比

发布时间:2024-08-18 17:49:33 作者:小樊
来源:亿速云 阅读:90

图像去雾是一种重要的图像增强技术,可以有效地提高图像的清晰度和细节。在OpenCV中,有多种图像去雾算法可以实现,比较常用的有Dark Channel Prior和Fast Visibility Restoration等算法。

下面我们将分别使用Dark Channel Prior和Fast Visibility Restoration算法对同一张含有雾的图像进行处理,然后进行对比。

首先是Dark Channel Prior算法的实现代码:

#include <opencv2/opencv.hpp>

using namespace cv;

Mat dehazeDarkChannelPrior(Mat& src, double w = 0.95, int r = 15) {
    Mat src_gray;
    cvtColor(src, src_gray, COLOR_BGR2GRAY);

    Mat dark_channel = Mat::zeros(src.size(), CV_8UC1);
    for (int i = 0; i < src.rows; ++i) {
        for (int j = 0; j < src.cols; ++j) {
            Vec3b pixel = src.at<Vec3b>(i, j);
            dark_channel.at<uchar>(i, j) = std::min({ pixel[0], pixel[1], pixel[2] });
        }
    }

    Mat dark_channel_blur;
    boxFilter(dark_channel, dark_channel_blur, CV_8UC1, Size(r, r));

    Mat A = Mat::zeros(src.size(), CV_8UC1);
    for (int i = 0; i < src.rows; ++i) {
        for (int j = 0; j < src.cols; ++j) {
            A.at<uchar>(i, j) = dark_channel_blur.at<uchar>(i, j);
        }
    }

    Mat transmission = Mat::zeros(src.size(), CV_64FC1);
    for (int i = 0; i < src.rows; ++i) {
        for (int j = 0; j < src.cols; ++j) {
            transmission.at<double>(i, j) = 1.0 - w * dark_channel.at<uchar>(i, j) / A.at<uchar>(i, j);
        }
    }

    Mat transmission_blur;
    boxFilter(transmission, transmission_blur, CV_64FC1, Size(r, r));

    Mat dehazed = Mat::zeros(src.size(), CV_8UC3);
    for (int i = 0; i < src.rows; ++i) {
        for (int j = 0; j < src.cols; ++j) {
            Vec3b pixel = src.at<Vec3b>(i, j);
            dehazed.at<Vec3b>(i, j) = pixel - (pixel - A.at<uchar>(i, j)) / transmission_blur.at<double>(i, j);
        }
    }

    return dehazed;
}

int main() {
    Mat src = imread("foggy_image.jpg");

    Mat dehazed = dehazeDarkChannelPrior(src);

    imshow("Original", src);
    imshow("Dehazed Dark Channel Prior", dehazed);
    waitKey();

    return 0;
}

然后是Fast Visibility Restoration算法的实现代码:

#include <opencv2/opencv.hpp>

using namespace cv;

Mat dehazeFastVisibilityRestoration(Mat& src, double beta = 1.0, double omega = 0.95, int r = 60) {
    Mat src_gray;
    cvtColor(src, src_gray, COLOR_BGR2GRAY);

    Mat dark_channel = Mat::zeros(src.size(), CV_8UC1);
    for (int i = 0; i < src.rows; ++i) {
        for (int j = 0; j < src.cols; ++j) {
            Vec3b pixel = src.at<Vec3b>(i, j);
            dark_channel.at<uchar>(i, j) = std::min({ pixel[0], pixel[1], pixel[2] });
        }
    }

    Mat dark_channel_blur;
    boxFilter(dark_channel, dark_channel_blur, CV_8UC1, Size(r, r));

    Mat A = Mat::zeros(src.size(), CV_8UC1);
    for (int i = 0; i < src.rows; ++i) {
        for (int j = 0; j < src.cols; ++j) {
            A.at<uchar>(i, j) = dark_channel_blur.at<uchar>(i, j);
        }
    }

    Mat transmission = Mat::zeros(src.size(), CV_64FC1);
    for (int i = 0; i < src.rows; ++i) {
推荐阅读:
  1. OpenCV-Python直方图均衡化如何实现图像去雾
  2. OpenCV与C++在图像质量增强中的新进展

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++

上一篇:上Awk的文本数据可视化方法

下一篇:OpenCV库在C++中的图像预处理流程设计

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》