python3+opencv生成不規(guī)則黑白mask實(shí)例
廢話不多說,直接上代碼吧!
# -*- 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 # 對鼠標(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, 表明這一維的長度是根據(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í)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python 異步之如何保護(hù)任務(wù)免于取消詳解
這篇文章主要為大家介紹了Python 異步之如何保護(hù)任務(wù)免于取消示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03python實(shí)現(xiàn)比較類的兩個(gè)instance(對象)是否相等的方法分析
這篇文章主要介紹了python實(shí)現(xiàn)比較類的兩個(gè)instance(對象)是否相等的方法,結(jié)合實(shí)例形式分析了Python判斷類的實(shí)例是否相等的判斷操作實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-06-06pandas.dataframe按行索引表達(dá)式選取方法
今天小編就為大家分享一篇pandas.dataframe按行索引表達(dá)式選取方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10用python實(shí)現(xiàn)操縱mysql數(shù)據(jù)庫插入
大家好,本篇文章主要講的是用python實(shí)現(xiàn)操縱mysql數(shù)據(jù)庫插入,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01pycharm專業(yè)版遠(yuǎn)程登錄服務(wù)器的詳細(xì)教程
這篇文章主要介紹了pycharm專業(yè)版遠(yuǎn)程登錄服務(wù)器的詳細(xì)教程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09PyCharm 2021.2 (Professional)調(diào)試遠(yuǎn)程服務(wù)器程序的操作技巧
本文給大家分享用 PyCharm 2021 調(diào)試遠(yuǎn)程服務(wù)器程序的過程,通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-08-08python格式化字符串的實(shí)戰(zhàn)教程(使用占位符、format方法)
我們經(jīng)常會用到%-formatting和str.format()來格式化,下面這篇文章主要給大家介紹了關(guān)于python格式化字符串的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08