您好,登錄后才能下訂單哦!
這篇文章主要介紹了Python如何實現特定場景去除高光算法,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
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; }
實現效果
感謝你能夠認真閱讀完這篇文章,希望小編分享的“Python如何實現特定場景去除高光算法”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。