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

蜗牛 互联网技术资讯 2024-08-19 10 0

图像去雾是一种重要的图像增强技术,可以有效地提高图像的清晰度和细节。在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) {
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:niceseo6@gmail.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

评论

有免费节点资源,我们会通知你!加入纸飞机订阅群

×
天气预报查看日历分享网页手机扫码留言评论Telegram