OpenCV: Difference between revisions

Mmkaram (talk | contribs)
m Added more context to the intro and rust sections
Mmkaram (talk | contribs)
m Fixed grammar and improved flow
Line 1: Line 1:
[https://opencv.org/ OpenCV] is the world's largest and most popular computer vision library<ref name=":0">https://opencv.org/</ref>. Originally developed by [https://www.intel.com/content/www/us/en/homepage.html Intel] in 2000, it is open source under the [[wikipedia:Apache_License|Apache License]] 2. OpenCV interfaces with many languages such as C++, Python and Java<ref name=":0" />.
[https://opencv.org/ OpenCV] is the world's largest and most popular computer vision library<ref name=":0">https://opencv.org/</ref> in the world. It was originally developed by [https://www.intel.com/content/www/us/en/homepage.html Intel] in 2000. It is open source under the [[wikipedia:Apache_License|Apache License]] 2. OpenCV interfaces with many languages such as C++, Python and Java<ref name=":0" />.


Due to dynamically linked libraries and not all necessary dependencies being packaged with the base version of the package for all uses, this page was created to help with the initial hassle of getting OpenCV to run in development environments.
Due to needing dynamically linked libraries and not having all necessary dependencies being packaged with the base version of the package for all uses, this page was created to help with the initial hassle of getting OpenCV to run in development environments.


== Python ==
== Python ==
To use OpenCV with Python it is generally recommended to use the Python3XXPackage package found in [[nixpkgs]]. Although, as OpenCV is written mainly in C++<ref>https://github.com/opencv/opencv</ref>, it relies heavily on stdlibc which is generally a dynamically linked library. Attempting to run any OpenCV algorithms in Python may thereby lead to errors such as this:<syntaxhighlight lang="bash">
To use OpenCV with Python it is generally recommended to use the Python3XXPackage package found in [[nixpkgs]]. Although, as OpenCV is written mainly in C++<ref>https://github.com/opencv/opencv</ref>, it relies heavily on stdlibc which is a dynamically linked library. Attempting to run any OpenCV algorithms in Python may thereby lead to errors such as this:<syntaxhighlight lang="bash">
ImportError: libstdc++.so.6: cannot open shared object file: No such file or directory
ImportError: libstdc++.so.6: cannot open shared object file: No such file or directory
</syntaxhighlight>There are two main ways of solving this issue, using a shell.nix or flakes.
</syntaxhighlight>There are two main ways of solving this issue, using a shell.nix or flakes.
Line 66: Line 66:
}
}


</syntaxhighlight>Both the dev shell and flake examples only allow for the base compilation of OpenCV, there are options you may want to enable like gtk support to use functions like cv2.imshow(). To enable said flags/features, you must use [https://nixos.org/guides/nix-pills/17-nixpkgs-overriding-packages.html overrides]. Beware, this may cause a full recompilation of OpenCV because you're changing build flags and that specific combination may not be cached for your system.
</syntaxhighlight>Both the dev shell and flake examples only allow for the base compilation of OpenCV. You can enable other flags such as gtk support to use functions like cv2.imshow() by using [https://nixos.org/guides/nix-pills/17-nixpkgs-overriding-packages.html overrides]. Beware, this may cause a full recompilation of OpenCV as you're changing build flags and that specific combination may not be cached for your system.


=== Shell.nix Override Example ===
=== Shell.nix Override Example ===
Line 127: Line 127:
It is important to note that the most popular OpenCV package for rust, [https://github.com/twistedfall/opencv-rust opencv-rust], is just a series of bindings for the C++ version of OpenCV, and is thereby unsafe. Errors that occur while trying to use these bindings may just fail quietly or return an error from the underlying C++ code. There is an effort to implement most of the main computer vision algorithms entirely in rust called [https://github.com/rust-cv/cv rust-cv]. The package has not been actively maintained since July of 2023.
It is important to note that the most popular OpenCV package for rust, [https://github.com/twistedfall/opencv-rust opencv-rust], is just a series of bindings for the C++ version of OpenCV, and is thereby unsafe. Errors that occur while trying to use these bindings may just fail quietly or return an error from the underlying C++ code. There is an effort to implement most of the main computer vision algorithms entirely in rust called [https://github.com/rust-cv/cv rust-cv]. The package has not been actively maintained since July of 2023.


The opencv-rust package does not include the C++ bindings, thereby requiring it to be installed alongside the package itself. Luckily, opencv is packaged in nixpkgs and using it is just a question of getting on our system, the crate does the rest of the work.
The opencv-rust package does not include the C++ bindings, thereby requiring it to be installed alongside the package itself. Luckily, it is packaged in nixpkgs and using it is just a question of getting on our system, the crate does the rest of the work.


=== Flake.nix Example ===
=== Flake.nix Example ===