How to make the opencv-contrib module work? - python

I followed this guide to install opencv (version 3.4.4) and the contrib modules because I want to work with the SIFT algorithm.
https://www.pyimagesearch.com/2018/05/28/ubuntu-18-04-how-to-install-opencv/
When I noticed, it was not working as expected after the installation, I deleted the build folder and tried again, but this did not work as well.
I imported like this:
import cv2 as cv
I tried to use SIFT (in python 3.6) in the following ways:
sift = cv.SIFT_create()
sift = cv.xfeatures2d.SIFT_create()
sift = cv.SIFT()
sift = cv.xfeatures2d.SIFT()
As this usually results in errors like this: Attribute Error: module cv2 has no attribute 'SIFT_create' (same thing happens for the other 3 options), I figure that I am either using it in an incorrect way or the installation process itself did not work properly.
After this, I found the pip install opencv-contrib-python and used it. With no results at all.
I would really appreciate some hints on how I can make opencv with contrib modules work.

You may have to say cv2.xfeatures2d_SIFT or similar. the modules of OpenCV don't necessarily map to python submodules.
Since the patent on SIFT expired in 2019, OpenCV moved it back into features2d (main repo) from xfeatures2d (opencv_contrib repo). Please use the most recent 3.4.x release, or 4.x.

Related

AttributeError: partially initialized module 'cv2' has no attribute 'CascadeClassifier' (most likely due to a circular import)

I've been going round in circles for ages trying to figure this out. Why am I getting this attribute error? I've tried using absolute references, and get the same issue. PyCharm is also highlighting CascadeClassifier, cvtColor and COLOR_BGR2GRAY saying it cannot find reference in cv2.py. I'm not sure if more information is relevant to solving this problem, so please ask if more is needed.
import cv2
face_cascade = cv2.CascadeClassifier('read_only/haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('read_only/haarcascade_eye.xml')
grayed_images = []
for x in np_images:
gray_img = cv2.cvtColor(x, cv2.COLOR_BGR2GRAY)
grayed_images.append(gray_img)
print(x)
If I am correct, you are using an environment different from where you run.
Step1: In command line/terminal where you see the opencv-python when you run pip list:
run python command. Copy your code, check if it works (you can simply import cv2 alternatively)
If it works, my idea should be correct. Otherwise, there is something bigger.
Step 2: (Assuming step1 works.) In Pycharm, Under Run > Edit Configurations, change python interpreter to whichever interpreter is you are that has opencv.
Step2 better alternative: On Pycharm, open the terminal, pip install opencv-python. After that you should have the opencv.
Downgrade opencv to the version 4.5.5.64.
You can install opencv using
pip install opencv-python==4.5.5.64.
This works for me

OpenCV has no drawKeypoints function

I'm using Python 3.7.1 and OpenCV 4.0.0
I'm just testing FAST algorithm with various configurations, and I have problems with the drawKeypoints function. In fact, I copied OPenCV tutorial code, just to realize that the function needs a third argument, the output image. After that, everything was working, and I commited to the repo.
Later on, I had to format my pc, and reinstall everything. Now, whenever I try to run that code, it complains about module 'cv2' has no attribute 'drawKeypoints'. Here is the most basic code I had running after the format.
import numpy as np
import cv2
import sys
img = cv2.imread(sys.argv[1], 0)
fast = cv2.FastFeatureDetector_create()
kp = fast.detect(img,None)
img2 = cv2.drawKeypoints(img, kp, None, color=(255,0,0))
cv2.imshow('Original',img)
cv2.imshow('Detected',img2)
cv2.waitKey(0)
cv2.destroyAllWindows()
I've been looking, and I have no clue about what could be wrong with this code.
It looks related to this bug which has a solution in the git repository (sources), but not necessarily is updated in the installation using pip. It was merged on Dec 11 2018, so for the time of this question it was probably not updated.
Maybe getting an updated wheel could solve the problem or you can build it yourself from sources. Here you can also see how this is generated (the one from pip install) and also offers you the tool to build your own opencv wheel.

Python extension scikit-image error

I am trying to use scikit-image to do some research. The system is Windows 7 64bit, and the python version is 2.7, 64bit.
The first program I run is from: http://scikit-image.org/
The code is
from skimage import data, io, filter
image = data.coins() # or any NumPy array!
edges = filter.sobel(image)
io.imshow(edges)
io.show()
However, the problem happens, and the error message is:
C:\Python27\lib\site-packages\skimage\io_plugins\null_plugin.py:14:
RuntimeWarning: No plugin has been loaded. Please refer to
skimage.io.plugins()
for a list of available plugins.
warnings.warn(RuntimeWarning(message))
I believe that both Python and scikit-image are correctly installed. So, may I know what is wrong with it?
Any suggestion is appreciated. Many thanks.
Just had precisely the same issue on amazon linux. The issue is that skimage requires PIL, and PIL is not installed. In the latest skimage they added the dependency, but the version that I got installed with pip didn't have it yet.
The solution is
pip install Pillow
EDIT: and after that you will probably immediately face another issue, with skimage loading image, but not being able to read it (in particular, shape being empty tuple). Here's the solution
Why does scipy.ndimage.io.imread return PngImageFile, not an array of values

How to build OpenCV 3.0.0 for Python with non-free modules?

I know this isn't a pure programming question, but I'd like to have a response like https://stackoverflow.com/a/18590112 .
I'd like to know how to build the OpenCV 3.0.0-beta library to use it with Python, with SIFT and SURF functions like seen in the OpenCV documentation, which are a non-free part of the lib, on a Debian Linux operating system, via the command line.
Thanks in advance, and I hope this topic will help in the future all the persons who Googled desperately to find a good tutorial to build this library.
with opencv3.0, sift and surf have been moved to a opencv_contrib repo, also, you will need to build the whole thing from src. so:
fork/clone/download that. take your time with the readme there.
add it to your cmake settings in the main opencv repo: cmake -DOPENCV_EXTRA_MODULES_PATH=<opencv_contrib>/modules <opencv_source_directory>
(re-)build: cmake, make, make-install.
if all went well, you can try it:
>>> import cv2
>>> help(cv2.xfeatures2d) # additional namespace !
Help on module cv2.xfeatures2d in cv2:
NAME
cv2.xfeatures2d
FILE
(built-in)
FUNCTIONS
SIFT_create(...)
SIFT_create([, nfeatures[, nOctaveLayers[, contrastThreshold[, edgeThreshold[, sigma]]]]]) -> retval
SURF_create(...)
SURF_create([, hessianThreshold[, nOctaves[, nOctaveLayers[, extended[,upright]]]]]) -> retval
>>> sift = cv2.xfeatures2d.SIFT_create()
>>> sift.detect(...)

Where can i get OpenCV for python?

Where can i get OpenCV for python?What are the pre-requisites?? i tried to install opencv-python through synaptic package manager but Python says
No module named CVtypes
CVTypes is a third party implementation that essentially wraps python around objects written in C, the language that OpenCV is written in (along with C++). If you want to use that, you will have to download and install it separately, as it is not part of the standard repositories of Ubuntu's Synaptic package manager that I know of at this time (I assume you are on Ubuntu because you mentioned 'Synaptic', Ubuntu's package manager).
However, there is an official python interface for OpenCV that is included in the OpenCV SVN repository and build packages. When installing version 1.0 from the package manager in Ubuntu, the python modules will be installed in the following directory:
/usr/lib/pymodules/python2.6/opencv
Ensure that is part of your PYTHONPATH environment variable and you should be able to import the modules as such:
from opencv.cv import *
from opencv.highgui import *
OpenCV over time has accumulated numerous Python bindings, mostly due to the strange way arrays are represented in OpenCV (IMHO). Here is a short list:
PyOpenCV
Scikits Image
Ctypes OpenCV
SWIG OpenCV
Choose which one you want to use and keep it consistent and upto date. I personally prefer the classic WillowGarage version[listed last] over its fancier cousins since it has most development and test muscle behind it.
get it from here unofficial binary packages.
by the way, they provide unofficial packages for many other projects
Tried the official website? http://opencv.willowgarage.com/wiki/Welcome
check your openCV version. Version 2 needs a simple
import cv
you may have a look at the samples/python folder.
this webpage explains in great depth on the installation
http://opencvpython.blogspot.com/2012/05/install-opencv-in-windows-for-python.html
after the installation try out the samples provided by opencv\samples\python2

Categories

Resources