您好,登錄后才能下訂單哦!
廢話不多說,直接上代碼吧!
# -*- coding: utf-8 -*- import cv2 import numpy as np # -----------------------鼠標操作相關------------------------------------------ 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 # 存入選擇的點 global pointsCount # 對鼠標按下的點計數 global img2, ROI_bymouse_flag img2 = img.copy() # 此行代碼保證每次都重新再原圖畫 避免畫多了 # ----------------------------------------------------------- # count=count+1 # print("callback_count",count) # -------------------------------------------------------------- if event == cv2.EVENT_LBUTTONDOWN: # 左鍵點擊 pointsCount = pointsCount + 1 # 感覺這里沒有用?2018年8月25日20:06:42 # 為了保存繪制的區域,畫的點稍晚清零 # if (pointsCount == pointsMax + 1): # pointsCount = 0 # tpPointsChoose = [] print('pointsCount:', pointsCount) point1 = (x, y) print (x, y) # 畫出點擊的點 cv2.circle(img2, point1, 10, (0, 255, 0), 2) # 將選取的點保存到list列表里 lsPointsChoose.append([x, y]) # 用于轉化為darry 提取多邊形ROI tpPointsChoose.append((x, y)) # 用于畫點 # ---------------------------------------------------------------------- # 將鼠標選的點用直線連起來 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) # ---------------------------------------------------------------------- # ----------點擊到pointMax時可以提取去繪圖---------------- if (pointsCount == pointsMax): # -----------繪制感興趣區域----------- ROI_byMouse() ROI_bymouse_flag = 1 lsPointsChoose = [] cv2.imshow('src', img2) # -------------------------右鍵按下清除軌跡----------------------------- if event == cv2.EVENT_RBUTTONDOWN: # 右鍵點擊 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是多邊形的頂點列表(頂點集) pts = pts.reshape((-1, 1, 2)) # 這里 reshape 的第一個參數為-1, 表明這一維的長度是根據后面的維度的計算出來的。 # OpenCV中需要先將多邊形的頂點坐標變成頂點數×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) # -----------------------定點ROI繪制,程序中未使用------------------- def fixed_ROI(): mask = np.zeros(img.shape, np.uint8) pts = np.array([[x1, y1], [x2, y2], [x3, y3], [x4, y4]], np.int32) # 頂點集 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') # --------------------------------------------------------- # --圖像預處理,設置其大小 # 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生成不規則黑白mask實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。