本文介绍了用于GrabCut算法的OpenCV Python绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我一直在尝试通过Python绑定使用抓取切割方法的OpenCV实现。我试着在cv和cv2中使用版本,但我无法找到正确的参数来使方法正确运行。我已经尝试了几个排列的参数,没有什么似乎工作(基本上每个例子我已经看到在Github)。以下是我尝试过的几个例子:







这里是方法的文档和一个已知的错误报告:







我可以获取代码使用下面的示例执行,但它返回一个空白(全黑)图像掩码。

  img = Image(pills.png)
mask = img.getEmpty(1)
bgModel = cv.CreateMat(1,13 * 5,cv.CV_64FC1)
fgModel = cv.CreateMat(1,13 * 5,cv.CV_64FC1)
for i in range(0,13 * 5 ):
cv.SetReal2D(fgModel,0,i,0)
cv.SetReal2D(bgModel,0,i,0)

rect =(150,70,170,220)
tmp1 = np.zeros((1,13 * 5))
tmp2 = np.zeros((1,13 * 5))
cv.GrabCut(img.getBitmap ,rect,tmp1,tmp2,5,cv.GC_INIT_WITH_RECT)

我使用SimpleCV加载图片。来自img.getBitmap()的掩码类型和返回类型如下:

  iplimage(nChannels = 1 width = 730 height = 530 widthStep = 732)
iplimage(nChannels = 3 width = 730 height = 530 widthStep = 2192)

如果有人有这个代码的工作示例,我很想看到它。对于什么值得我在OSX Snow Leopard上运行,我的版本的OpenCV是从SVN存储库(几个星期前)安装的。作为参考,我的输入图像是:



我试过将结果掩码枚举值更改为更可见的东西。这不是返回值是问题。这将返回一个完全黑色的图像。我会尝试几个更多的值。

  img = Image(pills.png)
mask = img.getEmpty(1)
bgModel = cv.CreateMat(1,13 * 5,cv.CV_64FC1)
fgModel = cv.CreateMat(1,13 * 5,cv.CV_64FC1)
for i in range(0,13 * 5 ):
cv.SetReal2D(fgModel,0,i,0)
cv.SetReal2D(bgModel,0,i,0)

rect =(150,70,170,220)
tmp1 = np.zeros((1,13 * 5))
tmp2 = np.zeros((1,13 * 5))
cv.GrabCut(img.getBitmap ,rect,tmp1,tmp2,5,cv.GC_INIT_WITH_MASK)
mask [mask == cv.GC_BGD] = 0
mask [mask == cv.GC_PR_BGD] = 0
mask [mask == cv.GC_FGD] = 255
mask [mask == cv.GC_PR_FGD] = 255
result = Image(mask)
result.show()
result.save result.png)


解决方案

Kat,此版本你的代码似乎为我工作。

  import numpy as np 
import matplotlib.pyplot as plt
import cv2


filename =pills.png
im = cv2.imread(filename)

h,w = im.shape [:2]
$ b b mask = np.zeros((h,w),dtype ='uint8')
rect =(150,70,170,220)
tmp1 = np.zeros((1,13 * 5))
tmp2 = np.zeros((1,13 * 5))

cv2.grabCut(im,mask,rect,tmp1,tmp2,10,mode = cv2.GC_INIT_WITH_RECT)

plt.figure()
plt.imshow(mask)
plt.colorbar()
plt.show()
pre>

生成一个这样的数字,标签为0,2和3.


I've been trying to use the OpenCV implementation of the grab cut method via the Python bindings. I have tried using the version in both cv and cv2 but I am having trouble finding out the correct parameters to use to get the method to run correctly. I have tried several permutations of the parameters and nothing seems to work (basically every example I've seen on Github). Here are a couple examples I have tried to follow:

Example 1

Example 2

And here is the method's documentation and a known bug report:

Documentation

Known Grabcut Bug

I can get the code to execute using the example below, but it returns a blank (all black) image mask.

img = Image("pills.png")
mask = img.getEmpty(1)
bgModel = cv.CreateMat(1, 13*5, cv.CV_64FC1)
fgModel = cv.CreateMat(1, 13*5, cv.CV_64FC1)
for i in range(0, 13*5):
    cv.SetReal2D(fgModel, 0, i, 0)
    cv.SetReal2D(bgModel, 0, i, 0)

rect = (150,70,170,220)
tmp1 = np.zeros((1, 13 * 5))
tmp2 = np.zeros((1, 13 * 5))
cv.GrabCut(img.getBitmap(),mask,rect,tmp1,tmp2,5,cv.GC_INIT_WITH_RECT)

I am using SimpleCV to load the images. The mask type and return type from img.getBitmap() are:

iplimage(nChannels=1 width=730 height=530 widthStep=732 )
iplimage(nChannels=3 width=730 height=530 widthStep=2192 )

If someone has a working example of this code I would love to see it. For what it is worth I am running on OSX Snow Leopard, and my version of OpenCV was installed from the SVN repository (as of a few weeks ago). For reference my input image is this:

I've tried changing the result mask enum values to something more visible. It is not the return values that are the problem. This returns a completely black image. I will try a couple more values.

img = Image("pills.png")
mask = img.getEmpty(1)
bgModel = cv.CreateMat(1, 13*5, cv.CV_64FC1)
fgModel = cv.CreateMat(1, 13*5, cv.CV_64FC1)
for i in range(0, 13*5):
    cv.SetReal2D(fgModel, 0, i, 0)
    cv.SetReal2D(bgModel, 0, i, 0)

rect = (150,70,170,220)
tmp1 = np.zeros((1, 13 * 5))
tmp2 = np.zeros((1, 13 * 5))
cv.GrabCut(img.getBitmap(), mask, rect, tmp1, tmp2, 5, cv.GC_INIT_WITH_MASK)
mask[mask == cv.GC_BGD] = 0
mask[mask == cv.GC_PR_BGD] = 0
mask[mask == cv.GC_FGD] = 255
mask[mask == cv.GC_PR_FGD] = 255
result = Image(mask)
result.show()
result.save("result.png")
解决方案

Kat, this version of your code seems to work for me.

import numpy as np
import matplotlib.pyplot as plt
import cv2


filename = "pills.png"
im = cv2.imread(filename)

h,w = im.shape[:2]

mask = np.zeros((h,w),dtype='uint8')
rect = (150,70,170,220)
tmp1 = np.zeros((1, 13 * 5))
tmp2 = np.zeros((1, 13 * 5))

cv2.grabCut(im,mask,rect,tmp1,tmp2,10,mode=cv2.GC_INIT_WITH_RECT)

plt.figure()
plt.imshow(mask)
plt.colorbar()
plt.show()

Produces a figure like this, with labels 0,2 and 3.

这篇关于用于GrabCut算法的OpenCV Python绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 07:36