First time here? Check out the FAQ!

Ask Your Question
-1

Problem with perspective

asked Feb 15 '15

Sparrow gravatar image

Hello evrybody

I'm trying to apply a perspective transform on an image but I doesn't worklike I would like. Since code and pictures are easier to understand than a long speech (and because my english is quite poor) here is the code (in C):

#include <stdio.h>
#include "opencv/highgui.h"
#include "opencv/cv.h"


int main() {

    int flags;
    CvScalar fillval;
    CvPoint2D32f P[3], Q[3];

     IplImage *image, *resultat;
     CvMat* matrice = cvCreateMat(3,3,CV_32FC1);


    cvNamedWindow("Entrée", CV_WINDOW_AUTOSIZE);
    cvNamedWindow("Résultat", CV_WINDOW_AUTOSIZE);

    image = cvLoadImage("C:\\Users\\Julien\\Pictures\\tennis-3d.jpg", CV_LOAD_IMAGE_COLOR);
    resultat=cvCloneImage(image);
    cvShowImage( "Entrée", image);
    cvShowImage( "Résultat", resultat);
    P[0].x=266;
    P[0].y=101;

    P[1].x=534;
    P[1].y=101;

    P[2].x=667;
    P[2].y=394;

    P[3].x=133;
    P[3].y=393;

    Q[0].x=0;
    Q[0].y=0;

    Q[1].x=image->width;
    Q[1].y=0;

    Q[2].x=image->width;
    Q[2].y=image->height;

    Q[3].x=0;
    Q[3].y=image->height;

cvGetPerspectiveTransform(P,Q,matrice);

cvWarpPerspective(image,resultat, matrice,flags=CV_WARP_FILL_OUTLIERS,fillval=cvScalarAll(0) );

cvShowImage( "Résultat", resultat);

    cvWaitKey(1000000);
    return 0;

}

And the result : image description The goal is to have a "top view" of the court

Thank you for your help

Sparrow

Preview: (hide)

Comments

sorry for the downvote, but using opencv's c-api is no more a valid solution in 2015 (which makes this a 'bad question')

berak gravatar imageberak (Feb 15 '15)edit

Sorry I didn't konw. It mays explain why I don't find many topic on opencv with C

Sparrow gravatar imageSparrow (Feb 15 '15)edit
1

your main problem is, that you have 4 points, but only allocate 3 here : P[3],Q[3];

so that's out of bounds / undefined behaviour, please always start a new program in debug mode, so the compiler can complain about things like this.

berak gravatar imageberak (Feb 15 '15)edit

I'm stupid Thank you very much I'll open in debug mode

Sparrow gravatar imageSparrow (Feb 15 '15)edit
1

^^ you're not stupid, you're a noob, and it's a sunday. relax, and have fun with opencv ;)

berak gravatar imageberak (Feb 15 '15)edit

1 answer

Sort by » oldest newest most voted
5

answered Feb 15 '15

berak gravatar image

updated Feb 15 '15

try with the c++ api, it's not very difficult:

#include "opencv2/opencv.hpp"   
using namespace cv;

int main() 
{
    Mat ocv = imread("tennis.jpg");

    vector<Point2f> P,Q;
    P.push_back(Point2f(163,101));
    Q.push_back(Point2f(10,10)); // 10 pixel border on all sides

    P.push_back(Point2f(432,99));
    Q.push_back(Point2f(210,10));

    P.push_back(Point2f(563,395));
    Q.push_back(Point2f(210,210));

    P.push_back(Point2f(31,393));
    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;
}

image description image description

Preview: (hide)

Comments

2

That the result I wanted

I'm goign to learn differences between C and C++ in order to understand the code

Thank you very much

Sparrow

Sparrow gravatar imageSparrow (Feb 15 '15)edit

Sorry but I tried and it doesn't work on my computer

What are the includes ? Should I use "using namespace cv" ?

Sparrow gravatar imageSparrow (Feb 15 '15)edit

ah, sorry, please see edit ^^

berak gravatar imageberak (Feb 15 '15)edit
1

thank you it works

Sparrow gravatar imageSparrow (Feb 15 '15)edit

is it possible to get the values using mousecallback()? I tried but i cant achieve it. can any one exlain the logic to do that? @berak

jamesnzt gravatar imagejamesnzt (Feb 24 '15)edit

^^ @jamesnzt , nice idea, actually.

maybe start a new question with your attempt ? (else it gets pretty crowded here)

berak gravatar imageberak (Feb 24 '15)edit

@berak i posted it as a new question

jamesnzt gravatar imagejamesnzt (Feb 24 '15)edit

^^ yes, that's good !

berak gravatar imageberak (Feb 24 '15)edit

Question Tools

1 follower

Stats

Asked: Feb 15 '15

Seen: 1,161 times

Last updated: Feb 15 '15