IplImage* BouyObject::ShapeMask(const IplImage * imgIn) const { IplImage * hsv = cvCloneImage(imgIn); IplImage * imgOut = cvCreateImage(cvGetSize(imgIn),IPL_DEPTH_8U, 1); //cvEqualizeHist( imgIn, hsv); IplImage * chan0 = cvCreateImage(cvGetSize(imgIn),IPL_DEPTH_8U, 1); IplImage * chan1 = cvCreateImage(cvGetSize(imgIn),IPL_DEPTH_8U, 1); IplImage * chan2 = cvCreateImage(cvGetSize(imgIn),IPL_DEPTH_8U, 1); IplImage * chan3 = cvCreateImage(cvGetSize(imgIn),IPL_DEPTH_8U, 1); cvCvtColor(imgIn, hsv, CV_BGR2YCrCb); cvSplit(hsv,chan0,chan1,chan2, NULL); CvScalar white = cvRealScalar(255); //imgOut = SegmentationMask(imgIn); imgOut = cvCloneImage(chan2); //invert black and white cvAbsDiffS(imgOut, imgOut, white); // cvShowImage("hue",chan0); // cvShowImage("sat",chan1); // cvShowImage("val",chan2); // cvShowImage("inv",imgOut); //cvWaitKey(0); cvReleaseImage(&hsv); cvReleaseImage(&chan0); cvReleaseImage(&chan1); cvReleaseImage(&chan2); cvReleaseImage(&chan3); CvSize imageSize = cvSize(imgIn->width & -2, imgIn->height & -2 ); IplImage* imgSmallCopy = cvCreateImage( cvSize(imageSize.width/2, imageSize.height/2), IPL_DEPTH_8U, 1 ); cvPyrDown( imgOut, imgSmallCopy); cvPyrUp( imgSmallCopy, imgOut); cvSmooth(imgOut, imgOut, CV_GAUSSIAN, 5); cvReleaseImage(&imgSmallCopy); CvMemStorage* storage = cvCreateMemStorage(0); CvSeq* results = cvHoughCircles( imgOut, storage, CV_HOUGH_GRADIENT, 2, imgOut->width/10, 100, 100); cvZero(imgOut); for( int i = 0; i < results->total; i++ ) { float* p = (float*) cvGetSeqElem( results, i ); CvPoint pt = cvPoint( cvRound( p[0] ), cvRound( p[1] ) ); cvCircle( imgOut, pt, cvRound( p[2] ), CV_RGB(0xff,0xff,0xff),CV_FILLED); } cvReleaseMemStorage(&storage); return imgOut; }
int cvL1QCSolve( CvMatOps AOps, CvMatOps AtOps, void* userdata, CvMat* B, CvMat* X, double epsilon, double mu, CvTermCriteria lb_term_crit, CvTermCriteria cg_term_crit ) { CvMat* Z = cvCreateMat( X->rows, 1, CV_MAT_TYPE(X->type) ); CvMat* W = cvCreateMat( B->rows, 1, CV_MAT_TYPE(B->type) ); CvAAtOpsData AAtData; AAtData.AOps = AOps; AAtData.AtOps = AtOps; AAtData.AtR = Z; AAtData.userdata = userdata; if ( cvCGSolve( icvAAtOps, &AAtData, B, W, cg_term_crit ) > .5 ) { cvReleaseMat( &W ); cvReleaseMat( &Z ); return -1; } AtOps( W, X, userdata ); AAtData.AR = W; CvMat* U = cvCreateMat( X->rows, X->cols, CV_MAT_TYPE(X->type) ); cvAbsDiffS( X, U, cvScalar(0) ); CvScalar sumAbsX = cvSum( U ); double minAbsX, maxAbsX; cvMinMaxLoc( U, &minAbsX, &maxAbsX ); cvConvertScale( U, U, .95, maxAbsX * .1 ); double tau = MAX( (2 * X->rows + 1) / sumAbsX.val[0], 1 ); if ( !(lb_term_crit.type & CV_TERMCRIT_ITER) ) lb_term_crit.max_iter = ceil( (log(2 * X->rows + 1) - log(lb_term_crit.epsilon) - log(tau)) / log(mu) ); CvTermCriteria nt_term_crit = cvTermCriteria( CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 50, lb_term_crit.epsilon ); int totaliter = 0; for ( int i = 0; i < lb_term_crit.max_iter; ++i ) { totaliter += icvL1QCNewton( AAtData, B, X, U, epsilon, tau, nt_term_crit, cg_term_crit ); tau *= mu; } cvReleaseMat( &U ); cvReleaseMat( &W ); cvReleaseMat( &Z ); return 0; }
IplImage* BouyBaseObject::SegmentationMask(const IplImage * imgIn) const { IplImage * imgOut = cvCreateImage(cvGetSize(imgIn),IPL_DEPTH_8U, 1); IplImage * src = cvCreateImage(cvGetSize(imgIn),IPL_DEPTH_8U, 1); IplImage* hsv = cvCreateImage( cvGetSize(imgIn), 8, 3 ); IplImage * chan0 = cvCreateImage(cvGetSize(imgIn),IPL_DEPTH_8U, 1); IplImage * chan1 = cvCreateImage(cvGetSize(imgIn),IPL_DEPTH_8U, 1); IplImage * chan2 = cvCreateImage(cvGetSize(imgIn),IPL_DEPTH_8U, 1); IplImage * chan3 = cvCreateImage(cvGetSize(imgIn),IPL_DEPTH_8U, 1); cvCvtColor( imgIn, hsv, CV_BGR2YCrCb ); cvSplit(hsv,chan0,chan1,chan2, NULL); //cvConvertImage(imgIn,src); src = cvCloneImage(chan0); CvMemStorage* storage = cvCreateMemStorage(0); CvSeq* comp = NULL; //lower last param for more segments cvPyrSegmentation( src, imgOut, storage, &comp, 3, 200, 50 ); cvAbsDiffS(imgOut,imgOut,CV_RGB(255,255,255)); //cvNormalize(imgOut,imgOut,255,0, CV_MINMAX); //cvThreshold(imgOut,imgOut,250,255,CV_THRESH_TOZERO); // int n_comp = comp->total; // for( int i=0; i<n_comp; i++ ) // { // CvConnectedComp* cc = (CvConnectedComp*) cvGetSeqElem( comp, i ); // cvDrawRect(imgOut,cvPoint(cc->rect.x,cc->rect.y),cvPoint(cc->rect.x+cc->rect.width,cc->rect.y+cc->rect.height),CV_RGB(255,255,255)); // //do_something_with( cc ); // } cvReleaseImage(&src); cvReleaseImage(&hsv); cvReleaseImage(&chan0); cvReleaseImage(&chan1); cvReleaseImage(&chan2); cvReleaseImage(&chan3); cvReleaseMemStorage( &storage ); return imgOut; }
IplImage* PathObject::ChannelMask2(const IplImage * imgIn) const { if(imgIn == NULL) return NULL; IplImage * hsv = cvCloneImage(imgIn); IplImage * imgOut = cvCreateImage(cvGetSize(imgIn),IPL_DEPTH_8U, 1); //cvEqualizeHist( imgIn, hsv); IplImage * chan0 = cvCreateImage(cvGetSize(imgIn),IPL_DEPTH_8U, 1); IplImage * chan1 = cvCreateImage(cvGetSize(imgIn),IPL_DEPTH_8U, 1); IplImage * chan2 = cvCreateImage(cvGetSize(imgIn),IPL_DEPTH_8U, 1); IplImage * chan3 = cvCreateImage(cvGetSize(imgIn),IPL_DEPTH_8U, 1); cvCvtColor(imgIn, hsv, CV_BGR2YCrCb); cvSplit(hsv,chan0,chan1,chan2, NULL); CvScalar white = cvRealScalar(255); imgOut = cvCloneImage(chan2); //invert black and white cvAbsDiffS(imgOut, imgOut, white); cvShowImage("hue",chan0); cvShowImage("sat",chan1); cvShowImage("val",chan2); cvShowImage("inv",imgOut); cvWaitKey(0); cvReleaseImage(&hsv); cvReleaseImage(&chan0); cvReleaseImage(&chan1); cvReleaseImage(&chan2); cvReleaseImage(&chan3); CvSize imageSize = cvSize(imgOut->width & -2, imgOut->height & -2 ); IplImage* imgSmallCopy = cvCreateImage( cvSize(imageSize.width/2, imageSize.height/2), IPL_DEPTH_8U, 1 ); cvPyrDown( imgOut, imgSmallCopy); cvPyrUp( imgSmallCopy, imgOut); cvReleaseImage(&imgSmallCopy); //cvThreshold(imgOut,imgOut,200, 255,CV_THRESH_TOZERO); return imgOut; }
int cvL1QCSolve( CvMat* A, CvMat* B, CvMat* X, double epsilon, double mu, CvTermCriteria lb_term_crit, CvTermCriteria cg_term_crit ) { CvMat* AAt = cvCreateMat( A->rows, A->rows, CV_MAT_TYPE(A->type) ); cvGEMM( A, A, 1, NULL, 0, AAt, CV_GEMM_B_T ); CvMat* W = cvCreateMat( A->rows, 1, CV_MAT_TYPE(X->type) ); if ( cvCGSolve( AAt, B, W, cg_term_crit ) > .5 ) { cvReleaseMat( &W ); cvReleaseMat( &AAt ); return -1; } cvGEMM( A, W, 1, NULL, 0, X, CV_GEMM_A_T ); cvReleaseMat( &W ); cvReleaseMat( &AAt ); CvMat* U = cvCreateMat( X->rows, X->cols, CV_MAT_TYPE(X->type) ); cvAbsDiffS( X, U, cvScalar(0) ); CvScalar sumAbsX = cvSum( U ); double minAbsX, maxAbsX; cvMinMaxLoc( U, &minAbsX, &maxAbsX ); cvConvertScale( U, U, .95, maxAbsX * .1 ); double tau = MAX( (2 * X->rows + 1) / sumAbsX.val[0], 1 ); if ( !(lb_term_crit.type & CV_TERMCRIT_ITER) ) lb_term_crit.max_iter = ceil( (log(2 * X->rows + 1) - log(lb_term_crit.epsilon) - log(tau)) / log(mu) ); CvTermCriteria nt_term_crit = cvTermCriteria( CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 50, lb_term_crit.epsilon ); for ( int i = 0; i < lb_term_crit.max_iter; ++i ) { icvL1QCNewton( A, B, X, U, epsilon, tau, nt_term_crit, cg_term_crit ); tau *= mu; } cvReleaseMat( &U ); return 0; }
IplImage* BouyObject::SegmentationMask2(const IplImage * imgIn, IplImage* debugOut) const { // CvSize imageSize = cvSize(imgIn->width & -2, imgIn->height & -2 ); // IplImage* imgSmallCopy = cvCreateImage( cvSize(imageSize.width/2, imageSize.height/2), IPL_DEPTH_8U, 1 ); IplImage * imgOut = cvCreateImage(cvGetSize(imgIn),IPL_DEPTH_8U, 1); IplImage * circleMask = cvCreateImage(cvGetSize(imgIn),IPL_DEPTH_8U, 1); IplImage * src = cvCloneImage(imgIn); IplImage * scratch = cvCloneImage(src); IplImage * hist = HistogramMask(imgIn); //IplImage * bestMask = cvCreateImage(cvGetSize(imgIn),IPL_DEPTH_8U, 1); CvMemStorage* storage = cvCreateMemStorage(0); CvSeq* comp = NULL; CvFont font; cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, .5,.5); std::ostringstream s; cvZero(imgOut); cvZero(circleMask); cvZero(scratch); //cvZero(bestMask); CvScalar avgColor; double bestColor = -1; CvRect bestRect; double bestDiag = 0; // IplImage* hsv = cvCreateImage( cvGetSize(imgIn), 8, 3 ); // IplImage * chan0 = cvCreateImage(cvGetSize(imgIn),IPL_DEPTH_8U, 1); // IplImage * segsum = cvCreateImage(cvGetSize(imgIn),IPL_DEPTH_8U, 1); //cvCvtColor( imgIn, hsv, CV_BGR2YCrCb ); //cvCopyImage(imgIn,hsv); //cvSplit(hsv,chan0,chan1,chan2, NULL); //cvConvertImage(imgIn,src); //lower last param for more segments //cvPyrSegmentation( hsv, scratch, storage, &comp, 3, 100, 90 ); cvPyrSegmentation( src, scratch, storage, &comp, 2, 0, 100); int n_comp = comp->total; std::list<CvBox2D> blobList; for( int i = n_comp-1; i>=1; i-- ) { CvConnectedComp* cc = (CvConnectedComp*) cvGetSeqElem( comp, i ); cvAbsDiffS(scratch,src,cc->value); cvNot(src,src); cvThreshold(src,src,254,255,CV_THRESH_BINARY); blobList = VisionUtils::GetBlobBoxes(src,0.0008,.95,false); for(std::list<CvBox2D>::iterator it = blobList.begin(); it != blobList.end(); it++) { CvRect rect = VisionUtils::ToCvRect(*it); VisionUtils::MakeSquare(rect); double diagonal = sqrt(rect.width * rect.width + rect.height * rect.height); cvDrawCircle(circleMask,cvPoint(rect.x+rect.width/2.,rect.y+rect.height/2),diagonal/2.5,CV_RGB(255,255,255),CV_FILLED); avgColor = cvAvg (hist,circleMask); if((bestColor < 0 || bestColor < avgColor.val[0]) && avgColor.val[0] > mSegment2Threshold) { bestDiag = diagonal; bestColor = avgColor.val[0]; bestRect = rect; cvCopy(circleMask,imgOut); } //cvMinMaxLoc(imgIn,) cvZero(circleMask); } } if(debugOut && bestColor > 0) { s.clear(); s << "bestColor(" << bestColor << ") " << mType; cvPutText(debugOut,s.str().c_str(),cvPoint(bestRect.x+bestRect.width/2.,bestRect.y+bestRect.height/2),&font,CV_RGB(255,255,255)); cvDrawCircle(debugOut,cvPoint(bestRect.x+bestRect.width/2.,bestRect.y+bestRect.height/2),bestDiag/2.5,CV_RGB(255,255,255)); } // cvShowImage("best",bestMask); // cvWaitKey(0); //VisionUtils::ClearEdges(imgOut); cvReleaseImage(&scratch); cvReleaseImage(&src); cvReleaseImage(&hist); cvReleaseImage(&circleMask); cvReleaseMemStorage( &storage ); return imgOut; }
void cv_AbsDiffS(const CvArr* src, CvArr* dst, CvScalar* value) { cvAbsDiffS(src, dst, *value); }
/*! //void shadowDetection(IplImage *src, IplImage *background,IplImage *foregroundSelection) // \link shadowDetection \endlink //Metodo per il detecting delle ombre (HSV space) // @param[in] src The source image for shadow detection // @param[in] background The background image // @param[in] foreground selection The mask (binary image) that limit the area of the shadow detection process // @param[out] result a mask that rappresent the shadow */ void shadowDetection(IplImage *src, IplImage *background,IplImage *foregroundSelection,IplImage *result,initializationParams initPar){ IplImage *temp,*Dbkg; CvScalar MED1,MED2,MAD,MED; float K = initPar.K; double alfa = initPar.alfa; double beta = initPar.beta; double Th = initPar.Th; double Ts = initPar.Ts; int Delta = initPar.Delta; LOG4CXX_DEBUG(loggershadowDetection, "Shadow Detection started...."); try{ IplImage *hsv; IplImage *H=cvCreateImage(cvGetSize(src), src->depth,1 ); IplImage *S=cvCreateImage(cvGetSize(src), src->depth,1 ); IplImage *V=cvCreateImage(cvGetSize(src), src->depth,1 ); IplImage *bH=cvCreateImage(cvGetSize(src), src->depth,1 ); IplImage *bS=cvCreateImage(cvGetSize(src), src->depth,1 ); IplImage *bV=cvCreateImage(cvGetSize(src), src->depth,1 ); cvZero(result); cvZero(H); cvZero(S); cvZero(V); cvZero(bH); cvZero(bS); cvZero(bV); hsv=cvCloneImage(src); cvCvtColor(src,hsv,CV_BGR2HSV); cvCvtColor(background,background,CV_BGR2HSV); LOG4CXX_DEBUG(loggershadowDetection, "Conversion to HSV"); //cvCvtColor(dst,dst,CV_BGR2HSV); if(alfa == -1 || beta ==-1 || Th == -1 || Ts == -1){ IplImage *temp,*Dbkg; Dbkg=cvCloneImage(hsv); cvZero(Dbkg); cvAbsDiff(hsv,background,Dbkg); CvScalar MED = cvAvg(Dbkg,foregroundSelection); temp=cvCloneImage(hsv); cvAbsDiffS(Dbkg,temp,MED); CvScalar MAD = cvAvg(temp,foregroundSelection); CvScalar MED1 = cvAvg(hsv,foregroundSelection); CvScalar MED2 = cvAvg(background,foregroundSelection); double med=((MED.val[2]+3*1.4826*MAD.val[2])/MED2.val[2]); if(alfa == -1) //alfa=med-K; alfa = MED1.val[2]/MED2.val[2]; if(beta == -1) beta = med; if(Th == -1) Th = (MED.val[0]+MAD.val[0])/2; if(Ts == -1) Ts = (MED.val[1]+MAD.val[1])/2; cvReleaseImage(&Dbkg); cvReleaseImage(&temp); } LOG4CXX_DEBUG(loggershadowDetection, "Param Define conclused"); cvSplit( hsv, H, S, V, 0); cvSplit( background, bH, bS, bV, 0); uchar* dataFs = (uchar *)foregroundSelection->imageData; int stepFs = foregroundSelection->widthStep/sizeof(uchar); uchar* data = (uchar *)result->imageData; int step = result->widthStep/sizeof(uchar); uchar* dataH = (uchar *)H->imageData; int stepH = H->widthStep/sizeof(uchar); uchar* dataS = (uchar *)S->imageData; int stepS = S->widthStep/sizeof(uchar); uchar* dataV = (uchar *)V->imageData; int stepV = V->widthStep/sizeof(uchar); uchar* databH = (uchar *)bH->imageData; int stepbH = bH->widthStep/sizeof(uchar); uchar* databS = (uchar *)bS->imageData; int stepbS = bS->widthStep/sizeof(uchar); uchar* databV = (uchar *)bV->imageData; int stepbV = bV->widthStep/sizeof(uchar); for(int i=0; i<hsv->height;i++){ for(int j=0; j<hsv->width;j++){ if((float)dataFs[i*stepFs+j] == 255){ if( ((float)dataV[i*stepV+j]/(float)databV[i*stepbV+j]>=alfa) && ((float)dataV[i*stepV+j]/(float)databV[i*stepbV+j]<=beta) && (fabsf((float)dataH[i*stepH+j]-(float)databH[i*stepbH+j])<=Th) && (((float)dataS[i*stepS+j]-(float)databS[i*stepbS+j])<=Ts) ){ data[i*step+j] = 255; }else data[i*step+j] = 0; } } } cvCvtColor(background,background,CV_HSV2BGR); cvReleaseImage(&hsv); cvReleaseImage(&H); cvReleaseImage(&S); cvReleaseImage(&V); cvReleaseImage(&bH); cvReleaseImage(&bS); cvReleaseImage(&bV); LOG4CXX_DEBUG(loggershadowDetection, "shadow detection and memory release conclused"); } catch(exception& e){ LOG4CXX_ERROR(loggershadowDetection,e.what()); throw e.what(); } }
void cvAbsDiffS_glue(const CvArr *src, CvArr *dest, CV_SCALAR_DECL(value)) { cvAbsDiffS(src, dest, CV_SCALAR(value)); }