欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)特定場景去除高光算法詳解

 更新時間:2021年12月30日 15:06:59   作者:watersink  
這篇文章主要介紹了如何利用Python+OpenCV實(shí)現(xiàn)特定場景去除高光算法,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Python有一定的幫助,需要的可以參考一下

算法思路

1、求取源圖I的平均灰度,并記錄rows和cols;

2、按照一定大小,分為N*M個方塊,求出每塊的平均值,得到子塊的亮度矩陣D;

3、用矩陣D的每個元素減去源圖的平均灰度,得到子塊的亮度差值矩陣E;

4、通過插值算法,將矩陣E差值成與源圖一樣大小的亮度分布矩陣R;

5、得到矯正后的圖像result=I-R;

應(yīng)用場景

光照不均勻的整體色澤一樣的物體,比如工業(yè)零件,ocr場景。

代碼實(shí)現(xiàn)

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)

實(shí)驗(yàn)效果

補(bǔ)充

OpenCV實(shí)現(xiàn)光照去除效果

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

實(shí)現(xiàn)效果

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

實(shí)現(xiàn)效果

到此這篇關(guān)于Python實(shí)現(xiàn)特定場景去除高光算法詳解的文章就介紹到這了,更多相關(guān)Python去除高光算法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

  • python將字符串以utf-8格式保存在txt文件中的方法

    python將字符串以utf-8格式保存在txt文件中的方法

    今天小編就為大家分享一篇python將字符串以utf-8格式保存在txt文件中的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • Python下載商品數(shù)據(jù)并連接數(shù)據(jù)庫且保存數(shù)據(jù)

    Python下載商品數(shù)據(jù)并連接數(shù)據(jù)庫且保存數(shù)據(jù)

    這篇文章主要介紹了Python下載商品數(shù)據(jù)并連接數(shù)據(jù)庫且保存數(shù)據(jù),包括發(fā)送請求、獲取數(shù)據(jù)、解析數(shù)據(jù)(篩選數(shù)據(jù))、保存數(shù)據(jù)、連接數(shù)據(jù)庫等內(nèi)容,需要的小伙伴可以參考一下
    2022-03-03
  • 詳解如何從TensorFlow的mnist數(shù)據(jù)集導(dǎo)出手寫體數(shù)字圖片

    詳解如何從TensorFlow的mnist數(shù)據(jù)集導(dǎo)出手寫體數(shù)字圖片

    這篇文章主要介紹了詳解如何從TensorFlow的mnist數(shù)據(jù)集導(dǎo)出手寫體數(shù)字圖片,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • Python對稱的二叉樹多種思路實(shí)現(xiàn)方法

    Python對稱的二叉樹多種思路實(shí)現(xiàn)方法

    這篇文章主要介紹了Python對稱的二叉樹多種思路實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • Python求兩個list的差集、交集與并集的方法

    Python求兩個list的差集、交集與并集的方法

    這篇文章主要介紹了Python求兩個list的差集、交集與并集的方法,是Python集合數(shù)組操作中常用的技巧,需要的朋友可以參考下
    2014-11-11
  • Python基礎(chǔ)之time庫詳解

    Python基礎(chǔ)之time庫詳解

    這篇文章主要介紹了Python基礎(chǔ)之time庫詳解,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)python基礎(chǔ)的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-04-04
  • python 實(shí)現(xiàn)簡單的FTP程序

    python 實(shí)現(xiàn)簡單的FTP程序

    這篇文章主要介紹了python 實(shí)現(xiàn)簡單的FTP程序,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-12-12
  • 下載安裝setuptool和pip linux安裝pip

    下載安裝setuptool和pip linux安裝pip

    這篇文章主要介紹了linux上安裝python組件pip,依賴wget,目前還不夠智能,大家參考使用吧
    2014-01-01
  • Python中實(shí)現(xiàn)定時任務(wù)詳解

    Python中實(shí)現(xiàn)定時任務(wù)詳解

    這篇文章主要介紹了Python中實(shí)現(xiàn)定時任務(wù)詳解的相關(guān)資料,需要的朋友可以參考下
    2023-07-07
  • 最新評論