OpenCV半小時(shí)掌握基本操作之圖像處理
【OpenCV】⚠️高手勿入! 半小時(shí)學(xué)會(huì)基本操作⚠️圖像處理
概述
OpenCV 是一個(gè)跨平臺(tái)的計(jì)算機(jī)視覺庫, 支持多語言, 功能強(qiáng)大. 今天小白就帶大家一起攜手走進(jìn) OpenCV 的世界.
圖像處理
圖像處理是非?;A(chǔ)和關(guān)鍵的, 今天就帶大家來一起了解一下圖像處理.
轉(zhuǎn)換圖像
cv.cvtColor
可以幫助我們轉(zhuǎn)換圖片通道.
格式:
cv2.cvtColor(src, code[, dst[, dstCn]])
參數(shù):
- src: 需要轉(zhuǎn)換的圖片
- code: 顏色空間轉(zhuǎn)換碼
- dst: 輸出圖像大小深度相同, 可選參數(shù)
- desCn: 輸出圖像的顏色通道, 可選參數(shù)
轉(zhuǎn)換成灰度圖
RGB 到灰度圖轉(zhuǎn)換公式:
Y' = 0.299 R + 0.587 G + 0.114 B
例子:
# 讀取數(shù)據(jù) img = cv2.imread("cat.jpg") # 轉(zhuǎn)換成灰度圖 img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 輸出維度 print(img_gray.shape) # (554, 640) # 展示圖像 cv2.imshow("img_gray", img_gray) cv2.waitKey(0) cv2.destroyAllWindows()
輸出結(jié)果:
HSV
HSV (Hue, Saturation, Value) 是根據(jù)顏色的直觀特性由 A.R. Smith 在 1978 年創(chuàng)建的一種顏色空間.
例子:
# 轉(zhuǎn)換成hsv img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # 輸出維度 print(img_hsv.shape) # (554, 640, 3) # 展示圖像 cv2.imshow("img_hsv", img_hsv) cv2.waitKey(0) cv2.destroyAllWindows()
輸出結(jié)果:
YUV
YUV 是一種顏色編碼的方法, 主要用在視頻, 圖形處理流水線中.
例子:
# 讀取數(shù)據(jù) img = cv2.imread("cat.jpg") # 轉(zhuǎn)換成hsv img_yuv = cv2.cvtColor(img, cv2.COLOR_BGR2YUV) # 輸出維度 print(img_yuv.shape) # (554, 640, 3) # 展示圖像 cv2.imshow("img_yuv", img_yuv) cv2.waitKey(0) cv2.destroyAllWindows()
輸出結(jié)果:
二值化操作
格式:
ret, dst = cv2.threshold(src, thresh, maxval, type)
參數(shù):
- src: 需要轉(zhuǎn)換的圖
- thresh: 閾值
- maxval: 超過閾值所賦的值
- type: 二值化操作類型
返回值:
- ret: 輸入的閾值
- dst: 處理好的圖片
原圖
Binary
大于閾值的設(shè)為 255, 低于或等于閾值的為 0.
例子:
# 讀取數(shù)據(jù) img_gray = cv2.imread("cat_gray.jpg") # 二值化 ret, thresh1 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY) # 圖片展示 cv2.imshow("thresh1", thresh1) cv2.waitKey(0) cv2.destroyAllWindows()
輸出結(jié)果:
Binary Inverse
與 Binary 相反.
例子:
# 讀取數(shù)據(jù) img_gray = cv2.imread("cat_gray.jpg") # 二值化 ret, thresh2 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY_INV) # 圖片展示 cv2.imshow("thresh2", thresh2) cv2.waitKey(0) cv2.destroyAllWindows()
輸出結(jié)果:
Trunc
大于閾值的設(shè)為 255, 低于或等于閾值的不變.
例子:
# 讀取數(shù)據(jù) img_gray = cv2.imread("cat_gray.jpg") # 截?cái)?ret, thresh3 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_TRUNC) # 圖片展示 cv2.imshow("thresh3", thresh3) cv2.waitKey(0) cv2.destroyAllWindows()
輸出結(jié)果:
Tozero
大于閾值部分不變, 否則設(shè)為 0.
代碼:
# 讀取數(shù)據(jù) img_gray = cv2.imread("cat_gray.jpg") # Tozero ret, thresh4 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_TOZERO) # 圖片展示 cv2.imshow("thresh4", thresh4) cv2.waitKey(0) cv2.destroyAllWindows()
輸出結(jié)果:
Tozero Inverse
與 Tozero 相反.
代碼:
# 讀取數(shù)據(jù) img_gray = cv2.imread("cat_gray.jpg") # Tozero ret, thresh5 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_TOZERO_INV) # 圖片展示 cv2.imshow("thresh5", thresh5) cv2.waitKey(0) cv2.destroyAllWindows()
輸出結(jié)果:
到此這篇關(guān)于OpenCV半小時(shí)掌握基本操作之圖像處理的文章就介紹到這了,更多相關(guān)OpenCV圖像處理內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。