5

I am trying to get an OpenCV hello world program to work using the nix package manager but its spewing out errors :

OpenCV Error: Unspecified error (The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script) in cvNamedWindow, file /tmp/nix-build-opencv-2.4.13.drv-0/opencv-2.4.13-src/modules/highgui/src/window.cpp, line 483 terminate called after throwing an instance of 'cv::Exception' what(): /tmp/nix-build-opencv-2.4.13.drv-0/opencv-2.4.13-src/modules/highgui/src/window.cpp:483: error: (-2) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function cvNamedWindow

I used the following default.nix file to create a nix-shell

with import <nixpkgs> {}; {
  cimgEnv = stdenv.mkDerivation {
  name = "cimgdev";
  buildInputs = [ pkgconfig stdenv cimg xorg.libX11 boost opencv gnome.gtk ];
  }; 
}

to compile and run the following code

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main() {

  Mat image;

  image = imread("lena.jpg", 1);

  if ( !image.data ) {
    cout << "No image data" << endl;
    return -1;
  }

  namedWindow("Display Image", WINDOW_AUTOSIZE);
  imshow("Display Image", image);

  waitKey(0);

  return 0;
}

using the following command

g++ `pkg-config --cflags opencv` opencvtest.cpp `pkg-config --libs opencv` -o opencvtest

It compiles, but if I try to run it, I get the previous error message.

I tried CMake, with the same outcome.

maou
  • 185
  • 6
  • 2
    I'm not familiar with nix, but the error message says `install libgtk2.0-dev and pkg-config` but you don't seem to be specifying `libgtk2.0-dev` anywhere. [See this answer too](http://stackoverflow.com/a/30979195/1270789). – Ken Y-N Nov 18 '16 at 00:25
  • @KenY-N - I added "gnome.gtk" to my default.nix file, which I believe is the equivalent of libgtk2.0-dev in nix packages. – maou Nov 19 '16 at 13:53

3 Answers3

7

OpenCV was built without GTK support. In the opencv package definition, enableGtk2 defaults to false. To enable it, override the argument in your default.nix:

let
  opencvGtk = opencv.override (old : { enableGtk2 = true; });
in 
   ...
   buildInputs = [... opencvGtk ...];
   ...

There's no need to add gtk2 nor anything besides pkgconfig to buildInputs unless you want to use it directly. After the lengthy opencv rebuild, building and running the code above works (given lena.jpg):

it works

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
brainrape
  • 326
  • 2
  • 7
  • `$ cd /nix/store/` `$ ls | grep opencv` `$ cd ..opencv..` `$ find . -name "*.pc"` gives opencv4.pc so I needed $ g++ `pkg-config --cflags opencv4` opencvtest.cpp `pkg-config --libs opencv4` -o opencvtest – Tom Huntington Jul 27 '21 at 21:42
2

I know that this question is for C++ but as this page is the first result in Google for people looking for the same problem with python, I think it can be useful to provide the solution for python as well…

So here is a shell.nix sample (replace python37 with the version of your choice) :

{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
  name = "dev-shell";
  buildInputs = [
    pkgs.python37Packages.numpy
    pkgs.python37Packages.flask
    # …
    (pkgs.python37Packages.opencv4.override { enableGtk2 = true; })
  ];
}

Or in a less verbose way :

{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
  name = "dev-shell";
  buildInputs = with pkgs.python37Packages; [
    numpy
    flask
    # …
    (opencv4.override { enableGtk2 = true; })
  ];
}

EDIT 2023-02-09

Actually, the right way seems to be :

{pkgs ? import <nixpkgs> {}}:

pkgs.mkShell {
  name = "dev-shell";

  packages = [
    (pkgs.python38.withPackages(ps: [
      ps.numpy
      ps.flask
      (ps.opencv4.override {enableGtk2 = true;})
    ]))
  ];
}
0

First glance at this.

Seem like your OpenCV build lack of UI support for appropriate platform (mainly they are in highgui library). I get these things from OpenCV CMakeLists.txt

status("")
status("  GUI: ")

if(HAVE_QT5)
  status("    QT 5.x:"            HAVE_QT        THEN "YES (ver ${Qt5Core_VERSION_STRING})" ELSE NO)
  status("    QT OpenGL support:" HAVE_QT_OPENGL THEN "YES (${Qt5OpenGL_LIBRARIES} ${Qt5OpenGL_VERSION_STRING})" ELSE NO)
elseif(HAVE_QT)
  status("    QT 4.x:"            HAVE_QT        THEN "YES (ver ${QT_VERSION_MAJOR}.${QT_VERSION_MINOR}.${QT_VERSION_PATCH} ${QT_EDITION})" ELSE NO)
  status("    QT OpenGL support:" HAVE_QT_OPENGL THEN "YES (${QT_QTOPENGL_LIBRARY})" ELSE NO)
else()
  if(DEFINED WITH_QT)
    status("    QT:" NO)
  endif()
  if(DEFINED WITH_WIN32UI)
    status("    Win32 UI:" HAVE_WIN32UI THEN YES ELSE NO)
  else()
    if(APPLE)
      if(WITH_CARBON)
        status("    Carbon:" YES)
      else()
        status("    Cocoa:"  YES)
      endif()
    else()
      if(HAVE_GTK3)
        status("    GTK+ 3.x:" HAVE_GTK THEN "YES (ver ${ALIASOF_gtk+-3.0_VERSION})" ELSE NO)
      elseif(HAVE_GTK)
        status("    GTK+ 2.x:" HAVE_GTK THEN "YES (ver ${ALIASOF_gtk+-2.0_VERSION})" ELSE NO)
      else()
        status("    GTK+:" NO)
      endif()
      status("    GThread :" HAVE_GTHREAD THEN "YES (ver ${ALIASOF_gthread-2.0_VERSION})" ELSE NO)
      status("    GtkGlExt:" HAVE_GTKGLEXT THEN "YES (ver ${ALIASOF_gtkglext-1.0_VERSION})" ELSE NO)
    endif()
  endif()
endif()

status("    OpenGL support:" HAVE_OPENGL THEN "YES (${OPENGL_LIBRARIES})" ELSE NO)
status("    VTK support:" HAVE_VTK THEN "YES (ver ${VTK_VERSION})" ELSE NO)

You can see a lot of GUI support, including Gtk2 as stated. So install ibgtk2.0-dev and pkg-config as said. Make sure HAVE_GTK variable valid. After configure by CMake, by default the support of GTK will ON. Otherwise you can check it.

Please edit/comment if you see anything not right.

khôi nguyễn
  • 626
  • 6
  • 15
  • I added "gnome.gtk" to my default.nix file, which I believe is the equivalent of libgtk2.0-dev in nix packages. pkgconfig is also in there. – maou Nov 19 '16 at 13:53