Problems with Aruco library on Debian 9.5 - OpenCV - python

I am trying to detect Aruco markers through my camera using OpenCV for Python 2.7 on Debian 9.5, but I can't run my code because of an errore dealing with cv2.aruco.detectMarkers(). Running it on Windows, it does not have any problem. In particular, I wrote in my code:
cv2.aruco.detectMarkers(image=gray, dictionary=aruco_dict, parameters=parameters,
cameraMatrix=camera_matrix, distCoeff=camera_distortion)
where camera_matrix and camera_distortion are respectively the camera matrix and the camera distortion parameters I got by camera calibration.
More precisely, the error says that there's no cameraMatrix input parameter for the function cv2.aruco.detectMarkers. How do I fix this problem? Thank you very much in advance.

Maybe your error is due to your opencv version. Check it with:
cv2.__version__
Older versions of opencv (such as 3.2.0, that is maybe your default version for Debian 9) do not have cameraMatrix or distCoeff as input parameters of cv2.aruco.detectMarkers function.
If you are interested in getting newer versions of opencv for your OS (such as 4.1.0.25), you have to do:
sudo pip install opencv-contrib-python==4.1.0.25
If you are not, just remove cameraMatrix and distCoeff from your inputs, it would run anyway.

Related

Python with Gstreamer pipeline

I'm working on an Udoo trying to get the camera to take a picture that I can manipulate inside Python.
So far, the camera works with
gst-launch-1.0 imxv4l2videosrc ! imxipuvideosink
I can also take a single picture with
gst-launch-1.0 imxv4l2videosrc num-buffers=1 ! video/x-raw ! jpegenc ! filesink location=output.jpg
From here it seems like you can read straight from a gstreamer stream in Python with OpenCV.
Here is my python code:
import cv2
cam = cv2.VideoCapture("imxv4l2videosrc ! video/x-raw ! appsink")
ret, image = cam.read()
However, ret is False, and image is nothing.
Some places say this only works with OpenCV 3.0+, and others say 2.4.x, but I can't seem to find an actual answer to what version it works on.
If I need to update to OpenCV 3.0, which part to I update? I downloaded OpenCV via the apt repositories under the package python-opencv. So do I need to update Python? Can I just build OpenCV from source, and Python will automatically be using the newest version? I'm so confused.
The Ubuntu/Debian version is old 2.4.x, to get the last one you need to compile it from source.
Here two tutorials on how to do that:
https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_setup/py_setup_in_fedora/py_setup_in_fedora.html#installing-opencv-from-source
http://www.pyimagesearch.com/2015/07/20/install-opencv-3-0-and-python-3-4-on-ubuntu/
The first is for Python 2.7 on Fedora, the second for Python 3.4 on Ubuntu.

How to set up power line frequency filtration in openCV

I use python and opencv to build a stereo tracker using two cameras.
The question is how to set up power line frequency filtration in openCV version 3.0.0 (or any orher). I know that the camera I use (microsoftHD3000) has this property and it can be set from v4l2 in linux and on windows I can use skype to set it once, but this is ugly.
Unfortunately I can not find the corresponding property in CAP_PROP_* constants.
What is the proper way to do it if any?
You can change the power line frequency setting as well as anything else using this answer, assuming you have installed GStreamer support (flag: -D WITH_GSTREAMER=ON).
If you don't have GStreamer, you can use v4l utils directly from terminal.
Since it is a prerequisite for installing OpenCV, so you should already have it.
Run:
for 50Hz:
v4l2-ctl --set-ctrl power_line_frequency=1
for 60Hz:
v4l2-ctl --set-ctrl power_line_frequency=2
to check current setting:
v4l2-ctl --all
Reference: Webcam flicker due to electricity grid frequency rate - Fedora Forums

Can not find KNearest() in python cv2

I am trying to write an OCR program in python. Now i got a program which perform OCR on digits and uses KNearest() function in cv2. But i didn't find any KNearest() function in cv2 while compiling the same. I already installed OpenCV form prebuilt binary cv2.pyd ( as per instructions given by site http://docs.opencv.org/trunk/doc/py_tutorials/py_setup/py_setup_in_windows/py_setup_in_windows.html#install-opencv-python-in-windows).
I'm assuming you are using OpenCV 3.x rather than 2.x. If this is the case, you'll want to replace
model = cv2.KNearest()
with
model = cv2.ml.KNearest_create()
>>> import cv2
>>> print cv2.KNearest
<built-in function KNearest>
If you're not able to do this, you most likely have a bad or old installation of opencv or its python bindings.
You didn't specify your OS, but if you're using any modern debian based distro (ubuntu, mint, ...), apt-get install python-opencv should suffice.

SIFT() in opencv is not working: 'module' object has no attribute 'SURF'

I am trying to run the simplest opencv SIFT code through the shell of Ubuntu, with no luck
I get an error:
AttributeError: 'module' object has no attribute 'SURF'
The code:
import cv2
cv2.SIFT()
My configurations:
Ubuntu version is 13.10 64bit
cv2.__version__ is 2.4.5
the output of dir(cv2) is (for the S letter only)
'scaleAdd', 'segmentMotion', 'sepFilter2D', 'setIdentity',
'setMouseCallback', 'setTrackbarPos', 'setUseOptimized',
'setWindowProperty', 'solve', 'solveCubic', 'solvePnP',
'solvePnPRansac', 'solvePoly', 'sort', 'sortIdx', 'split', 'sqrt',
'startWindowThread', 'stereoCalibrate', 'stereoRectify',
'stereoRectifyUncalibrated', 'subtract', 'sumElems'
This was driving me crazy but scratch all the other suggestions, turns out you can now get SIFT and SURF with just two terminal commands!
Be sure there is no other opencv on you computer...
pip uninstall opencv-python
Then get the contribute version (has SIFT and SURF + others)...
pip install opencv-contrib-python
It should install but note that the names are a little different.
import cv2
sift = cv2.xfeatures2d.SIFT_create()
!!!pip pip hurray!!! (that's just a pun, not part of the code)
import cv2
sift = cv2.SIFT()
This code will not work if you are using opencv version 3.0 or greater.
an alternative of this code is
sift = cv2.xfeatures2d.SIFT_create()
(Only works if you have installed opencv-contrib-python library )
Now again if you have an opencv-contrib-python version > 3.4
than it won't work with a different erro
error: OpenCV(4.1.0)
C:\projects\opencv-python\opencv_contrib\modules\xfeatures2d\src\sift.cpp:1207:
error: (-213:The function/feature is not implemented) This algorithm
is patented and is excluded in this configuration; Set
OPENCV_ENABLE_NONFREE CMake option and rebuild the library in function
'cv::xfeatures2d::SIFT::create'
best solution for this is:
**step 1: pip uninstall opencv-python**
**step 2: pip install opencv-contrib-python==3.4.2.16**
This worked for me.
[Note : If you have not installed opencv using pip install opencv-python, than just go and delete the downloaded library and follow the above instruction]
Not the smoothest way to do it, but it worked for me.
#Berak explained to me, as you can observe in the comments on my question, that the SIFT algorithm, as well as FAST algorithm are patented, which means that they are not part of the regular opencv installation.
Therefore I searched for a python distribution that will have it all - and I found one. So, I didn't actually solved the problem, as #Berak suggested, alternatively I bypassed it using Python(x,y)
Thanks for the help,
Ozrad
I also had problem in using SIFt because i had only openCV. But after installing ROS Hydro, i am able to use SIFT/SURF as they come under nonfree part.
A simple code i found for SIFT
import cv2
import numpy as np
img = cv2.imread('home.jpg')
gray= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
sift = cv2.SIFT()
kp = sift.detect(gray,None)
img=cv2.drawKeypoints(gray,kp)
cv2.imwrite('sift_keypoints.jpg',img)
And i tested the code, it works
I followed the openCV python windows installation guide. I went to try using cv2.SIFT and found it was not available in this installation.
After completely removing python 2.7 and openCV, I then installed python(x,y) and included openCV. I get
cv2.version
'2.4.8'
And:
cv2.SIFT ->
cv2.SURF
So python(x,y) does include SIFT,SURF modules.
You can use it easily as follows:
sift = cv2.xfeatures2d_SIFT()
keypoints_detector = sift.detect(image=your_grayscale_image, mask=None)
Both the SIFT and SURF are pattented algorithms by their respective authors. Previously they were not available in the main repository of OpenCV but in contrib but now as per OpenCV, their patent has been expired in 2020 hence SIFT and SURF are added in the main repository in latest releases. I have tried 4.5.2 and it works fine.
You can install this opencv version using pip3 install opencv-python=4.5.2.
To instantiate and test SIFT use the below code.
import numpy as np
import cv2 as cv
img = cv.imread('home.jpg')
gray= cv.cvtColor(img,cv.COLOR_BGR2GRAY)
sift = cv.SIFT_create()
kp = sift.detect(gray,None)
img=cv.drawKeypoints(gray,kp,img)
cv.imwrite('sift_keypoints.jpg',img)
Use a version above 4.4.0, SIFT was moved into main lib again.
https://opencv.org/opencv-4-4-0/
pip install opencv-python==4.4.0.46
import cv2
sift = cv2.SIFT_create()

Using OpenKinect on Raspberry pi in python

I am very new to raspberry pi and python.
I am trying write a progam using python on raspberry pi to use the Kinect. I aim to install OpenKinect on Raspberry pi.
So far I have done:
apt-cache search OpenKinect
sudo apt-get install python-freenect
sudo apt-get update
Next i tried writing a code in python from this link https://github.com/OpenKinect/libfreenect/blob/master/wrappers/python/demo_cv_async.py
When i try to run the programe, it says that
error in line 5,
import cv.
ImportError:no module named cv.
I am not sure if i have installed all the necessary files. I am also not sure what i have done wrong.
I also have been trying to look for tutorials on installing and using OpenKinect.
Congradtulations on starting python! That sounds like a complicated project to start on. You should probably try doing the tutorial first at python.org. I particularily like the google video tutorials (if you are a classroom kind of person): http://www.youtube.com/watch?v=tKTZoB2Vjuk
After that you can dig into more detailed stuff :)
It looks like you still dont have opencv package for python. Try to install it:
sudo apt-get install python-opencv
The OpenGL or GTK-Warning: Cannot open display. Or the other one you stated
Number of deviced found:1 GL thread write reg 0x0105 <= 0x00 freeglut(freenect-glview):
OpenGL GLX extension not supported by display ':o.o'
is because freenect does not support OpenGL. It probably uses EGL.
bmwesting (Brandt) wrote:
"The freenect library provides a demo for the Kinect called glview. The glview program will > not work with the Pi because the program is written using OpenGL. The Raspberry Pi only supports GLES through EGL.
It seems like you will be able to use libfreenect to grab the depth stream and rgb stream, > but will be unable to run the demo program because it uses the incorrect graphics API."
If you read through that thread, it should show the alternatives (i.e. ASUS XTion instead of Kinect). They reach 30fps at high (~ 1024x800) resolution for depth data if using console output mode. I plan to go for Xtion now too - and I hope to get deactivate as much as possible from the USB bus (as this seems to be the bottleneck, for the Kinect too I think).
When you install OpenCV using apt-get install python-opencv you are installing version 2. However, you can still use the methods from version 1 by doing so:
import cv2.cv as cv

Categories