- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我从https://github.com/fengxianghu/GrabCut-1读取代码为grabcut_opencv.py 的图片时,发生以下情况:
1.第一次无法读取图片
2.鼠标事件不响应
我尝试了以下
1:将cv.namedwindow()的默认值设置为1,但是它不起作用
cv.namedWindow('output', 1)
cv.namedWindow('input', 1)
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)
最佳答案
我想我解决了这个难题:
删除行:
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)
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/
我正在尝试从 reference code 在 Android 中实现 Grabcut .但是当我运行代码时,会生成以下错误。 错误: OpenCV Error: Assertion failed (
这个问题我想了很久,但一直没有真正进行过详细的分析。是否使用 GrabCut 进行前景分割[1] 算法取决于输入图像的大小?直觉上,在我看来,由于grabcut 是基于颜色模型的,颜色分布不应随着图像
我正在使用调用函数 grabcut() 的 opencv 的 grabcut.cpp 和 graphcut.cpp 代码。我必须找到一种方法来保存从某个图像计算出的背景和前景模型并将其应用于另一个图像
我想知道下面代码中实际发生了什么。 cv::Rect rectangle(x,y,width,height); cv::Mat result; cv::Mat bgModel,fgMo
当我在图像上运行 Grabcut 时,我将抓取的边界框设置为图像本身的边缘。 但是,由于图像的底部是前景的一部分而不是背景,因此它会切掉下半部分。 有没有什么办法可以防止这种情况发生,比如只在顶部、左
我正在尝试使用 C++ 在 OpenCV 中实现抓取算法我偶然发现 this site并找到了一种非常简单的方法。不幸的是,代码似乎对我不起作用 #include "opencv2/opencv.hp
在opencv示例自带的标准grabcut程序中,我添加了几行代码将结果保存到文件中。但是,文件中没有任何内容。只是黑色纯色。这是我输入的代码 -- 在主函数中 case 's': gcapp
我正在浏览 GrabCut算法,我想将吉布斯能量的数据项更新为以下内容: 其中,和 . p^f 和 p^b 分别是具有 4 个和 8 个分量的高斯混合模型 (GMM)。我正在查看 GrabCut 的代
我正在尝试从背景趋于白色的几张图像中删除相对琐碎的背景。但是,生成的蒙版会与前景重叠,即使它不包含太多白色。例如,给定以下输入: GrabCut 给出: 这是我的代码: img = cv2.imrea
我使用 Grabcut 算法实现了背景减除。但是我在源图像的裁剪图像中有一些不需要的空白。如何去除裁剪图像的空白。? 步骤: 从相机读取视频并读取图像。我的输入图像: 应用 Grabcut 算法。 c
我正在使用 grabcut 从图像中删除白色背景。边缘的部分背景仍然存在。你能帮我把阴影部分也去掉吗? 输入图片 输出图片 尝试了抓取、轮廓、正常阈值处理,但阴影仍然存在 img_ray = cv.c
我有一张图片(.jpg 图片),我想从原始图片中提取背景。我用谷歌搜索了很多,但只找到了提取前景图像的教程。 我从另一个 stackoverflow question 获取了代码.该代码对我来说工作正
有谁知道 GrabCut 是否有 GPU 版本?我似乎在 opencv_contrib 中找不到 GrabCut 模块. GrabCut 只能在 CPU 上运行吗? 最佳答案 我认为 GPU 版本已从
我刚刚实现了 GrapCV 算法(OpenCV、C++、Visual Studio 12): grabCut( *image, mask, rect, bgdModel, fgdModel, 1, G
他,如何用掩码初始化第一个 Grabcut?就我而言,每次出现此错误时:(-215) !bgdSamples.empty() && !fgdSamples.empty() 在函数 initGMMs 中
我正在 OpenCV 中测试“grabcut.cpp”,但在编译时我收到以下错误消息: OpenCV Error: Assertion failed std::numeric_limits::eps
我想使用在 OpenCV 上实现的 GrabCut 算法。 如documentation所示这是函数签名: void grabCut( InputArray img, InputOut
我正在使用 JavaCV 版本 3.4.2 以及 Android SDK 27。我尝试首先使用带有掩码的grabcut。 这是我的输入图像(左)和蒙版(右): 请注意,我在这里展示的是带有衬衣的面具。
我尝试在以下论文中实现想法:BiCoS: A Bi-level Co-Segmentation Method for Image Classification但我对以下要求感到困惑:这里的 GrabC
我关注了this opencv tutorial并使用我自己的图像和掩码使用 opencv grabcut 算法改进分割,但生成的掩码与初始掩码相同。 代码: import cv2 init_mask
我是一名优秀的程序员,十分优秀!