Skip to content

Usage of imread(): magic number 0, unchecked result #23107

@crackwitz

Description

@crackwitz

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_GRAYSCALE and IMREAD_COLOR
  • instances where the result is not checked for being empty/None

Example

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions