findcontours

TypeError: Layout of the output array image is incompatible 问题解决

馋奶兔 提交于 2020-03-01 02:18:47
本周在使用findContours的过程中遇到了以下问题: TypeError: Layout of the output array image is incompatible with cv::Mat (step[ndims-1] != elemsize or step[1] != elemsize*nchannels) 感谢这篇文章: https://www.jianshu.com/p/cc3f4baf35bb ,引导我往正确的方向进行思考。 先讲结论造成以上问题的原因是: 输入与输出的numpy格式不一致 。 我一开始的第一反应是为什么findContours函数会有“output array image”,我只关注contours。这其实是我在写代码中一个盲区,对于findContours函数我一般都是这么写: _, contours, _ = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 对于第一个与第三个返回值现阶段直接就跳过,这其实恰恰是产生问题的关键原因,完整的findContours函数输出应该如下: binary, contours, hierarchy = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN

Opencv之提取图像轮廓

孤街浪徒 提交于 2020-01-28 08:41:16
提取图像轮廓 cv2.findContours(img,mode,method) mode:轮廓检索模式 RETR_EXTERNAL :只检索最外面的轮廓; RETR_LIST:检索所有的轮廓,并将其保存到一条链表当中; RETR_CCOMP:检索所有的轮廓,并将他们组织为两层:顶层是各部分的外部边界,第二层是空洞的边界; RETR_TREE(最常用):检索所有的轮廓,并重构嵌套轮廓的整个层次; method:轮廓逼近方法 CHAIN_APPROX_NONE:以Freeman链码的方式输出轮廓,所有其他方法输出多边形(顶点的序列)。 CHAIN_APPROX_SIMPLE(最常用):压缩水平的、垂直的和斜的部分,也就是,函数只保留他们的终点部分。 以下图举例: 寻找并绘制轮廓 首先导入图像: # 为了更高的准确率,使用二值图像。 img = cv2 . imread ( 'contours.png' ) gray = cv2 . cvtColor ( img , cv2 . COLOR_BGR2GRAY ) ret , thresh = cv2 . threshold ( gray , 127 , 255 , cv2 . THRESH_BINARY ) cv_show ( thresh , 'thresh' ) 使用cv2.findContours()函数将轮廓提取出来: #

opencv-轮廓提取findContours和drawContours

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-03 03:30:43
转载: https://blog.csdn.net/maomao1011120756/article/details/49794997 最近用OPENCV的轮廓提取函数,总结一下。不然老忘记。。。。。。。 void findContours//提取轮廓,用于提取图像的轮廓 ( InputOutputArray image,//输入图像,必须是8位单通道图像,并且应该转化成二值的 OutputArrayOfArrays contours,//检测到的轮廓,每个轮廓被表示成一个point向量 OutputArray hierarchy,//可选的输出向量,包含图像的拓扑信息。其中元素的个数和检测到的轮廓的数量相等 int mode,//说明需要的轮廓类型和希望的返回值方式 int method,//轮廓近似方法 Point offset = Point() ) void drawContours//绘制轮廓,用于绘制找到的图像轮廓 ( InputOutputArray image,//要绘制轮廓的图像 InputArrayOfArrays contours,//所有输入的轮廓,每个轮廓被保存成一个point向量 int contourIdx,//指定要绘制轮廓的编号,如果是负数,则绘制所有的轮廓 const Scalar& color,//绘制轮廓所用的颜色 int thickness

关于OpenCv中使用Findcontours时所引发的程序崩溃问题(Debug assertion Failed)

谁说胖子不能爱 提交于 2019-12-16 09:03:27
最近在windows平台使用OpenCV的findContours函数时,会出现崩溃问题。 如显示is_block_type_valid等。 在网上搜了很多解决方案,比如: 1.配置属性->常规->项目默认值->MFC的使用->在共享DLL中使用MFC; 2.C/C++>代码生成->运行库->多线程DLL(/MD); 3.代码中vector要使用cv::vector,vector<vector>要事先分配空间; 我就不挨着列举了,相信很多小伙伴使用上述方法根本无法解决问题。或者个别小伙伴可以碰巧把问题解决,但程序的移植性肯定会存在风险。 接下来,我会提供一个真正能解决这个问题的方案。 问题原因 我们要明白,为什么findContours会出现异常。 我们来看findContours的源代码: void cv::findContours( InputOutputArray _image, OutputArrayOfArrays _contours, OutputArray _hierarchy, int mode, int method, Point offset ) { Mat image = _image.getMat(); MemStorage storage(cvCreateMemStorage()); CvMat _cimage = image; CvSeq*

OpenCV TypeError: contour is not a numpy array, neither a scalar

匿名 (未验证) 提交于 2019-12-03 02:31:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm trying to use OpenCV to extract tags from Nike images. This is a tutorial code taken from: http://opencv-code.com/tutorials/ocr-ing-nikes-new-rsvp-program/ I've modified few lines of code though and there is no error in that part (not sure if it's working because I haven't been able to successfully completely run it.) When I run command 'python a.py'. This error is displayed:- Traceback (most recent call last): File "a.py", line 42, in <module> otcnt = [c for c in cnt if cv2.contourArea(c) < 100] TypeError: contour is not a numpy array,

Error using cv2.findContours(), with python

匿名 (未验证) 提交于 2019-12-03 02:30:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I've recently started learning OpenCV on Python. I'm referring to this tutorial here, to get some help on getting the contours of an image. My code is - import cv2 import numpy as np img = cv2.imread('shapes.jpg', 0) img = cv2.medianBlur(img, 5) thresh = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ cv2.THRESH_BINARY,11,2) cv2.imshow('Thresh', thresh) cv2.waitKey(0) cv2.destroyAllWindows() image, contours, hierarchy = cv2.findContours(thresh.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE) cv2.drawContours(image, countours, -1,

OpenCV Contours - need more than 2 values to unpack

匿名 (未验证) 提交于 2019-12-03 02:15:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am trying to implement contours using the following code.. im = cv2.imread('C:\Users\Prashant\Desktop\T.jpg') imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) ret,thresh = cv2.threshold(imgray,127,255,0) image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) img = cv2.drawContour(im, contours, -1, (0,255,0), 3) cv2.imshow('Image1',img) but i am continously getting the following error. Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python27\lib\site-packages\spyderlib\widgets

OpenCV version 4.1.0 drawContours

匿名 (未验证) 提交于 2019-12-03 01:39:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have the following code that worked well with OPenCV 3.4.1 and now is not working with OpenCV 4.1.0 and gives an error. I do not know how to adapt the code with the newer version, can you help me with that? Thanks a lot def ImageProcessing(image): image = cv2.absdiff(image, background) h, gray = cv2.threshold(image, 65, 255, cv2.THRESH_BINARY_INV); gray = cv2.medianBlur(gray,5) kernel = np.ones((3,3), np.uint8) gray = cv2.erode(gray, kernel, iterations=1)#1 des = cv2.bitwise_not(gray) tmp = cv2.findContours(des,cv2.RETR_CCOMP,cv2.CHAIN

compatibility issue with contourArea in openCV 3

匿名 (未验证) 提交于 2019-12-03 01:25:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am trying to do a simple area calculation of contours I get from findContours. My openCv version is 3.1.0 My code is: 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' Cant seem to solve it, I have a feeling its just typecasting altough I expect the findContours result to match the type

OpenCV python: ValueError: too many values to unpack

匿名 (未验证) 提交于 2019-12-03 01:17:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm writing an opencv program and I found a script on another stackoverflow question: Computer Vision: Masking a human hand When I run the scripted answer, I get the following error: Traceback (most recent call last): File "skinimagecontour.py", line 13, in contours, _ = cv2.findContours(skin_ycrcb, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) ValueError: too many values to unpack The code: import sys import numpy import cv2 im = cv2.imread('Photos/test.jpg') im_ycrcb = cv2.cvtColor(im, cv2.COLOR_BGR2YCR_CB) skin_ycrcb_mint = numpy.array((0,