Opencv中cv2.cvtColor彩色圖轉(zhuǎn)灰度圖的其他6種方法
1.公式集成:

2.代碼實(shí)現(xiàn):
import os
import cv2
import queue
import threading
import numpy as np
#用戶存取函數(shù)的返回值
q=queue.Queue()
def rgb2gray(image,method):
h,w,c=image.shape
gray=np.zeros((h,w),dtype=np.uint8)
y=0
for row in range(h):
for col in range(w):
#Opencv讀取出的圖片格式為BGR
b,g,r=np.int32(image[row,col])
if method==1:
y=0.299*r+0.587*g+0.114*b
if method==2:
y=(max([r,g,b])+min([r,g,b]))//2
if method==3:
y=(r+g+b)//3
if method==4:
y=0.21*r+0.72*g+0.07*b
if method==5:
y=max([r,g,b])
if method==6:
y=min([r,g,b])
gray[row,col]=y
# return gray
q.put((gray))
if __name__ == '__main__':
print('Pycharm')
curr_path=os.getcwd()+'\\5.jpg'
print(curr_path)
image=cv2.imread(curr_path)
cv2.imshow('BGR: ',image)
#opencv提供的轉(zhuǎn)灰度圖的方法
gray_cvColor=cv2.cvtColor(image,cv2.COLOR_BGRA2GRAY)
cv2.imshow('gray_cvColor',gray_cvColor)
gray1=rgb2gray(image,1)
gray2 = rgb2gray(image, 2)
gray3 = rgb2gray(image, 3)
gray4 = rgb2gray(image, 4)
gray5 = rgb2gray(image, 5)
gray6 = rgb2gray(image, 6)
cv2.imshow('一般轉(zhuǎn)換方法',gray1)
cv2.imshow('亮度優(yōu)化轉(zhuǎn)換', gray2)
cv2.imshow('平均亮度轉(zhuǎn)換', gray3)
cv2.imshow('權(quán)重亮度轉(zhuǎn)換', gray4)
cv2.imshow('最大亮度轉(zhuǎn)換', gray5)
cv2.imshow('最小亮度轉(zhuǎn)換', gray6)
cv2.waitKey(0)
cv2.destroyAllWindows()
3.實(shí)驗結(jié)果:








4.參考文章:
https://mp.weixin.qq.com/s/jqVVZbZZRIqVt_Fs7HiUkg
到此這篇關(guān)于Opencv中cv2.cvtColor彩色圖轉(zhuǎn)灰度圖的其他6種方法的文章就介紹到這了,更多相關(guān)Opencv cv2.cvtColor彩色圖轉(zhuǎn)灰度圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談Python的自省Introspection和反射機(jī)制Reflection
這篇文章主要介紹了淺談Python的自省Introspection和反射機(jī)制Reflection,反射就是通過字符串的形式去對象(模塊)中操作(查找/獲取/刪除/添加)成員,一種基于字符串的事件驅(qū)動,需要的朋友可以參考下2023-08-08
python 爬蟲百度地圖的信息界面的實(shí)現(xiàn)方法
這篇文章主要介紹了python 爬蟲百度地圖的界面的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Python 批量驗證和添加手機(jī)號碼為企業(yè)微信聯(lián)系人
你是否也有過需要添加很多微信好友的時候,一個個輸入添加太麻煩了,本篇文章手把手教你用Python替我們完成這繁瑣的操作,大家可以在過程中查缺補(bǔ)漏,看看自己掌握程度怎么樣2021-10-10

