Python g2=收缩的图像复制() #输入和输出窗口 cv.namedWindow(“输出”) cv.namedWindow('input') cv.setMouseCallback('input',onmouse) cv.moveWindow('input',收缩形状[1]+10,90)#cv.moveWindow('input',形状[1]+10,90) 打印(“说明:\n”) 打印(“使用鼠标右键在对象周围绘制一个矩形\n”) 而(一): #在显示之前,将图像在每个轴上收缩8倍: 收缩_输出=cv.resize(输出,(output.shape[1]//8,output.shape[0]//8),插值=cv.INTER_区域) #收缩img=cv.resize(img,(img.shape[1]//8,img.shape[0]//8),插值=cv.INTER\u区域) #显示缩小的图像,而不是全分辨率图像 cv.imshow(“输出”,收缩输出) cv.imshow('输入',收缩\u img) k=等速等待键(1) #密钥绑定 如果k==27:#退出esc 打破 elif k==ord('0'):#BG图纸 打印(“用鼠标左键标记背景区域\n”) 值=绘图 elif k==ord('1'):#FG图纸 打印(“用鼠标左键标记前景区域\n”) 值=绘图\u FG elif k==ord('2'):#PR#BG图纸 值=绘图\u PR\u BG elif k==ord('3'):#PR#FG图纸 值=绘图\u PR\u FG elif k==ord('s'):#保存图像 条形=np.零((形状[0],5,3),np.uint8) res=np.hstack((img2,bar,img,bar,输出)) cv.imwrite('grabcut\u output.png',res) 打印(“结果另存为图像\n”) elif k==ord('r'):#重置所有内容 打印(“重置\n”) rect=(0,0,1,1) 绘图=错误 矩形=假 矩形或矩形遮罩=100 rect_over=False 值=绘图\u FG img=img2.copy() 掩码=np.zero(img.shape[:2],dtype=np.uint8)#掩码初始化为PR_BG 输出=np.零(图像形状,np.uint8)#显示输出图像 elif k==ord('n'):#分割图像 打印(“”)对于更精细的修饰,按0-3键后标记前景和背景 然后再次按“n”\n“)) 打印(矩形) 如果(rect_或_mask==0):#使用rect抓取切割 bgdmodel=np.zero((1,65),np.float64) fgdmodel=np.zero((1,65),np.float64) cv.grabCut(img2、掩码、矩形、bgdmodel、fgdmodel、1、cv.GC_INIT_WITH_rect) 矩形或矩形掩码=1 elif rect_或_mask==1:#带mask的grabcut bgdmodel=np.zero((1,65),np.float64) fgdmodel=np.zero((1,65),np.float64) cv.grabCut(img2,掩码,rect,bgdmodel,fgdmodel,1,cv.GC_INIT_,带掩码) mask2=np.where((mask==1)+(mask==3),255,0).astype('uint8') 输出=cv.按位_和(img2、img2、掩码=mask2) cv.destroyAllWindows()

Python g2=收缩的图像复制() #输入和输出窗口 cv.namedWindow(“输出”) cv.namedWindow('input') cv.setMouseCallback('input',onmouse) cv.moveWindow('input',收缩形状[1]+10,90)#cv.moveWindow('input',形状[1]+10,90) 打印(“说明:\n”) 打印(“使用鼠标右键在对象周围绘制一个矩形\n”) 而(一): #在显示之前,将图像在每个轴上收缩8倍: 收缩_输出=cv.resize(输出,(output.shape[1]//8,output.shape[0]//8),插值=cv.INTER_区域) #收缩img=cv.resize(img,(img.shape[1]//8,img.shape[0]//8),插值=cv.INTER\u区域) #显示缩小的图像,而不是全分辨率图像 cv.imshow(“输出”,收缩输出) cv.imshow('输入',收缩\u img) k=等速等待键(1) #密钥绑定 如果k==27:#退出esc 打破 elif k==ord('0'):#BG图纸 打印(“用鼠标左键标记背景区域\n”) 值=绘图 elif k==ord('1'):#FG图纸 打印(“用鼠标左键标记前景区域\n”) 值=绘图\u FG elif k==ord('2'):#PR#BG图纸 值=绘图\u PR\u BG elif k==ord('3'):#PR#FG图纸 值=绘图\u PR\u FG elif k==ord('s'):#保存图像 条形=np.零((形状[0],5,3),np.uint8) res=np.hstack((img2,bar,img,bar,输出)) cv.imwrite('grabcut\u output.png',res) 打印(“结果另存为图像\n”) elif k==ord('r'):#重置所有内容 打印(“重置\n”) rect=(0,0,1,1) 绘图=错误 矩形=假 矩形或矩形遮罩=100 rect_over=False 值=绘图\u FG img=img2.copy() 掩码=np.zero(img.shape[:2],dtype=np.uint8)#掩码初始化为PR_BG 输出=np.零(图像形状,np.uint8)#显示输出图像 elif k==ord('n'):#分割图像 打印(“”)对于更精细的修饰,按0-3键后标记前景和背景 然后再次按“n”\n“)) 打印(矩形) 如果(rect_或_mask==0):#使用rect抓取切割 bgdmodel=np.zero((1,65),np.float64) fgdmodel=np.zero((1,65),np.float64) cv.grabCut(img2、掩码、矩形、bgdmodel、fgdmodel、1、cv.GC_INIT_WITH_rect) 矩形或矩形掩码=1 elif rect_或_mask==1:#带mask的grabcut bgdmodel=np.zero((1,65),np.float64) fgdmodel=np.zero((1,65),np.float64) cv.grabCut(img2,掩码,rect,bgdmodel,fgdmodel,1,cv.GC_INIT_,带掩码) mask2=np.where((mask==1)+(mask==3),255,0).astype('uint8') 输出=cv.按位_和(img2、img2、掩码=mask2) cv.destroyAllWindows(),python,opencv,Python,Opencv,注: 有一个很好的机会,我错过了几个案例(当从小分辨率图像转换为全分辨率图像和返回时) 我想我解决了这个难题: 删除该行: cv.moveWindow('input',img.shape[1]+10,90) 上述代码将输入图像放置在输出图像的正确大小上。 因为您的输入图像很大,输入窗口的位置在屏幕之外,不可见 您也可以在读取图像后减小图像大小。 示例: 在img=cv.imread(文件名)之后,调整图像大小: img = cv.resize(img, (img.shape[1]//8, im

注:

有一个很好的机会,我错过了几个案例(当从小分辨率图像转换为全分辨率图像和返回时)

我想我解决了这个难题:

删除该行:

cv.moveWindow('input',img.shape[1]+10,90)
上述代码将输入图像放置在输出图像的正确大小上。
因为您的输入图像很大,
输入
窗口的位置在屏幕之外,不可见

您也可以在读取图像后减小图像大小。
示例:
img=cv.imread(文件名)
之后,调整图像大小:

img = cv.resize(img, (img.shape[1]//8, img.shape[0]//8), interpolation=cv.INTER_AREA)

更新: 您可以在每个轴上按1/8的系数调整图像的大小(例如)

  • 显示低分辨率图像,并在低分辨率图像上进行选择
  • 在全分辨率图像上应用GrabCut算法
这是grabcut\u opencv.py的一个修改版本:

#!/usr/bin/env python
'''
===============================================================================
Interactive Image Segmentation using GrabCut algorithm.

This sample shows interactive image segmentation using grabcut algorithm.

USAGE:
    python grabcut.py <filename>

README FIRST:
    Two windows will show up, one for input and one for output.

    At first, in input window, draw a rectangle around the object using
mouse right button. Then press 'n' to segment the object (once or a few times)
For any finer touch-ups, you can press any of the keys below and draw lines on
the areas you want. Then again press 'n' for updating the output.

Key '0' - To select areas of sure background
Key '1' - To select areas of sure foreground
Key '2' - To select areas of probable background
Key '3' - To select areas of probable foreground

Key 'n' - To update the segmentation
Key 'r' - To reset the setup
Key 's' - To save the results
===============================================================================
'''

# Python 2/3 compatibility
from __future__ import print_function

import numpy as np
import cv2 as cv
import sys

BLUE = [255,0,0]        # rectangle color
RED = [0,0,255]         # PR BG
GREEN = [0,255,0]       # PR FG
BLACK = [0,0,0]         # sure BG
WHITE = [255,255,255]   # sure FG

DRAW_BG = {'color' : BLACK, 'val' : 0}
DRAW_FG = {'color' : WHITE, 'val' : 1}
DRAW_PR_FG = {'color' : GREEN, 'val' : 3}
DRAW_PR_BG = {'color' : RED, 'val' : 2}

# setting up flags
rect = (0,0,1,1)
drawing = False         # flag for drawing curves
rectangle = False       # flag for drawing rect
rect_over = False       # flag to check if rect drawn
rect_or_mask = 100      # flag for selecting rect or mask mode
value = DRAW_FG         # drawing initialized to FG
thickness = 3           # brush thickness

def onmouse(event,x,y,flags,param):
    global img,img2,drawing,value,mask,rectangle,rect,rect_or_mask,ix,iy,rect_over
    global shrunk_img, shrunk_img2, shrunk_output

    # Draw Rectangle
    if event == cv.EVENT_RBUTTONDOWN:
        rectangle = True
        ix,iy = x,y

    elif event == cv.EVENT_MOUSEMOVE:
        if rectangle == True:
            shrunk_img = shrunk_img2.copy() #img = img2.copy()
            # Draw the rectangle on the shrunk image.
            cv.rectangle(shrunk_img, (ix,iy), (x,y), BLUE, 2) #cv.rectangle(img,(ix,iy),(x,y),BLUE,2)
            # Multiply coordinates by 8 because the selection was performed on the shrunk image
            rect = (min(ix*8,x*8),min(iy*8,y*8),abs(ix*8-x*8),abs(iy*8-y*8)) #rect = (min(ix,x),min(iy,y),abs(ix-x),abs(iy-y))
            rect_or_mask = 0

    elif event == cv.EVENT_RBUTTONUP:
        rectangle = False
        rect_over = True
        cv.rectangle(shrunk_img, (ix,iy), (x,y), BLUE, 2) #cv.rectangle(img,(ix,iy),(x,y),BLUE,2)

        # Multiply coordinates by 8 because the selection was performed on the shrunk image
        rect = (min(ix*8,x*8),min(iy*8,y*8),abs(ix*8-x*8),abs(iy*8-y*8)) #rect = (min(ix,x),min(iy,y),abs(ix-x),abs(iy-y))
        rect_or_mask = 0
        print(" Now press the key 'n' a few times until no further change \n")

    # draw touchup curves

    if event == cv.EVENT_LBUTTONDOWN:
        if rect_over == False:
            print("first draw rectangle \n")
        else:
            drawing = True
            cv.circle(img,(x,y),thickness,value['color'],-1)
            cv.circle(mask,(x,y),thickness,value['val'],-1)

    elif event == cv.EVENT_MOUSEMOVE:
        if drawing == True:
            cv.circle(img,(x,y),thickness,value['color'],-1)
            cv.circle(mask,(x,y),thickness,value['val'],-1)

    elif event == cv.EVENT_LBUTTONUP:
        if drawing == True:
            drawing = False
            cv.circle(img,(x,y),thickness,value['color'],-1)
            cv.circle(mask,(x,y),thickness,value['val'],-1)

if __name__ == '__main__':

    # print documentation
    print(__doc__)

    # Loading images
    if len(sys.argv) == 2:
        filename = sys.argv[1] # for drawing purposes
    else:
        print("No input image given, so loading default image, messi5.jpg \n")
        print("Correct Usage: python grabcut.py <filename> \n")
        filename = 'messi5.jpg'

    img = cv.imread(filename)

    #img = cv.resize(img, (img.shape[1]//8, img.shape[0]//8), interpolation=cv.INTER_AREA)

    img2 = img.copy()                               # a copy of original image
    mask = np.zeros(img.shape[:2],dtype = np.uint8) # mask initialized to PR_BG
    output = np.zeros(img.shape, np.uint8)           # output image to be shown

    # Shrink the image by a factor of 8 in each axis.
    shrunk_img = cv.resize(img, (img.shape[1]//8, img.shape[0]//8), interpolation=cv.INTER_AREA)
    shrunk_img2 = shrunk_img.copy()


    # input and output windows
    cv.namedWindow('output')
    cv.namedWindow('input')
    cv.setMouseCallback('input',onmouse)
    cv.moveWindow('input',shrunk_img.shape[1]+10,90) #cv.moveWindow('input',img.shape[1]+10,90)

    print(" Instructions: \n")
    print(" Draw a rectangle around the object using right mouse button \n")

    while(1):
        # Shrink the image by a factor of 8 in each axis before showing:
        shrunk_output = cv.resize(output, (output.shape[1]//8, output.shape[0]//8), interpolation=cv.INTER_AREA)
        #shrunk_img = cv.resize(img, (img.shape[1]//8, img.shape[0]//8), interpolation=cv.INTER_AREA)

        # Show shrunk images, instead of full resolution images
        cv.imshow('output', shrunk_output)
        cv.imshow('input', shrunk_img)
        k = cv.waitKey(1)

        # key bindings
        if k == 27:         # esc to exit
            break
        elif k == ord('0'): # BG drawing
            print(" mark background regions with left mouse button \n")
            value = DRAW_BG
        elif k == ord('1'): # FG drawing
            print(" mark foreground regions with left mouse button \n")
            value = DRAW_FG
        elif k == ord('2'): # PR_BG drawing
            value = DRAW_PR_BG
        elif k == ord('3'): # PR_FG drawing
            value = DRAW_PR_FG
        elif k == ord('s'): # save image
            bar = np.zeros((img.shape[0],5,3),np.uint8)
            res = np.hstack((img2,bar,img,bar,output))
            cv.imwrite('grabcut_output.png',res)
            print(" Result saved as image \n")
        elif k == ord('r'): # reset everything
            print("resetting \n")
            rect = (0,0,1,1)
            drawing = False
            rectangle = False
            rect_or_mask = 100
            rect_over = False
            value = DRAW_FG
            img = img2.copy()
            mask = np.zeros(img.shape[:2],dtype = np.uint8) # mask initialized to PR_BG
            output = np.zeros(img.shape,np.uint8)           # output image to be shown
        elif k == ord('n'): # segment the image
            print(""" For finer touchups, mark foreground and background after pressing keys 0-3
            and again press 'n' \n""")
            print(rect)
            if (rect_or_mask == 0):         # grabcut with rect
                bgdmodel = np.zeros((1,65),np.float64)
                fgdmodel = np.zeros((1,65),np.float64)
                cv.grabCut(img2,mask,rect,bgdmodel,fgdmodel,1,cv.GC_INIT_WITH_RECT)
                rect_or_mask = 1
            elif rect_or_mask == 1:         # grabcut with mask
                bgdmodel = np.zeros((1,65),np.float64)
                fgdmodel = np.zeros((1,65),np.float64)
                cv.grabCut(img2,mask,rect,bgdmodel,fgdmodel,1,cv.GC_INIT_WITH_MASK)

        mask2 = np.where((mask==1) + (mask==3),255,0).astype('uint8')
        output = cv.bitwise_and(img2,img2,mask=mask2)

    cv.destroyAllWindows()
#/usr/bin/env python
'''
===============================================================================
使用GrabCut算法的交互式图像分割。
此示例显示了使用grabcut算法的交互式图像分割。
用法:
python.py
自述文件优先:
将显示两个窗口,一个用于输入,一个用于输出。
首先,在输入窗口中,使用
鼠标右键。然后按“n”分割对象(一次或几次)
对于任何更精细的润色,您可以按下面的任意键并在上面画线
你想要的区域。然后再次按“n”更新输出。
按键“0”-选择背景区域
按键“1”-选择前景区域
按键“2”-选择可能背景的区域
按键“3”-选择可能前景的区域
键“n”-更新分段
键“r”-重置设置
键“s”-保存结果
===============================================================================
'''
#Python 2/3兼容性
来自未来导入打印功能
将numpy作为np导入
将cv2作为cv导入
导入系统
蓝色=[255,0,0]#矩形颜色
红色=[0,0255]#PR BG
绿色=[0255,0]#PR FG
黑色=[0,0,0]#当然可以
白色=[255255]#当然可以
DRAW_BG={'color':黑色,'val':0}
绘制_FG={'color':白色,'val':1}
绘制_PR_FG={'color':绿色,'val':3}
绘制_PR_BG={'color':红色,'val':2}
#设置旗帜
rect=(0,0,1,1)
绘图=假#绘制曲线的标志
矩形=假#用于绘制矩形的标志
rect_over=False#用于检查是否绘制了rect的标志
rect_或_mask=100#选择rect或mask模式的标志
值=绘图(最终聚集)绘图已初始化为最终聚集
厚度=3#刷子厚度
def onmouse(事件、x、y、标志、参数):
全局img、img2、绘图、值、掩码、矩形、矩形、矩形或矩形掩码、ix、iy、矩形覆盖
全局收缩\u img、收缩\u img2、收缩\u输出
#画矩形
如果event==cv.event\u RBUTTONDOWN:
矩形=真
ix,iy=x,y
elif event==cv.event\u MO
img = cv.resize(img, (img.shape[1]//8, img.shape[0]//8), interpolation=cv.INTER_AREA)
#!/usr/bin/env python
'''
===============================================================================
Interactive Image Segmentation using GrabCut algorithm.

This sample shows interactive image segmentation using grabcut algorithm.

USAGE:
    python grabcut.py <filename>

README FIRST:
    Two windows will show up, one for input and one for output.

    At first, in input window, draw a rectangle around the object using
mouse right button. Then press 'n' to segment the object (once or a few times)
For any finer touch-ups, you can press any of the keys below and draw lines on
the areas you want. Then again press 'n' for updating the output.

Key '0' - To select areas of sure background
Key '1' - To select areas of sure foreground
Key '2' - To select areas of probable background
Key '3' - To select areas of probable foreground

Key 'n' - To update the segmentation
Key 'r' - To reset the setup
Key 's' - To save the results
===============================================================================
'''

# Python 2/3 compatibility
from __future__ import print_function

import numpy as np
import cv2 as cv
import sys

BLUE = [255,0,0]        # rectangle color
RED = [0,0,255]         # PR BG
GREEN = [0,255,0]       # PR FG
BLACK = [0,0,0]         # sure BG
WHITE = [255,255,255]   # sure FG

DRAW_BG = {'color' : BLACK, 'val' : 0}
DRAW_FG = {'color' : WHITE, 'val' : 1}
DRAW_PR_FG = {'color' : GREEN, 'val' : 3}
DRAW_PR_BG = {'color' : RED, 'val' : 2}

# setting up flags
rect = (0,0,1,1)
drawing = False         # flag for drawing curves
rectangle = False       # flag for drawing rect
rect_over = False       # flag to check if rect drawn
rect_or_mask = 100      # flag for selecting rect or mask mode
value = DRAW_FG         # drawing initialized to FG
thickness = 3           # brush thickness

def onmouse(event,x,y,flags,param):
    global img,img2,drawing,value,mask,rectangle,rect,rect_or_mask,ix,iy,rect_over
    global shrunk_img, shrunk_img2, shrunk_output

    # Draw Rectangle
    if event == cv.EVENT_RBUTTONDOWN:
        rectangle = True
        ix,iy = x,y

    elif event == cv.EVENT_MOUSEMOVE:
        if rectangle == True:
            shrunk_img = shrunk_img2.copy() #img = img2.copy()
            # Draw the rectangle on the shrunk image.
            cv.rectangle(shrunk_img, (ix,iy), (x,y), BLUE, 2) #cv.rectangle(img,(ix,iy),(x,y),BLUE,2)
            # Multiply coordinates by 8 because the selection was performed on the shrunk image
            rect = (min(ix*8,x*8),min(iy*8,y*8),abs(ix*8-x*8),abs(iy*8-y*8)) #rect = (min(ix,x),min(iy,y),abs(ix-x),abs(iy-y))
            rect_or_mask = 0

    elif event == cv.EVENT_RBUTTONUP:
        rectangle = False
        rect_over = True
        cv.rectangle(shrunk_img, (ix,iy), (x,y), BLUE, 2) #cv.rectangle(img,(ix,iy),(x,y),BLUE,2)

        # Multiply coordinates by 8 because the selection was performed on the shrunk image
        rect = (min(ix*8,x*8),min(iy*8,y*8),abs(ix*8-x*8),abs(iy*8-y*8)) #rect = (min(ix,x),min(iy,y),abs(ix-x),abs(iy-y))
        rect_or_mask = 0
        print(" Now press the key 'n' a few times until no further change \n")

    # draw touchup curves

    if event == cv.EVENT_LBUTTONDOWN:
        if rect_over == False:
            print("first draw rectangle \n")
        else:
            drawing = True
            cv.circle(img,(x,y),thickness,value['color'],-1)
            cv.circle(mask,(x,y),thickness,value['val'],-1)

    elif event == cv.EVENT_MOUSEMOVE:
        if drawing == True:
            cv.circle(img,(x,y),thickness,value['color'],-1)
            cv.circle(mask,(x,y),thickness,value['val'],-1)

    elif event == cv.EVENT_LBUTTONUP:
        if drawing == True:
            drawing = False
            cv.circle(img,(x,y),thickness,value['color'],-1)
            cv.circle(mask,(x,y),thickness,value['val'],-1)

if __name__ == '__main__':

    # print documentation
    print(__doc__)

    # Loading images
    if len(sys.argv) == 2:
        filename = sys.argv[1] # for drawing purposes
    else:
        print("No input image given, so loading default image, messi5.jpg \n")
        print("Correct Usage: python grabcut.py <filename> \n")
        filename = 'messi5.jpg'

    img = cv.imread(filename)

    #img = cv.resize(img, (img.shape[1]//8, img.shape[0]//8), interpolation=cv.INTER_AREA)

    img2 = img.copy()                               # a copy of original image
    mask = np.zeros(img.shape[:2],dtype = np.uint8) # mask initialized to PR_BG
    output = np.zeros(img.shape, np.uint8)           # output image to be shown

    # Shrink the image by a factor of 8 in each axis.
    shrunk_img = cv.resize(img, (img.shape[1]//8, img.shape[0]//8), interpolation=cv.INTER_AREA)
    shrunk_img2 = shrunk_img.copy()


    # input and output windows
    cv.namedWindow('output')
    cv.namedWindow('input')
    cv.setMouseCallback('input',onmouse)
    cv.moveWindow('input',shrunk_img.shape[1]+10,90) #cv.moveWindow('input',img.shape[1]+10,90)

    print(" Instructions: \n")
    print(" Draw a rectangle around the object using right mouse button \n")

    while(1):
        # Shrink the image by a factor of 8 in each axis before showing:
        shrunk_output = cv.resize(output, (output.shape[1]//8, output.shape[0]//8), interpolation=cv.INTER_AREA)
        #shrunk_img = cv.resize(img, (img.shape[1]//8, img.shape[0]//8), interpolation=cv.INTER_AREA)

        # Show shrunk images, instead of full resolution images
        cv.imshow('output', shrunk_output)
        cv.imshow('input', shrunk_img)
        k = cv.waitKey(1)

        # key bindings
        if k == 27:         # esc to exit
            break
        elif k == ord('0'): # BG drawing
            print(" mark background regions with left mouse button \n")
            value = DRAW_BG
        elif k == ord('1'): # FG drawing
            print(" mark foreground regions with left mouse button \n")
            value = DRAW_FG
        elif k == ord('2'): # PR_BG drawing
            value = DRAW_PR_BG
        elif k == ord('3'): # PR_FG drawing
            value = DRAW_PR_FG
        elif k == ord('s'): # save image
            bar = np.zeros((img.shape[0],5,3),np.uint8)
            res = np.hstack((img2,bar,img,bar,output))
            cv.imwrite('grabcut_output.png',res)
            print(" Result saved as image \n")
        elif k == ord('r'): # reset everything
            print("resetting \n")
            rect = (0,0,1,1)
            drawing = False
            rectangle = False
            rect_or_mask = 100
            rect_over = False
            value = DRAW_FG
            img = img2.copy()
            mask = np.zeros(img.shape[:2],dtype = np.uint8) # mask initialized to PR_BG
            output = np.zeros(img.shape,np.uint8)           # output image to be shown
        elif k == ord('n'): # segment the image
            print(""" For finer touchups, mark foreground and background after pressing keys 0-3
            and again press 'n' \n""")
            print(rect)
            if (rect_or_mask == 0):         # grabcut with rect
                bgdmodel = np.zeros((1,65),np.float64)
                fgdmodel = np.zeros((1,65),np.float64)
                cv.grabCut(img2,mask,rect,bgdmodel,fgdmodel,1,cv.GC_INIT_WITH_RECT)
                rect_or_mask = 1
            elif rect_or_mask == 1:         # grabcut with mask
                bgdmodel = np.zeros((1,65),np.float64)
                fgdmodel = np.zeros((1,65),np.float64)
                cv.grabCut(img2,mask,rect,bgdmodel,fgdmodel,1,cv.GC_INIT_WITH_MASK)

        mask2 = np.where((mask==1) + (mask==3),255,0).astype('uint8')
        output = cv.bitwise_and(img2,img2,mask=mask2)

    cv.destroyAllWindows()