python3+opencv生成不規(guī)則黑白mask實(shí)例
更新時(shí)間:2020年02月19日 10:12:26 作者:watersink
今天小編就為大家分享一篇python3+opencv生成不規(guī)則黑白mask實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
廢話不多說,直接上代碼吧!
# -*- coding: utf-8 -*-
import cv2
import numpy as np
# -----------------------鼠標(biāo)操作相關(guān)------------------------------------------
lsPointsChoose = []
tpPointsChoose = []
pointsCount = 0
count = 0
pointsMax = 10
def on_mouse(event, x, y, flags, param):
global img, point1, point2, count, pointsMax
global lsPointsChoose, tpPointsChoose # 存入選擇的點(diǎn)
global pointsCount # 對(duì)鼠標(biāo)按下的點(diǎn)計(jì)數(shù)
global img2, ROI_bymouse_flag
img2 = img.copy() # 此行代碼保證每次都重新再原圖畫 避免畫多了
# -----------------------------------------------------------
# count=count+1
# print("callback_count",count)
# --------------------------------------------------------------
if event == cv2.EVENT_LBUTTONDOWN: # 左鍵點(diǎn)擊
pointsCount = pointsCount + 1
# 感覺這里沒有用?2018年8月25日20:06:42
# 為了保存繪制的區(qū)域,畫的點(diǎn)稍晚清零
# if (pointsCount == pointsMax + 1):
# pointsCount = 0
# tpPointsChoose = []
print('pointsCount:', pointsCount)
point1 = (x, y)
print (x, y)
# 畫出點(diǎn)擊的點(diǎn)
cv2.circle(img2, point1, 10, (0, 255, 0), 2)
# 將選取的點(diǎn)保存到list列表里
lsPointsChoose.append([x, y]) # 用于轉(zhuǎn)化為darry 提取多邊形ROI
tpPointsChoose.append((x, y)) # 用于畫點(diǎn)
# ----------------------------------------------------------------------
# 將鼠標(biāo)選的點(diǎn)用直線連起來
print(len(tpPointsChoose))
for i in range(len(tpPointsChoose) - 1):
print('i', i)
cv2.line(img2, tpPointsChoose[i], tpPointsChoose[i + 1], (0, 0, 255), 2)
# ----------------------------------------------------------------------
# ----------點(diǎn)擊到pointMax時(shí)可以提取去繪圖----------------
if (pointsCount == pointsMax):
# -----------繪制感興趣區(qū)域-----------
ROI_byMouse()
ROI_bymouse_flag = 1
lsPointsChoose = []
cv2.imshow('src', img2)
# -------------------------右鍵按下清除軌跡-----------------------------
if event == cv2.EVENT_RBUTTONDOWN: # 右鍵點(diǎn)擊
print("right-mouse")
pointsCount = 0
tpPointsChoose = []
lsPointsChoose = []
print(len(tpPointsChoose))
for i in range(len(tpPointsChoose) - 1):
print('i', i)
cv2.line(img2, tpPointsChoose[i], tpPointsChoose[i + 1], (0, 0, 255), 2)
cv2.imshow('src', img2)
def ROI_byMouse():
global src, ROI, ROI_flag, mask2
mask = np.zeros(img.shape, np.uint8)
pts = np.array([lsPointsChoose], np.int32) # pts是多邊形的頂點(diǎn)列表(頂點(diǎn)集)
pts = pts.reshape((-1, 1, 2))
# 這里 reshape 的第一個(gè)參數(shù)為-1, 表明這一維的長(zhǎng)度是根據(jù)后面的維度的計(jì)算出來的。
# OpenCV中需要先將多邊形的頂點(diǎn)坐標(biāo)變成頂點(diǎn)數(shù)×1×2維的矩陣,再來繪制
# --------------畫多邊形---------------------
mask = cv2.polylines(mask, [pts], True, (255, 255, 255))
##-------------填充多邊形---------------------
mask2 = cv2.fillPoly(mask, [pts], (255, 255, 255))
cv2.imshow('mask', mask2)
cv2.imwrite('mask.jpg', mask2)
ROI = cv2.bitwise_and(mask2, img)
#cv2.imwrite('ROI.bmp', ROI)
#cv2.imshow('ROI', ROI)
# -----------------------定點(diǎn)ROI繪制,程序中未使用-------------------
def fixed_ROI():
mask = np.zeros(img.shape, np.uint8)
pts = np.array([[x1, y1], [x2, y2], [x3, y3], [x4, y4]], np.int32) # 頂點(diǎn)集
pts = pts.reshape((-1, 1, 2))
mask = cv2.polylines(mask, [pts], True, (255, 255, 255))
mask2 = cv2.fillPoly(mask, [pts], (255, 255, 255))
cv2.imshow('mask', mask2)
# cv2.imwrite('mask.bmp', mask2)
# cv2.drawContours(mask,points,-1,(255,255,255),-1)
ROI = cv2.bitwise_and(mask2, img)
cv2.imshow('ROI', ROI)
# cv2.imwrite('ROI.bmp', ROI)
img = cv2.imread('yuantu.jpg')
# ---------------------------------------------------------
# --圖像預(yù)處理,設(shè)置其大小
# height, width = img.shape[:2]
# size = (int(width * 0.3), int(height * 0.3))
# img = cv2.resize(img, size, interpolation=cv2.INTER_AREA)
# ------------------------------------------------------------
ROI = img.copy()
cv2.namedWindow('src')
cv2.setMouseCallback('src', on_mouse)
cv2.imshow('src', img)
cv2.waitKey(0)

以上這篇python3+opencv生成不規(guī)則黑白mask實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python處理json文件的四個(gè)常用函數(shù)
這篇文章主要介紹了python處理json文件的四個(gè)常用函數(shù),主要包括json.load()和json.dump()及json.loads()還有json.dumps(),需要的朋友可以參考一下2022-07-07
Python自動(dòng)化測(cè)試中yaml文件讀取操作
這篇文章主要介紹了Python自動(dòng)化測(cè)試中yaml文件讀取操作,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
一個(gè)基于flask的web應(yīng)用誕生 記錄用戶賬戶登錄狀態(tài)(6)
一個(gè)基于flask的web應(yīng)用誕生第六篇,這篇文章主要介紹了記錄用戶賬戶登錄狀態(tài)功能開發(fā),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
基于tensorflow指定GPU運(yùn)行及GPU資源分配的幾種方式小結(jié)
今天小編就為大家分享一篇基于tensorflow指定GPU運(yùn)行及GPU資源分配的幾種方式小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-02-02

