gpt4 book ai didi

python - OpenCV错误:断言在ContourArea中失败(contour.checkVector(2)> = 0 &&(contour.depth()== CV_32F ||轮廓.depth()== CV_32S))

转载 作者:行者123 更新时间:2023-12-02 17:37:01 27 4
gpt4 key购买 nike

这是虚拟更衣室的代码
因此,基本上是为了运行此代码ubuntu 12.04,python 2.7.3,gtk2和opencv 2。它删除背景屏幕,在几乎任何光线条件下检测T恤,替换T恤颜色。编写用于替换衬衫和在衬衫上进行设计。它适用于绿色背景下的蓝色衬衫。

在运行代码时,我发现以下错误-

> **OpenCV Error: Assertion failed (contour.checkVector(2) >= 0 && (contour.depth() == CV_32F || contour.depth() == CV_32S)) in contourArea, file /build/buildd/opencv-2.3.1/modules/imgproc/src/contours.cpp, line 1673**
Traceback (most recent call last):
File "vdr/main.py", line 179, in display_frame
self.final=self.getOutput_frames()
File "vdr/main.py", line 224, in getOutput_frames
res=self.replace.replace_design(cntr,self.p_mat, self.design_template, res)
File "vdr/color_replace.py", line 49, in replace_design
area.append(cv2.contourArea(cntr[i].astype('int')))
cv2.error: /build/buildd/opencv-2.3.1/modules/imgproc/src/contours.cpp:1673: error: (-215) contour.checkVector(2) >= 0 && (contour.depth() == CV_32F || contour.depth() == CV_32S) in function contourArea

在代码中,检测T恤的轮廓存在一些问题。

main.py中用于getOutput_frames的代码如下-
        def getOutput_frames(self):
if TEST_MODE:
_,frame=self.vid.read() #NOTE: Testing without camera. uncomment this to feed from camera.
else:
frame=self.v.outFrame() #NOTE: feeding from Camera.

self.norm.getRGB(frame) #input to Normalized RGB

norm_rgb=self.norm.normalized() #normalized RGB
print 'Got normalized RGB '
rgb_planes=self.v.imagePlanes(norm_rgb)

self.back.loadBackground()
self.back.getFrames(rgb_planes[1])
self.back.subtract_back(norm_rgb)

subtracted=self.back.remove(frame)
print 'background subtracted now'

self.tshirt.getFrames(norm_rgb)
mask,cntr=self.tshirt.detect_shirt()
print 'found tshirt'

self.replace.getFrames(subtracted,mask)

res=self.replace.replace_color(self.color)

if self.design is not 7:
res=self.replace.replace_design(cntr,self.p_mat, self.design_template, res)

#replace.replace_design(cntr, p_mat,design_template,res)

#cv2.imshow("subtracted", res)

return res

在tshirt.py中,detectshirt的代码如下-
    def detect_shirt(self):


#self.dst=cv2.inRange(self.norm_rgb,np.array([self.lb,self.lg,self.lr],np.uint8),np.array([self.b,self.g,self.r],np.uint8))
self.dst=cv2.inRange(self.norm_rgb,np.array([20,20,20],np.uint8),np.array([255,110,80],np.uint8))
cv2.threshold(self.dst,0,255,cv2.THRESH_OTSU+cv2.THRESH_BINARY)
fg=cv2.erode(self.dst,None,iterations=2)
#cv2.imshow("fore",fg)
bg=cv2.dilate(self.dst,None,iterations=3)
_,bg=cv2.threshold(bg, 1,128,1)
#cv2.imshow("back",bg)

mark=cv2.add(fg,bg)
mark32=np.int32(mark)
cv2.watershed(self.norm_rgb,mark32)
self.m=cv2.convertScaleAbs(mark32)
_,self.m=cv2.threshold(self.m,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
#cv2.imshow("final_tshirt",self.m)

cntr,h=cv2.findContours(self.m,cv2.cv.CV_RETR_EXTERNAL,cv2.cv.CV_CHAIN_APPROX_SIMPLE)


return self.m,cntr

在color_replace.py中,replace_design的代码如下
         def replace_design(self,cntr,p_mat,design_template,input_image):
area=list()

for i in range(len(cntr)):

area.append(cv2.contourArea(cntr[i].astype('int')))

max_area=max(area)

for i in range(len(area)):
if max_area==area[i]:
index=i
mom=cv2.moments(cntr[index].astype('int'))
#print mom
x=mom['m10']
y=mom['m01']
cx=int(x/max_area)
cy=int(y/max_area)
#print 'center-X',cx
#print 'center-Y',cy

rect=cv2.boundingRect(np.array(cntr[index],np.float32))

x=rect[0]
y=rect[1]
w=rect[2]
h=rect[3]

q=np.array([
[x+w/3,(y+h)/3],
[x+w/2,(y+h)/3],
[x+w/2,y+h/3],
[x+w/3,y+h/3]
],np.float32)


mat=cv2.getPerspectiveTransform(p_mat,q)

dst=cv2.warpPerspective(design_template,mat(config.width,config.height))


temp=cv2.cvtColor(input_image,cv2.cv.CV_BGR2BGRA)
temp=cv2.addWeighted(temp,1.0,dst,1.0,1.0)
res=cv2.cvtColor(temp,cv2.cv.CV_BGRA2BGR)

return res

您能帮我解决这个错误吗?
完整代码可在以下位置获得:
entire Code of virtual dressing room

最佳答案

该错误告诉您所传递的列表为空或不带int / float。然后,解决方案是:

1)检查空列表->如果它们是轮廓的后处理,则可能会发生。我认为如果它不变的话是不可能的。

2)列表必须是int或float。您将astype与int一起使用,通常OpenCV依赖numpy类型,因此最好使用np.int32代替。另外,如果未通过findContours函数进行更改,则根本不需要astype。它们已经是int值。

关于python - OpenCV错误:断言在ContourArea中失败(contour.checkVector(2)> = 0 &&(contour.depth()== CV_32F ||轮廓.depth()== CV_32S)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50131912/

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