1

I get this error:

OpenCV Error: Unsupported format or combination of formats() in unknown function, file C:\slave\WinInstallerMegaPack\src\opencv\modules\imgproc\src\canny.cpp, line 67 Traceback (most recent call last): edges= cv2.Canny(frame,100,100) cv2.error : C:\ slave\WinInstallerMegaPack\srx\opencv\modules\imgproc\src\canny.cpp:67: error: (-210)

When I run this code:

import cv2
import numpy as np

cap = cv2.VideoCapture(0)

while(1):
   _, frame = cap.read()    
   cv2.imshow('Original',frame)
   edges = cv2.Canny(frame,100,100)
   cv2.imshow('Edges',edges)
   k = cv2.waitKey(5) & 0xFF
   if k == 27:
      break

cv2.destroyAllWindows()
cap.release()
ganchito55
  • 3,559
  • 4
  • 25
  • 46
TrikiAmine
  • 13
  • 1
  • 5

1 Answers1

2

Canny need a grayscale image as input, but your frame is a 3 channel (BGR) image. You need to convert it to grayscale before passing it to Canny:

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 100, 100)

As a sidenote, remember that Canny thresholds are used for hysteresis, so you may want to set the first_threshold to be something like [0.25 - 0.5] * second_threshold

edges = cv2.Canny(gray, 100, 200) 
Miki
  • 40,887
  • 13
  • 123
  • 202
  • Just a question that doesn't necessarily pertain to this question, but do you know of a way to fill the insides of the detected edges, as opposed to tracing the outside of the detected edges? Should I start another thread? – Shmack Jun 22 '20 at 20:39
  • drawContours(... cv2. FILLED... ) or fillPoly. There are already a lot of questions/answer on this – Miki Jun 23 '20 at 06:19
  • Thank you. So, I am wanting to find the biggest contour, and remove the background from it. I've seen the function to find contours, but I don't know how to distinguish between the contour, the contents inside of the contour, and the background. I am also concerned that the biggest contour might be the contents and not the bounding box. Consider a box. Inside the box, I draw one continuous line back and forth across the page such that the lines never cross and I end at the starting point (line wraps back). That contour would be longer than the bounding box. Unlikely? Not for what I need it. – Shmack Jun 26 '20 at 20:32