【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
- python如何安裝OpenCV?PythonOpenCV
- OpenCV-Python -- Fourier TransformOpenCVPythonORM
- python OpenCV 入門教程PythonOpenCV
- opencv-python 影像 二OpenCVPython
- 利用Python和OpenCV將URL直接轉換成OpenCV格式PythonOpenCV
- Porting OpenCV 2.3.1 to iPhone 3GS/4/4SOpenCViPhone
- python opencv轉換bytesioPythonOpenCV
- python中使用OpenCV畫線PythonOpenCV
- Python/OpenCV:回撥函式PythonOpenCV函式
- opencv-python學習之旅OpenCVPython
- 【Python】【OpenCV】定位二維碼PythonOpenCV
- Python - opencv-python 獲取影片尺寸PythonOpenCV
- Python 影像處理 OpenCV (4):影像算數運算以及修改顏色空間PythonOpenCV
- OpenCV學習筆記(4)——mixChannels函式OpenCV筆記函式
- OpenCV成長之路(4):影象直方圖OpenCV直方圖
- 學習OpenCV:濾鏡系列(4)——波浪:正弦OpenCV
- python OpenCV加法操作的實現PythonOpenCV
- OpenCV-Python 視訊讀取OpenCVPython
- 影象二值化(python+opencv)PythonOpenCV
- Python的Opencv庫怎麼裝PythonOpenCV
- OpenCV_python全屏顯示影像OpenCVPython
- Python3 安裝Opencv方法PythonOpenCV
- MacOS配置Python+Opencv環境MacPythonOpenCV
- Python的OpenCV轉換影像大小PythonOpenCV
- Python opencv 將一張圖片任意 N 等分並儲存_opencv 等分影像PythonOpenCV
- Python 影像處理 OpenCV (1):入門PythonOpenCV
- python opencv讀取網路圖片PythonOpenCV
- python使用OpenCV設定圖片尺寸PythonOpenCV
- OpenCV&&python_影象平滑(Smoothing Images)OpenCVPython
- Python opencv USB攝像頭 讀寫PythonOpenCV
- 解決OpenCV(4.1.2) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:376: error: (-215:AOpenCVProjectPythonGUIError
- Python 影像處理 OpenCV (15):影像輪廓PythonOpenCV