[英]ValueError: too many values to unpack (expected 2) in openCV while trying to detect nameplates of cars
我是圖像處理新手。 我有一個包含各種汽車圖像的文件夾。 我試圖僅提取其銘牌並將其放在其他文件夾中。 我在以下代碼的第5行中收到一條錯誤消息,指出“ ValueError:太多值無法解包(預期2)”。 我在互聯網上查找了此代碼並試圖理解它。 據我所知,我們首先使用imread函數讀取圖像並將其轉換為灰色空間。 Canny功能可幫助檢測邊緣,而findContours可幫助查找圖像的輪廓。 我似乎不太明白后面的代碼。 如果有人可以指導我完成代碼並幫助我對錯誤進行分類,將很有幫助。
import cv2
image = cv2.imread("path")
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,0)
im2, contours, hierarchy =
cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cnt = contours[4]
cv2.drawContours(im2, [cnt], 0, (0,255,0), 3)
idx = 0
for c in cnts:
x,y,w,h = cv2.boundingRect(c)
if w>50 and h>50:
idx+=1
new_img=image[y:y+h,x:x+w]
cv2.imwrite(str(idx) + '.png', new_img)
cv2.imshow("im",image)
cv2.waitKey(0)
cv 3.0有所更改與.findContours
現在返回3個值。
https://docs.opencv.org/3.1.0/d4/d73/tutorial_py_contours_begin.html
import cv2
image = cv2.imread("path")
gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
edged = cv2.Canny(image, 10, 250)
#old way
#(cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 3.0 way
_, cnts, hierarchy = cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
idx = 0
for c in cnts:
x,y,w,h = cv2.boundingRect(c)
if w>50 and h>50:
idx+=1
new_img=image[y:y+h,x:x+w]
cv2.imwrite(str(idx) + '.png', new_img)
cv2.imshow("im",image)
cv2.waitKey(0)
im2, contours, hierarchy =
cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.