Using OpenCV remap function crops image

血红的双手。 提交于 2019-12-04 16:59:00

I don't think the image is being cropped. Rather, the values are "crowded" in the top-middle pixels, so that they get overwritten. Consider the following example with a simple function on a checkerboard.

import numpy as np
import cv2
import pickle

y_size=200
x_size=200

x=np.linspace(0,x_size,x_size+1)
y=(-(x-x_size/2)*(x-x_size/2))/x_size+x_size
plt.plot(x,y)

The function looks like this:

Then let's produce an image with a regular pattern.

test=np.zeros((x_size,y_size),dtype=np.float32)

for i in range(0, y_size):
    for j in range(0,x_size):
        if i%2 and j%2:
            test[i][j]=255
cv2.imwrite('checker.png',test)

Now let's apply your shift function to that pattern:

map_y=np.zeros((x_size,y_size),dtype=np.float32)
map_x=np.zeros((x_size,y_size),dtype=np.float32)

for i in range(0, y_size):
    for j in range(0,x_size):
        map_y[i][j]= y_size-((y_size - i) *  ((y_size - y[j]) / y_size))
        map_x[i][j]=j

warped=cv2.remap(test,map_x,map_y,cv2.INTER_LINEAR)

cv2.imwrite('warped.png',warped)

If you notice, because of the shift, more than one value corresponds to the top-middle areas, which makes it look like it is cropped. But if you check to the top left and right corners of the image, notice that the values are sparser, thus the "cropping" effect does not occur much. I hope the simple example helps better to understand what is going on.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!