Python OpenCV chamerMatching function reference - python

I have searched everywhere and I find it truly amazing that there is no reference on the chamerMatching function, especially in Python. Someone else also had the same problem with no answer:
I don't really want to know about the algorithm - I know how the algorithm works - I want to know how to call it in Python and retrieve the costs, results and bestfit. I have tried the following code.
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
frame = cv2.GaussianBlur(frame, (13, 13), 0)
frame = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
frame = cv2.adaptiveThreshold(frame,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
templ = cv2.imread("template.png",cv2.CV_LOAD_IMAGE_GRAYSCALE)
cannyframe = cv2.Canny(frame,5,50,apertureSize=3)
cannytempl = cv2.Canny(templ,5,50,apertureSize=3)
cv2.imshow("cannyframe",cannyframe)
cv2.imshow("cannytempl", cannytempl)
cv2.waitKey(0)
#The line below, and NOT any other line, crashes the program
cv2.chamerMatching(cannytempl,cannyframe)
All of it runs fine except the final call to the chamerMatching function which causes the python interpreter to crash and stop working for some reason with a message that looks like this:
With absolutely zero documentation on the function, I can't figure out why.
EDIT:
The code above now includes all the required lines to run and below is template.png.

i don't know if you still need this information. But I also needed chamfer matching for my research. And here's what I found after encountering the same dilemma as you've had.
http://code.opencv.org/issues/3602
:)

Related

Using value pointer is unsafe and deprecated error in Opencv Python

I am new to opencv and python and was learning the basics from the docs. I was learning about the trackbar in opencv (python).
I wrote the same code as shown in the docs here. (Note the version is 4.5.3)
Here's my code:
import numpy as np
import cv2 as cv
def nothing(x):
pass
# Create a black image, a window
img = np.zeros((300,512,3), np.uint8)
cv.namedWindow('image')
# create trackbars for color change
cv.createTrackbar('R','image',0,255,nothing)
cv.createTrackbar('G','image',0,255,nothing)
cv.createTrackbar('B','image',0,255,nothing)
# create switch for ON/OFF functionality
switch = '0 : OFF \n1 : ON'
cv.createTrackbar(switch, 'image',0,1,nothing)
while(1):
cv.imshow('image',img)
k = cv.waitKey(1) & 0xFF
if k == 27:
break
# get current positions of four trackbars
r = cv.getTrackbarPos('R','image')
g = cv.getTrackbarPos('G','image')
b = cv.getTrackbarPos('B','image')
s = cv.getTrackbarPos(switch,'image')
if s == 0:
img[:] = 0
else:
img[:] = [b,g,r]
cv.destroyAllWindows()
When I run this code, I get the following annoying long warning:
Using 'value' pointer is unsafe and deprecated. Use NULL as value pointer. To fetch trackbar value setup callback.
This is the full warning message if anyone wants to refer:
[ WARN:0] global C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-q3d_8t8e\opencv\modules\highgui\src\window.cpp (704) cv::createTrackbar UI/Trackbar(R#image): Using 'value' pointer is unsafe and deprecated. Use NULL as value pointer. To fetch trackbar value setup callback.
I tried to understand what it is trying to say, but couldn't understand much. AFAIU, the error is in the line (and subsequent):
r = cv.getTrackbarPos('R','image')
Even thought it is a warning, I would like to get rid of it, as it uses the word unsafe and deprecated.
I have opencv-python of version 4.5.3.56. I've tried getting through the docs, but seems they are the same as for older versions, and this particular feature is deprecated in my version.
Can anyone suggest me what should be done to avoid this?
this bug in 4.5.3 is already reported and fixed (at least for python)
however, unless you rebuild it locally, you will have to wait for the next pypi release ;(

How should I resolve this error:TypeError: Canny() takes no arguments

I am new to python and trying to create an OpenCV demo while learning how to use the Canny() function.
This error occurs when I run the program:TypeError: Canny() takes no arguments
Here is my complete demo code:
import cv2
# 载入图片
img = cv2.imread("WhiteDolphinPic/WhiteDolphin_1.jpg")
print(img.shape)
# 自定义图片大小
imgResize = cv2.resize(img,(400,300))
print(imgResize.shape)
# 将imgResize设置为灰度图、高斯模糊和边缘检测
imgGray = cv2.cvtColor(imgResize,cv2.COLOR_BGR2GRAY)
imgBlur = cv2.GaussianBlur(imgGray,(7,7),0)
imgCanny = cv2.Canny(imgGray,100,100)
cv2.imshow("ImgResize",imgResize)
cv2.imshow("ImgGray",imgGray)
cv2.imshow("ImgBlur",imgBlur)
cv2.imshow("ImgCanny",imgCanny)
cv2.waitKey(0)
I have solved the problem.According to #Knight Forked's reply, I checked the Settings in the Python interpreter and found that I had used the configuration shown in Figure 1, but the configuration shown in Figure 2 worked fine.
Thank you all for your help.

cv2.cuda_CascadeClassifier in python

I am attempting to use the Haarclassifier from opencv cuda, for this I found the object cv.cuda_CascadeClassifier. However, assigning cv.cuda_CascadeClassifier() to a variable spit the following error:
this object has no ''load'' attribute. I could successfully verify it
by printing their dir() print(dir(cv.cuda_CascadeClassifier)).
Is there any other way to call this object or did anyone effectively exploite the cascadeclassifier with opencv cuda?
thx
The lack of documentation for the python API really is a pain. Speaking about the version 4.5 of OpenCV, you have to call the create method when reading a xml cascade file or it'll yield segmentation fault when trying to detect. In my experience you'll also need to convert to gray scale or it will yield (-215:Assertion failed) src.type() == CV_8UC1.
Here's my working code on OpenCV 4.5.1:
import cv2
img = cv2.imread('./img_sample.jpg')
cascade = cv2.cuda_CascadeClassifier.create('./cascade_file.xml')
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cuFrame = cv2.cuda_GpuMat(gray_img)
result = cascade.detectMultiScale(cuFrame).download() # download() gets the result as UMat
if result is not None:
print(result[0])
I hope this answers your question about the cuda version usage.
This is most likely due to the use of a version of openCV between 4.0.0 and 4.3.0, In those versions cuda_CascadeClassifier was disabled. In 4.4.0 This functionallity was brought back. (https://github.com/opencv/opencv_contrib/pull/2554/files)
Even though this seems to work fine in C++ it gives me a segmentation fault using the python wrapper using the following code:
classifier_cuda = cv2.cuda_CascadeClassifier('cascades_file.xml')
while True:
success, frame = vidcap.read()
cuFrame = cv2.cuda_GpuMat(frame)
result = classifier_cuda.detectMultiScale(cuFrame)
print (result)
Any solutions?
As already Breno Alef wrote, the problem of OpenCV for Python is the lack of documentation and also of some code examples, which makes difficult to understand how to write correctly Python code to use OpenCV.
Looking at the documentation of the cuda_CascadeClassifier or using the Python built-in function help() you can see that the cuda_CascadeClassifier is a subclass of cv.Algorithm, which contains the load() method, but the problem is the way cuda_CascadeClassifier works is a bit different from the Cascade class declared in opencv2/objdetect.hpp.

Python CV Webcam - Null Argument To Internal Routine

having recently learned the basics of python, I thought I would dive in with a small project to build on.
A webcam application. I would then add tools as I developed my skills.
I have installed matplotlib, CV, numpy, and various others,the code I found:
import cv
cv.namedWindow("lll")
cap = cv.VideoCapture(0)
while( cap.isOpened() ) :
ret,img = cap.read()
cv.imshow("lll",img)
k = cv.waitKey(10)
if k == 27:
break
Now the initial "video Source" dialogue comes up, I select my webcam and press OK.
Then I get an error:
while(cap.isOpened()):
SystemError: null argument to internal routine
Done a bit of googling. Found others with the same issue but no resolution...
Any tips?
Sorry for not addressing your specific problem, but you can always use the newer cv2 module:
import cv2
cv2.namedWindow("lll")
cap = cv2.VideoCapture(0)
while True:
ret,img = cap.read()
cv2.imshow("lll",img)
k = cv2.waitKey(10)
if k == 27:
break
Sounds like something that would require digging into the source of PyCV (or whatever the interface to OpenCV is called) to fix. My tip to you would be to go to the OpenCV IRC-channel and ask or/and file a bug report.

Error while trying to save webcam picture with OpenCV

import cv
capture = cv.CaptureFromCAM(0)
img = cv.QueryFrame(capture)
cv.SaveImage("test.JPG", img)
Hi,
I just want to save a picture from my webcam with OpenCv and Python on my Ubuntu 10.
OpenCv can connect with the webcam.
But I get this error:
OpenCV Error: Null pointer (NULL array pointer is passed) in cvGetMat, file /build/buildd/opencv-2.1.0/src/cxcore/cxarray.cpp, line 2376
Traceback (most recent call last):
File "video.py", line 5, in <module>
cv.SaveImage("test.JPG", img)
cv.error: NULL array pointer is passed
Save yourself a trip to the emergency room and use SimpleCV. It's a Pythonic wrapper for OpenCV's Python bindings and a few more tools (it uses Numpy, Scipy and PIL):
from SimpleCV import *
camera = Camera()
image = camera.getImage()
image.save('test.JPG')
I see this mistake over and over and over and over again: the CaptureFromCAM() call is failing, which means that QueryFrame() is failing as a consequence and returning NULL as image, causing SaveImage() to fail as well.
Two things that you need to take into consideration here:
1) your webcam might not be index 0 (try -1, or 1)
2) learn to code safely! Always check the return of the functions that are being called. This practice will save you a lot of time in the future:
capture = cv.CaptureFromCAM(0)
if not capture:
// deal with error, return, print a msg or something else.
img = cv.QueryFrame(capture)
if not img:
// deal with error again, return, print a msg or something else entirely.
cv.SaveImage("test.JPG", img)

Categories