Python实现特定场景去除高光算法详解
作者:watersink 时间:2021-11-16 09:50:03
算法思路
1、求取源图I的平均灰度,并记录rows和cols;
2、按照一定大小,分为N*M个方块,求出每块的平均值,得到子块的亮度矩阵D;
3、用矩阵D的每个元素减去源图的平均灰度,得到子块的亮度差值矩阵E;
4、通过插值算法,将矩阵E差值成与源图一样大小的亮度分布矩阵R;
5、得到矫正后的图像result=I-R;
应用场景
光照不均匀的整体色泽一样的物体,比如工业零件,ocr场景。
代码实现
import cv2
import numpy as np
def unevenLightCompensate(gray, blockSize):
#gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
average = np.mean(gray)
rows_new = int(np.ceil(gray.shape[0] / blockSize))
cols_new = int(np.ceil(gray.shape[1] / blockSize))
blockImage = np.zeros((rows_new, cols_new), dtype=np.float32)
for r in range(rows_new):
for c in range(cols_new):
rowmin = r * blockSize
rowmax = (r + 1) * blockSize
if (rowmax > gray.shape[0]):
rowmax = gray.shape[0]
colmin = c * blockSize
colmax = (c + 1) * blockSize
if (colmax > gray.shape[1]):
colmax = gray.shape[1]
imageROI = gray[rowmin:rowmax, colmin:colmax]
temaver = np.mean(imageROI)
blockImage[r, c] = temaver
blockImage = blockImage - average
blockImage2 = cv2.resize(blockImage, (gray.shape[1], gray.shape[0]), interpolation=cv2.INTER_CUBIC)
gray2 = gray.astype(np.float32)
dst = gray2 - blockImage2
dst[dst>255]=255
dst[dst<0]=0
dst = dst.astype(np.uint8)
dst = cv2.GaussianBlur(dst, (3, 3), 0)
#dst = cv2.cvtColor(dst, cv2.COLOR_GRAY2BGR)
return dst
if __name__ == '__main__':
file = 'www.png'
blockSize = 8
img = cv2.imread(file)
b,g,r = cv2.split(img)
dstb = unevenLightCompensate(b, blockSize)
dstg = unevenLightCompensate(g, blockSize)
dstr = unevenLightCompensate(r, blockSize)
dst = cv2.merge([dstb, dstg, dstr])
result = np.concatenate([img, dst], axis=1)
cv2.imwrite('result.jpg', result)
实验效果
补充
OpenCV实现光照去除效果
1.方法一(RGB归一化)
int main(int argc, char *argv[])
{
//double temp = 255 / log(256);
//cout << "doubledouble temp ="<< temp<<endl;
Mat image = imread("D://vvoo//sun_face.jpg", 1);
if (!image.data)
{
cout << "image loading error" <<endl;
return -1;
}
imshow("原图", image);
Mat src(image.size(), CV_32FC3);
for (int i = 0; i < image.rows; i++)
{
for (int j = 0; j < image.cols; j++)
{
src.at<Vec3f>(i, j)[0] = 255 * (float)image.at<Vec3b>(i, j)[0] / ((float)image.at<Vec3b>(i, j)[0] + (float)image.at<Vec3b>(i, j)[2] + (float)image.at<Vec3b>(i, j)[1]+0.01);
src.at<Vec3f>(i, j)[1] = 255 * (float)image.at<Vec3b>(i, j)[1] / ((float)image.at<Vec3b>(i, j)[0] + (float)image.at<Vec3b>(i, j)[2] + (float)image.at<Vec3b>(i, j)[1]+0.01);
src.at<Vec3f>(i, j)[2] = 255 * (float)image.at<Vec3b>(i, j)[2] / ((float)image.at<Vec3b>(i, j)[0] + (float)image.at<Vec3b>(i, j)[2] + (float)image.at<Vec3b>(i, j)[1]+0.01);
}
}
normalize(src, src, 0, 255, CV_MINMAX);
convertScaleAbs(src,src);
imshow("rgb", src);
imwrite("C://Users//TOPSUN//Desktop//123.jpg", src);
waitKey(0);
return 0;
}
实现效果
2.方法二
void unevenLightCompensate(Mat &image, int blockSize)
{
if (image.channels() == 3) cvtColor(image, image, 7);
double average = mean(image)[0];
int rows_new = ceil(double(image.rows) / double(blockSize));
int cols_new = ceil(double(image.cols) / double(blockSize));
Mat blockImage;
blockImage = Mat::zeros(rows_new, cols_new, CV_32FC1);
for (int i = 0; i < rows_new; i++)
{
for (int j = 0; j < cols_new; j++)
{
int rowmin = i*blockSize;
int rowmax = (i + 1)*blockSize;
if (rowmax > image.rows) rowmax = image.rows;
int colmin = j*blockSize;
int colmax = (j + 1)*blockSize;
if (colmax > image.cols) colmax = image.cols;
Mat imageROI = image(Range(rowmin, rowmax), Range(colmin, colmax));
double temaver = mean(imageROI)[0];
blockImage.at<float>(i, j) = temaver;
}
}
blockImage = blockImage - average;
Mat blockImage2;
resize(blockImage, blockImage2, image.size(), (0, 0), (0, 0), INTER_CUBIC);
Mat image2;
image.convertTo(image2, CV_32FC1);
Mat dst = image2 - blockImage2;
dst.convertTo(image, CV_8UC1);
}
int main(int argc, char *argv[])
{
//double temp = 255 / log(256);
//cout << "doubledouble temp ="<< temp<<endl;
Mat image = imread("C://Users//TOPSUN//Desktop//2.jpg", 1);
if (!image.data)
{
cout << "image loading error" <<endl;
return -1;
}
imshow("原图", image);
unevenLightCompensate(image, 12);
imshow("rgb", image);
imwrite("C://Users//TOPSUN//Desktop//123.jpg", image);
waitKey(0);
return 0;
}
实现效果
来源:https://blog.csdn.net/qq_14845119/article/details/122193134
标签:Python,场景,去除,高光
0
投稿
猜你喜欢
Elasticsearch的删除映射类型操作示例
2022-05-03 09:46:50
python实现从尾到头打印单链表操作示例
2021-12-20 00:09:32
sql server 复制表从一个数据库到另一个数据库
2024-01-18 23:51:55
MySQL存储过程的概念与用法实例
2024-01-20 07:45:30
VScode中不同目录间python库函数的调用
2021-03-16 00:24:38
python加速器numba使用详解
2022-02-27 15:24:22
Vue router安装及使用方法解析
2023-07-02 16:50:00
MySQL 数据库的监控方式小结
2024-01-14 19:07:14
使用MyISAM表和InnoDB的一些记录
2009-12-20 18:21:00
ACCESS转SQL Server2000需要注意的问题
2007-11-18 15:25:00
Vue+tracking.js 实现前端人脸检测功能
2024-05-05 09:24:56
Nodejs之TCP服务端与客户端聊天程序详解
2024-05-03 15:55:48
python GUI库图形界面开发之PyQt5布局控件QVBoxLayout详细使用方法与实例
2022-10-12 11:37:27
PyQt5实现五子棋游戏(人机对弈)
2022-05-22 12:00:50
Python创建增量目录的代码实例
2021-12-07 04:12:55
5.PHP的其他功能
2023-11-14 16:45:42
sqlserver 聚集索引和非聚集索引实例
2024-01-18 21:29:12
Python 写小游戏吃金币+打乒乓+滑雪(附源码)
2021-05-17 20:56:37
python基础教程之获取本机ip数据包示例
2022-09-28 13:31:45
php 静态页面中显示动态内容
2023-11-18 22:09:22