当前位置:网站首页>Grabcut image segmentation in opencv
Grabcut image segmentation in opencv
2022-06-25 09:41:00 【Keep_ Trying_ Go】
List of articles
1.GrabCut
Get foreground objects through interaction ;
principle :
(1) The user specifies the general area of the foreground , The rest is the background area ;
(2) Users can also specify certain places as foreground or background ;
(3)GrabCut The model tree is formed by analyzing the foreground objects in the way of subsection iteration ;
(4) Determine whether a pixel is foreground or background by weight ;
grabCut(img, mask, rect, bgdModel, fgdModel, iterCount, mode=None):
Img: Input the original image ;
Mask: The mask generated after segmentation ;
Rect: The size of the area selected with the mouse ;
bgdModel:np.float64 ; The size is 1-65 Array with zero ;
fgdModel:np.float64 ; The size is 1-65 Array with zero ;
Intercount: The number of iterations ;
Mode:GC_INIT_WITH_RECT;GC_INIT_WITH_MASK;
Output Mask Corresponding to each pixel value :
BGD: background -0;
FGD: prospects -1;
PR_BGD: Maybe the background -2;
PR_FGD: It could be the future -3;
import os
import cv2
import numpy as np
# Define structure classes
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(' Press the left key ')
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)
# structure rect
self.rect=(min(self.startX,x),min(self.startY,y),abs(self.startX-x),abs(self.startY-y))
print(' Pop up left button ')
elif(event&cv2.EVENT_MOUSEMOVE==cv2.EVENT_MOUSEMOVE):
if self.flag==True:
# Each move is drawn in the first picture
self.img=self.img2.copy()
cv2.rectangle(self.img, pt1=(self.startX, self.startX), pt2=(x, y), color=(0, 255, 0), thickness=3)
print(' Mouse movement ')
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()
# structure 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)
# When judged as foreground , by 255, Otherwise 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())
demonstration
边栏推荐
- Is it safe to open an account in a mobile phone or a securities company?
- [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate
- Notes on writing questions in C language -- monkeys eat peaches
- I put a two-dimensional code with rainbow candy
- Matplotlib decision boundary drawing function plot in Matplotlib_ decision_ Boundary and plt Detailed explanation of contour function
- 【mysql学习笔记21】存储引擎
- 【mysql学习笔记22】索引
- 在指南针上面开户好不好,安不安全?
- Is it safe to open an account with Great Wall Securities by mobile phone?
- 瑞吉外卖项目(二)
猜你喜欢
![[buuctf.reverse] 117-120](/img/6c/8a90fff2bd46f1494a9bd9c77eeafc.png)
[buuctf.reverse] 117-120

独步武林,架构选型手册(包含 PDF)

Vscode attempted to write the procedure to a pipeline that does not exist

汇付国际为跨境电商赋能:做合规的跨境支付平台!
![[final review notes] digital logic](/img/57/4431208b1c8b653f5c3f4ff4212cc3.png)
[final review notes] digital logic

On the underlying index principle of MySQL

Cubemx stm32f105rb USB flash drive reading and writing detailed tutorial
![[project part - structure and content writing of technical scheme] software system type mass entrepreneurship and innovation project plan and Xinmiao guochuang (Dachuang) application](/img/4a/1e83ea9e8e79c0ae7244a159943480.jpg)
[project part - structure and content writing of technical scheme] software system type mass entrepreneurship and innovation project plan and Xinmiao guochuang (Dachuang) application

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

I put a two-dimensional code with rainbow candy
随机推荐
请问在手机上开户股票,还是去证券公司开户安全?
Atguigu---17-life cycle
Socket programming -- epoll model
Format analysis and explanation of wav file
[buuctf.reverse] 121-125
Voiceprint Technology (IV): Engineering deployment of voiceprint recognition
Lvs-dr mode single network segment case
[competition -kab micro entrepreneurship competition] KAB National College Students' micro entrepreneurship action participation experience sharing (including the idea of writing the application form)
Is it safe to open an account in a mobile phone or a securities company?
Applet cloud development joint table data query and application in cloud function
Summarize two methods of configuring pytorch GPU environment
puzzle(019.2)六边锁
[IOU] intersection over union
Flitter gets the height of the top status bar
TLAB mechanism of JVM object memory allocation and TLAB process in G1
Rxjs TakeUntil 操作符的学习笔记
Voiceprint Technology (I): the past and present life of voiceprint Technology
Study on correlation of pumpkin price and design of price prediction model based on BP neural network
手机办理长城证券开户靠谱安全吗?
Make a skylearn high-dimensional dataset_ Circles and make_ moons