Failure of the simplest possible test cv2.remap (), otherwise. how can i use remap () in python?

Here is the simplest possible test case for remap ():

import cv2
import numpy as np
inimg = np.arange(2*2).reshape(2,2).astype(np.float32)
inmap = np.array([[0,0],[0,1],[1,0],[1,1]]).astype(np.float32)
outmap = np.array([[10,10],[10,20],[20,10],[20,20]]).astype(np.float32)
outimg = cv2.remap(inimg,inmap,outmap,cv2.INTER_LINEAR)
print "inimg:",inimg
print "inmap:",inmap
print "outmap:",outmap
print "outimg:", outimg

and here is the conclusion:

inimg: [[ 0.  1.]
 [ 2.  3.]]
inmap: [[ 0.  0.]
 [ 0.  1.]
 [ 1.  0.]
 [ 1.  1.]]
outmap: [[ 10.  10.]
 [ 10.  20.]
 [ 20.  10.]
 [ 20.  20.]]
outimg: [[ 0.  0.]
 [ 0.  0.]
 [ 0.  0.]
 [ 0.  0.]]

As you can see, outimg produces 0.0, and this is not even in the correct form. I expect a 20x20 or 10x10 image with interpolated values ​​from 0 to 3.

I have read all the documentation. He and each SO report that you enter an array (map) of source points, a map of end points, and then remap () will put all the values ​​in img in their new positions, interpolating any empty space. I do this, but it just doesn't work. What for? Most examples for C ++. Is it destroyed in python?

+4
source share
1 answer

, ... , . , , , , ; , , .

remap() , , . remap() , , , , . , , , . ( , ).

remap() docs:

map1. (x,y) , x , CV_16SC2, CV_32FC1 CV_32FC2. . convertMaps() .

map2. y, CV_16UC1, CV_32FC1 none ( , map1 (x,y)) .

map1 " ..." . , , ... src map_x(x, y), map_y(x, y), dst x, y. , . , :

dst(x,y) =  src(map_x(x,y),map_y(x,y))

map_x(x, y) map_x , x, y. . x, y src, x, y dst. , - . (0, 0) map_x map_y, , (0, 0) , . , remap() ; , , .

,

img = np.uint8(np.random.rand(8, 8)*255)
#array([[230,  45, 153, 233, 172, 153,  46,  29],
#       [172, 209, 186,  30, 197,  30, 251, 200],
#       [175, 253, 207,  71, 252,  60, 155, 124],
#       [114, 154, 121, 153, 159, 224, 146,  61],
#       [  6, 251, 253, 123, 200, 230,  36,  85],
#       [ 10, 215,  38,   5, 119,  87,   8, 249],
#       [  2,   2, 242, 119, 114,  98, 182, 219],
#       [168,  91, 224,  73, 159,  55, 254, 214]], dtype=uint8)

map_y = np.array([[0, 1], [2, 3]], dtype=np.float32)
map_x = np.array([[5, 6], [7, 10]], dtype=np.float32)
mapped_img = cv2.remap(img, map_x, map_y, cv2.INTER_LINEAR)
#array([[153, 251],
#       [124,   0]], dtype=uint8)

, ? , img, , . :

map_y
=====
0  1
2  3

map_x
=====
5  6
7  10

, (0, 0) , map_y(0, 0), map_x(0, 0) = 0, 5, 0 5 153. , mapped_img[0, 0] = 153. , . (map_x[1, 1] = 10, ), , 0, .

, remap(), . , true_dst src. , , , , , . remap() .

import numpy as np
import cv2

# read images
true_dst = cv2.imread("img1.png")
src = cv2.imread("img2.png")

# ground truth homography from true_dst to src
H = np.array([
    [8.7976964e-01,   3.1245438e-01,  -3.9430589e+01],
    [-1.8389418e-01,   9.3847198e-01,   1.5315784e+02],
    [1.9641425e-04,  -1.6015275e-05,   1.0000000e+00]])

# create indices of the destination image and linearize them
h, w = true_dst.shape[:2]
indy, indx = np.indices((h, w), dtype=np.float32)
lin_homg_ind = np.array([indx.ravel(), indy.ravel(), np.ones_like(indx).ravel()])

# warp the coordinates of src to those of true_dst
map_ind = H.dot(lin_homg_ind)
map_x, map_y = map_ind[:-1]/map_ind[-1]  # ensure homogeneity
map_x = map_x.reshape(h, w).astype(np.float32)
map_y = map_y.reshape(h, w).astype(np.float32)

# remap!
dst = cv2.remap(src, map_x, map_y, cv2.INTER_LINEAR)
blended = cv2.addWeighted(true_dst, 0.5, dst, 0.5, 0)
cv2.imshow('blended.png', blended)
cv2.waitKey()

Remap for warping

.

+5

Source: https://habr.com/ru/post/1686739/


All Articles