0

I am trying to understand image warping and wanted warp an image using OpenCV's remap. I tried the following where I try to remap each pixel in the original image but as I run the code, I get an error.

import cv2
import numpy as np


img = cv2.imread("./images/cube.png", 0)

h, w = img.shape

blank_image = np.zeros((h,w))

blank_image[::2,::2] = 10

warped = cv2.remap(src = img, map1 = img + blank_image, map2 = img + blank_image, interpolation = cv2.INTER_CUBIC, borderMode = cv2.BORDER_REFLECT101)

cv2.imwrite("./images/warped.png", warped)

The error says:

    OpenCV(3.4.1) Error: Assertion failed (((map1.type() == (((5) & ((1 << 3) - 1)) + (((2)-1) << 3))
 || map1.type() == (((3) & ((1 << 3) - 1)) + (((2)-1) << 3))) && map2.empty()) || 
 (map1.type() == (((5) & ((1 << 3) - 1)) + (((1)-1) << 3)) && map2.type() == (((5) & ((1 << 3) - 1)) 
 + (((1)-1) << 3)))) in remap, file /io/opencv/modules/imgproc/src/imgwarp.cpp, line 1840
Traceback (most recent call last):
File "warp.py", line 13, in <module>
    warped = cv2.remap(src = img, map1 = img + blank_image, map2 = img + blank_image, 
    interpolation = cv2.INTER_CUBIC, borderMode = cv2.BORDER_REFLECT101)
cv2.error: OpenCV(3.4.1) /io/opencv/modules/imgproc/src/imgwarp.cpp:1840: error: (-215)
 ((map1.type() == (((5) & ((1 << 3) - 1)) + (((2)-1) << 3)) || map1.type() == (((3)
  & ((1 << 3) - 1)) + (((2)-1) << 3))) && map2.empty()) || (map1.type() == (((5) & ((1 << 3) - 1))
   + (((1)-1) << 3)) && map2.type() == (((5) & ((1 << 3) - 1)) + (((1)-1) << 3))) in function remap

I could not understand the error. What am I doing incorrectly?

Sophia
  • 367
  • 1
  • 8
  • 14
  • what data/information do you expect map1 and map2 to hold? Assertion tells you that it's the wrong type. – Micka Mar 04 '19 at 19:48
  • @Micka I am not sure. I just want to move every pixel along X by some magnitude and along Y by some magnitude. – Sophia Mar 04 '19 at 20:11
  • try to replace "map1 = img + blank_image" by a numpy array where only the x-shift values are inside and same for map2. img is a 3 channel matrix, so it cannot be used as x/y shift values and wouldnt make sense because it is color values. – Micka Mar 04 '19 at 20:24
  • 1
    Possible duplicate of [Failing the simplest possible cv2.remap() test, aka. how do I use remap() in python?](https://stackoverflow.com/questions/46520123/failing-the-simplest-possible-cv2-remap-test-aka-how-do-i-use-remap-in-pyt) – alkasm Mar 04 '19 at 20:46
  • If the suggested duplicate does not answer your question, please edit this question to provide more context with what you've tried from there. I gave a pretty thorough answer there so hopefully it should clear up any confusion! – alkasm Mar 04 '19 at 20:48

0 Answers0