我正在尝试对findContours中获得的轮廓进行简单的面积计算。 我的openCv版本是3.1.0
我的代码是:
cc = cv2.findContours(im_bw.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.contourArea(cc[0])
error: 'C:\\builds\\master_PackSlaveAddon-win32-vc12-static\\opencv\\modules\\imgproc\\src\\shapedescr.cp...: error: (-215) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function cv::contourArea\n'
似乎无法解决它,我感觉它只是类型转换,我希望findContours结果与contourArea的类型相匹配
谢谢:)
编辑:事实证明我需要采取findContours的第二个参数
im2, cc, hierarchy = cv2.findContours(im_bw.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
答案 0 :(得分:28)
在Opencv 3 API版本中,cv2.findContours()
返回3 objects
所以你需要将你的陈述重写为:
image, contours, hierarchy = cv2.findContours(im_bw.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
答案 1 :(得分:2)
此问题是由不同OpenCV版本中cv2.findContours的不同返回值引起的。
在OpenCV 4.0.0中,此错误可能类似于static void OnUpdateInGameState( Socket::Server* Server, int Client, const char* Data ) {
Messages::MessageUpdateInGameState Message( Data );
for ( auto GameState : Server->m_ClientGameState ) {
if ( !GameState )
continue;
if ( GameState->Client == Client ) {
GameState->Message.InGame = Message.message( ).InGame;
snprintf( GameState->Message.Server, sizeof( Message.message( ).Server ), Message.message( ).Server );
return;
}
}
//No such object found, create it
Server->m_ClientGameState.push_back( new InGameState_t( Client, Message.message( ) ) );
}
您可以在此处找到详细的说明和解决方案:How to use `cv2.findContours` in different OpenCV versions?
答案 2 :(得分:0)
取决于OpenCV版本,cv2.findContours()
具有不同的返回签名。
在OpenCV 3.4.X中,cv2.findContours()
返回3个项目
image, contours, hierarchy = cv.findContours(image, mode, method[, contours[, hierarchy[, offset]]])
在OpenCV 2.X和4.1.X中,cv2.findContours()
返回2个项目
contours, hierarchy = cv.findContours(image, mode, method[, contours[, hierarchy[, offset]]])
不管这样的版本,您都可以轻松获得轮廓:
cnts = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
答案 3 :(得分:0)
感谢@ZdaR; 顺便说一下,您可以在OpenCV 4.1中执行以下操作:
contours, hierarchy = cv2.findContours(im_bw.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)