python OpenCV GrabCut使用實(shí)例解析
這篇文章主要介紹了python OpenCV GrabCut使用實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
先上一個(gè)效果圖:

使用Python3.7+OpenCV 3.x.
需要引入 numpy庫。
在圖上用鼠標(biāo)左鍵和右鍵標(biāo)記前景和后景即可.
如果需要重新標(biāo)記圖像,關(guān)閉程序重新運(yùn)行.
以下是具體實(shí)現(xiàn)代碼
# -*- coding:utf-8 -*-
'''
Python: 3.5.7
opencv 3.x
在圖上用鼠標(biāo)左鍵和右鍵標(biāo)記前景和后景即可.
如果需要重新標(biāo)記圖像,關(guān)閉程序重新運(yùn)行.
By Ynxf http://www.zhouws.com
'''
import cv2
import numpy as np
import time
img_src = '../test_images/3.jpg'
drawing = False
mode = False
class GrabCut:
def __init__(self, t_img):
self.img = t_img
self.img_raw = img.copy()
self.img_width = img.shape[0]
self.img_height = img.shape[1]
self.scale_size = 640 * self.img_width // self.img_height
if self.img_width > 640:
self.img = cv2.resize(self.img, (640, self.scale_size), interpolation=cv2.INTER_AREA)
self.img_show = self.img.copy()
self.img_gc = self.img.copy()
self.img_gc = cv2.GaussianBlur(self.img_gc, (3, 3), 0)
self.lb_up = False
self.rb_up = False
self.lb_down = False
self.rb_down = False
self.mask = np.full(self.img.shape[:2], 2, dtype=np.uint8)
self.firt_choose = True
# 鼠標(biāo)的回調(diào)函數(shù)
def mouse_event2(event, x, y, flags, param):
global drawing, last_point, start_point
# 左鍵按下:開始畫圖
if event == cv2.EVENT_LBUTTONDOWN:
drawing = True
last_point = (x, y)
start_point = last_point
param.lb_down = True
print('mouse lb down')
elif event == cv2.EVENT_RBUTTONDOWN:
drawing = True
last_point = (x, y)
start_point = last_point
param.rb_down = True
print('mouse rb down')
# 鼠標(biāo)移動(dòng),畫圖
elif event == cv2.EVENT_MOUSEMOVE:
if drawing:
if param.lb_down:
cv2.line(param.img_show, last_point, (x,y), (0, 0, 255), 2, -1)
cv2.rectangle(param.mask, last_point, (x, y), 1, -1, 4)
else:
cv2.line(param.img_show, last_point, (x, y), (255, 0, 0), 2, -1)
cv2.rectangle(param.mask, last_point, (x, y), 0, -1, 4)
last_point = (x, y)
# 左鍵釋放:結(jié)束畫圖
elif event == cv2.EVENT_LBUTTONUP:
drawing = False
param.lb_up = True
param.lb_down = False
cv2.line(param.img_show, last_point, (x,y), (0, 0, 255), 2, -1)
if param.firt_choose:
param.firt_choose = False
cv2.rectangle(param.mask, last_point, (x,y), 1, -1, 4)
print('mouse lb up')
elif event == cv2.EVENT_RBUTTONUP:
drawing = False
param.rb_up = True
param.rb_down = False
cv2.line(param.img_show, last_point, (x,y), (255, 0, 0), 2, -1)
if param.firt_choose:
param.firt_choose = False
param.mask = np.full(param.img.shape[:2], 3, dtype=np.uint8)
cv2.rectangle(param.mask, last_point, (x,y), 0, -1, 4)
print('mouse rb up')
if __name__ == '__main__':
img = cv2.imread(img_src)
if img is None:
print('error: 圖像為空')
g_img = GrabCut(img)
cv2.namedWindow('image')
# 定義鼠標(biāo)的回調(diào)函數(shù)
cv2.setMouseCallback('image', mouse_event2, g_img)
while (True):
cv2.imshow('image', g_img.img_show)
if g_img.lb_up or g_img.rb_up:
g_img.lb_up = False
g_img.rb_up = False
start = time.process_time()
bgdModel = np.zeros((1, 65), np.float64)
fgdModel = np.zeros((1, 65), np.float64)
rect = (1, 1, g_img.img.shape[1], g_img.img.shape[0])
print(g_img.mask)
mask = g_img.mask
g_img.img_gc = g_img.img.copy()
cv2.grabCut(g_img.img_gc, mask, rect, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_MASK)
elapsed = (time.process_time() - start)
mask2 = np.where((mask == 2) | (mask == 0), 0, 1).astype('uint8') # 0和2做背景
g_img.img_gc = g_img.img_gc * mask2[:, :, np.newaxis] # 使用蒙板來獲取前景區(qū)域
cv2.imshow('result', g_img.img_gc)
print("Time used:", elapsed)
# 按下ESC鍵退出
if cv2.waitKey(20) == 27:
break
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python計(jì)算時(shí)間間隔(精確到微妙)的代碼實(shí)例
今天小編就為大家分享一篇關(guān)于Python計(jì)算時(shí)間間隔(精確到微妙)的代碼實(shí)例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-02-02
python?pygame實(shí)現(xiàn)打磚塊游戲
這篇文章主要為大家詳細(xì)介紹了python?pygame實(shí)現(xiàn)打磚塊游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
Python 用matplotlib畫以時(shí)間日期為x軸的圖像
這篇文章主要介紹了Python 用matplotlib畫以時(shí)間日期為x軸的圖像,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
理解生產(chǎn)者消費(fèi)者模型及在Python編程中的運(yùn)用實(shí)例
生產(chǎn)者消費(fèi)者模型一般用于體現(xiàn)程序的多線程并發(fā)性,Python的多線程雖然受到GIL控制,但依然可以構(gòu)建隊(duì)列來簡(jiǎn)單體現(xiàn)出模型的思路,這里我們就來共同理解生產(chǎn)者消費(fèi)者模型及在Python編程中的運(yùn)用實(shí)例:2016-06-06
python利用小波分析進(jìn)行特征提取的實(shí)例
今天小編就為大家分享一篇python利用小波分析進(jìn)行特征提取的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01
Python面向?qū)ο蟮娜筇匦苑庋b、繼承、多態(tài)
這篇文章介紹了Python面向?qū)ο蟮娜筇匦苑庋b、繼承、多態(tài),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
python中實(shí)現(xiàn)json數(shù)據(jù)和類對(duì)象相互轉(zhuǎn)化的四種方式
在日常的軟件測(cè)試過程中,測(cè)試數(shù)據(jù)的構(gòu)造是一個(gè)占比非常大的活動(dòng),對(duì)于測(cè)試數(shù)據(jù)的構(gòu)造,分為結(jié)構(gòu)化的數(shù)據(jù)構(gòu)造方式和非結(jié)構(gòu)化的數(shù)據(jù)構(gòu)造方式,此篇文章,會(huì)通過4種方式來展示json數(shù)據(jù)與python的類對(duì)象相互轉(zhuǎn)化,需要的朋友可以參考下2024-07-07

