gpt4 book ai didi

python - 读取highpiexl图像并使用GrabCut获取片段

转载 作者:行者123 更新时间:2023-12-02 16:34:26 25 4
gpt4 key购买 nike

当我从https://github.com/fengxianghu/GrabCut-1读取代码为grabcut_opencv.py 的图片时,发生以下情况:
1.第一次无法读取图片
2.鼠标事件不响应

我尝试了以下

1:将cv.namedwindow()的默认值设置为1,但是它不起作用

cv.namedWindow('output', 1)  
cv.namedWindow('input', 1)
  • 将namedwindow()移入while循环是没有用的
  •      img = cv.imread(filename)
    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

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

    while(1):
    cv.namedWindow('output', 1)
    cv.namedWindow('input', 1)

    cv.setMouseCallback('input', onmouse)
    cv.moveWindow('input', img.shape[1] + 10, 90)

    cv.imshow('output',output)

    cv.imshow('input',img)

    你有什么考虑
    我的照片也附上了
    My picture

    最佳答案

    我想我解决了这个难题:

    删除行:

    cv.moveWindow('input',img.shape[1]+10,90)

    上面的代码将输入图像放置在输出图像的正确尺寸上。
    由于您输入的图像很大,因此 input窗口的位置在屏幕外部,因此不可见。

    您也可以在读取图像后缩小图像尺寸。
    例:
    img = cv.imread(filename)之后,调整图像大小:
    img = cv.resize(img, (img.shape[1]//8, img.shape[0]//8), interpolation=cv.INTER_AREA)

    更新:

    您可以在每个轴上将图像缩小1/8倍(例如)。
  • 显示较低分辨率的图像,并在较低分辨率的图像上进行选择。
  • 在全分辨率图像上应用GrabCut算法。

  • 这是 grabcut_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()

    注意:
    我很有可能错过了很少的案例(当从小分辨率图像转换为全分辨率然后返回时)。

    关于python - 读取highpiexl图像并使用GrabCut获取片段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61160175/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com