【python】OpenCV—findContours(4)
根據 mask 標籤(跟原圖一樣大小的二值圖),1)把 mask(cv2.addWeighted
) 畫在原圖上,2)把 mask 輪廓(cv2.findContours
)畫在原圖上
【1】 cv2.addWeighted
來自 Python-OpenCV 影像疊加or影像混合加權(cv2.addWeighted)
import numpy as np
img1 = cv2.imread("/home/Downloads/cat.jpg")
img2 = cv2.imread("/home/Downloads/1.bmp") # 顏色通道順序為 BGR
img2[:,:,0] *= 0 # 藍色通道為0
img2[:,:,1] *= 0 # 綠色通道為0
img2[:,:,2] *= 1 # 保留紅色通道
imgadd = cv2.addWeighted(img1,1,img2,0.5,0) # 原圖權重1,mask 權重 0.5(調透明度)
cv2.imshow("image",imgadd)
cv2.waitKey(3000)
1.bmp
cat.jpg
效果圖如下
【2】cv2.findContours
import cv2
import numpy as np
img1 = cv2.imread("/home/yanmeng/Downloads/cat.jpg")
img2 = cv2.imread("/home/yanmeng/Downloads/1.bmp") # BGR
gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) # 彩色圖變灰度圖
_,binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY) # 灰度圖變二值圖
_, contours, _ = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # 根據二值圖找輪廓
cv2.drawContours(img1,contours,-1,(0,0,255),3) # 把輪廓畫在原圖上(0,0,255) 表示 RGB 三通道,紅色
cv2.imshow("image",img1) # 顯示原圖
cv2.waitKey(3000)
注意 cv2.findContours
的返回值個數可能因 opencv 版本不同而不一樣,我的版本是三個返回值,有的版本是兩個
效果圖如下
我們把其他的 mask 的輪廓也都畫在原圖上,有 1.bmp~4.bmp
import cv2
def bgr2binary(image): # 彩色轉二值
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
_, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
return binary
binarys = []
img = cv2.imread("/home/yanmeng/Downloads/cat.jpg") # 原圖
for i in range(1,5): # 遍歷 mask 圖片
mask_path = "/home/yanmeng/Downloads/{}.bmp".format(str(i))
mask = cv2.imread(mask_path)
binarys.append(bgr2binary(mask)) # 讀 mask 並轉換為二值圖
for binary_mask in binarys: # 遍歷二值化後的 mask
_, contours, _ = cv2.findContours(binary_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # 找輪廓,並用紅色標記出來
cv2.drawContours(img,contours,-1,(0,0,255),3) # 把輪廓畫在原圖上
cv2.imshow("image",img) # 顯示原圖
cv2.waitKey(3000)
mask 具體形式如下所示
2.bmp
3.bmp
4.bmp
最終效果圖
相關文章
- OpenCV findContours 與 drawContours 用法OpenCV
- OpenCV中的findContours函式引數詳解OpenCV函式
- OpenCv查詢輪廓-cv2.findContours()函式OpenCV函式
- 【Python】【OpenCV】關於cv2.findContours()輪廓索引(編號)解析(RETR_TREE)PythonOpenCV索引
- python安裝opencvPythonOpenCV
- 最新openCV-Python安裝教程(opencv-python版本4.4.0, Python版本: 3.9)OpenCVPython
- 『OpenCV-Python』滑鼠事件OpenCVPython事件
- python如何安裝OpenCV?PythonOpenCV
- OpenCV-Python -- Fourier TransformOpenCVPythonORM
- python opencv轉換bytesioPythonOpenCV
- Python - opencv-python 獲取影片尺寸PythonOpenCV
- 【Python】【OpenCV】定位二維碼PythonOpenCV
- opencv-python學習之旅OpenCVPython
- python中使用OpenCV畫線PythonOpenCV
- Python/OpenCV:回撥函式PythonOpenCV函式
- Porting OpenCV 2.3.1 to iPhone 3GS/4/4SOpenCViPhone
- Python 影像處理 OpenCV (4):影像算數運算以及修改顏色空間PythonOpenCV
- 解決OpenCV(4.1.2) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:376: error: (-215:AOpenCVProjectPythonGUIError
- Python的OpenCV轉換影像大小PythonOpenCV
- python OpenCV加法操作的實現PythonOpenCV
- Python的Opencv庫怎麼裝PythonOpenCV
- OpenCV_python全屏顯示影像OpenCVPython
- OpenCV-Python 視訊讀取OpenCVPython
- 影象二值化(python+opencv)PythonOpenCV
- OpenCV學習筆記(4)——mixChannels函式OpenCV筆記函式
- python使用OpenCV設定圖片尺寸PythonOpenCV
- python OpenCV VideoWriter 視訊格式及大小PythonOpenCVIDE
- Python 影像處理 OpenCV (1):入門PythonOpenCV
- python opencv讀取網路圖片PythonOpenCV
- Python opencv 將一張圖片任意 N 等分並儲存_opencv 等分影像PythonOpenCV
- opencv和Python的區別是什麼?Python開發!OpenCVPython
- python OpenCV中的閾值是什麼PythonOpenCV
- python+opencv邊緣檢測方法整理PythonOpenCV
- python--顏色的RGB轉BGR(opencv)PythonOpenCV
- Python 影像處理 OpenCV (15):影像輪廓PythonOpenCV
- python基於opencv 實現影像時鐘PythonOpenCV
- python-opencv顏色分析小工具PythonOpenCV
- OpenCV-Python 人臉眼睛嘴識別OpenCVPython