高反差保留濾鏡學習OpenCV:濾鏡系列(11)——高反差保留

這幾周筆者幾篇文章介紹了改高反差保留濾鏡的文章. 關聯文章的地址java

    高反差保留就是高通濾波程序員

    r=(pix[x,y]-avg(R))/128測試

    pix[x,y]*r+128*(1-r)ui

#include <math.h>
#include <opencv/cv.h>
#include <opencv/highgui.h>

using namespace cv;
using namespace std;

int R=5;

int main()
{
	Mat src = imread("D:/10.jpg",1);
	int width=src.cols;
	int heigh=src.rows;
	Mat img;
	src.copyTo(img);
	Mat avg;
	//GaussianBlur(img,avg,Size(R,R),0.0);
	blur(img,avg,Size(R,R));
	Mat dst(img.size(),CV_8UC3);
	float tmp;
	for (int y=0;y<heigh;y++)
	{
		uchar* imgP=img.ptr<uchar>(y);
		uchar* avgP=avg.ptr<uchar>(y);
		uchar* dstP=dst.ptr<uchar>(y);
		for (int x=0;x<width;x++)
		{
			float r0 = abs((float)imgP[3*x]-(float)avgP[3*x])/128;  
			tmp = abs( ((float)imgP[3*x]  )*r0 + 128*(1-r0) );
			tmp=tmp>255?255:tmp;
			tmp=tmp<0?0:tmp;
			dstP[3*x]=(uchar)(tmp);

			float r1 = abs((float)imgP[3*x+1]-(float)avgP[3*x+1])/128;
			tmp = (uchar)abs( ((float)imgP[3*x+1])*r1 + 128*(1-r1) );
			tmp=tmp>255?255:tmp;
			tmp=tmp<0?0:tmp;
			dstP[3*x+1]=(uchar)(tmp);
			
			float r2 = abs((float)imgP[3*x+2]-(float)avgP[3*x+2])/128;
			tmp = (uchar)abs( ((float)imgP[3*x+2])*r2 + 128*(1-r2) );
			tmp=tmp>255?255:tmp;
			tmp=tmp<0?0:tmp;
			dstP[3*x+2]=(uchar)(tmp);
		}
	}
	imshow("high",dst);
	
	//高通濾波測試

	Mat kern = (Mat_<char>(3,3) <<  -1, -1,  -1,
					 			    -1,  5, -1,
									-1, -1,  -1);
	Mat dstF;
	filter2D(img,dstF,img.depth(),kern);
	imshow("kernel",dstF);

	waitKey();
	imwrite("D:/高反差保留.jpg",dst);
	imwrite("D:/高通濾波.jpg",dstF);
}
    每日一道理
「一年之計在於春」,十幾歲的年紀,正是人生的春天,別辜負了歲月老人的厚愛與恩賜。行動起來,播種夢想吧!

    

    原圖:spa

    高反差保留和濾鏡

    

    高反差保留:code

    高反差保留和濾鏡

    

    高通濾波器:it

    高反差保留和濾鏡

    

文章結束給你們分享下程序員的一些笑話語錄: 剎車失靈
有一個物理學家,工程師和一個程序員駕駛着一輛汽車行駛在阿爾卑斯山脈 上,在下山的時候,突然,汽車的剎車失靈了,汽車沒法控制地向下衝去, 眼看前面就是一個懸崖峭壁,可是很幸運的是在這個懸崖的前面有一些小樹 讓他們的汽車停了下來, 而沒有掉下山去。 三個驚魂未定地從車裏爬了出來。
物理學家說, 「我以爲咱們應該創建一個模型來模擬在下山過程當中剎車片在高 溫狀況下失靈的情形」。
工程師說, 「我在車的後備廂來有個扳手, 要不咱們把車拆開看看究竟是什麼 緣由」。
程序員說,「爲何咱們不找個相同的車再來一次以重現這個問題呢?」
io

--------------------------------- 原創文章 By
高反差保留和濾鏡
--------------------------------- opencv

相關文章
相關標籤/搜索