Finding 3d coordinates (xyz) of detected object. (Object detected using Haar cascade) - python-3.x

have been able to successfully detect an object using haar cascade classifier in python using opencv.
When the object is detected, a rectangle is shown around the object. The x and y position of this rectangle is known and so is the height and width (x,y,w,h).
The problem I have is trying the get the 3d coordinates form this rectangle (x,y,z).
I have not been able to find an resources on getting the z coordinate of the detected object.
I calibrated my camera and found the camera matrix and also the distortion coefficient but not sure next what to do next. I looked into pose estimation but do not think that will be able to get the 3D coordinates of this object.
Please any help will be sufficient. I just need to be pointed in the right direction so i can continue my project
Thank you.

Related

Live camera to shape distance calculation based on computer vision

The goal is to live detect the walls and export the distacne to wall .There is a setup , A closed 4 wall , one set of unique & ideal shape in each wall ( Triangle , Square .....) A robot with camera will roam inside the walls and have computer vision. Robot should detect the shape and export the distance between camera and wall( or that shape ).
I have implemented this goal by Opencv and the shape detection ( cv2.approxPolyDP ) and distance calculation ( perimeter calculation and edge counting then conversion of pixel length to real distance ).
It perfectly works in 90 degree angle , but not effective when happening in other angles.
Any better way of doing it.
Thanks
for cnt in contours[1:]:
# considering countours from 1 because from practical experience whole frame is often considered as a contour
area = cv2.contourArea(cnt)
# area of detected contour
approx = cv2.approxPolyDP(cnt, 0.02*cv2.arcLength(cnt, True), True)
#It predicts and makes pixel connected contour to a shape
x = approx.ravel()[0]
y = approx.ravel()[1]
# detected shape type label text placement
perimeter = cv2.arcLength(cnt,True)
# find perimeter
in other degrees you have the perspective view of the shapes.
you must use Geometric Transformations to neutralize perspective effect (using a known-shape object or angle of the camera).
also consider that using rectified images is highly recommended Camera Calibration.
Edit:
lets assume you have a square on the wall. when camera capture an image from non-90-degree straight-on view of the object. the square is not align and looks out of shape, this causes measurement error.
but you can use cv2.getPerspectiveTransform() .the function calculates the 3x3 matrix of a perspective transform M.
after that use warped = cv2.warpPerspective(img, M, (w,h)) and apply perspective transformation to the image. now the square (in warped image) looks like 90-degree straight-on view and your current code works well on the output image (warped image).
and excuse me for bad explanation. maybe this blog posts can help you:
4 Point OpenCV getPerspective Transform Example
Find distance from camera to object/marker using Python and OpenCV

contour detection and manipulating the contour

Original Image before finding contoursFinding the diameter of the inner circle after the contour detection
I have an image, and I am using openCv and python to detect the contour as shown in the attached image.
The object under examination is an irregular shaped object(Image attached), need to find the diameter of that detected object.
I am using findcontours and filtering out the contours based on their areasDescription of the image :green lines-contours detected, black line looking to find the inner circle like the one marked
Is there a way to get the circle as shown using openCV-python
Can anyone let know how this can be achieved.

Calculate the angle from camera to detected object

I am currently using haar cascade to detect an object and this returns the x and y pixel coordinates of the object and also the width and height of the object in pixels. (object size is unknown)
I would like to calculate the angle from the camera to the object.
I researched and found out that using just the field of view of the camera and the camera resolution, the angle can be calculated but I am currently not sure how to do this because I could not find anymore information.
I kindly need some help in computing this. Any assistance will be appreciated.

How do I find the world coordinates of a pixel on the image plane?

A bit of background
I am writing a simple ray tracer in C++. I have most of the core complete but don't understand how to retrieve the world coordinate of a pixel on the image plane. I need this location so that I can cast the ray into the world.
Currently I have a Camera with a position(aka my perspective reference point), a direction (vector) which is not normalized. The directions length signifies the center of the image plane and which way the camera is facing.
There are other values associated with the camera but they should not be relevant.
My image coordinates will range from -1 to 1 and the perspective(focal length), will change based on the distance of the direction associated with the camera.
What I need help with
I need to go from pixel coordinates (say [0, 256] in an image 256 pixels on each side) to my world coordinates.
I will also want to program this so that no matter where the camera is placed and where it is directed, that I can find the pixel in the world coordinates. (Currently the camera will almost always be centered at the origin and will look down the negative z axis. I would like to program this with the future changes in mind.) It is also important to know if this code should be pushed down into my threaded code as well. Otherwise it will be calculated by the main thread and then the ray will be used in the threaded code.
(source: in.tum.de)
I did not make this image and it is only there to give an idea of what I need.
Please leave comments if you need any additional info. Otherwise I would like a simple theory/code example of what to do.
Basically you have to do the inverse process of V * MVP which transforms the point to unit cube dimensions. Look at the following urls for programming help
http://nehe.gamedev.net/article/using_gluunproject/16013/ https://sites.google.com/site/vamsikrishnav/gluunproject

how to convert depth in Z-cordinate

I'm making a project in which i need to draw user's feet in a rectangle of 640*480
and i'm mapping the skeleton joints coordinate into depth coordinates to fit it in the box,
but the DepthImagePoint gives x-y coordinate and depth(in mm) but i want x-z coordinate.
How can I get the z coordinate to fit in the resolution of 640*480?
or Can i somehow convert skeleton joints coordinate to proper resolution to fit the box?
I'm using Microsoft Kinect SDK with C#.
Thanks in Advance.
There are several functions in the CoordinateMapper that will map points between the different data streams. MapSkeletonPontToDepthPoint being the first to come to mind. Others in the mapper may also assist in finding the proper depth mapping you are looking for.
http://msdn.microsoft.com/en-us/library/jj883690.aspx

Resources