學習OpenCV:濾鏡系列(12)——計算模式(強光)
阿新 • • 發佈:2019-01-26
==============================================
版權所有:小熊不去實驗室CSDN部落格
==============================================
R(上)>127.5
R=R(下)+(255-R(下))*(R(上)-127.5)/127.5;
R(上)<127.5
R=R(下)-R(下)*(127.5-R(上))/127.5=(R(上)*R(下))/127.5;
#include <math.h> #include <opencv/cv.h> #include <opencv/highgui.h> using namespace cv; using namespace std; int R=11; int main() { Mat src = imread("D:/img/liushishi02.jpg",1); imshow("src",src); int width=src.cols; int heigh=src.rows; Mat img; src.copyTo(img); Mat dst(img.size(),CV_8UC3); Mat dst1u[3]; float tmp,r; for (int y=0;y<heigh;y++) { uchar* imgP=img.ptr<uchar>(y); uchar* dstP=dst.ptr<uchar>(y); for (int x=0;x<width;x++) { r = (float)imgP[3*x]; if(r>127.5) tmp = r+(255-r)*(r-127.5)/127.5; else tmp = r*r/127.5; tmp=tmp>255?255:tmp; tmp=tmp<0?0:tmp; dstP[3*x]=(uchar)(tmp); r = (float)imgP[3*x+1]; if(r>127.5) tmp = r+(255-r)*(r-127.5)/127.5; else tmp = r*r/127.5; tmp=tmp>255?255:tmp; tmp=tmp<0?0:tmp; dstP[3*x+1]=(uchar)(tmp); r = (float)imgP[3*x+2]; if(r>127.5) tmp = r+(255-r)*(r-127.5)/127.5; else tmp = r*r/127.5; tmp=tmp>255?255:tmp; tmp=tmp<0?0:tmp; dstP[3*x+2]=(uchar)(tmp); } } imshow("強光",dst); split(dst,dst1u); imshow("綠通道強光",dst1u[1]); waitKey(); imwrite("D:/img/強光.jpg",dst); imwrite("D:/img/強光_藍通道.jpg",dst1u[0]); imwrite("D:/img/強光_綠通道.jpg",dst1u[1]); imwrite("D:/img/強光_紅通道.jpg",dst1u[2]); }
原圖:
計算強光:
紅通道:
綠通道:
藍通道: