【问题标题】:Unsupported format or combination of formats) ,FindContours不支持的格式或格式组合),FindContours
【发布时间】:2019-02-26 06:27:58
【问题描述】:

我想使用 haar cascade 来计算汽车数量。

#import libraries of python opencv
import numpy as np 
import cv2
import gc
import uuid
import datetime
import time
import csv

cap = cv2.VideoCapture('v3.mp4')

car_cascade = cv2.CascadeClassifier('cars.xml') 
W = cap.get(3)
H = cap.get(4)
areaTH = 700

 H1 = (H/2)+10
W1 = W/2
mx = 0
my = 30

while (cap.isOpened()):
#capture frame by frame
ret, frame = cap.read()

#convert video into gray scale of each frames

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

#detect cars in the video
cars = car_cascade.detectMultiScale(gray, 1.1, 3)


#to draw arectangle in each cars 
for (x,y,w,h) in cars:
    cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)      

#display the resulting frame
cv2.imshow('video', frame)
#press Q on keyboard to exit
if cv2.waitKey(25) & 0xFF == ord('q'):
    break

Line1 = np.array([[20,H1],[310,H1]], np.int32).reshape((-1,1,2))
frame = cv2.polylines(frame,[Line1],False,(0,0,255),thickness=5)
fram, contours0, hierarchy = 
cv2.findContours(frame,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
for cnt in contours0:
   # cv2.drawContours(frame, cnt, -1, (0,255,0), 2, 8)
    area = cv2.contourArea(cnt)
    #print ('Area : '+str(area))

    if area > areaTH:
        #################
        #   TRACKING    #
        #################            
        M = cv2.moments(cnt)
        cx = int(M['m10']/M['m00'])
        cy = int(M['m01']/M['m00'])
        x,y,w,h = cv2.boundingRect(cnt)
        cv2.circle(frame,(cx,cy), 3, (255,0,0), -1)            
        img = cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)
        #print ('H1 : '+str(H1))
        print('cy : '+str(cy))

        if (cy >= 147) and (cy<= 155):
            Vehicles = Vehicles + 1
            Line1 = np.array([[200,H1],[880,H1]], 
    np.int32).reshape((-1,1,2))
            frame = cv2.polylines(frame,[Line1],False,(255,0,0),thickness=5)


        cv2.imshow('Frame',frame)

   #Abort and exit with 'Q' or ESC
   k = cv2.waitKey(30) & 0xff
   if k == 27:
    break

cap.release() #release video file
cv2.destroyAllWindows()

我将图像正确转换为灰度并成功在框架上绘制了一条线问题是我收到此错误

错误:OpenCV(3.4.2) C:\build\3_4_winpack-bindings-win32-vc14-static\opencv\modules\imgproc\src\contours.cpp:199:错误:(-210:不支持的格式或组合格式)[开始]FindContours 在 mode != CV_RETR_FLOODFILL 时仅支持 CV_8UC1 图像,否则仅在函数 'cvStartFindContours_Impl' 中支持 CV_32SC1 图像

当我在这个代码行中传递一个变量“框架”时,有人可以帮我解决这个错误吗

fram, contours0, hierarchy = 
cv2.findContours(frame,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)

【问题讨论】:

  • 是否可以给我们一个案例,其中没有您的所有代码,但只有出现错误的相关部分?此外,您的错误消息说明了发生了什么:在第 199 行,不支持作为参数传入的格式。
  • 这个错误是因为你的数据类型不是uint8类型或者有渠道。

标签: python opencv


【解决方案1】:

如果您查看 OpenCV 文档,您会看到函数 cv2.findContour() 需要单色图像。你通过了frame,它仍然在 3 个频道中。您应该改用 gray 变量 (cv2.findContours(gray,...).

【讨论】:

    【解决方案2】:

    从documentation 到cv2.findContour()

    图像 – 来源,8 位单通道图像。非零像素被视为 1。零像素保持为 0,因此图像被视为二进制。您可以使用 compare() 、 inRange() 、 threshold() 、adaptiveThreshold() 、 Canny() 等从灰度或彩色图像中创建二值图像。该函数在提取轮廓的同时修改图像。如果 mode 等于 CV_RETR_CCOMP 或 CV_RETR_FLOODFILL,则输入也可以是一个 32 位整数的标签图像(CV_32SC1)。

    试试这条线来缩放像素

    image = cv2.convertScaleAbs(image)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      • 1970-01-01
      • 2016-05-30
      • 1970-01-01
      相关资源
      最近更新 更多