Ask Your Question

WarpPerspective Advice with correct BBox Pixels

asked 2018-01-07 15:58:43 -0500

MRDaniel gravatar image

updated 2018-01-07 16:22:38 -0500


I am trying to do some template matching with OpenCV. The templates in the new image could be warped and rotated. However, we know the rotation matrix before-hand.

1) Create ROI from source image.

2) Warp template ROI to new image.

3) Have resultant bounding box containing border with correct pixels.

image description

I have seen the reverse of this process done, when used in "Deskew" methods. This is generally achieved using getPerspectiveTransform and is pretty standard opencv. However, the reverse seems a little more difficult if using a pure OpenCV approach.

image description

Question: How do i perform the perspective transform so that all pixels in the bounding box of the warped ROI are included?



edit retag flag offensive close merge delete


1 answer

Sort by ยป oldest newest most voted

answered 2018-01-07 17:00:03 -0500

try these code please

#include  "opencv2/highgui.hpp"
#include  "opencv2/imgproc.hpp"
#include  <iostream>
#include  <stdio.h>
using  namespace  cv;
using  namespace  std;
/**  @function  main  */
int  main(  int  argc,  char**  argv  )
             cv::Mat src= cv::imread( "test.jpg",0);
                 if (!
                                 return 0;
                vector<Point> not_a_rect_shape;
                 // For debugging purposes, draw green lines connecting those points
                 // and save it on disk
                const Point* point = &not_a_rect_shape[0];
                int n = (int )not_a_rect_shape.size();
                Mat draw = src.clone();
                polylines(draw, &point, &n, 1, true, Scalar(0, 255, 0), 3, CV_AA);
                imwrite( "draw.jpg", draw);
                 //  topLeft, topRight, bottomRight, bottomLeft
                cv::Point2f src_vertices[4];
                src_vertices[0] = not_a_rect_shape[0];
                src_vertices[1] = not_a_rect_shape[1];
                src_vertices[2] = not_a_rect_shape[2];
                src_vertices[3] = not_a_rect_shape[3];

                Point2f dst_vertices[4];
                dst_vertices[0] = Point(0, 0);
                dst_vertices[1] = Point(960,0);
                dst_vertices[2] = Point(0,540);
                dst_vertices[3] = Point(960,540);
                Mat warpMatrix = getPerspectiveTransform(src_vertices, dst_vertices);
                cv::Mat rotated;
                warpPerspective(src, rotated, warpMatrix, rotated.size(), INTER_LINEAR, BORDER_CONSTANT);
                 // Display the image
                cv::namedWindow( "Original Image");
                cv::imshow( "Original Image",src);
                cv::namedWindow( "warp perspective");
                cv::imshow( "warp perspective",rotated);
                imwrite( "result.jpg",src);
                 return 0;
edit flag offensive delete link more


Thank you for your input, but this doesn't quite address the issue i am facing. Also src_vertices is the wrong way around for the last two vertices.

MRDaniel gravatar imageMRDaniel ( 2018-01-07 17:41:52 -0500 )edit

May this help you. getPerspectiveTransform

supra56 gravatar imagesupra56 ( 2018-01-08 10:23:27 -0500 )edit

I have done many time using getPerspectiveTransform with boundingbox using python 3.5.3

supra56 gravatar imagesupra56 ( 2018-01-08 10:26:20 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-01-07 15:58:43 -0500

Seen: 686 times

Last updated: Jan 07 '18