溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

python3+opencv生成不規(guī)則黑白mask實(shí)例

發(fā)布時(shí)間:2020-09-22 17:15:00 來(lái)源:腳本之家 閱讀:210 作者:watersink 欄目:開發(fā)技術(shù)

廢話不多說(shuō),直接上代碼吧!

# -*- 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)用直線連起來(lái)
  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ì)算出來(lái)的。
 # OpenCV中需要先將多邊形的頂點(diǎn)坐標(biāo)變成頂點(diǎn)數(shù)×1×2維的矩陣,再來(lái)繪制
 
 # --------------畫多邊形---------------------
 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í)例

以上這篇python3+opencv生成不規(guī)則黑白mask實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI