OPENCV檢測矩形並計算其中心
阿新 • • 發佈:2019-01-05
#include "cv.h"
#include "highgui.h"
#include <stdio.h>
#include <math.h>
#include <string.h>
#pragma comment(lib, "cv.lib")
#pragma comment(lib, "cxcore.lib")
#pragma comment(lib, "highgui.lib")
IplImage* img =NULL;
IplImage* img0 = NULL;
CvMemStorage* storage =NULL;
const char * wndname = "正方形檢測 demo";
//angle函式用來返回(兩個向量之間找到角度的餘弦值)
double angle( CvPoint* pt1, CvPoint* pt2, CvPoint* pt0 )
{
double dx1 = pt1->x - pt0->x;
double dy1 = pt1->y - pt0->y;
double dx2 = pt2->x - pt0->x;
double dy2 = pt2->y - pt0->y;
return (dx1*dx2 + dy1*dy2)/sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10);
}
// 返回影象中找到的所有輪廓序列,並且序列儲存在記憶體儲存器中
CvSeq* findSquares4( IplImage* img, CvMemStorage* storage )
{
CvSeq* contours;
int i, l, N = 11;
//IplImage* timg = cvCloneImage( img );
//IplImage* gray = cvCreateImage( sz, 8, 1 );
//IplImage* pyr = cvCreateImage( cvSize(sz.width/2, sz.height/2), 8, 1 );
CvSeq* result;
double s, t;
// 建立一個空序列用於儲存輪廓角點
CvSeq* squares = cvCreateSeq( 0, sizeof(CvSeq), sizeof(CvPoint), storage );
// 嘗試各種閾值提取得到的(N=11)
for( l = 0; l < N; l++ )
{
// apply Canny. Take the upper threshold from slider
// Canny helps to catch squares with gradient shading
if( l == 0 )
{
cvCanny( img, img, 100,255, 3 ); //正常情況下
//使用任意結構元素膨脹影象
cvDilate( img, img, 0, 1 );
}
else
{
// apply threshold if l!=0:
cvThreshold( img, img, (l+1)*255/N, 255, CV_THRESH_BINARY );
}
// 找到所有輪廓並且儲存在序列中
cvFindContours( img, storage, &contours, sizeof(CvContour),
CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) );
// 遍歷找到的每個輪廓contours
while( contours )
{
//用指定精度逼近多邊形曲線
result = cvApproxPoly( contours, sizeof(CvContour), storage,
CV_POLY_APPROX_DP, cvContourPerimeter(contours)*0.02, 0 );
if( result->total == 4 &&
fabs(cvContourArea(result,CV_WHOLE_SEQ)) > 500 &&
fabs(cvContourArea(result,CV_WHOLE_SEQ)) < 1000000 &&
cvCheckContourConvexity(result) )
{
s = 0;
for( i = 0; i < 5; i++ )
{
// find minimum angle between joint edges (maximum of cosine)
if( i >= 2 )
{
t = fabs(angle(
(CvPoint*)cvGetSeqElem( result, i ),
(CvPoint*)cvGetSeqElem( result, i-2 ),
(CvPoint*)cvGetSeqElem( result, i-1 )));
s = s > t ? s : t;
}
}
// if 餘弦值 足夠小,可以認定角度為90度直角
//cos0.1=83度,能較好的趨近直角
if( s < 0.1 )
for( i = 0; i < 4; i++ )
cvSeqPush( squares,
(CvPoint*)cvGetSeqElem( result, i ));
}
// 繼續查詢下一個輪廓
contours = contours->h_next;
}
}
return squares;
}
//drawSquares函式用來畫出在影象中找到的所有正方形輪廓
void drawSquares( IplImage* img, CvSeq* squares )
{
CvSeqReader reader;
CvPoint pt3;
IplImage* cpy = cvCloneImage( img );
int i;
cvStartReadSeq( squares, &reader, 0 );
// read 4 sequence elements at a time (all vertices of a square)
for( i = 0; i < squares->total; i += 4 )
{
CvPoint pt[4], *rect = pt;
int count = 4;
// read 4 vertices
CV_READ_SEQ_ELEM( pt[0], reader );
CV_READ_SEQ_ELEM( pt[1], reader );
CV_READ_SEQ_ELEM( pt[2], reader );
CV_READ_SEQ_ELEM( pt[3], reader );
pt3.x=(pt[0].x+pt[1].x+pt[2].x+pt[3].x)/4;
pt3.y=(pt[0].y+pt[1].y+pt[2].y+pt[3].y)/4;
cvLine(cpy,pt3,pt3,CV_RGB(255, 255, 255),4,8,0);
printf("(%d,%d)",pt3.x,pt3.y);
// draw the square as a closed polyline
cvPolyLine( cpy, &rect, &count, 1, 1, CV_RGB(255,255,255), 2, CV_AA, 0 );
}
cvShowImage( wndname, cpy );
cvReleaseImage( &cpy );
}
int main(int argc, char** argv)
{
storage = cvCreateMemStorage(0);
img0 = cvLoadImage( "000.jpg", 0 );
img = cvCloneImage( img0 );
cvNamedWindow( wndname, 1 );
// find and draw the squares
drawSquares( img, findSquares4( img, storage ) );
cvWaitKey(0);
cvReleaseImage( &img );
cvReleaseImage( &img0 );
cvClearMemStorage( storage );
cvDestroyWindow( wndname );
}
#include "highgui.h"
#include <stdio.h>
#include <math.h>
#include <string.h>
#pragma comment(lib, "cv.lib")
#pragma comment(lib, "cxcore.lib")
#pragma comment(lib, "highgui.lib")
IplImage* img =NULL;
IplImage* img0 = NULL;
CvMemStorage* storage =NULL;
const char * wndname = "正方形檢測 demo";
//angle函式用來返回(兩個向量之間找到角度的餘弦值)
double angle( CvPoint* pt1, CvPoint* pt2, CvPoint* pt0 )
{
double dx1 = pt1->x - pt0->x;
double dy1 = pt1->y - pt0->y;
double dx2 = pt2->x - pt0->x;
double dy2 = pt2->y - pt0->y;
return (dx1*dx2 + dy1*dy2)/sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10);
}
// 返回影象中找到的所有輪廓序列,並且序列儲存在記憶體儲存器中
CvSeq* findSquares4( IplImage* img, CvMemStorage* storage )
{
CvSeq* contours;
int i, l, N = 11;
//IplImage* timg = cvCloneImage( img );
//IplImage* gray = cvCreateImage( sz, 8, 1 );
//IplImage* pyr = cvCreateImage( cvSize(sz.width/2, sz.height/2), 8, 1 );
CvSeq* result;
double s, t;
// 建立一個空序列用於儲存輪廓角點
CvSeq* squares = cvCreateSeq( 0, sizeof(CvSeq), sizeof(CvPoint), storage );
// 嘗試各種閾值提取得到的(N=11)
for( l = 0; l < N; l++ )
{
// apply Canny. Take the upper threshold from slider
// Canny helps to catch squares with gradient shading
if( l == 0 )
{
cvCanny( img, img, 100,255, 3 ); //正常情況下
//使用任意結構元素膨脹影象
cvDilate( img, img, 0, 1 );
}
else
{
// apply threshold if l!=0:
cvThreshold( img, img, (l+1)*255/N, 255, CV_THRESH_BINARY );
}
// 找到所有輪廓並且儲存在序列中
cvFindContours( img, storage, &contours, sizeof(CvContour),
CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) );
// 遍歷找到的每個輪廓contours
while( contours )
{
//用指定精度逼近多邊形曲線
result = cvApproxPoly( contours, sizeof(CvContour), storage,
CV_POLY_APPROX_DP, cvContourPerimeter(contours)*0.02, 0 );
if( result->total == 4 &&
fabs(cvContourArea(result,CV_WHOLE_SEQ)) > 500 &&
fabs(cvContourArea(result,CV_WHOLE_SEQ)) < 1000000 &&
cvCheckContourConvexity(result) )
{
s = 0;
for( i = 0; i < 5; i++ )
{
// find minimum angle between joint edges (maximum of cosine)
if( i >= 2 )
{
t = fabs(angle(
(CvPoint*)cvGetSeqElem( result, i ),
(CvPoint*)cvGetSeqElem( result, i-2 ),
(CvPoint*)cvGetSeqElem( result, i-1 )));
s = s > t ? s : t;
}
}
// if 餘弦值 足夠小,可以認定角度為90度直角
//cos0.1=83度,能較好的趨近直角
if( s < 0.1 )
for( i = 0; i < 4; i++ )
cvSeqPush( squares,
(CvPoint*)cvGetSeqElem( result, i ));
}
// 繼續查詢下一個輪廓
contours = contours->h_next;
}
}
return squares;
}
//drawSquares函式用來畫出在影象中找到的所有正方形輪廓
void drawSquares( IplImage* img, CvSeq* squares )
{
CvSeqReader reader;
CvPoint pt3;
IplImage* cpy = cvCloneImage( img );
int i;
cvStartReadSeq( squares, &reader, 0 );
// read 4 sequence elements at a time (all vertices of a square)
for( i = 0; i < squares->total; i += 4 )
{
CvPoint pt[4], *rect = pt;
int count = 4;
// read 4 vertices
CV_READ_SEQ_ELEM( pt[0], reader );
CV_READ_SEQ_ELEM( pt[1], reader );
CV_READ_SEQ_ELEM( pt[2], reader );
CV_READ_SEQ_ELEM( pt[3], reader );
pt3.x=(pt[0].x+pt[1].x+pt[2].x+pt[3].x)/4;
pt3.y=(pt[0].y+pt[1].y+pt[2].y+pt[3].y)/4;
cvLine(cpy,pt3,pt3,CV_RGB(255, 255, 255),4,8,0);
printf("(%d,%d)",pt3.x,pt3.y);
// draw the square as a closed polyline
cvPolyLine( cpy, &rect, &count, 1, 1, CV_RGB(255,255,255), 2, CV_AA, 0 );
}
cvShowImage( wndname, cpy );
cvReleaseImage( &cpy );
}
int main(int argc, char** argv)
{
storage = cvCreateMemStorage(0);
img0 = cvLoadImage( "000.jpg", 0 );
img = cvCloneImage( img0 );
cvNamedWindow( wndname, 1 );
// find and draw the squares
drawSquares( img, findSquares4( img, storage ) );
cvWaitKey(0);
cvReleaseImage( &img );
cvReleaseImage( &img0 );
cvClearMemStorage( storage );
cvDestroyWindow( wndname );
}