欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python实现特定场景去除高光算法详解

程序员文章站 2022-03-05 16:15:42
目录算法思路应用场景代码实现实验效果补充算法思路1、求取源图i的平均灰度,并记录rows和cols;2、按照一定大小,分为n*m个方块,求出每块的平均值,得到子块的亮度矩阵d;3、用矩阵d的每个元素减...

算法思路

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)

实验效果

Python实现特定场景去除高光算法详解

补充

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;
}

实现效果

Python实现特定场景去除高光算法详解

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;
}

实现效果

Python实现特定场景去除高光算法详解

到此这篇关于python实现特定场景去除高光算法详解的文章就介绍到这了,更多相关python去除高光算法内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!