您好,登錄后才能下訂單哦!
圖像去霧是一種重要的圖像增強技術,可以有效地提高圖像的清晰度和細節。在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) {
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。