From: Dominic L. <ma...@us...> - 2005-06-08 13:46:01
|
Update of /cvsroot/robotflow/RobotFlow/Vision/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9322 Modified Files: CvBiModalTest.cc Log Message: added threshold output Index: CvBiModalTest.cc =================================================================== RCS file: /cvsroot/robotflow/RobotFlow/Vision/src/CvBiModalTest.cc,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** CvBiModalTest.cc 17 May 2005 18:18:28 -0000 1.3 --- CvBiModalTest.cc 8 Jun 2005 13:45:47 -0000 1.4 *************** *** 51,56 **** --- 51,62 ---- * @output_description binarized image * + * @output_name THRESHOLD + * @output_type float + * @output_description Binarization threshold + * END*/ + #define TILE_SIZE 8 + class CvBiModalTest : public BufferedNode { *************** *** 62,69 **** --- 68,77 ---- IplImage *m_gray; IplImage *m_binarized; + IplImage *m_background; int m_imageInID; int m_imageOutID; int m_binImageOutID; + int m_binThresholdOutID; double m_minDist; double m_maxStd; *************** *** 77,81 **** m_imageOutID = addOutput("CONTOUR_IMG"); m_binImageOutID = addOutput("BINARIZED_IMG"); ! m_minDist = dereference_cast<float>(parameters.get("MIN_DIST")); m_maxStd = dereference_cast<float>(parameters.get("MAX_STD")); --- 85,89 ---- m_imageOutID = addOutput("CONTOUR_IMG"); m_binImageOutID = addOutput("BINARIZED_IMG"); ! m_binThresholdOutID = addOutput("THRESHOLD"); m_minDist = dereference_cast<float>(parameters.get("MIN_DIST")); m_maxStd = dereference_cast<float>(parameters.get("MAX_STD")); *************** *** 89,92 **** --- 97,101 ---- m_gray = cvCreateImage( cvSize(320,240), IPL_DEPTH_8U, 1 ); m_binarized = cvCreateImage( cvSize(320,240), IPL_DEPTH_8U, 1 ); + m_background = cvCreateImage( cvSize(320,240), IPL_DEPTH_8U, 1 ); } *************** *** 98,101 **** --- 107,111 ---- cvReleaseImage(&m_gray); cvReleaseImage(&m_binarized); + cvReleaseImage(&m_background); } *************** *** 108,111 **** --- 118,181 ---- + void processBackground(CvRect rect, IplImage *src, IplImage *dest, double std_max) { + + cvSetImageROI(src,rect); + cvSetImageROI(dest,rect); + + CvScalar mean0; + CvScalar stddev0; + + //Get Mean, std dev + cvAvgSdv(src,&mean0,&stddev0); + + double std2sum = sqrt(stddev0.val[0] * stddev0.val[0]+ + stddev0.val[1] * stddev0.val[1] + + stddev0.val[2] * stddev0.val[2]); + + unsigned char color = 0; + + if (stddev0.val[0] < std_max && + stddev0.val[1] < std_max && + stddev0.val[2] < std_max) { + color = 0xFF; + } + else { + color = 0x00; + } + + //draw on the image the background + for (int row = rect.y; row < rect.y + rect.height; row++) { + + char *basePtr = &dest->imageData[(row * dest->widthStep) + (rect.x * dest->nChannels)]; + char *imgPtr = basePtr; + + for (; imgPtr < basePtr + (dest->nChannels * rect.width); imgPtr += dest->nChannels) { + *imgPtr = color; + } + } + + } + + bool insideBackgroundContour(CvSeq *backgroundContour, CvRect rect) { + + for( ; backgroundContour != 0; backgroundContour = backgroundContour->h_next ) { + + CvRect backgroundRect = cvBoundingRect(backgroundContour,0); + + if ((rect.x >= backgroundRect.x) && + (rect.y >= backgroundRect.y) && + ((rect.x + rect.width) <= (backgroundRect.x + backgroundRect.width)) && + ((rect.y + rect.height) <= (backgroundRect.y + backgroundRect.height))) { + return true; + } + + } + + + return false; + + } + + bool processRect(CvRect rect, IplImage *src, IplImage *dest, double std_max, double min_dist, double *threshold = 0) { *************** *** 128,133 **** --- 198,212 ---- cvAvgSdv(src,&mean0,&stddev0); + /* + double test = sqrt(stddev0.val[0] * stddev0.val[0] + + stddev0.val[1] * stddev0.val[1] + + stddev0.val[2] * stddev0.val[2]); + + if (test > std_max) return true; + else return false; + */ + //cerr<<"CV made (mean0): "<<mean0.val[0]<<" "<<mean0.val[1]<<" "<<mean0.val[2]<<endl; //cerr<<"CV made (stddev0): "<<stddev0.val[0]<<" "<<stddev0.val[1]<<" "<<stddev0.val[2]<<endl; *************** *** 242,255 **** } ! ! ! //TEST CONDITIONS ! if (mean_dist > min_dist && ! (stddev1.val[0] < mean_dist / std_max) && (stddev1.val[1] < mean_dist / std_max) && (stddev1.val[2] < mean_dist / std_max) && (stddev2.val[0] < mean_dist / std_max) && (stddev2.val[1] < mean_dist / std_max) && ! (stddev2.val[2] < mean_dist / std_max)) { bimodal = true; } --- 321,338 ---- } ! /* ! (stddev1.val[0] < mean_dist / std_max) && (stddev1.val[1] < mean_dist / std_max) && (stddev1.val[2] < mean_dist / std_max) && (stddev2.val[0] < mean_dist / std_max) && (stddev2.val[1] < mean_dist / std_max) && ! (stddev2.val[2] < mean_dist / std_max) ! */ ! /* ! (std1sum < mean_dist / std_max) && ! (std2sum < mean_dist / std_max) ! */ ! //TEST CONDITIONS ! if (mean_dist > min_dist) { bimodal = true; } *************** *** 275,298 **** cvResetImageROI(m_gray); cvResetImageROI(m_binarized); cvCopy(m_src,m_dest,NULL); //clear binarized image ! memset(m_binarized->imageData,0xFF,320*240); struct timeb t1, t2; ftime(&t1); ! //TILE MODE ! /* ! for (int y = 0; y < 240; y+= 16) { ! for (int x = 0; x < 320; x+= 16) { CvRect rect; rect.x = x; rect.y = y; ! rect.width = 16; ! rect.height = 16; ! if (processRect(rect,m_src,m_dest,m_maxStd,m_minDist)) { CvScalar color = CV_RGB(255,0,0); CvPoint p1,p2; --- 358,393 ---- cvResetImageROI(m_gray); cvResetImageROI(m_binarized); + cvResetImageROI(m_background); cvCopy(m_src,m_dest,NULL); //clear binarized image ! memset(m_binarized->imageData,0x80,320*240); struct timeb t1, t2; ftime(&t1); ! cvCvtColor(m_src, m_gray, CV_BGR2GRAY); ! cvCvtColor(m_src, m_background, CV_BGR2GRAY); ! cvSmooth(m_gray,m_gray,CV_GAUSSIAN,3); ! cvSmooth(m_background,m_background,CV_GAUSSIAN,3); ! //TILE MODE FOR BACKGROUND ! CvMemStorage* backgroundStorage = cvCreateMemStorage(0); ! CvSeq* backgroundContour = 0; ! ! for (int y = 0; y < 240; y+= TILE_SIZE) { ! for (int x = 0; x < 320; x+= TILE_SIZE) { CvRect rect; rect.x = x; rect.y = y; ! rect.width = TILE_SIZE; ! rect.height = TILE_SIZE; ! ! processBackground(rect,m_src,m_background,m_maxStd); ! ! /* ! double threshold; ! if (processRect(rect,m_src,m_dest,m_maxStd,m_minDist,&threshold)) { CvScalar color = CV_RGB(255,0,0); CvPoint p1,p2; *************** *** 302,317 **** p2.y = rect.y + rect.height; cvRectangle(m_dest,p1,p2,color); } } } ! */ //EDGE MODE ! cvCvtColor(m_src, m_gray, CV_BGR2GRAY); ! cvSmooth(m_gray,m_gray,CV_GAUSSIAN,3); ! cvCanny(m_gray, m_edges, 128, 255, 3); CvMemStorage* storage = cvCreateMemStorage(0); CvSeq* contour = 0; cvFindContours(m_edges, storage, &contour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE ); --- 397,418 ---- p2.y = rect.y + rect.height; cvRectangle(m_dest,p1,p2,color); + //cerr<<"threshold "<<threshold<<endl; + binarize(rect,m_gray,m_binarized,threshold); } + */ } } ! cvResetImageROI(m_background); ! cvFindContours(m_background, backgroundStorage, ! &backgroundContour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE ); ! //EDGE MODE ! ! cvCanny(m_gray, m_edges, 55, 200, 3); CvMemStorage* storage = cvCreateMemStorage(0); CvSeq* contour = 0; + double threshold; cvFindContours(m_edges, storage, &contour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE ); *************** *** 320,324 **** CvRect rect = cvBoundingRect(contour,0); ! double threshold; if (processRect(rect,m_src,m_dest,m_maxStd,m_minDist, &threshold)) { --- 421,428 ---- CvRect rect = cvBoundingRect(contour,0); ! ! ! if (!insideBackgroundContour(backgroundContour,rect)) continue; ! if (processRect(rect,m_src,m_dest,m_maxStd,m_minDist, &threshold)) { *************** *** 335,339 **** cvRectangle(m_dest,p1,p2,color); ! //void binarize(CvRect rect, IplImage *src, IplImage *dest, double threshold) { binarize(rect,m_gray,m_binarized,threshold); } --- 439,443 ---- cvRectangle(m_dest,p1,p2,color); ! binarize(rect,m_gray,m_binarized,threshold); } *************** *** 344,347 **** --- 448,454 ---- + cvReleaseMemStorage(&backgroundStorage); + + ftime(&t2); double timeDiff=(t2.time-t1.time)+((t2.millitm-t1.millitm)/1000.0); *************** *** 357,363 **** ! memcpy(m_binImg->get_data(), m_binarized->imageData,m_binImg->get_size()); (*outputs[m_binImageOutID].buffer)[count] = ObjectRef(m_binImg); } catch (BaseException *e) { --- 464,473 ---- ! memcpy(m_binImg->get_data(), m_background->imageData,m_binImg->get_size()); (*outputs[m_binImageOutID].buffer)[count] = ObjectRef(m_binImg); + (*outputs[m_binThresholdOutID].buffer)[count] = ObjectRef(Float::alloc(threshold / 255.0)); + + } catch (BaseException *e) { |