Ask Your Question
-1

Problem with perspective

asked 2015-02-15 06:50:22 -0600

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

edit retag flag offensive close merge delete

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 ( 2015-02-15 06:52:01 -0600 )edit

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

Sparrow gravatar imageSparrow ( 2015-02-15 06:59:19 -0600 )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 ( 2015-02-15 07:18:11 -0600 )edit

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

Sparrow gravatar imageSparrow ( 2015-02-15 08:19:55 -0600 )edit
1

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

berak gravatar imageberak ( 2015-02-15 08:28:04 -0600 )edit

1 answer

Sort by » oldest newest most voted
5

answered 2015-02-15 07:07:23 -0600

berak gravatar image

updated 2015-02-15 10:50:48 -0600

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

edit flag offensive delete link more

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 ( 2015-02-15 07:20:43 -0600 )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 ( 2015-02-15 08:02:53 -0600 )edit

ah, sorry, please see edit ^^

berak gravatar imageberak ( 2015-02-15 10:43:44 -0600 )edit
1

thank you it works

Sparrow gravatar imageSparrow ( 2015-02-15 11:35:10 -0600 )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 ( 2015-02-24 02:18:49 -0600 )edit

^^ @jamesnzt , nice idea, actually.

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

berak gravatar imageberak ( 2015-02-24 02:29:57 -0600 )edit

@berak i posted it as a new question

jamesnzt gravatar imagejamesnzt ( 2015-02-24 02:50:52 -0600 )edit

^^ yes, that's good !

berak gravatar imageberak ( 2015-02-24 02:56:02 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-02-15 06:50:22 -0600

Seen: 1,161 times

Last updated: Feb 15 '15