Search code examples
pythonopencv

Can the superres module in OpenCV only be used in C++?


I have built OpenCV4 on Windows 11. When I attempt to use the superres module by Python, I encountered an error: module 'cv2' has no attribute 'superres'

import cv2
model = cv2.superres.createSuperResolution_BTVL1()

I had included the superres module when building OpenCV.

Cmake info: Cmake info

Then I found using C++ is ok. So can this module only be used in C++?

#include <iostream>
#include <cstring>
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp" 
#include "opencv2/superres.hpp"

int main(int argc, char* argv[])
{
        // no error.
    cv::superres::createSuperResolution_BTVL1();
    return 0;
}


Solution

  • The Python wrappers for OpenCV are generally automatically generated (there are some hand-written ones, but that's a tiny fraction). In order for this to happen, annotations in form of macros have to present in the header file(s) of the respective module. For free-standing functions and classes this is usually CV_EXPORTS_W and friends, for class member functions some variation of CV_WRAP, and there are some more that may need to be used for function arguments. I've described a few in an answer to this question, and the OpenCV documentation contains some useful bits as well.

    If we examine the header of superres module, we can se no such annotations (CV_EXPORTS is just a generic export/import doohickey, no wrappers are generated there). The superres module was moved out from main to contrib during transition to 4.0, apparently due to lack of maintenance. When I look at the 3.x branch, I can't find any annotations either. I didn't dig any deeper, so who knows, it might have had something available in 2.x, but that's not really relevant today.

    Generally, when Python wrappers are generated, so is a function signature for the documentation. None can be seen in the documentation, so that's some more evidence to support our conclusion.


    Based on the above, this module's functionality is not available in the standard Python bindings for OpenCV. That does not necessarily mean it's available only from C++, since some bindings for other languages are implemented differently, but since this is an unmaintained contrib module, it's likely the case.

    Possible solutions:

    • As Christoph mentioned, have a look at dnn_superres -- that has Python bindings available.
    • Adding the annotations to superres likely wouldn't be too difficult, if you took inspiration from some other modules that have them on classes. I have no idea how easy it would be to get such a pull request accepted there, but might be worth a try, unless you want to ship your own custom build with your program.
    • I suppose you could try to use it via ctypes, but given it's C++, it's like opening another can of worms, and portability goes outta the window.
    • You could write a thin wrapper around the needed functionality using Boost.Python or pybind11. Both have support for numpy arrays, so it shouldn't be too hard to make it play nice with regular OpenCV Python code.