OpenCV 顏色差異:解決載入影像和繪製影像之間的差異
當利用OpenCV 載入彩色影像並使用Matplotlib 顯示它們時,它不是顯示顏色差異的情況並不常見。這是由於 OpenCV 和 Matplotlib 之間預設顏色順序的差異所造成的。
OpenCV 以藍綠紅 (BGR) 格式儲存影像,而 Matplotlib 期望以紅綠藍 (RGB) 格式儲存影像格式。在 Matplotlib 中顯示使用 OpenCV 載入的影像時,顏色通道會反轉,導致顏色不正確。
解決方案:將 BGR 轉換為 RGB
要修正此問題,我們需要使用OpenCV的cvtColor函數將載入的映像從BGR顯式轉換為RGB。
<code class="python">RGB_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)</code>
透過在Matplotlib圖中使用RGB_img而不是原始img,我們確保顏色順序符合Matplotlib的期望,顏色顯示正確。
更新程式碼
<code class="python">import cv2 import matplotlib.pyplot as plt # Load image with BGR order (default for OpenCV) img = cv2.imread('lena_caption.png', cv2.IMREAD_COLOR) # Convert BGR to RGB for compatibility with Matplotlib RGB_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # Create grayscale image bw_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Create figure for plotting fig, axes = plt.subplots(1, 2) # Plot original image with corrected color order axes[0].imshow(RGB_img) axes[0].set_title('Original Image (RGB)') axes[0].set_xticks([]); axes[0].set_yticks([]) # Plot grayscale image axes[1].imshow(bw_img, cmap='gray') axes[1].set_title('BW Image') axes[1].set_xticks([]); axes[1].set_yticks([]) plt.show()</code>
以上是為什麼 OpenCV 載入的圖像在 Matplotlib 中顯示的顏色不準確?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

ForhandlinglargedatasetsinPython,useNumPyarraysforbetterperformance.1)NumPyarraysarememory-efficientandfasterfornumericaloperations.2)Avoidunnecessarytypeconversions.3)Leveragevectorizationforreducedtimecomplexity.4)Managememoryusagewithefficientdata

Inpython,ListSusedynamicMemoryAllocationWithOver-Asalose,而alenumpyArraySallaySallocateFixedMemory.1)listssallocatemoremoremoremorythanneededinentientary上,respizeTized.2)numpyarsallaysallaysallocateAllocateAllocateAlcocateExactMemoryForements,OfferingPrediCtableSageButlessemageButlesseflextlessibility。

Inpython,YouCansspecthedatatAtatatPeyFelemereModeRernSpant.1)Usenpynernrump.1)Usenpynyp.dloatp.dloatp.ploatm64,formor professisconsiscontrolatatypes。

NumPyisessentialfornumericalcomputinginPythonduetoitsspeed,memoryefficiency,andcomprehensivemathematicalfunctions.1)It'sfastbecauseitperformsoperationsinC.2)NumPyarraysaremorememory-efficientthanPythonlists.3)Itoffersawiderangeofmathematicaloperation

Contiguousmemoryallocationiscrucialforarraysbecauseitallowsforefficientandfastelementaccess.1)Itenablesconstanttimeaccess,O(1),duetodirectaddresscalculation.2)Itimprovescacheefficiencybyallowingmultipleelementfetchespercacheline.3)Itsimplifiesmemorym

SlicingaPythonlistisdoneusingthesyntaxlist[start:stop:step].Here'showitworks:1)Startistheindexofthefirstelementtoinclude.2)Stopistheindexofthefirstelementtoexclude.3)Stepistheincrementbetweenelements.It'susefulforextractingportionsoflistsandcanuseneg

numpyallowsforvariousoperationsonArrays:1)basicarithmeticlikeaddition,減法,乘法和division; 2)evationAperationssuchasmatrixmultiplication; 3)element-wiseOperations wiseOperationswithOutexpliitloops; 4)

Arresinpython,尤其是Throughnumpyandpandas,weessentialFordataAnalysis,offeringSpeedAndeffied.1)NumpyArseNable efflaysenable efficefliceHandlingAtaSetSetSetSetSetSetSetSetSetSetSetsetSetSetSetSetsopplexoperationslikemovingaverages.2)


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

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

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

WebStorm Mac版
好用的JavaScript開發工具

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。