Convert OpenCV keypoints to CGPoint - ios

Currently, I am object tracking with OpenCV. When OpenCV returns the keypoints of where the objects are, they are out of the range of the iPhone screen. I'm thinking that there is some sort of conversion that needs to be done in order to use these points with swift.
Does anyone know the conversion that needs to be done?
Any help would be appreciated.

The cvPoint values are image coordinates (if the tracking algorithm is valid). You can mark these points on the image using cvCircle and then display the image on the iPhone screen to check if they are valid.
When an image is displayed on the screen using UIImageView, the size of the displayed image might not be the same as the image resolution. In that case, you will need to scale the coordinates, if you want to position something on the image. See here for an example.

Related

Warp a 2D image onto another image

I have obtained a 3D wavefront model of face inside a portrait and then that face is transformed using mesh_numpy and a 2D transformed image is saved. As the images are attached. Now I want to warp this transformed image back onto the original image. It's like face swap where I have both source and target image. What is the better way to achieve it.
Original:
Transformed:
Update: If anyone faces this issue in near future, I managed to achieve this by using Homography.

Measuring distance between objects from a photo, Perspective transform

I have two questions which could be related:
1.) I would like to estimate distances between objects which are positioned in one plane from a photo. Geometrical shape of one object in the photo is rectangular and its dimensions are known, but there is no information on the photo (Camera focal length, photo angle, senor size etc…). For example, say I have the following PCB photo and dimensions of the rectangular chip are known to be 20x10mm, all objects lie in a plane. Is it even possible to estimate the distances (in top view) between other PCB components ?
In this particular case, maximum distance error of 2-3mm would be acceptable.
2.) Say I have similar PCB photo like the above, where I have one feature (object) for which I know it is rectangular shaped. I would like to transform the image perspective so that the object looks rectangular. I have tried imageJ (Fiji) and Interactive Perspective Plugin for this task. First I display rectangular grid over the image and then manually transform the image using the plugin till the object does not appear rectangular. But for some photo angles I find it impossible to manually adjust the control points in order to get rectangular object shape.
Does somebody know alternative approach using imageJ (Fiji) or Octave ? A solution in python would also be ok, although I don’t have much python experience (just recently installed Anaconda with Spyder).
A few years ago, I created a software that seems good for you. It corrects perspective transforming a quadrilateral to a rectangle.
Here is the result:
,
where you can measure distances.

OpenCV Image Matching

I have two images from a stereo camera of the same scene, but few different perspectives (imgLeft and imgRight).
Now, I want to find a ROI (red rectangle in the image below) of the right image in the left one. I need to do this very fast, because I'm doing this in a video. How can I do this? I do not have the nonfree of OpenCV; but I have CUDA installed.
imgRight:
imgLeft:
This should be your friend http://docs.opencv.org/2.4/modules/video/doc/motion_analysis_and_object_tracking.html#calcopticalflowpyrlk
All you need to do is to find feature points inside this rectangle and pass them to the cv::calcopticalflowpyrlk to get there peers in the second image. You may need to make some filtering for the points to make sure that the tracking was perfect like for example pass them to cv::findHomography using CV_RANSAC flag and check the mask output.
The operation is fast and real-time. There is also a CUDA version of this method.

Using OpenCV to correct stereo images

I intend to make a program which will take stereo pair images, taken by a single camera, and then correct and crop them so that when the images are viewed side by side with the parallel or cross eye method, the best 3D effect will be achieved. The left image will be the reference image, the right image will be modified for corrections. I believe OpenCV will be the best software for these purposes. So far I believe the processing will occur something like this:
Correct for rotation between images.
Correct for y axis shift.
Doing so will I imagine result in irregular black borders above and below the right image so:
Crop both images to the same height to remove borders.
Compute stereo-correspondence/disparity
Compute optimal disparity
Correct images for optimal disparity
Okay, so that's my take on what needs doing and the order it occurs in, what I'm asking is, does that seem right, is there anything I've missed, anything in the wrong order etc. Also, which specific functions of OpenCV would I need to use for all the necessary steps to complete this project? Or is OpenCV not the way to go? Much thanks.
OpenCV is great for this.
There is a whole chapter in:
And all the sample code for this in the book ships with the opencv distribution
edit: Roughly the steps are:
Remap each image to remove lens distortions and rotate/translate views to image center.
Crop pixels that don't appear in both views (optional)
Find matching objects in each view (stereoblock matching) create disparity map
Reproject disparity map into 3D model

How would you find the height of objects given an image?

This isn't exactly a programming question exactly. I just want to know what your approach would be to a common problem in Digital image processing.
Let's say you have an image of a few trees in say jpg format. How would you go about finding the heights of each of these trees? The photo is the only input you have.
I want to know the approaches you have not to code. So it doesn't matter if your answers are vague, or non DIP-ish.
Small correction :
The height need not be the actual height of the tree. The height can be taken to any scale. But should be consistent to all objects in the pic.
Yes it is possible. What you are describing has an entire industry around it, called Photogrammetry
There is a fair amount of computer vision research in this area. Assuming you don't know the camera constraints, you'll have to make assumptions about the scene and camera to determine the heights up to a scale factor. Note that without camera constraints or a reference height in the image it is impossible to tell the difference between a tall tree photographed from a distance or a short tree photographed up close. A great start is the Single View Metrology work by Criminisi.
It is simple to find the size of an object from images using Photogrammetry.
Photogrammetry is the science of making measurements from photographs.
For this we need to know two things,
the distance between the camera and the image plane(distance from camera to object).
Focal-length(in mm and pixels per mm) or physical size of the image sensor.
Following are the steps:
Calibrate the Camera
Use openCV to calibrate the camera.You can use the OpenCV calibrate.py tool and the Chessboard pattern PNG provided in the source code to generate a calibration matrix. Camera calibration is done to find the camera parameters. I took about a dozen of photos of the chessboard photos from many angles as I could with my webcam (to calibrate my webcam). For more details check openCV camera calibration.
We will get f_x,f_y,c_x,c_y from calibration matrix.
Checking the details of the photos you took, you will find the native resolution of the photos(heightXwidth) and in their EXIF headers you can find the focal length value(f). These items may vary depending on your camera.
Pixels per millimeter
We need to know the pixels per millimeter(px/mm) on the image sensor.
f_x=f*m_x
f_y=f*m_y
Since we have two of the variables for each formula we can solve for m_x and m_y.I just averaged f_x and f_y to get f_xy.
m=f_xy/focal_length_of_camera
Insert the image
Insert your image from which you need to find the actual size of image. You should know the distance between object and camera. Find the dimension of the image (height1Xwidth1)
Find the Object size in pixels
Determine the size of object in pixels. I simply use distance formula to find length of a selected line. You can adopt any other method.
Convert px/mm in the lower resolution
pxpermm_in_lower_resolution = (width1*m)/width
Size of object in the image sensor
size_of_object_in_image_sensor = object_size_in_pixels/(pxpermm_in_lower_resolution)
Actual size of object
The actual size of object can be found with the above data as,
real_size = (dist*size_of_object_in_image_sensor)/focal_length
Assuming they're all the same distance away, all to scale, you'd want to find a single unit of measurement you can guarantee. For example, if there's a person in the photo, again, same scale, and you know they're exactly 6 feet tall, you use that as your measure. You then take that, and count how many stacked make the tree. For example, if you need 3.5 of this person, then:
3.5 * 6 = 21
gives you a 21 foot tall tree.
Without a single point of reference for everything, or if they're all on different scales, you would need a lot more information than you could easily get without having been there.
I would rely on an object of known dimensions to be present in the picture. For instance, a man.
Or perhaps, we could use the EXIF data to reverse engineer the size of the object based on the camera's sensor dimensions, the lens and the focal length used. This again depends on the angle. We should be getting most accurate results when the camera has been held perpendicular to the subject.
If your image is 3*3 and you want to find out the size of image (i.e 3x3..so 3x3 = 9) now we have 8 pixels starting from 0 up to 8. So 9/8=(___)kb.
If you want to find the size of image in MB, like doing above example, just do like that (9/8)/(1024)=(----)MB..
So you will get the result in Mb.

Resources