|
发表于 2015-5-31 21:11:03
|
显示全部楼层
#include "time.h"
#include <stdio.h>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
/*
src and dst are grayscale, 8-bit images;
Default input value:
[low, high] = [0,1]; X-Direction
[bottom, top] = [0,1]; Y-Direction
gamma ;
if adjust successfully, return 0, otherwise, return non-zero.
*/
int ImageAdjust(IplImage* src, IplImage* dst,
double low, double high, // X方向:low and high are the intensities of src
double bottom, double top, // Y方向:mapped to bottom and top of dst
double gamma )
{
if( low<0 && low>1 && high <0 && high>1&&
bottom<0 && bottom>1 && top<0 && top>1 && low>high)
return -1;
double low2 = low*255;
double high2 = high*255;
double bottom2 = bottom*255;
double top2 = top*255;
double err_in = high2 - low2;
double err_out = top2 - bottom2;
int x,y;
double val;
// intensity transform
for( y = 0; y < src->height; y++)
{
for (x = 0; x < src->width; x++)
{
val = ((uchar*)(src->imageData + src->widthStep*y))[x];
val = pow((val - low2)/err_in, gamma) * err_out + bottom2;
if(val>255) val=255; if(val<0) val=0; // Make sure src is in the range [low,high]
((uchar*)(dst->imageData + dst->widthStep*y))[x] = (uchar) val;
}
}
return 0;
}
int main(int argc, char* argv[])
{
IplImage* img_src = NULL;
IplImage* img_show = NULL;
IplImage* img_gray = NULL;
IplImage* img_edge = NULL;
IplImage* img_dst = NULL;
IplImage* img_temp = NULL;
CvPoint p_center;
CvSize size;
CvMemStorage* storage = cvCreateMemStorage(0);
CvSeq* seq = 0;
int i, j, k;
LONGLONG persecond, t1, t2;
double t_diff = 0, time = 0;
/********Parameter for circle detection********/
CvSeq* ellipses;
CvBox2D32f* box;
CvPoint* PointArray;
CvPoint2D32f* PointArray2D32f;
CvPoint center;
int count = 0, n_ellipse = 0;
double ratio = 0, perim = 0;
double find_x = 0, find_y = 0;
double max_h = 0, min_h = 1000;
int cur_x = 0, cur_y = 0;
int pre_x = 0, pre_y = 0;
double max_scale = 0, min_scale = 0;
double dist = 0;
cvNamedWindow("Raw", 1);
cvNamedWindow("Interim",1);
cvNamedWindow("Result",1);
cvMoveWindow("Raw", 30, 0);
cvMoveWindow("Interim", 360, 0);
cvMoveWindow("Result", 690, 0);
img_src = cvLoadImage("iris.bmp",1);
img_show = cvCreateImage(cvSize(img_src->width, img_src->height),IPL_DEPTH_8U,3);
img_gray = cvCreateImage(cvSize(img_src->width, img_src->height),IPL_DEPTH_8U,1);
img_edge = cvCreateImage(cvSize(img_src->width, img_src->height),IPL_DEPTH_8U,1);
img_temp = cvCreateImage(cvSize(img_src->width, img_src->height),IPL_DEPTH_8U,3);
img_dst = cvCreateImage(cvSize(img_src->width, img_src->height),IPL_DEPTH_8U,1);
p_center = cvPoint(cvRound(img_src->width/2), cvRound(img_src->height/2));
QueryPerformanceFrequency((LARGE_INTEGER *)&persecond);
QueryPerformanceCounter((LARGE_INTEGER *)&t1);
/************************Part1:************************/
/********1.0:Convert to gray channel********/
cvCvtColor(img_src, img_gray, CV_BGR2GRAY);
/********1.1:Contrast enhancement********/
ImageAdjust( img_gray, img_edge, 0, 0.5, 0.5, 1, 1);
/********1.2:Thresholding********/
cvThreshold(img_edge, img_edge, 0, 255, CV_THRESH_BINARY|CV_THRESH_OTSU);
cvCvtColor(img_edge, img_temp, CV_GRAY2BGR);
/********2.5:Ellipse detection********/
n_ellipse = 0, max_h = 0, min_h = 1000;
cvCopyImage(img_src, img_show);
img_show->origin = img_src->origin;
ellipses = cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint), storage);
cvFindContours(img_edge, storage, &ellipses, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_NONE, cvPoint(0,0));
for(;ellipses;ellipses = ellipses->h_next)
{
/*******Min required points for fitting*******/
count = ellipses->total;
if(count < 100)
continue;
/*******Allocate memory for point sets*******/
PointArray = (CvPoint*)malloc(count*sizeof(CvPoint));
PointArray2D32f= (CvPoint2D32f*)malloc(count*sizeof(CvPoint2D32f));
box = (CvBox2D32f*)malloc(sizeof(CvBox2D32f));
cvCvtSeqToArray(ellipses, PointArray, CV_WHOLE_SEQ);
/********Convert CvPoint set to CvPoint2D32f********/
for(i=0; i<count; i++)
{
PointArray2D32f[i].x = (float)PointArray[i].x;
PointArray2D32f[i].y = (float)PointArray[i].y;
}
/********Fitting current contour********/
cvFitEllipse(PointArray2D32f, count, box);
/********Convert ellipse data from float to integer representation********/
center.x = cvRound(box->center.x);
center.y = cvRound(box->center.y);
size.width = cvRound(box->size.width*0.5);
size.height = cvRound(box->size.height*0.5);
box->angle = -box->angle;
/********Check validity ellipse********/
if(size.width>size.height)
{
ratio = box->size.height/box->size.width;
perim = 2*3.1415*size.height+4*(size.width-size.height);
}
else
{
ratio = box->size.width/box->size.height;
perim = 2*3.1415*size.width+4*(size.height-size.width);
}
if(ratio>0.5&&((int)perim>count))
{
cvEllipse(img_show, center, size, box->angle, 0, 360, CV_RGB(255,0,0), 1, CV_AA, 0);
}
/********Free memory********/
free(PointArray);
free(PointArray2D32f);
free(box);
}
cvSaveImage("result.jpg", img_show);
/********Compute time lapse********/
QueryPerformanceCounter((LARGE_INTEGER *)&t2);
t_diff = t2-t1;
time = t_diff/persecond; //(单位:秒)
printf("Time lapse:%.6f\n", time);
/********3:Show image result********/
img_gray->origin = img_src->origin;
img_edge->origin = img_src->origin;
img_temp->origin = img_src->origin;
cvShowImage("Raw", img_src);
cvShowImage("Result", img_show);
cvShowImage("Interim", img_temp);
cvWaitKey(0);
/********Cleanup:********/
cvDestroyWindow("Raw");
cvDestroyWindow("Interim");
cvDestroyWindow("Result");
cvReleaseImage(&img_dst);
cvReleaseImage(&img_temp);
return 0;
}
|
-
|