python實(shí)現(xiàn)圖像增強(qiáng)算法
本文實(shí)例為大家分享了python實(shí)現(xiàn)圖像增強(qiáng)算法的具體代碼,供大家參考,具體內(nèi)容如下
圖像增強(qiáng)算法,圖像銳化算法
1)基于直方圖均衡化
2)基于拉普拉斯算子
3)基于對(duì)數(shù)變換
4)基于伽馬變換
5) CLAHE
6) retinex-SSR
7) retinex-MSR
其中,基于拉普拉斯算子的圖像增強(qiáng)為利用空域卷積運(yùn)算實(shí)現(xiàn)濾波
基于同一圖像對(duì)比增強(qiáng)效果
直方圖均衡化:對(duì)比度較低的圖像適合使用直方圖均衡化方法來增強(qiáng)圖像細(xì)節(jié)
拉普拉斯算子可以增強(qiáng)局部的圖像對(duì)比度
log對(duì)數(shù)變換對(duì)于整體對(duì)比度偏低并且灰度值偏低的圖像增強(qiáng)效果較好
伽馬變換對(duì)于圖像對(duì)比度偏低,并且整體亮度值偏高(對(duì)于相機(jī)過曝)情況下的圖像增強(qiáng)效果明顯
import cv2 import numpy as np import matplotlib.pyplot as plt # 直方圖均衡增強(qiáng) def hist(image): ? ? r, g, b = cv2.split(image) ? ? r1 = cv2.equalizeHist(r) ? ? g1 = cv2.equalizeHist(g) ? ? b1 = cv2.equalizeHist(b) ? ? image_equal_clo = cv2.merge([r1, g1, b1]) ? ? return image_equal_clo # 拉普拉斯算子 def laplacian(image): ? ? kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]]) ? ? image_lap = cv2.filter2D(image, cv2.CV_8UC3, kernel) ? ?# cv2.imwrite('th1.jpg', image_lap) ? ? return image_lap # 對(duì)數(shù)變換 def log(image): ? ? image_log = np.uint8(np.log(np.array(image) + 1)) ? ? cv2.normalize(image_log, image_log, 0, 255, cv2.NORM_MINMAX) ? ? # 轉(zhuǎn)換成8bit圖像顯示 ? ? cv2.convertScaleAbs(image_log, image_log) ? ? return image_log # 伽馬變換 def gamma(image): ? ? fgamma = 2 ? ? image_gamma = np.uint8(np.power((np.array(image) / 255.0), fgamma) * 255.0) ? ? cv2.normalize(image_gamma, image_gamma, 0, 255, cv2.NORM_MINMAX) ? ? cv2.convertScaleAbs(image_gamma, image_gamma) ? ? return image_gamma # 限制對(duì)比度自適應(yīng)直方圖均衡化CLAHE def clahe(image): ? ? b, g, r = cv2.split(image) ? ? clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8)) ? ? b = clahe.apply(b) ? ? g = clahe.apply(g) ? ? r = clahe.apply(r) ? ? image_clahe = cv2.merge([b, g, r]) ? ? return image_clahe def replaceZeroes(data): ? ? min_nonzero = min(data[np.nonzero(data)]) ? ? data[data == 0] = min_nonzero ? ? return data # retinex SSR def SSR(src_img, size): ? ? L_blur = cv2.GaussianBlur(src_img, (size, size), 0) ? ? img = replaceZeroes(src_img) ? ? L_blur = replaceZeroes(L_blur) ? ? dst_Img = cv2.log(img/255.0) ? ? dst_Lblur = cv2.log(L_blur/255.0) ? ? dst_IxL = cv2.multiply(dst_Img, dst_Lblur) ? ? log_R = cv2.subtract(dst_Img, dst_IxL) ? ? dst_R = cv2.normalize(log_R,None, 0, 255, cv2.NORM_MINMAX) ? ? log_uint8 = cv2.convertScaleAbs(dst_R) ? ? return log_uint8 def SSR_image(image): ? ? size = 3 ? ? b_gray, g_gray, r_gray = cv2.split(image) ? ? b_gray = SSR(b_gray, size) ? ? g_gray = SSR(g_gray, size) ? ? r_gray = SSR(r_gray, size) ? ? result = cv2.merge([b_gray, g_gray, r_gray]) ? ? return result # retinex MMR def MSR(img, scales): ? ? weight = 1 / 3.0 ? ? scales_size = len(scales) ? ? h, w = img.shape[:2] ? ? log_R = np.zeros((h, w), dtype=np.float32) ? ? for i in range(scales_size): ? ? ? ? img = replaceZeroes(img) ? ? ? ? L_blur = cv2.GaussianBlur(img, (scales[i], scales[i]), 0) ? ? ? ? L_blur = replaceZeroes(L_blur) ? ? ? ? dst_Img = cv2.log(img/255.0) ? ? ? ? dst_Lblur = cv2.log(L_blur/255.0) ? ? ? ? dst_Ixl = cv2.multiply(dst_Img, dst_Lblur) ? ? ? ? log_R += weight * cv2.subtract(dst_Img, dst_Ixl) ? ? dst_R = cv2.normalize(log_R,None, 0, 255, cv2.NORM_MINMAX) ? ? log_uint8 = cv2.convertScaleAbs(dst_R) ? ? return log_uint8 def MSR_image(image): ? ? scales = [15, 101, 301] ?# [3,5,9] ? ? b_gray, g_gray, r_gray = cv2.split(image) ? ? b_gray = MSR(b_gray, scales) ? ? g_gray = MSR(g_gray, scales) ? ? r_gray = MSR(r_gray, scales) ? ? result = cv2.merge([b_gray, g_gray, r_gray]) ? ? return result if __name__ == "__main__": ? ? image = cv2.imread('img/FJ(93).png') ? ? image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) ? ? plt.subplot(4, 2, 1) ? ? plt.imshow(image) ? ? plt.axis('off') ? ? plt.title('Offical') ? ? # 直方圖均衡增強(qiáng) ? ? image_equal_clo = hist(image) ? ? plt.subplot(4, 2, 2) ? ? plt.imshow(image_equal_clo) ? ? plt.axis('off') ? ? plt.title('equal_enhance') ? ? # 拉普拉斯算法增強(qiáng) ? ? image_lap = laplacian(image) ? ? plt.subplot(4, 2, 3) ? ? plt.imshow(image_lap) ? ? plt.axis('off') ? ? plt.title('laplacian_enhance') ? ? # LoG對(duì)象算法增強(qiáng) ? ? image_log = log(image) ? ? plt.subplot(4, 2, 4) ? ? plt.imshow(image_log) ? ? plt.axis('off') ? ? plt.title('log_enhance') ? ? # # 伽馬變換 ? ? image_gamma = gamma(image) ? ? plt.subplot(4, 2, 5) ? ? plt.imshow(image_gamma) ? ? plt.axis('off') ? ? plt.title('gamma_enhance') ? ? # CLAHE ? ? image_clahe = clahe(image) ? ? plt.subplot(4, 2, 6) ? ? plt.imshow(image_clahe) ? ? plt.axis('off') ? ? plt.title('CLAHE') ? ? # retinex_ssr ? ? image_ssr = SSR_image(image) ? ? plt.subplot(4, 2, 7) ? ? plt.imshow(image_ssr) ? ? plt.axis('off') ? ? plt.title('SSR') ? ? # retinex_msr ? ? image_msr = MSR_image(image) ? ? plt.subplot(4, 2, 8) ? ? plt.imshow(image_msr) ? ? plt.axis('off') ? ? plt.title('MSR') ? ? plt.show()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解常用查找數(shù)據(jù)結(jié)構(gòu)及算法(Python實(shí)現(xiàn))
本篇文章主要介紹了Python實(shí)現(xiàn)常用查找數(shù)據(jù)結(jié)構(gòu)及算法,具有一定的參考價(jià)值,有興趣的可以了解一下。2016-12-12Python中關(guān)于字典的常規(guī)操作范例以及介紹
今天小編幫大家簡(jiǎn)單介紹下Python的一種數(shù)據(jù)結(jié)構(gòu): 字典,字典是 Python 提供的一種常用的數(shù)據(jù)結(jié)構(gòu),它用于存放具有映射關(guān)系的數(shù)據(jù),通讀本篇對(duì)大家的學(xué)習(xí)或工作具有一定的價(jià)值,需要的朋友可以參考下2021-09-09深入了解Python?Opencv數(shù)據(jù)增強(qiáng)
常見的數(shù)據(jù)增強(qiáng)操作有:按比例放大或縮小圖片、旋轉(zhuǎn)、平移、水平翻轉(zhuǎn)、改變圖像通道等。本文將通過Python?OpenCV實(shí)現(xiàn)這些操作,需要的可以參考一下2022-02-02使用批處理腳本自動(dòng)生成并上傳NuGet包(操作方法)
這篇文章主要介紹了使用批處理腳本自動(dòng)生成并上傳NuGet包的操作方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11Python OpenCV 使用滑動(dòng)條來調(diào)整函數(shù)參數(shù)的方法
這篇文章主要介紹了Python OpenCV 使用滑動(dòng)條來調(diào)整函數(shù)參數(shù)的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07PyChon中關(guān)于Jekins的詳細(xì)安裝(推薦)
這篇文章主要介紹了PyChon中關(guān)于Jekins的詳細(xì)安裝(推薦),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12python通過robert、sobel、Laplace算子實(shí)現(xiàn)圖像邊緣提取詳解
這篇文章主要介紹了python通過robert、sobel、Laplace算子實(shí)現(xiàn)圖像邊緣提取詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08Python PyQt5模塊實(shí)現(xiàn)一個(gè)瀏覽器的示例代碼
在項(xiàng)目開發(fā)中,有的應(yīng)用程序可以運(yùn)行在web瀏覽器,本文主要介紹了Python PyQt5模塊實(shí)現(xiàn)一個(gè)瀏覽器的示例代碼,分享給大家,感興趣的可以了解一下2021-07-07