{"action":"created","issue":{"url":"https://api.github.com/repos/Itseez/opencv/issues/6030","labels_url":"https://api.github.com/repos/Itseez/opencv/issues/6030/labels{/name}","comments_url":"https://api.github.com/repos/Itseez/opencv/issues/6030/comments","events_url":"https://api.github.com/repos/Itseez/opencv/issues/6030/events","html_url":"https://github.com/Itseez/opencv/issues/6030","id":129702946,"number":6030,"title":"OpenCV projectPoints jacobian matrix is not correct","user":{"login":"navid-mahmoudian","id":10260746,"avatar_url":"https://avatars.githubusercontent.com/u/10260746?v=3","gravatar_id":"","url":"https://api.github.com/users/navid-mahmoudian","html_url":"https://github.com/navid-mahmoudian","followers_url":"https://api.github.com/users/navid-mahmoudian/followers","following_url":"https://api.github.com/users/navid-mahmoudian/following{/other_user}","gists_url":"https://api.github.com/users/navid-mahmoudian/gists{/gist_id}","starred_url":"https://api.github.com/users/navid-mahmoudian/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/navid-mahmoudian/subscriptions","organizations_url":"https://api.github.com/users/navid-mahmoudian/orgs","repos_url":"https://api.github.com/users/navid-mahmoudian/repos","events_url":"https://api.github.com/users/navid-mahmoudian/events{/privacy}","received_events_url":"https://api.github.com/users/navid-mahmoudian/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-01-29T08:39:34Z","updated_at":"2016-01-31T07:45:08Z","closed_at":null,"body":"We compared the Jacobian matrix coming from projectPoints in OpenCV 2.4.11 against the one we have provided with matlab code (we have written the projection formula exactly based on the OpenCV source code). We found out that when the Tangential distortion coefficients (p1 and p2) are not zero the output of MATLAB code and the OpenCV projectPoints are not identical for the Jacobian of rotation and translation. It seems that the OpenCV source code is not correct for computing the Jacobian for rotation and translation.\r\nwe are using Ubuntu 14.04 64 bit with gcc version 4.8.4.\r\nfor your convenience you can find our MATLAB code in the following:\r\n\r\n% convert rotation vector to rotation matrix (world_in_camera)\r\nsyms rx ry rz real;\r\nr=[rx; ry; rz];\r\ntheta=norm(r);\r\nr=r/theta;\r\nR=cos(theta)*eye(3)+(1-cos(theta))*(r*r')+sin(theta)*[0,-r(3), r(2);r(3),0,-r(1);-r(2),r(1),0];\r\n\r\n% camera Position world_in_camera\r\nsyms tx ty tz real;\r\nT = [tx; ty; tz];\r\n\r\nsyms fx fy cx cy real ; \r\n\r\n\r\n% 3D points in camera coordinate system\r\nsyms X Y Z real;   % position of 3D Points in world coordinate system\r\nP = R*[X; Y; Z] + [tx; ty; tz];\r\n\r\n% distortion\r\nsyms k1 k2 p1 p2 k3\r\nx_perim = P(1)/P(3);\r\ny_perim = P(2)/P(3);\r\nr2 = (x_perim * x_perim) + (y_perim * y_perim);\r\nr4 = r2 * r2;\r\nr6 = r4 * r2;\r\n\r\ncdist = 1 + k1*r2 + k2*r4 + k3*r6;\r\na1 = 2*x_perim*y_perim;\r\na2 = r2 + 2*x_perim*x_perim;\r\na3 = r2 + 2*y_perim*y_perim;\r\n\r\nx_zegond = x_perim * cdist + p1 * a1 + p2 * a2;\r\ny_zegond = y_perim * cdist + p1 * a3 + p2 * a1;\r\n\r\n % 3D point projection equation\r\nxp(X, Y, Z) = x_zegond * fx + cx;\r\nyp(X, Y, Z) = y_zegond * fy + cy;\r\n\r\nsyms X3 Y3 Z3 real;\r\nx = xp(X3, Y3, Z3);\r\ny = yp(X3, Y3, Z3);\r\n\r\nimagePoints=[x;y];\r\n\r\n% disp('imagePoints = ');\r\n% pretty(imagePoints);\r\n\r\ndp_dfx    = diff(imagePoints, fx);\r\ndp_dfy    = diff(imagePoints, fy);\r\ndp_dcx    = diff(imagePoints, cx);\r\ndp_dcy    = diff(imagePoints, cy);\r\n\r\ndp_dk1    = diff(imagePoints, k1);\r\ndp_dk2    = diff(imagePoints, k2);\r\ndp_dp1    = diff(imagePoints, p1);\r\ndp_dp2    = diff(imagePoints, p2);\r\ndp_dk3    = diff(imagePoints, k3);\r\n\r\ndp_drx    = diff(imagePoints, rx);\r\ndp_dry    = diff(imagePoints, ry);\r\ndp_drz    = diff(imagePoints, rz);\r\n\r\ndp_dtx    = diff(imagePoints, tx);\r\ndp_dty    = diff(imagePoints, ty);\r\ndp_dtz    = diff(imagePoints, tz);\r\n\r\njacobianMat = [dp_drx, dp_dry, dp_drz ,...\r\n            dp_dtx, dp_dty, dp_dtz ,...\r\n            dp_dfx, dp_dfy, dp_dcx, dp_dcy, ...\r\n            dp_dk1, dp_dk2, dp_dp1, dp_dp2, dp_dk3];\r\nfprintf('generating c code for jacobian \\n');\r\nccode(jacobianMat,'file','jacobian.cpp');"},"comment":{"url":"https://api.github.com/repos/Itseez/opencv/issues/comments/177425817","html_url":"https://github.com/Itseez/opencv/issues/6030#issuecomment-177425817","issue_url":"https://api.github.com/repos/Itseez/opencv/issues/6030","id":177425817,"user":{"login":"amiryanj","id":3169342,"avatar_url":"https://avatars.githubusercontent.com/u/3169342?v=3","gravatar_id":"","url":"https://api.github.com/users/amiryanj","html_url":"https://github.com/amiryanj","followers_url":"https://api.github.com/users/amiryanj/followers","following_url":"https://api.github.com/users/amiryanj/following{/other_user}","gists_url":"https://api.github.com/users/amiryanj/gists{/gist_id}","starred_url":"https://api.github.com/users/amiryanj/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/amiryanj/subscriptions","organizations_url":"https://api.github.com/users/amiryanj/orgs","repos_url":"https://api.github.com/users/amiryanj/repos","events_url":"https://api.github.com/users/amiryanj/events{/privacy}","received_events_url":"https://api.github.com/users/amiryanj/received_events","type":"User","site_admin":false},"created_at":"2016-01-31T07:45:08Z","updated_at":"2016-01-31T07:45:08Z","body":"Hi Navid,\r\nAfter running your code in MATLAB and generating c code using ccode, I noticed that, as you mentioned the jacobian result differs from opencv 'projectPoints' output when Tangential distortion coefficients (p1 and p2) are not zero.\r\nIt might be a bug in opencv 'projectPoints' function."}}