Using TriangulatePoints with SFM with Python - python

I'm trying to use the built in opencv function TriangulatePoints for multiple views: https://docs.opencv.org/3.4.4/d0/dbd/group__triangulation.html#ga211c855276b3084f3bbd8b2d9161dc74.
Using python on linux, does anyone have experience using this function? The website only has the syntax for cpp and in python I don't know how to code it.
My code now:
import cv2
import numpy as np
point_2D = np.array([[17.4485, 709.7993], [17.4382, 709.8409]])
Proj_Matrices = np.array([ [1037.5, -6.9927, -10.0190, -4780.7], [6.9747, 1043.3, -5.8867, -731.9206], [644.7895, 383.4982, -3231.1], [1036.937, -22.8371, -28.3254, -5607.7], [23.0587, 1043.1, 3.1815, -633.4485], [650.4355, 373.6, -15.3504, -3706.5] ])
OutputArray = np.zeros((3,2))
Points_3D = cv2.sfm.triangulatePoints(point_2D, Proj_Matrices, OutputArray)
However, when I run it from the terminal I get the following error:
AttributeError: module 'cv2' has no attribute 'sfm.
I installed sfm on my computer using the instructions on the opencv site.
When I omit sfm I get the following error:
AttributeError: module 'cv2' has no attribute 'sfm'
I think I get that error since the machine thinks I'm trying to use the previous version of TriangulatePoints.
Examples of opencv code using sfm exist on the opencv website but they are in cpp.
I'm wondering what the syntax for using TriangulatePoints(with sfm) is in python? Even in cpp, I don't understand how the output array is an input to the function? Also, if anyone knows how to fix the sfm error I am receiving that would be appreciated.
Thanks!

Related

059 Python: Tensorflow - AttributeError: 'OwnedIterator' object has no attribute 'string_handle'

the code is as follows :
iterator = tf.compat.v1.data.make_one_shot_iterator(batched_dataset)
handle = iterator.string_handle()
i try to run a Captcha crack code which is this github project and despite the fact that i had to modify so many parts of the code for compatibility issues i face this error and the error says :
AttributeError: 'OwnedIterator' object has no attribute 'string_handle'
i have been trying for more than 1 day and couldn't and can't find any solution to this issue hope someone help me run the code with No Error!
i am using Pycharm and python and Tensorflow version = 2.4.1

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.

I am getting error when printing the pixel value of the read image in python-opencv, TypeError: 'NoneType' object has no attribute '__getitem__'

I have freshly installed opencv, and checked that its properly installed by typing:
pkg-config --modversion opencv
at the command terminal.
I started using pything-opencv for reading and displaying an image, but when I run my code, it throws an error:
TypeError: 'NoneType' object has no attribute '__getitem__'
My code is very minimal, but not getting where is there error.
The code which I am running is:
import cv2
import numpy as np
from matplotlib import pyplot as plt
import argparse
img = cv2.imread('messi5.jpg')
print(img)
print("end of file")
It gives the output:
None
end of file
When I write two more lines as this:
px = img[100,100]
print(px)
then it throws error:
Traceback (most recent call last):
File "testing_opencv_python.py", line 23, in
px = img[100,100]
TypeError: 'NoneType' object has no attribute 'getitem'
The same code runs perfectly on other systems.
I would be thankful if you can point out the mistake.
I basically want to install caffe, but when i did that i was getting error, and seems like it depends on opencv, thats whey I have installed opencv.
Thanks and regards.
The returned image is None (you can see it when you print it), which is causing the other error down the line.
This is most likely due to specifying the wrong image path ('messi5.jpg'). In the the documentation here, it states:
Warning Even if the image path is wrong, it won’t throw any error, but print img will give you None
Either provide a correct path to 'messi5.jpg', or copy the image into your current directory (where you execute the python script).

Background Subtraction MOG in OpenCV

I have been following some tutorials on OpenCV and many of them reference the cv2.BackgroundSubtractorMOG(). The issue is that the name or this functionality has changed or been removed. Does anyone know where I can find more current documentation. Does anyone know how to use this feature now?
This is the message I get from python:
File "OpenCV.py", line 5, in
bgs = cv2.BackgroundSubtractorMOG(24*60, 1, 0.9, 0.01)
AttributeError: 'module' object has no attribute 'BackgroundSubtractorMOG'
In the Opencv3.0 version, only the BackgroundSubtractorMOG2 was kept as part of the main module as it provided better performance. You can find that here
https://github.com/Itseez/opencv/blob/master/modules/video/include/opencv2/video/background_segm.hpp#L90
Or if looking for BackgroundSubtractorMOG, it has been shifted to the opencv contrib module
https://github.com/Itseez/opencv_contrib/tree/master/modules

Windows OpenCV Python: 'module' object has no attribute 'SIFT'

I've seen this question answered to Linux operating systems, but I'm working with Windows (VS 2010), and I can't find a solution.
This is my code:
import numpy
import cv2
campo = cv2.imread("fotograma.png")
balon = cv2.imread("balon.png")
#SIFT detector
sif = cv2.SIFT()
I'm using Python 2.7, as I said, with VS 2010, but even if I try typping my code in the Python interpreter, it shows me the same error message. The funny thing is, when I type cv2.SI in my editor, it suggests me the SIFT function, so it looks to recognize it.
Please help me.

Categories