Python的圖像增強:直方圖均衡教程
曾經被模糊,低質量的圖像感到失望嗎? 想像一下增強圖像以揭示清晰的細節並提高清晰度。 本教程向您展示瞭如何使用Python和直方圖均衡的力量實現此目標。 直方圖均衡是一種顯著增強圖像對比度的技術。 這是幾乎所有相機系統中用於提高圖像質量的基本過程,到本教程結束時,您將了解原因。
>我們將探討哪些直方圖和直方圖均衡是如何影響圖像的,然後在Python中實現該技術。讓我們開始!
理解圖像直方圖
>。 直方圖集中在狹窄的強度範圍內的
>圖像通常缺乏清晰度和細節。 均衡的圖像表現出更廣泛,更均勻的強度分佈。什麼是直方圖均衡?
> 直方圖均衡直方圖拉伸圖像的直方圖以利用全強度範圍。 這意味著將強度值傳播到包括黑暗和光面積,從而導致更高的對比度和改善的細節可見性。雖然並不總是適合標準攝影的理想選擇,但它在需要增強細節(例如衛星或熱成像)的應用中是無價的。
>我們將使用灰度猴子圖像(對比度減少)為例:
>訪問像素強度
讓我們研究如何使用Python和OpenCV訪問像素強度值:
此代碼讀取圖像,確定其尺寸,並打印一個像素值的示例。 OPENCV使用BGR(藍色,綠色,紅色)排序,因此
表示每個通道的強度為113。 直方圖均衡的直方圖修飾這些像素強度以增強對比度。 我們可以使用直方圖可視化這一點:每個顏色通道(或灰度單一直方圖)。 X軸顯示強度值,Y軸顯示它們的頻率。使用來自單獨的圖像直方圖文章中的代碼,我們的示例圖像的直方圖如下:
此直方圖顯示了跨BGR通道的分佈。來自像素值代碼的樣本輸出顯示了跨通道的一致強度值:
import cv2, random img = cv2.imread('monkey.jpg') img_shape = img.shape height = img_shape[0] width = img_shape[1] for row in range(width): for column in range(height): if random.randint(0, width) == row and row: print(img[column][row])分析強度頻率
此Python代碼計算像素強度的頻率:
>輸出顯示最頻繁的強度值及其計數,突出了濃度有限的範圍。 在Python中實現直方圖均衡
><code>[113 113 113] [110 110 110] [106 106 106] ...</code>我們將使用OpenCV的
函數,但它僅適用於灰度圖像。 因此,我們將轉換為Yuv色彩空間,均衡Y通道(亮度),然後轉換回BGR:
完整的示例和結果
equalizeHist()
這是完整的代碼:
import cv2 img = cv2.imread('monkey.jpg') img_shape = img.shape height = img_shape[0] width = img_shape[1] frequency = {} for row in range(width): for column in range(height): intensity = img[column][row][0] count = frequency.get(intensity, 0) frequency[intensity] = count + 1 print("Unique Intensities", len(frequency)) most_frequent = dict(sorted(frequency.items(), key=lambda elem: elem[1], reverse=True)) intensity_values = most_frequent.keys() i = 0 for intensity in intensity_values: i += 1 if i <= 5: print(intensity, most_frequent[intensity])
產生的圖像(
):
import cv2 import numpy img = cv2.imread('monkey.jpg') img_to_yuv = cv2.cvtColor(img, cv2.COLOR_BGR2YUV) img_to_yuv[:, :, 0] = cv2.equalizeHist(img_to_yuv[:, :, 0]) hist_equalization_result = cv2.cvtColor(img_to_yuv, cv2.COLOR_YUV2BGR) cv2.imwrite('result.jpg', hist_equalization_result)
result.jpg
的原始圖像和增強圖像的比較:
增強的圖像明顯提高了清晰度。 增強圖像的直方圖是平坦的,表明強度的均勻分佈。 強度頻率分析還將顯示值更均勻的值。
結論
本教程演示瞭如何使用python中的直方圖均衡來增強圖像對比度。 結果突出了該技術在提高圖像質量和細節可見性方面的有效性。 由此產生的扁平直方圖證實了像素強度的成功重新分佈。
以上是Python中的直方圖均衡的詳細內容。更多資訊請關注PHP中文網其他相關文章!

pythonlistscanStoryDatatepe,ArrayModulearRaysStoreOneType,and numpyArraySareSareAraysareSareAraysareSareComputations.1)列出sareversArversAtileButlessMemory-Felide.2)arraymoduleareareMogeMogeNareSaremogeNormogeNoreSoustAta.3)

WhenyouattempttostoreavalueofthewrongdatatypeinaPythonarray,you'llencounteraTypeError.Thisisduetothearraymodule'sstricttypeenforcement,whichrequiresallelementstobeofthesametypeasspecifiedbythetypecode.Forperformancereasons,arraysaremoreefficientthanl

pythonlistsarepartofthestAndArdLibrary,herilearRaysarenot.listsarebuilt-In,多功能,和Rused ForStoringCollections,而EasaraySaraySaraySaraysaraySaraySaraysaraySaraysarrayModuleandleandleandlesscommonlyusedDduetolimitedFunctionalityFunctionalityFunctionality。

ThescriptisrunningwiththewrongPythonversionduetoincorrectdefaultinterpretersettings.Tofixthis:1)CheckthedefaultPythonversionusingpython--versionorpython3--version.2)Usevirtualenvironmentsbycreatingonewithpython3.9-mvenvmyenv,activatingit,andverifying

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

useanArray.ArarayoveralistinpythonwhendeAlingwithHomoGeneData,performance-Caliticalcode,orinterfacingwithccode.1)同質性data:arraysSaveMemorywithTypedElements.2)績效code-performance-calitialcode-calliginal-clitical-clitical-calligation-Critical-Code:Arraysofferferbetterperbetterperperformanceformanceformancefornallancefornalumericalical.3)

不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactsperformance.2)listssdonotguaranteeconecontanttanttanttanttanttanttanttanttanttimecomplecomecomplecomecomecomecomecomecomplecomectacccesslectaccesslecrectaccesslerikearraysodo。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Linux新版
SublimeText3 Linux最新版

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),