当前位置:网站首页>Opencv中的GrabCut图像分割
Opencv中的GrabCut图像分割
2022-06-25 09:32:00 【Keep_Trying_Go】
文章目录
1.GrabCut
通过交互的方式获得前景物体;
原理:
(1)用户指定前景的大体区域,剩下的为背景区域;
(2)用户也可以明确指定某些地方为前景或者背景;
(3)GrabCut采用分段迭代的方式分析前景物体形成模型树;
(4)通过权重决定某个像素是前景还是背景;
grabCut(img, mask, rect, bgdModel, fgdModel, iterCount, mode=None):
Img:输入的原始图像;
Mask:分割之后产生的掩码;
Rect:使用鼠标选取的区域大小;
bgdModel:np.float64 ;大小为1-65且为零的数组;
fgdModel:np.float64 ;大小为1-65且为零的数组;
Intercount:迭代的次数;
Mode:GC_INIT_WITH_RECT;GC_INIT_WITH_MASK;
输出的Mask对应的每个像素值:
BGD:背景-0;
FGD:前景-1;
PR_BGD:可能是背景-2;
PR_FGD:可能是前景-3;
import os
import cv2
import numpy as np
#定义结构类
class App:
flag=False
startX=0
startY=0
rect=(0,0,0,0)
def onmouse(self,event,x,y,flags,param):
if(event&cv2.EVENT_LBUTTONDOWN==cv2.EVENT_LBUTTONDOWN):
self.flag=True
self.startX=x
self.startY=y
print('按下左键')
elif(event&cv2.EVENT_LBUTTONUP==cv2.EVENT_LBUTTONUP):
self.flag=False
cv2.rectangle(self.img,pt1=(self.startX,self.startX),pt2=(x,y),color=(0,255,0),thickness=3)
#构造rect
self.rect=(min(self.startX,x),min(self.startY,y),abs(self.startX-x),abs(self.startY-y))
print('弹起左键')
elif(event&cv2.EVENT_MOUSEMOVE==cv2.EVENT_MOUSEMOVE):
if self.flag==True:
#每一次移动都是在最开始的图片中绘制
self.img=self.img2.copy()
cv2.rectangle(self.img, pt1=(self.startX, self.startX), pt2=(x, y), color=(0, 255, 0), thickness=3)
print('鼠标移动')
def run(self):
cv2.namedWindow(winname='input',flags=cv2.WINDOW_AUTOSIZE)
cv2.setMouseCallback('input',self.onmouse)
self.img=cv2.imread('images/hometomn1.jpg')
self.img=cv2.resize(src=self.img,dsize=(450,450))
self.img2=self.img.copy()
#构造mask
self.mask=np.zeros(shape=(self.img.shape[0],self.img.shape[1]),dtype=np.uint8)
self.output=np.zeros(shape=(self.img.shape[0],self.img.shape[1],self.img.shape[2]),dtype=np.uint8)
while True:
cv2.imshow('input',self.img)
cv2.imshow('output',self.output)
key=cv2.waitKey(100)
if (key&0xFF==27):
break
if key==ord('s'):
bgmodel=np.zeros((1,65),dtype=np.float64)
cv2.grabCut(img=self.img2,mask=self.mask,rect=self.rect,bgdModel=bgmodel,
fgdModel=bgmodel,iterCount=1,mode=cv2.GC_INIT_WITH_RECT)
#当判断为前景时,为255,否则为0
mask2=np.where((self.mask==1)|(self.mask==3),255,0).astype('uint8')
self.output=cv2.bitwise_and(src1=self.img2,src2=self.img2,mask=mask2)
cv2.destroyAllWindows()
if __name__ == '__main__':
print('Pycharm')
app=App()
print(app.run())
演示
边栏推荐
- 华泰证券在上面开户安全吗?靠谱吗?
- Huipay international permet au commerce électronique transfrontalier de devenir une plate - forme de paiement transfrontalière conforme!
- 匯付國際為跨境電商賦能:做合規的跨境支付平臺!
- 3、 Automatically terminate training
- 在指南针上面开户好不好,安不安全?
- Reasons for Meiye to choose membership system
- 《JVM》对象内存分配的TLAB机制与G1中的TLAB流程
- 35 websites ICER must know
- SQL高级
- Voiceprint Technology (I): the past and present life of voiceprint Technology
猜你喜欢

CSV parameterization in JMeter

Matplotlib simple logistic regression visualization

Specific usage of sklearn polynomialfeatures
![[competition - Rural Revitalization] experience sharing of Zhejiang Rural Revitalization creative competition](/img/b4/84c30ed112c4dffd8d51697b2f4a4f.jpg)
[competition - Rural Revitalization] experience sharing of Zhejiang Rural Revitalization creative competition

瑞吉外卖项目(二)

Wallys/MULTI-FUNCTION IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL

Format analysis and explanation of wav file

vscode试图过程写入管道不存在

The meshgrid() function in numpy
![[untitled] * * database course design: complete the student information management system in three days**](/img/69/762819fa1b11085a7e36c95acbe880.png)
[untitled] * * database course design: complete the student information management system in three days**
随机推荐
[smart agriculture program] smart agriculture small program project is currently popular.
Matplotlib decision boundary drawing function plot in Matplotlib_ decision_ Boundary and plt Detailed explanation of contour function
Cubemx stm32f105rb USB flash drive reading and writing detailed tutorial
matplotlib 简单逻辑回归可视化
Mapping mode of cache
1、 Construction of single neural network
华泰证券在上面开户安全吗?靠谱吗?
Wechat official account can reply messages normally, but it still prompts that the service provided by the official account has failed. Please try again later
203 postgraduate entrance examination Japanese self-study postgraduate entrance examination experience post; Can I learn Japanese by myself?
4、 Convolution neural networks
富时A50开户什么地方安全
C # startup program loses double quotation marks for parameters passed. How to solve it?
Is it safe to open a stock account on the compass?
手机办理长城证券开户靠谱安全吗?
股票在线开户安全吗?找谁可以办理?
SQL advanced
Voiceprint Technology (IV): Engineering deployment of voiceprint recognition
CYCA少儿形体礼仪 乐清市培训成果考核圆满落幕
Is it safe to open an account in a mobile phone or a securities company?
pmp考试题型需要注意哪些?