Python 有人能帮我解决以下错误吗;列表索引超出范围“;?
任何机构能否解决这一错误: 我试图分割包含数字的ROI。 该代码适用于某些图像。但对于其他人,它显示以下错误:Python 有人能帮我解决以下错误吗;列表索引超出范围“;?,python,opencv,Python,Opencv,任何机构能否解决这一错误: 我试图分割包含数字的ROI。 该代码适用于某些图像。但对于其他人,它显示以下错误: --------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-58-1a8d1317985a> in &
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-58-1a8d1317985a> in <module>
1 #segment digits from images in Marks folder
----> 2 digits_in_cell("./Cropped/Marks/")
<ipython-input-57-8fe190745c08> in digits_in_cell(cropped_dir_path)
23 cv2.CHAIN_APPROX_SIMPLE)
24
---> 25 cnts = cnts[0] if imutils.is_cv2() else cnts[1]
26
27 orig = cell.copy()
IndexError: list index out of range
---------------------------------------------------------------------------
索引器回溯(最后一次最近调用)
在里面
1#从标记文件夹中的图像中分割数字
---->单元格中的2位数字(“/裁剪/标记/”)
在\u单元格中的数字\u(裁剪的\u dir\u路径)
23 cv2.链条(近似简单)
24
--->25 cnts=cnts[0]如果imutils.is_cv2()其他cnts[1]
26
27 orig=单元格。复制()
索引器:列表索引超出范围
这是我的代码:
i=7
cell = cv2.imread(str(i) + '.png')
gray = cv2.cvtColor(cell, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (7, 7), 0)
# threshold the image
ret,thresh1 = cv2.threshold(gray ,112,255,cv2.THRESH_BINARY_INV)
cv2.imshow("Image", thresh1)
cv2.waitKey(0)
# dilate the white portions
dilate = cv2.dilate(thresh1, None, iterations=5)
cv2.imshow("Image", dilate)
cv2.waitKey(0)
# find contours in the image
cnts, hierarchy = cv2.findContours(dilate.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
orig = cell.copy()
#for cnt in cnts:
# Check the area of contour, if it is very small ignore it
#if(cv2.contourArea(cnt) < 100):
# continue
# Filtered countours are detected
x,y,w,h = cv2.boundingRect(cnts)
# Taking ROI of the cotour
roi = cell[y:y+h, x:x+w]
# Mark them on the image if you want
cv2.rectangle(orig,(x,y),(x+w,y+h),(0,255,0),2)
# Save your contours or characters
cv2.imwrite("roi" + str(i) + ".png", roi)
i = i + 1
cv2.imshow("Image", orig)
cv2.waitKey(0)
i=7
cell=cv2.imread(str(i)+'.png')
灰色=cv2.CVT颜色(单元格,cv2.COLOR\u BGR2GRAY)
灰色=cv2.高斯模糊(灰色,(7,7,0)
#对图像设置阈值
ret,thresh1=cv2.阈值(灰色,112255,cv2.THRESH\u二进制\u INV)
cv2.imshow(“图像”,阈值1)
cv2.等待键(0)
#扩大白色部分
扩张=cv2。扩张(阈值1,无,迭代次数=5)
cv2.imshow(“图像”,放大)
cv2.等待键(0)
#在图像中查找轮廓
cnts,hierarchy=cv2.findContours(displate.copy(),cv2.RETR_EXTERNAL,
cv2.链条(近似简单)
cnts=cnts[0]如果imutils.is_cv2()其他cnts[1]
orig=单元格。复制()
#对于cnt中的cnt:
#检查轮廓区域,如果它非常小,忽略它
#如果(cv2.轮廓面积(cnt)<100):
#继续
#检测到已过滤的计数
x、 y,w,h=cv2.boundingRect(碳纳米管)
#采取科托的投资回报率
roi=单元[y:y+h,x:x+w]
#如果需要,请在图像上标记它们
cv2.矩形(原点,(x,y),(x+w,y+h),(0255,0),2)
#保存您的轮廓或字符
cv2.imwrite(“roi”+str(i)+“.png”,roi)
i=i+1
cv2.imshow(“图像”,原版)
cv2.等待键(0)
行的执行
cnts, hierarchy = cv2.findContours
返回
cnts
变量的空列表。这只是意味着函数无法找到任何轮廓,但在下一行中,您要求的是第一个轮廓,因此出现了错误。您能告诉我为什么检测到一些CONTOR,而其中一些没有检测到吗?如果输入图像中的所有像素都大于112或都小于112,则使用该值进行阈值化后,没有轮廓。您能否为失败的输入上载thresh1并放大图像?