1
votes

I am attempting to remap an image with openCv, using cv2 and numpy but I am having some issues. The error I receive is:

OpenCV Error: Assertion failed (((map1.type() == CV_32FC2 || map1.type() == CV_16SC2) && map2.empty()) || (map1.type() == CV_32FC1 && map2.type() == CV_32FC1)) in remap, file /tmp/binarydeb/ros-kinetic-opencv3-3.2.0/modules/imgproc/src/imgwarp.cpp, line 5043

But both maps are float32 type - so I don't quite understand the error. I am running the following code in a function:

rows, cols, channels = img.shape

map_x = np.zeros( (rows,cols,channels), np.float32 )
map_y = np.zeros( (rows,cols,channels), np.float32 )

for i in xrange( rows ):
    for j in xrange( cols ):#

        p = [ i, j ]

        x = np.array( [ p[0], p[1], 1, p[0]*p[1]] )
        #Compute pixel transform
        trans = np.round( x.dot( self.w ) )

        map_x[i,j] = trans[0]
        map_y[i,j] = trans[1] 


outimg = cv2.remap( img, map1, map2, cv2.INTER_CUBIC )

Thanks in advance

1
A good guess would be that channels is not 1, yet the arrays should be single channel (CV_32FC1 -- 32bit float, 1 channel)Dan Mašek
Ah, you are right, thank youtjp212
@tjp212 do you remember what did you correct to make it work?Lorenzo Sciuto

1 Answers

0
votes

Convert to required type

Mat mat1 = new Mat() // or may be the input mat/map     
Imgproc.cvtColor(mat1, mat1, Imgproc.COLOR_BGR2GRAY); //Call this before conversion otherwise it won't work
mat1.convertTo(mat1,CvType.CV_32FC1);

Useful Links : https://docs.opencv.org/3.1.0/da/d54/group__imgproc__transform.html#gab75ef31ce5cdfb5c44b6da5f3b908ea4