c++ - cv::warpPerspective only shows part of warped image -
im changing image front perspective bids eye view using gethomography , warpperspective.
it works in image warps desired perspective crop off. moves warped image largely outside image box. assume reason because operation results in negative coordinates.
i have calculated points calculation of translation matrix manually , not using of opencv:s functions doing since i.e. chessboard functions failed detect proper points.
i guess can fixed doing additional changes transformation matrix. how done? also, there way make sure transformed image centered along x-axis , let y-axis adjusted desired position?
code snippet job now:
cv::mat image; // image loaded original image cv::mat warppers; // container resulting image cv::mat h; std::vector<cv::point2f> src; std::vector<cv::point2f> dst; // in reality several more points. src.push_back(cv::point2f(264,301)); src.push_back(cv::point2f(434,301)); src.push_back(cv::point2f(243,356)); src.push_back(cv::point2f(476,356)); dst.push_back(cv::point2f(243,123)); dst.push_back(cv::point2f(476,123)); dst.push_back(cv::point2f(243,356)); dst.push_back(cv::point2f(476,356)); h = cv::findhomography(src, dst, cv_ransac); cv::warpperspective(image, newpers, h, cv::size(3000,3000), cv::inter_nearest | cv_warp_fill_outliers ); cv::namedwindow("warped persp", cv::window_autosize ); cv::imshow( "warped persp", newpers);
opencv gives convenient way perpective transform. thing have take care of homography return findhomography. indeed, maybe points of image provide go in negative part of x or y axis. have check before warp image.
step 1: find homography h findhomography classic structure homography
h = [ h00, h01, h02; h10, h11, h12; h20, h21, 1];
step 2: search position of image's corners after warping
so let me define order corner:
(0,0) ________ (0, w) | | |________| (h,0) (h,w)
to that, create matrix that:
p = [0, w, w, 0; 0, 0, h, h; 1, 1, 1, 1]
make product h , warped coordinates:
p' = h * p
step 3: check minimum in x , y these new 4 points , size of warped image after, have done product receive that:
p' = [s1*x1, s2*x2, s3*x3, s4*x4; s1*y1, s2*y2, s3*y3, s4*y4; s1 , s2 , s3 , s4]
so obtain, new valid coordinate divide line 1 , 2 line 3
after check minimum column on first line, , minimum row on second line (use cvreduce)
to find bounding box contains image (ie dimension of dst matrix warpperspective function) find cvreduce maximum on each line
let minx minimum on first row (ie column), maxx (the maximum 1 row) miny , maxy second row.
so size of warped image should cvsize(maxx-minx, maxy-miny)
step 4: add correction homography check if minx and/or miny is/are negative, if minx < 0 add -minx h02 , if miny < 0, add -miny h12
so h should be:
h = [ h00, h01, h02-minx; //if minx <0 h10, h11, h12-miny; //if miny <0 h20, h21, 1];
step 5: warp image
Comments
Post a Comment