cocoa-xu / evision

Evision: An OpenCV-Erlang/Elixir binding
https://evision.app
Apache License 2.0
322 stars 22 forks source link

`Evision.imread` gives confusing `{:error, "empty matrix"}` error when file is not found #220

Closed thbar closed 9 months ago

thbar commented 9 months ago

Using an absolute path fixed it, but it took a good while before figuring things out.

I wonder if the error message could be made more user-friendly!

cocoa-xu commented 9 months ago

tl;dr

The best way is to check if the file exists before passing its path to Evision.imread; and OpenCV does support relative file path and you can obtain current working path in Elixir via File.cwd.

Some extra info

This is the default behaviour in OpenCV, returning an empty matrix if it cannot open the file. And as evision is a binding between OpenCV and Erlang/Elixir, it would simply convert any cv::Mat into a valid Erlang resource object or an error-tuple.

There is no special handling inside that conversion function because it's basically impossible to know why an OpenCV function decide to return an empty matrix. And that's why I would suggest to check if the file can be found in Elixir before calling Evision.imread.

thbar commented 9 months ago

@cocoa-xu thanks for the input! It makes sense :smile:

I only realise now that the documentation at https://hexdocs.pm/evision/Evision.html#imread/2 states:

@anchor imread The function imread loads an image from the specified file and returns it. If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format), the function returns an empty matrix ( Mat::data==NULL )

So this is even documented.

I will read closer next round :smile: thank you!