简体   繁体   English

OpenCV Python 重映射数据类型错误

[英]OpenCV Python remap data type error

I am attempting to remap an image with openCv, using cv2 and numpy but I am having some issues.我正在尝试使用 cv2 和 numpy 使用 openCv 重新映射图像,但我遇到了一些问题。 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 OpenCV 错误:断言失败 (((map1.type() == CV_32FC2 || map1.type() == CV_16SC2) && map2.empty()) || (map1.type() == CV_32FC1 && map2.type( ) == CV_32FC1)) 在重映射中,文件 /tmp/binarydeb/ros-kinetic-opencv3-3.2.0/modules/imgproc/src/imgwarp.cpp,第 5043 行

But both maps are float32 type - so I don't quite understand the error.但是这两个地图都是 float32 类型 - 所以我不太明白这个错误。 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提前致谢

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有用的链接: https : //docs.opencv.org/3.1.0/da/d54/group__imgproc__transform.html#gab75ef31ce5cdfb5c44b6da5f3b908ea4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM