-
-
Notifications
You must be signed in to change notification settings - Fork 56.5k
Usage of imread(): magic number 0, unchecked result #23107
Description
Descripe the doc issue
Note: template needs a fix in the "descripe" wording up there
Example code should fail explicitly rather than quietly, and it should teach use of named constants rather than magic numbers.
Docs contain issues related to imread():
- instances of the magic numbers 0 and 1, instead of
IMREAD_GRAYSCALEandIMREAD_COLOR - instances where the result is not checked for being empty/
None
Example
- in docs: https://docs.opencv.org/4.x/da/d22/tutorial_py_canny.html, https://docs.opencv.org/4.x/d5/daf/tutorial_py_histogram_equalization.html
- in the wild: https://stackoverflow.com/questions/75039808/cv2-error-opencv4-7-0-1-error-5bad-argument-in-function-imwrite-img
Newbies have issues finding their image files because they don't know how relative paths work or what directory they're working in. Teaching that is beyond the scope of OpenCV.
Newbies expect to be thrown exceptions from code when any errors happen. To demonstrate basic error checking should definitely be expected of OpenCV example code.
Fix suggestion
In docs, examples, and apps, find all instances of imread(), check for second argument, rewrite magic numbers to named constants.
In docs and basic examples, add at least an assertion on the result so the code fails explicitly. Perhaps add an explicit assertion check for the file's existence.