1

I am encountering unexplained behaviour when using the cv2.remap function in Opencv. I have made a function that translates the y coordinate of each pixel upwards by a number that differs on each x-coordinate. The number is calculated according to an underlying curve that starts from x=0 and ends at x=640 (for a 360 x 640 picture)

About a week ago I posted a question on the same subject but with a different problem - the top part of the image was cropped. This was answered and I understood that the problem was that several pixels from the original image were mapped to the same positions in the new image and therefore the pixels that were first mapped to the top parts were overwritten by others from the lower parts.

However the issue I am facing now is a bit different which is why I posted a new question. In this instance the problem is that since I am lifting all y coordinates upwards I would expect to have the bottom section of the image blank. This is achieved with the below piece of code

original = cv2.imread('C:\\Users\\User\\Desktop\\alaskan-landscaps3.jpg')

        y_size,x_size=original.shape[:2]
        map_x = np.zeros(original.shape[:2], np.float32)
        map_y = np.zeros(original.shape[:2], np.float32)
for i in range(0, y_size):
    for j in range(0,x_size):
        map_y[i][j]=i+yvalues[j]
        map_x[i][j] = j

where yvalues[j] is the y-coordinate of the curve at x-coordinate j. Sure enough, when printing the y-mappings for the first 10 rows of the map_y array at x=383 the lowest number is 199.102

for j in range(0, 360):
    print('At x=383,y=' + str(j) + ' mapping of y-coordinate is:' + str(map_y[j][383]))

At x=383,y=0 mapping of y-coordinate is:199.102
At x=383,y=1 mapping of y-coordinate is:200.102
At x=383,y=2 mapping of y-coordinate is:201.102
At x=383,y=3 mapping of y-coordinate is:202.102
At x=383,y=4 mapping of y-coordinate is:203.102
At x=383,y=5 mapping of y-coordinate is:204.102
At x=383,y=6 mapping of y-coordinate is:205.102

When I use a more complicated warping function I get the below results for the same first 10 lines - the idea being that a number of consecutive pixels in the vertical direction should be mapped to the same pixel in the new image

At x=383,y=0mapping is:199.102
At x=383,y=1mapping is:199.102
At x=383,y=2mapping is:199.102
At x=383,y=3mapping is:199.102
At x=383,y=4mapping is:199.102
At x=383,y=5mapping is:200.102
At x=383,y=6mapping is:200.102
At x=383,y=7mapping is:200.102
At x=383,y=8mapping is:200.102
At x=383,y=9mapping is:201.102
At x=383,y=10mapping is:201.102

However, this time, the image is completely filled in and there is no blank space at the bottom part. You can see both images below:

enter image description here

(the pixelated effect is not what I intended but that's for me to fix :) ). I have checked all the entries of the specific column in the map_y array for x=383 and all values are > 199.102 so it appears that is not the same problem as in the previous case i.e there are no pixels higher up in the column that get mapped to the lower part.

So my question in, what is different between the two mappings to have caused this drastic change? I expected that since the mappings of the second image also start from a high value there would have been a blank space at the bottom as well

Apologies for the long post but I have been trying to work out the reason for a few days and came out none the wiser. Any help will be appreciated.

1 Answer 1

1

I think you misunderstand how remap() works. Let me quote from the OpenCV documentation for the function initUndistortRectifyMap():

"The function actually builds the maps for the inverse mapping algorithm that is used by remap(). That is, for each pixel (u, v) in the destination image, the function computes the corresponding coordinates in the source image."

So your line

At x=383,y=0mapping is:199.102

actually means that the value of pixel (383, 0) in the mapped image is taken from pixel (383, 199.102) in the original image.

A pixel is not mapped, if: a) the value of map_x[i][j] or map_y[i][j] is -1 (-16 in case of short map type) or b) the value is outside the boundaries of the source image.

1
  • You are correct, I did misunderstand how the mapping function worked. I have now managed to get the image transformation working as expected. Thank you!
    – Socrats
    Feb 10, 2017 at 17:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.