I am trying to convert c++ opencv cv2.remap code to python. I am not getting any error but result is not as expected.I am getting zoomed image
c++ code
int main()
{
Mat img = imread("captcha1.jpg");
float phase = -0.8 * CV_PI;
float omega = 2.0 * CV_PI / img.cols;
float amp = 15;
Mat_<Vec2f> proj(img.size());
for (int y=0; y<img.rows; y++) {
for (int x=0; x<img.cols; x++) {
float u = 0;
float v = sin(phase + float(x) * omega) * amp;
proj(y,x) = Vec2f(float(x) + u, float(y) + v);
}
}
Mat corr;
cv::remap(img, corr, proj, cv::Mat(), INTER_LINEAR);
imshow("in",img);
imshow("out",corr);
waitKey();
}
python code :
from __future__ import print_function
import cv2
import numpy as np
from past.builtins import xrange
def update():
for j in xrange(rows):
for i in xrange(cols):
phase = -0.8 * np.pi
omega = 2.0 * np.pi / cols
amp = 15
u = -100
v = np.sin(phase + float(i) * omega) * amp
value_xy = (float(i) + u) + (float(j) + v)
map_x.itemset((j, i), value_xy)
map_y.itemset((j, i), 0)
img = cv2.imread('test.jpg')
map_x = np.zeros(img.size, np.float32)
map_y = np.zeros(img.shape[:2], np.float32)
rows, cols = img.shape[:2]
update()
dst = cv2.remap(img, map_x, map_y, cv2.INTER_LINEAR)
cv2.imshow('dst', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
Original image :
C++ Result :
Python result:
u = 0
in c++ andu = -100
in Python? Why not passrows, cols
toupdate
as parameters, rather then relying on global variables? IMHOmap_y
can just beNone
. – Davidadaviddefor
loops, and replace them with vectorizednumpy
operations. Say, like this – Davidadavidde