GW-白平衡美白算法

    技术2022-07-10  94

    #include<opencv2/opencv.hpp> using namespace cv; using namespace std; cv::Mat GW(const cv::Mat srcImage) { int beta;//美白系数 cv::Mat dstImage; vector<cv::Mat> Channels; cv::split(srcImage, Channels); cv::Mat B = Channels[0]; cv::Mat G = Channels[1]; cv::Mat R = Channels[2]; double Baver = cv::mean(B)[0]; double Gaver = cv::mean(G)[0]; double Raver = cv::mean(R)[0]; beta = 2; double K = (Baver + Gaver + Raver) / beta; //计算增益系数 double Kb, Kg, Kr; Kb = K / Baver; Kg = K / Gaver; Kr = K / Raver; //白平衡处理后的通道 cv::Mat dstB, dstG, dstR; dstB = B * Kb; dstG = G * Kg; dstR = R * Kr; cout << cv::mean(dstB)[0] << " " << cv::mean(dstG)[0] << " " << cv::mean(dstB)[0] << endl; vector<cv::Mat> dstChanges; dstChanges.push_back(dstB); dstChanges.push_back(dstG); dstChanges.push_back(dstR); cv::merge(dstChanges, dstImage);//合并通道 return dstImage; } int main() { cv::Mat src = cv::imread("G:\\12.png"); cv::Mat dst = GW(src); cv::imshow("dst", dst); cv::imshow("src", src); cv::waitKey(0); system("pause"); return 0; } import cv2 import numpy as np def GW(orgImg): B, G, R = np.double(orgImg[:, :, 0]), np.double(orgImg[:, :, 1]), np.double(orgImg[:, :, 2]) B_ave, G_ave, R_ave = np.mean(B),np.mean(G),np.mean(R) K = (B_ave + G_ave + R_ave) / 3 Kb,Kg,Kr = K/B_ave,K/G_ave,K/R_ave Ba = (B * Kb) Ga = (G * Kg) Ra = (R * Kr) print(np.mean(Ba),np.mean(Ga),np.mean(Ra)) dst_img = np.uint8(np.zeros_like(orgImg)) dst_img[:, :, 0] = Ba dst_img[:, :, 1] = Ga dst_img[:, :, 2] = Ra cv2.imshow("dstimg", dst_img) cv2.waitKey(0) if __name__ == '__main__': orgImg = cv2.imread("E:\Py3.6_Proje\AWB\img3.jpg") cv2.imshow("orgImg",orgImg) cv2.waitKey(0) GW(orgImg)

     

     

    Processed: 0.010, SQL: 9