1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| #include <cv.h>
#include <highgui.h>
#include <math.h>
//--------------- focnction de rotation -----------------------
IplImage *rotateImage(const IplImage *src, float angleDegrees) {
// Create a map_matrix, where the left 2x2 matrix
// is the transform and the right 2x1 is the dimensions.
float m[6];
CvMat M = cvMat(2, 3, CV_32F, m);
int w = src->width;
int h = src->height;
float angleRadians = angleDegrees * ((float)CV_PI / 180.0f);
m[0] = (float)( cos(angleRadians) );
m[1] = (float)( sin(angleRadians) );
m[3] = -m[1];
m[4] = m[0];
m[2] = w*0.5f;
m[5] = h*0.5f;
// Make a spare image for the result
CvSize sizeRotated;
sizeRotated.width = cvRound(w);
sizeRotated.height = cvRound(h);
// Rotate
IplImage *imageRotated = cvCreateImage( sizeRotated,
src->depth, src->nChannels );
// Transform the image
cvGetQuadrangleSubPix( src, imageRotated, &M);
return imageRotated;
}
int main(int argc, char** argv) {
IplImage* img =cvLoadImage("D:/11a.png", 0);
// ----------- Dans les 10 lignes suivant c'est hough --------------//
IplImage* dst = cvCreateImage( cvGetSize(img), 8, 1 );
IplImage* color_dst = cvCreateImage( cvGetSize(img), 8, 3 );
CvMemStorage* storage = cvCreateMemStorage(0);
CvSeq* lines = 0;
int i;
cvCanny( img, dst, 50, 200, 3 );
lines = cvHoughLines2( dst, storage, CV_HOUGH_PROBABILISTIC, 1 ,CV_PI/180, 80, 30, 10 );
for( i = 0; i < lines->total; i++ ) {
CvPoint* line = (CvPoint*)cvGetSeqElem(lines,i);
cvLine( color_dst, line[0], line[1], CV_RGB(255,0,0), 3, 8 );
}
IplImage *test_rotation =rotateImage(img,-10);
//cvCvtColor(img,greyImage,CV_BGR2GRAY);
//--------------- affichage ---------------------
cvNamedWindow( "result",CV_WINDOW_AUTOSIZE);
cvNamedWindow( "rotation",CV_WINDOW_AUTOSIZE);
cvNamedWindow( "testrotation",CV_WINDOW_AUTOSIZE);
cvShowImage( "result", img );
cvShowImage( "rotation", color_dst );
cvShowImage( "testrotation", test_rotation );
cvWaitKey(0);
cvReleaseImage( &img );
cvReleaseImage( &color_dst );
cvReleaseImage( &test_rotation );
cvDestroyWindow("result");
cvDestroyWindow("rotation");
cvDestroyWindow("testrotation");
} |
Partager