I am trying to implement an image warp using the ThinPlateSplineShapeTransformer in OpenCV using Python. I am using a C++ example posted in the OpenCV forum (link) but I am encountering various problems due to the differences in the OpenCV Python API.
As in the linked example, I am working with a single image onto which I will define a small number of source points and the corresponding target points. The end result should be a warped copy of the image. The code so far is as follows:
tps=cv2.createThinPlateSplineShapeTransformer()
sourceshape= np.array([[200,10],[400,10]],np.int32)
targetshape= np.array([[250,10],[450,30]],np.int32)
matches=list()
matches.append(cv2.DMatch(1,1,0))
matches.append(cv2.DMatch(2,2,0))
tps.estimateTransformation(sourceshape,targetshape,matches)
But I am getting an error in the estimateTransformation method:
cv2.error: D:\Build\OpenCV\opencv-3.1.0\modules\shape\src\tps_trans.cpp:193: error: (-215)
(pts1.channels()==2) && (pts1.cols>0) && (pts2.channels()==2) && (pts2.cols>0) in function cv::ThinPlateSplineShapeTransformerImpl::estimateTransformation
I can understand that something is incorrect in the data structures that I have passed onto estimateTransformation and I'm guessing it has to do with the channels since the rows and columns seem to be correct but I do not know how I can satisfy the assertion (pts1.channels()==2) since the parameter is an array of points which I am creating and not an array generated from an image load
I'd be grateful for any pointers to a TPS implementation for image transformation with Python or indeed any help on how to resolve this particular issue. I've tried to find the Python documentation for the ThinPlateShapeTransformer class but it has proved impossible - all I've found is the C++ docs and the only thing i have to go on are the results of the help() function - apologies if I am missing something obvious
I had the same problem. Simple reshaping solved that issue. It is late but someone might find it useful. Here are the lines for reshaping sourceshape and targetshape:
sourceshape=sourceshape.reshape(1,-1,2)
targetshape=targetshape.reshape(1,-1,2)
Can you try to check the number of matching points. In your code, there are only two matching points, it is difficult to interpolate. May be you can increase up to four matching points, than it will work.
sourceshape= np.array([[200,10],[400,10]],np.int32)//increase more point here
targetshape= np.array([[250,10],[450,30]],np.int32)//increase more point here
matches=list()
matches.append(cv2.DMatch(1,1,0))
matches.append(cv2.DMatch(2,2,0))
//add more matches here.
Related
I am trying to detect the differences in two images in python (object present or not). I tried different approaches with opencv and pillow for python. The goal is to check if an object is present or not. And if possible i want to extract the coordinates of the changes (with a bounding box)
The problem is, that the images are not 100% identical. There is always a very slight change in angle or lighting. Thresholding didnt do the trick as expected....
Is there any other approaches that you would suggest?
Thanks in advance
You can use the Structural similarity index for a robust image comparison:
https://scikit-image.org/docs/dev/auto_examples/transform/plot_ssim.html
This is implemented on scikit-image package.
I have a short video of a moving object. for some reason I need to estimate the object's movement distance between two specific frames. It is not necessary to be exact.
Does anyone know how I can do this in python and opencv or any image processing library?
Thanks
I don't know in opencv, there is for sure something similar but you can easily do it with scikit-image module. I do it on a regular basis to align frames.
See this example here:
https://scikit-image.org/docs/dev/auto_examples/transform/plot_register_translation.html#sphx-glr-auto-examples-transform-plot-register-translation-py
EDIT: I found something very similar in opencv here
https://docs.opencv.org/4.2.0/db/d61/group__reg.html
I'm trying to make a program that rotates and crops an image ROI (without losing a single pixel of the frame) based just on what minAreaRect() returns (from seeing what it can mark with drawContours).
Now since I don't understand what the function returns other than the rotation angle(list [0:1]) I'm struggling to make that myself. All I found on the internet was a Stack Overflow question with code that wasn't really explained very well and didn't realy work (atleast for openCV 3.6 not)
May I have any clues to what is the return syntax of this function and what is the method and keywords to search for such things, as well as a short little function that can maybe do that rotation and cropping? Since that looks like a quite common and simple thing to achieve.
I was implementing the depth map construction, code of which (in Python) is available here OpenCv Docs - depthMap I was successful in getting the depth map as they showed in the doc for their given images-pair (left and right stereo images) tsukuba_l.png and tsukuba_2.png. I considered testing my own image pairs, so I took from my mobile a pair of images, as shown below:
When I run the code, I'm getting the depth map something like this
I tried playing with numDisparities and blocksize, but it didn't help in getting the best map.
I thought of checking the script of cv2.StereoBM_create in its master folder in Github, but couldn't get that online. Can you help me with a way to implement depth maps for custom images taken by me? is there a way that we can play with the parameters or at least get me the link to GitHub master module that has all Stereo related modules. Thank you.
I guess you did not rectify the images which is fundamental for stereo matching.
You should first calibrate your stereo system (if you took them with mobile phone every image pair you take will have a different transform, the two cameras need to have always the same transformation between each other) and then rectify the images, in that way they are projected onto the same plane, then the stereo match algorithm looks for correspondences in the other image on the same rows.
Check in the docs for stereoRectify(), you will see some images as example of the rectification process.
By the way there is another python example based on SemiGlboal Block Matching algorithm in opencv/samples/python/stereo_match.py.
I wanted to create a photomosaic in python using the PIL library.
Will FFT help me in finding the best match for a picture from the given set of pictures?
I have tried with rgb colour space but the ouput seems to be ok.
I would need robust performance so thought of using fft.
Kindly provide some other valuable suggestions too.
An implementation to spark ideas might be useful. link
Otherwise google "image similarity measures". The top link is going to be this question which again might provide the needed direction your looking for.