I am currently developing an medical image Follow-up system which requires to use registration function provided in SimpleElastix version of SimpleITK (Modified SimpleITK 2.0.0), however, I will also need to use some functions only in SimpleITK 2.2.0.
SimpleElastix: https://simpleelastix.github.io/
Is there any method that maybe I can build the SimpleElastix version to a different name so that I can import both version??
If you build the source code yourself, you can enable the SimpleITK_USE_ELASTIX CMake variable. Then SimpleElastix will be built in to SimpleITK.
v2.2.0 was the first SimpleITK release that includes the Elastix source code. However the binary Python packages are not built with Elastix enabled. Thus you need to build it from source.
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.
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()
I'm trying to write some programs using opencv and python. I installed opencv and the python libraries from the repositories. I'm using ubuntu 12.04. The folder /var/lib/python-support/python2.7 contains just 5 files :
cv2.so
cv.py
cv.pyc
simplegeneric-0.7.egg-info
simplegeneric.py
simplegeneric.pyc
From what reading I have done, I think there's supposed to be an opencv folder around here. I'm able to import cv library using
import cv
but
from opencv import cv
And I cannot load the highgui module. Any way to work around this? I would really really like to do something in opencv
You must have installed OpenCV >= 2.3.1. In OpenCV 2.3.1 and higher, Python bindings do not have highgui.
import cv2
import cv2.cv as cv
and you're good to go.
import cv2
img = cv2.imread("image name")
cv2.imshow("window name", img)
cv2.waitKey(0)
You can find more help on OpenCV docs and you could also look at some of the opwncv stuff that I have been doing here.
I hope this helps.
You will get the highgui file in OpenCV folder(installed folder) in include/opencv/highgui.h.
I'm trying to use OpenCV with Python and converting some C++ code. Anyway, if I do:
import cv
img = cv.LoadImage('image.jpg')
It's ok. Or:
import opencv.cv as opcv
size = opcv.cvSize(40, 50)
But anyway, the cv module doesn't have the cvSize data structure and the opencv.cv doesn't have the LoadImage. So, what exactly does each module have? I tried looking in the documentation but couldn't find it. Am I supposed to use it like this or is my setup misconfigured?
The real answer is :) that both "import opencv.cv" or "from opencv import cv" are the old-style wrapping imports.
Since OpenCV 2.0, the new-style Python wrapping is used, and the style you should use looks like this:
# import only cv, no opencv
# this also brings in sub modules such as highgui
import cv
# no "cv" prepended before all method names
src_mat = cv.LoadImageM('yourfilename.png', cv.CV_LOAD_IMAGE_GRAYSCALE)
# let's show the image in a window
cv.NamedWindow('your name', 1)
cv.ShowImage('your name', src_mat)
cv.WaitKey
The old-style wrappings made use of SWIG, the new-style wrappings, judging by the opencv 2.2 source code, seem to be self-made.
With:
import cv
You're importing the cv module from wherever it exists in the python modules search directories. This could be a different version of the module stored somewhere outside the opencv package install as it appears to be in this case.
But with:
import opencv.cv
You're explicitly importing the opencv packages version of cv, i.e. the one in the install directory for the opencv package. This version almost guarantees you have the one from the opencv package and it seems to be the same as using the syntax:
from opencv import cv