一、概述
實現步驟:
1.將圖像轉為灰度圖
2.使用濾波器去除圖像中的噪音
3.創建一個光模式圖像
4.用光模式矩陣減去處理過后的圖像矩陣
5.輸出圖像
ps:此案例並不適合所有的情況,特別是生成光模式背景。如果是較為復雜且是彩色圖像則完全沒法發使用這種方式生成。
二、示例代碼
//原圖 Mat src = imread(inputImagePath); imshow("input", src); waitKey(0); //灰度圖 Mat gray; cvtColor(src, gray, COLOR_BGR2GRAY); //中值濾波去除椒鹽噪聲,此處卷積核用3、5都不是很理想,所以選擇了7。有興趣可以試試其他的。 Mat mBlur; medianBlur(gray, mBlur, 7); imshow("mBlur", mBlur); waitKey(0); //對原始圖像執行大模糊以得到光模式(和輸入圖像背景差不多的的背景圖) Mat pattern; blur(mBlur, pattern, Size(mBlur.cols / 3, mBlur.rows / 3)); imshow("pattern", pattern); waitKey(0); //減除輸入圖像背景:有兩種算法:1.減法=光模式圖像-原始矩陣圖像。2.除法=255*(1-(原生圖像/光模式)) Mat removeLightPattern; removeLightPattern = pattern - mBlur; //輸出背景減除后的圖像 imshow("removeLightPattern", removeLightPattern); waitKey(0);
三、測試對比效果圖
對比圖1:

