Ask Your Question
0

Getting coordinates for perspective transform using mousecallback() [closed]

asked 2015-02-24 02:49:21 -0600

jamesnzt gravatar image

updated 2015-02-24 08:50:54 -0600

I tried the following code for perspective transform

  #include "opencv2/opencv.hpp"  
#include<iostream>

using namespace cv;
using namespace std;

int ival(int x, int y);

void coordinate(int event, int x, int y, int flags, void* temp)
{
    int check;
     if  ( event == EVENT_LBUTTONDOWN && flags==EVENT_FLAG_CTRLKEY)
     {
         check=ival(x,y);
         if(check=0)
             return;
     }
}


vector<Point2f> P,Q;
int main() 
{
    Mat ocv = imread("E:\\Tennis.jpg");
    namedWindow("Input");
    imshow("input",ocv);
    setMouseCallback("Input", coordinate,NULL );


    Q.push_back(Point2f(10,10)); // 10 pixel border on all sides
    Q.push_back(Point2f(210,10));
    Q.push_back(Point2f(210,210));
    Q.push_back(Point2f(10,210));

    Mat rot = cv::getPerspectiveTransform(P,Q);

    Mat result;
    cv::warpPerspective(ocv, result, rot, Size(220,220));

    imshow("result",result);
    waitKey();
    return 0;
}

I tried to get the coordinates of the source object for transform with mousecallback() but i cant able to achieve it Please tell me the logic to get four co-ordinate points using mousecallback function?

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by jamesnzt
close date 2015-02-25 04:19:20.536091

Comments

1

Where is your problem? How does your current mouse callback look like? Showing that you invested some time to solve the problem always improves the motivation to help...

FooBar gravatar imageFooBar ( 2015-02-24 04:27:04 -0600 )edit

@FooBar I edited the question with my program. There is somewhere logic is missing. I cant figure it out.

jamesnzt gravatar imagejamesnzt ( 2015-02-24 08:56:40 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-02-25 03:41:39 -0600

berak gravatar image
struct MouseParams
{
    Mat img;
    vector<Point2f> P,Q;
};


static void onMouse( int event, int x, int y, int, void* param)
{
    // Mount back the parameters
    MouseParams* mp = (MouseParams*)param;
    Mat & img = mp->img;
    if (event == 1) // left button
    {
        Mat result;

        // remember, that you have to go clockwise,
        // starting at top-left (same order as Q)!
        mp->P.push_back(Point2f(x,y)); 

        if (mp->P.size() == 4)
        {
            Mat rot = cv::getPerspectiveTransform(mp->P,mp->Q);
            cv::warpPerspective(img, result, rot, Size(220,220));
            imshow("result",result);
        } 
        else 
        {
            result = img.clone();
            for (size_t i=0; i<mp->P.size(); i++)
            {
                circle(result,mp->P[i],3,Scalar(0,0,255));
            }
            imshow("input",result);
        }
    }
    if (event == 2) // right button, clear & try again
    {
        mp->P.clear();
        imshow("input",img);
    }
}

int main() 
{
    Mat ocv = imread("tennis.jpg");
    namedWindow("input");
    imshow("input",ocv);

    MouseParams mp;
    mp.Q.push_back(Point2f(10,10)); // 10 pixel border on all sides
    mp.Q.push_back(Point2f(210,10));
    mp.Q.push_back(Point2f(210,210));
    mp.Q.push_back(Point2f(10,210));
    mp.img = ocv;

    setMouseCallback("input", onMouse, (void*)&mp);
    waitKey();
    return 0;
}
edit flag offensive delete link more

Comments

1

Thanks @berak . I think i have to work more with programming. Thanks again

jamesnzt gravatar imagejamesnzt ( 2015-02-25 04:18:59 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-02-24 02:49:21 -0600

Seen: 979 times

Last updated: Feb 25 '15