用Python去除高光的算法思路和应用场景是怎样
Admin 2022-05-24 群英技术资讯 532 次浏览
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; }
实现效果
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
ROI区域又叫感兴趣区域。在机器视觉、图像处理中,从被处理的图像以方框、圆、椭圆、不规则多边形等方式勾勒出需要处理的区域,称为感兴趣区域,ROI。本文主要为大家介绍如何通过Python+OpenCV提取ROI区域,需要的朋友可以了解一下
需要安装pyechartspipinstallpyecharts-U创建【demo6.py】并输入以下编码:frompyechartsimportoptionsasoptsfromp
字符串英文名string,简称str。 字符串就是由一个个字符连接起来的组合, 你平时所见的字母、数字、汉字、符号都是字符。
telepath库它提供了一种将包括Python对象在内的结构化数据打包为JSON可序列化格式的机制。通过向相应的JavaScript实现注册该机制,可以扩展该机制以支持任何Python类。然后,打包的数据可以包含在HTTP响应中,并在JavaScript中解压缩以获得与原始数据等效的数据结构。
要想在Flask中处理好异常,有一套自己的异常处理机制,首先,我们必须先知道Flask自己是如何处理异常的。去flask的源码里找一找会发现,在flask源码的app.py文件下,有很多会抛出异常的方法,其中拿一个举例:
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008