00001 #include "segmentation.h"
00002
00003 Segmentation::Segmentation(Ui::mainFrameClass pui) {
00004 ui=pui;
00005 }
00006
00007 Segmentation::~Segmentation() {}
00008
00009 IplImage* Segmentation::drawContour(IplImage* src) {
00010 IplImage* dst = cvCreateImage( cvGetSize(src), 8, 3 );
00011 CvMemStorage* storage = cvCreateMemStorage(0);
00012 CvSeq* contour = 0;
00013
00014 if (!ui.checkThreshold->isChecked())
00015 cvThreshold(src, src, 1, 255, CV_THRESH_BINARY );
00016
00017 if (ui.comboLog->currentIndex()>0)
00018 ui.txInform->append(QString("Applying cvFindContours filter and coloring"));
00019
00020 cvFindContours(src, storage, &contour, sizeof(CvContour),CV_RETR_LIST, CV_CHAIN_APPROX_NONE );
00021 cvZero( dst );
00022 for( ; contour != 0; contour = contour->h_next ){
00023
00024 CvScalar color = CV_RGB( rand()&255, rand()&255, rand()&255 );
00025
00026 cvDrawContours(dst, contour, color, color, -1, CV_FILLED, 8 );
00027
00028 if (ui.checkContourRect->isChecked()){
00029 CvRect rect = cvBoundingRect(contour);
00030 cvRectangle(dst,cvPoint(rect.x,rect.y),cvPoint(rect.x+rect.width,rect.y+rect.height),CV_RGB(255,0,0));
00031 if (ui.comboLog->currentIndex()>0)
00032 ui.txInform->append(QString(" -> Drawing rectangle from contour, vertices (%1,%2)-(%3,%4)")
00033 .arg(rect.x).arg(rect.y).arg(rect.x+rect.width).arg(rect.y+rect.height));
00034
00035 if (ui.checkContourSnake->isChecked()){
00036 if (ui.comboLog->currentIndex()>0)
00037 ui.txInform->append(QString(" -> Snake from rectangle contour")
00038 .arg(rect.x).arg(rect.y).arg(rect.x+rect.width).arg(rect.y+rect.height));
00039 std::vector <CvPoint> vec;
00040 int x = rect.x;
00041 int y = rect.y;
00042 int width = rect.width;
00043 int height = rect.height;
00044
00045 int divSnake=(ui.leDivSnakeContour->text()).toInt();
00046
00047 divSnake=(divSnake)?divSnake:5;
00048
00049 int div=(width>divSnake && height>divSnake)?divSnake:1;
00050
00051 for (int i=0; i<height; i+=div) vec.push_back(cvPoint(x, y+i));
00052 for (int i=0; i<width; i+=div) vec.push_back(cvPoint(x+i, y+height));
00053 for (int i=0; i<height; i+=div) vec.push_back(cvPoint(x+width, y+height-i));
00054 for (int i=0; i<width; i+=div) vec.push_back(cvPoint(x+width-i, y));
00055
00056
00057 if (ui.comboLogPaint->currentIndex()==1){
00058 paintCoord(dst,cvPoint(x, y));
00059 paintCoord(dst,cvPoint(x, y+height));
00060 paintCoord(dst,cvPoint(x+width, y+height));
00061 paintCoord(dst,cvPoint(x+width, y));
00062 }
00063
00064 int n = vec.size();
00065 CvPoint *point = new CvPoint[n];
00066 if (ui.comboLog->currentIndex()>1)
00067 ui.txInform->append(QString(" + Contour w=%1 h=%2 Snake it with %3 points, %4 divisions")
00068 .arg(width).arg(height).arg(n).arg(div));
00069 for (int i=0; i<n; i++) point[i] = vec[i];
00070
00071 float alpha=1.0;
00072 float beta=0.9;
00073 float gamma=1.0;
00074 CvTermCriteria criteria;
00075 criteria.max_iter=1000;
00076 criteria.epsilon=0.1;
00077 criteria.type=CV_TERMCRIT_EPS|CV_TERMCRIT_ITER;
00078 CvSize win;
00079 win.height=3;
00080 win.width=3;
00081 cvSnakeImage(src, point, n, &alpha, &beta, &gamma, CV_VALUE,win, criteria, 0);
00082 cvPolyLine( dst, &point, &n, 1,1,CV_RGB( 0, 255, 0 ), 1,8 );
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105 }
00106 }
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142 }
00143 cvReleaseMemStorage(&storage);
00144 cvReleaseImage(&src);
00145 return dst;
00146 }
00147
00148 IplImage* Segmentation::drawHoughLines(IplImage* src, int houghMethod)
00149 {
00150 IplImage* dst = cvCloneImage(src);
00151 IplImage* color_dst=cvCreateImage( cvGetSize(src), 8, 3 );
00152 CvMemStorage* storage = cvCreateMemStorage(0);
00153 CvSeq* lines = 0;
00154 int i;
00155
00156 double thetaminleft = 30*CV_PI/180;
00157 double thetamaxleft = 80*CV_PI/180;
00158 double thetaminright = 100*CV_PI/180;
00159 double thetamaxright = 150*CV_PI/180;
00160
00161
00162 cvCvtColor( dst, color_dst, CV_GRAY2BGR );
00163 int iThres=(ui.leThresHough->text()).toInt();
00164 int iRho=(ui.leRhoHough->text()).toInt();
00165 int iTheta=(ui.leThetaHough->text()).toInt();
00166
00167 iThres=(iThres)?iThres:80;
00168 iRho=(iRho)?iRho:1;
00169 iTheta=(iTheta)?iTheta:1;
00170 if(houghMethod==CV_HOUGH_STANDARD){
00171 if (ui.comboLog->currentIndex()>0)
00172 ui.txInform->append(QString("Applying Standard Hough filter with accumulator: %1").arg(iThres));
00173 lines = cvHoughLines2(dst, storage, CV_HOUGH_STANDARD, iRho, iTheta*(CV_PI/180), iThres);
00174 if (ui.comboLog->currentIndex()>1)
00175 ui.txInform->append(QString(" -> Total lines detected = %1").arg(lines->total));
00176 for( i = 0; i < MIN(lines->total,100); i++ )
00177 {
00178 float* line = (float*)cvGetSeqElem(lines,i);
00179 double rho = line[0];
00180 double theta = line[1];
00181 if ((theta>=thetaminleft && theta<=thetamaxleft)||(theta>=thetaminright && theta<=thetamaxright)){
00182 CvPoint pt1, pt2;
00183 double a = cos(theta), b = sin(theta);
00184 double x0 = a*rho, y0 = b*rho;
00185 int iSc=1000;
00186
00187 pt1.x = cvRound(x0 + iSc*(-b));
00188 pt1.y = cvRound(y0 + iSc*(a));
00189 pt2.x = cvRound(x0 - iSc*(-b));
00190 pt2.y = cvRound(y0 - iSc*(a));
00191 cvLine(color_dst, pt1, pt2, CV_RGB(255,0,0), 3, 8 );
00192 if (ui.comboLog->currentIndex()>1)
00193 ui.txInform->append(QString(" + Rho=%1, Theta=%2. Origen-Fin: (%3,%4)-(%5,%6)").arg(rho).arg(theta*180/CV_PI).arg(pt1.x).arg(pt1.y).arg(pt2.x).arg(pt2.y));
00194 if (ui.comboLogPaint->currentIndex()==1){
00195
00196 paintCoord(color_dst,pt1);
00197 paintCoord(color_dst,pt2);
00198 }
00199 }
00200 }
00201 }else if (houghMethod==CV_HOUGH_PROBABILISTIC) {
00202 int iMinLineLength=(ui.leMinLineHough->text()).toInt();
00203
00204
00205 int iSegmentGap=(ui.leGapHough->text()).toInt();
00206 if (ui.comboLog->currentIndex()>0)
00207 ui.txInform->append(QString("Applying Probabilistic Hough filter with accumulator: %1").arg(iThres));
00208 lines = cvHoughLines2( dst, storage, CV_HOUGH_PROBABILISTIC, iRho, iTheta*(CV_PI/180), iThres, iMinLineLength, iSegmentGap );
00209 if (ui.comboLog->currentIndex()>1)
00210 ui.txInform->append(QString("\tTotal lines detected = %1").arg(lines->total));
00211 for( i = 0; i < lines->total; i++ )
00212 {
00213 float* linePol = (float*)cvGetSeqElem(lines,i);
00214 double rho = linePol[0];
00215 double theta = linePol[1];
00216 CvPoint* line = (CvPoint*)cvGetSeqElem(lines,i);
00217 cvLine( color_dst, line[0], line[1], CV_RGB(255,0,0), 3, 8);
00218 if (ui.comboLog->currentIndex()>1)
00219
00220 ui.txInform->append(QString(" + Rho=%1, Theta=%2. Origen-Fin: (%3,%4)-(%5,%6)").arg(rho).arg(theta*180/CV_PI).arg(line[0].x).arg(line[0].y).arg(line[1].x).arg(line[1].y));
00221 if (ui.comboLogPaint->currentIndex()==1){
00222
00223 paintCoord(color_dst,line[0]);
00224 paintCoord(color_dst,line[1]);
00225 }
00226 }
00227 } else if (houghMethod==CV_HOUGH_MULTI_SCALE) {
00228 int iRadialRes=(ui.leRadialResHough->text()).toInt();
00229 int iAngleRes=(ui.leAngleResHough->text()).toInt();
00230 }
00231 cvReleaseMemStorage(&storage);
00232 cvReleaseImage(&dst);
00233 cvReleaseImage(&src);
00234 return color_dst;
00235 }
00236
00237 IplImage* Segmentation::drawLocalScaleSpace(IplImage* src) {
00238 IplImage* dst = cvCreateImage( cvGetSize(src), 8, 3 );
00239 cvCvtColor( src, dst, CV_GRAY2BGR );
00240 CvMemStorage* storage = cvCreateMemStorage(0);
00241 CvSeq* keypoints = 0;
00242
00243 int iSize=(ui.comboSizeScale->currentText()).toInt();
00244 int iThres1=(ui.leThres1Scale->text()).toInt();
00245 int iThres2=(ui.leThres2Scale->text()).toInt();
00246 int iThres3=(ui.leThres3Scale->text()).toInt();
00247 int iNonmax=(ui.leNonmaxScale->text()).toInt();
00248
00249 iThres1=(iThres1)?iThres1:30;
00250 iThres2=(iThres2)?iThres2:10;
00251 iThres3=(iThres3)?iThres3:8;
00252 iNonmax=(iNonmax)?iNonmax:5;
00253
00254 if (iNonmax>(iSize*3+1)){
00255 iNonmax=iSize*3+1;
00256 ui.leNonmaxScale->setText(QString("%1").arg(iNonmax));
00257 }
00258 if (ui.comboLog->currentIndex()>0)
00259 ui.txInform->append(QString("Applying LocalScaleSpace filter with: "
00260 "maxSize=%1,responseThreshold=%2,lineThresholdProjected=%3,lineThresholdBinarized=%4,suppressNonmaxSize=%5")
00261 .arg(iSize).arg(iThres1).arg(iThres2).arg(iThres3).arg(iNonmax));
00262
00263 keypoints = cvGetStarKeypoints( src, storage, cvStarDetectorParams(iSize,iThres1,iThres2,iThres3,iNonmax) );
00264
00265 for(int i = 0; i < (keypoints ? keypoints->total : 0); i++) {
00266 CvStarKeypoint kpt = *(CvStarKeypoint*)cvGetSeqElem(keypoints, i);
00267 int r = kpt.size/2;
00268 cvCircle( dst, kpt.pt, r, CV_RGB(0,255,0));
00269 cvLine( dst, cvPoint(kpt.pt.x + r, kpt.pt.y + r),
00270 cvPoint(kpt.pt.x - r, kpt.pt.y - r), CV_RGB(0,255,0));
00271 cvLine( dst, cvPoint(kpt.pt.x - r, kpt.pt.y + r),
00272 cvPoint(kpt.pt.x + r, kpt.pt.y - r), CV_RGB(0,255,0));
00273 }
00274 cvReleaseMemStorage(&storage);
00275 cvReleaseImage(&src);
00276 return dst;
00277 }
00278
00279 void Segmentation::paintCoord(IplImage *src,CvPoint coord){
00280
00281 CvFont font;
00282 cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.4, 0.4, 0, 1, CV_AA);
00283 QString qCoord=QString("(%1,%2)").arg(coord.x).arg(coord.y);
00284 QByteArray ba = qCoord.toLatin1();
00285 const char *cqCoord = ba.data();
00286
00287 cvPutText(src,cqCoord,coord,&font,CV_RGB(0, 255, 0));
00288 }