Skip to content

area=cv2.contourArea(contour) Error #10

@WinAndri

Description

@WinAndri
import cv2
import numpy as np
import pyfirmata
cap = cv2.VideoCapture(0)
port = pyfirmata.Arduino('COM6')
port.digital[13].write(0)
port.digital[12].write(0)
port.digital[11].write(0)




while True:
    _,frame = cap.read()
    hsv_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    
    #kırmızı
    low_red = np.array([161, 100, 100])
    high_red `=` np.array([170, 255, 255])
    mask_red = cv2.inRange(hsv_frame, low_red, high_red)
    #yeşil
    low_green = np.array([38, 100, 100])
    high_green = np.array([75, 255, 255])
    mask_green = cv2.inRange(hsv_frame, low_yesil, high_yesil)
    #mavi
    low_blue = np.array([94, 80, 2])
    high_blue = np.array([126, 255, 255])
    mask_blue = cv2.inRange(hsv_frame, low_blue, high_blue)

    kernal = np.ones((5, 5), "uint8")

    #kırmızı
    mask_red = cv2.dilate(mask_red, kernal)
    res_red = cv2.bitwise_and(frame, frame, mask= mask_red)
    #yeşil
    mask_green = cv2.dilate(mask_green, kernal)
    res_green = cv2.bitwise_and(frame, frame, mask= mask_green)
    #mavi
    mask_blue = cv2.dilate(mask_blue, kernal)
    res_blue = cv2.bitwise_and(frame, frame, mask= mask_blue)



    contours = cv2.findContours(mask_blue,
                                           cv2.RETR_EXTERNAL,
                                           cv2.CHAIN_APPROX_SIMPLE)
    contours = contours[0] if len(contours) == 2 else contours[1]
    for contour in enumerate(contours):  
        area=cv2.contourArea(contour)
        if(area > 300):
            x, y,w, h = cv2.boundingRect(contour)
            frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
            cv2.putText(frame, "blue", (x, y), cv2.Font_HERSHEY_SIMPLEX, 1.0, (0, 0,255), 1)
            port.digital[13].write(1)
        else:
            port.digital[13].write(0)



    contours, hierarchy= cv2.findContours(mask_green,
                                                    cv2.RETR_EXTERNAL,
                                                    cv2.CHAIN_APPROX_SIMPLE)
    contours = cnts.astype(np.uint32)
    for pic, contour in enumerate(contours):
        area = cv2.contourArea(contour)
        if(area > 300):
            x, y,w, h = cv2.boundingRect(contour)
            frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
            cv2.putText(frame, "green", (x, y), cv2.Font_HERSHEY_SIMPLEX, 1.0, (0, 0,255), 1)

    cv2.imshow("Multiple Color Detection İn Real Time", frame)
    if cv2.waitKey(10) & 0xFF == ord('q'):
        cap.realse()
        cv2.destroyALLWindows()
        break
cv2.destroyALLWindows()

Traceback (most recent call last):
File "C:\Users\erkan\AppData\Local\Programs\Python\Python39\kamera1.py", line 49, in
area=cv2.contourArea(contour)
cv2.error: OpenCV(4.5.3) 👎 error: (-5:Bad argument) in function 'contourArea'
Overload resolution failed

  • contour is not a numerical tuple
  • Expected Ptrcv::UMat for argument 'contour'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions