CBlob getNearestBlob(CBlobResult blobs, coord coordinate){ int tot = blobs.GetNumBlobs(); CBlob Blob; float distance[10]; // 10 è il numero massimo di blob trovabile in un video float minimum; coord tempCoord; //Questo ciclo for fa la distanza manhattan tra le coordinate passate e tutti i blob catturati e crea il vettore con tutte le distanze. for (int i=0; i<tot; i++){ Blob = blobs.GetBlob(i); tempCoord.set( (int) Blob.MaxX(), (int) Blob.MinX(), (int) Blob.MaxY(), (int) Blob.MinY()); distance[i] = sqrt((double)(tempCoord.cX - coordinate.cX)*(tempCoord.cX - coordinate.cX) + (tempCoord.cY - coordinate.cY)*(tempCoord.cY - coordinate.cY)); } int minDistanceId=0; //Questo ciclo for becca la minima distanza fra tutte quelle calcolate for (int j=0; j<tot; j++){ minimum = min( distance[j], distance[minDistanceId]); if ( distance[j] == minimum ) minDistanceId = j; } //Ottenuta la minima distanza si va a ritornare il Blob corrispondente Blob = blobs.GetBlob( minDistanceId ); //delete[] distance; return Blob; }
/* Detect blobs larger than min_size in a given IplImage. */ CBlobResult MarkerCapture::detect_blobs(IplImage *img, int min_size = 10){ // find white blobs in thresholded image CBlobResult blobs = CBlobResult(img, NULL, 0); // exclude ones smaller than min_size. blobs.Filter(blobs, B_EXCLUDE, CBlobGetArea(), B_LESS, min_size); return blobs; }
void ScheinrieseApp::findBlobs() { CBlobResult blobs; int i; CBlob *currentBlob; IplImage *original, *originalThr; // load an image and threshold it original = cvLoadImage("pic1.png", 0); cvThreshold( original, originalThr, 100, 0, 255, CV_THRESH_BINARY ); // find non-white blobs in thresholded image blobs = CBlobResult( originalThr, NULL, 255 ); // exclude the ones smaller than param2 value blobs.Filter( blobs, B_EXCLUDE, CBlobGetArea(), B_LESS, param2 ); // get mean gray color of biggest blob CBlob biggestBlob; CBlobGetMean getMeanColor( original ); double meanGray; blobs.GetNth( CBlobGetArea(), 0, biggestBlob ); meanGray = getMeanColor( biggestBlob ); // display filtered blobs cvMerge( originalThr, originalThr, originalThr, NULL, displayedImage ); for (i = 0; i < blobs.GetNumBlobs(); i++ ) { currentBlob = blobs.GetBlob(i); currentBlob->FillBlob( displayedImage, CV_RGB(255,0,0)); } }
void ForegroundDetector::nextIteration(const Mat &img) { if(bgImg.empty()) { return; } Mat absImg = Mat(img.cols, img.rows, img.type()); Mat threshImg = Mat(img.cols, img.rows, img.type()); absdiff(bgImg, img, absImg); threshold(absImg, threshImg, fgThreshold, 255, CV_THRESH_BINARY); IplImage im = (IplImage)threshImg; CBlobResult blobs = CBlobResult(&im, NULL, 0); blobs.Filter(blobs, B_EXCLUDE, CBlobGetArea(), B_LESS, minBlobSize); vector<Rect>* fgList = detectionResult->fgList; fgList->clear(); for(int i = 0; i < blobs.GetNumBlobs(); i++) { CBlob *blob = blobs.GetBlob(i); CvRect rect = blob->GetBoundingBox(); fgList->push_back(rect); } }
void drawInitialBlobs(IplImage * tmp_frame, CBlobResult blobs){ coord drawCoord; for (int i=0; i<blobs.GetNumBlobs();i++){ //!Creating the coordinate struct drawCoord.set( (int) blobs.GetBlob(i).MaxX(), (int) blobs.GetBlob(i).MinX(), (int) blobs.GetBlob(i).MaxY(), (int) blobs.GetBlob(i).MinY()); drawBlob(tmp_frame, drawCoord, 255, 255, 0); } }
/* Fetch a frame (if available) and process it, calling appropriate callbacks when data becomes available. */ void MarkerCapture::tick(){ IplImage *thresh_frame = NULL; CBlobResult blobs; // Acquire the lock, update the current frame. pthread_mutex_lock(&frame_mutex); current_frame = cvCloneImage(cvQueryFrame(camera)); if(color_acquired && current_frame){ thresh_frame = apply_threshold(current_frame, target_color); }else{ // create a suplicant. thresh_frame = cvCreateImage(cvGetSize(current_frame),IPL_DEPTH_8U,1); } pthread_mutex_unlock(&frame_mutex); // Lock released. Done messing with buffers. if(frame_update_callback){ (*frame_update_callback)(this, current_frame, thresh_frame); } if(color_acquired){ blobs = detect_blobs(thresh_frame, CV_BLOB_SIZE_MIN); if(blobs.GetNumBlobs() >= 2){ // need 2 or more blobs for positional fix. MarkerPositionEstimate position; // fetch the two largest blobs, by area. CBlob blob0, blob1; blobs.GetNthBlob(CBlobGetArea(), 0, blob0); blobs.GetNthBlob(CBlobGetArea(), 1, blob1); // perform positional calculations position.distance = distance(blob0, blob1); position.angle = angle(blob0, blob1); position.blob0_center = blob_center(blob0); position.blob1_center = blob_center(blob1); // call the update handler. if(position_update_callback){ (*position_update_callback)(this, position); } } blobs.ClearBlobs(); } pthread_mutex_lock(&frame_mutex); cvReleaseImage(¤t_frame); cvReleaseImage(&thresh_frame); pthread_mutex_unlock(&frame_mutex); int curr_time = clock(); fps = CLOCKS_PER_SEC/(double)(curr_time - time); time = curr_time; }
vector<Bubble> OMRSheet::getBubbles(int xi1, int yi1, int xi2, int yi2){ vector <Bubble> bubbles; cout<<"Bubble area "<<bubbleArea; int minArea = bubbleArea/2, maxArea = bubbleArea*1.5; CBlobResult blobs = ImageUtils::findBlobs(rawSheet, minArea, maxArea, cvRect(xi1, yi1, xi2-xi1, yi2-yi1)); for (int i = 0; i < blobs.GetNumBlobs(); i++ ) { CvRect rect = blobs.GetBlob(i)->GetBoundingBox(); Point centroid = ImageUtils::findCentroid(rawSheet, &rect); Point centroidMM((centroid.x() - x1)/15, (centroid.y() - y1)/15); Bubble bubble(blobs.GetBlob(i), ¢roidMM, ¢roid); bubbles.push_back(bubble); } return bubbles; }
void extractBots() { //RED TEAM imgTransform(TEAM_R_HUE_U, TEAM_R_HUE_L, TEAM_R_SAT_U, TEAM_R_SAT_L, VAL_U, VAL_L); blobRes = CBlobResult(dst, NULL, 0); blobRes.Filter( blobRes, B_EXCLUDE, CBlobGetArea(), B_LESS, BLOB_SIZE_MIN );// keep blobs larger than BLOB_SIZE_MIN numOfBlobs = blobRes.GetNumBlobs(); cout << numOfBlobs << endl; if(numOfBlobs == 2) { for (int i=0; i<2; i++) blobRes.GetBlob(i) for(int i=0; i<numOfBlobs; i++) blobs[i] = blobRes.GetBlob(i); }; void printBlobs() { CBlobGetXCenter getXC; CBlobGetYCenter getYC; CBlobGetArea getArea; CBlobGetCompactness getCompactness; printf("-----Printng Blobs------\n"); for(int i=0; i<numOfBlobs; i++) { printf("%d\t(%3.2f,%3.2f),%3.2f %3.2f\n", i, getXC(blobs[i]), getYC(blobs[i]), getArea(blobs[i]), getCompactness(blobs[i])); } printf("\n"); cvNamedWindow("old", 1); cvNamedWindow("new", 1); cvMoveWindow("old", 0,0); cvMoveWindow("new", 0,400); cvShowImage("old", img); cvShowImage("new", dst); cvWaitKey(); };
/** - FUNCTION: CBlobResult - FUNCTIONALITY: Copy constructor - PARAMETERS: - source: object to copy - RESULT: - RESTRICTIONS: - AUTHOR: Ricard Borràs - CREATION DATE: 25-05-2005. - MODIFICATION: Date. Author. Description. */ CBlobResult::CBlobResult( const CBlobResult &source ) { m_blobs = blob_vector( source.GetNumBlobs() ); // creem el nou a partir del passat com a paràmetre m_blobs = blob_vector( source.GetNumBlobs() ); // copiem els blobs de l'origen a l'actual blob_vector::const_iterator pBlobsSrc = source.m_blobs.begin(); blob_vector::iterator pBlobsDst = m_blobs.begin(); while( pBlobsSrc != source.m_blobs.end() ) { // no podem cridar a l'operador = ja que blob_vector és un // vector de CBlob*. Per tant, creem un blob nou a partir del // blob original *pBlobsDst = new CBlob(**pBlobsSrc); pBlobsSrc++; pBlobsDst++; } }
void givedepth(IplImage *localimagergb) { IplImage*localimage=cvCreateImage(cvGetSize(localimagergb),IPL_DEPTH_8U,3); cvCvtColor(localimagergb,localimage,CV_BGR2HSV); IplImage *blobbedscaling=cvCreateImage(cvGetSize(localimagergb),IPL_DEPTH_8U,3); uchar *itemp=(uchar *)(localimage->imageData); IplImage *binaryscaling=cvCreateImage(cvGetSize(localimagergb),IPL_DEPTH_8U,1); uchar *itemp1=(uchar *)(binaryscaling ->imageData); for(int i=0;i<hi2->height;i++){ for(int j=0;j<hi2->width;j++){ if((itemp[i*localimage->widthStep+j*localimage->nChannels] <hh) && (itemp[i*localimage->widthStep+j*localimage->nChannels]>hl) && (itemp[i*localimage->widthStep+j*localimage->nChannels+1]<sh) && (itemp[i*localimage->widthStep+j*localimage->nChannels+1]>sl) && ( itemp[i*localimage->widthStep+j*localimage->nChannels+2]<vh) && ( itemp[i*localimage->widthStep+j*localimage->nChannels+2]>vl) //previous 124 ) { itemp1[i*binaryscaling->widthStep+j]=0; //dark regions black rest white } else itemp1[i*binaryscaling->widthStep+j]=255; }} cvErode( binaryscaling, binaryscaling, NULL, 4); cvDilate(binaryscaling, binaryscaling, NULL, 4); CBlobResult blob; CBlob *currentBlob=NULL; blob=CBlobResult(binaryscaling,NULL,255); blob.Filter(blob,B_EXCLUDE,CBlobGetArea(),B_LESS,500); cvMerge(binaryscaling,binaryscaling,binaryscaling,NULL,blobbedscaling); CBlob hand1,hand2; //two blobs,one for each hand blob.GetNthBlob( CBlobGetArea(), 0, (hand2)); blob.GetNthBlob( CBlobGetArea(), 1, (hand1 )); hand1.FillBlob(blobbedscaling,CV_RGB(0,0,255)); //fill the color of blob of hand one with blue hand2.FillBlob(blobbedscaling,CV_RGB(0,255,0)); //fill the color of blob of hand two with green coordinates (blobbedscaling,0); }
CBlobResult getBlobs(IplImage* tmp_frame, IplImage* binFore){ //IplImage* binFore = cvCreateImage(cvGetSize(tmp_frame),IPL_DEPTH_8U,1); //get the binary foreground object //cvSub( getBinaryImage(tmp_frame) , binBack, binFore, NULL ); //if(!cvSaveImage("binFore.jpg",binFore)) printf("Could not save the backgroundimage\n"); //!Starting the extracting of Blob CBlobResult blobs; //! get the blobs from the image, with no mask, using a threshold of 100 blobs = CBlobResult( binFore, NULL, 10, true ); //! Create a file with all the found blob blobs.PrintBlobs( "blobs.txt" ); //! discard the blobs with less area than 60 pixels blobs.Filter( blobs, B_INCLUDE, CBlobGetArea(), B_GREATER, 40); //!This two row of code are to filter the blob find from the library by a bug that match ablob like all the image and return the center of it blobs.Filter( blobs, B_INCLUDE, CBlobGetArea(), B_LESS, (tmp_frame->height)*(tmp_frame->width)*0.8); blobs.Filter( blobs, B_INCLUDE, CBlobGetPerimeter(), B_LESS, (tmp_frame->height)+(tmp_frame->width)*2*0.8); //! Create a file with filtered results blobs.PrintBlobs( "filteredBlobs.txt" ); //return blobs; return blobs; }
// threshold trackbar callback void on_trackbar( int dummy ) { if(!originalThr) { originalThr = cvCreateImage(cvGetSize(original), IPL_DEPTH_8U,1); } if(!displayedImage) { displayedImage = cvCreateImage(cvGetSize(original), IPL_DEPTH_8U,3); } // threshold input image cvThreshold( original, originalThr, param1, 255, CV_THRESH_BINARY ); // get blobs and filter them using its area CBlobResult blobs; int i; CBlob *currentBlob; // find blobs in image blobs = CBlobResult( originalThr, NULL, 255 ); blobs.Filter( blobs, B_EXCLUDE, CBlobGetArea(), B_LESS, param2 ); // display filtered blobs cvMerge( originalThr, originalThr, originalThr, NULL, displayedImage ); for (i = 0; i < blobs.GetNumBlobs(); i++ ) { currentBlob = blobs.GetBlob(i); currentBlob->FillBlob( displayedImage, CV_RGB(255,0,0)); } cvShowImage( wndname, displayedImage ); }
/** - FUNCTION: CBlobResult - FUNCTIONALITY: Copy constructor - PARAMETERS: - source: object to copy - RESULT: - RESTRICTIONS: - AUTHOR: Ricard Borr�s - CREATION DATE: 25-05-2005. - MODIFICATION: Date. Author. Description. */ CBlobResult::CBlobResult( const CBlobResult &source ) { // creem el nou a partir del passat com a par�metre //m_blobs = Blob_vector( source.GetNumBlobs() ); m_blobs.reserve(source.GetNumBlobs()); // copiem els blobs de l'origen a l'actual Blob_vector::const_iterator pBlobsSrc = source.m_blobs.begin(); Blob_vector::iterator pBlobsDst = m_blobs.begin(); while( pBlobsSrc != source.m_blobs.end() ) { // no podem cridar a l'operador = ja que Blob_vector �s un // vector de CBlob*. Per tant, creem un blob nou a partir del // blob original m_blobs.push_back(new CBlob(**pBlobsSrc)); pBlobsSrc++; } }
void extractBall() { imgTransform(BALL_HUE_U, BALL_HUE_L, BALL_SAT_U, BALL_SAT_L, VAL_U, VAL_L); blobRes = CBlobResult(dst, NULL, 0); blobRes.Filter( blobRes, B_EXCLUDE, CBlobGetArea(), B_LESS, BLOB_SIZE_MIN );// keep blobs larger than BLOB_SIZE_MIN numOfBlobs = blobRes.GetNumBlobs(); cout << numOfBlobs << endl; blobRes.Filter( blobRes, B_EXCLUDE, CBlobGetArea(), B_GREATER, BALL_SIZE_MAX );// keep blobs smaller than BALL_SIZE_MAX numOfBlobs = blobRes.GetNumBlobs(); cout << numOfBlobs << endl; blobRes.Filter( blobRes, B_INCLUDE, CBlobGetCompactness(), B_GREATER, BALL_COMPACTNESS );// keep blobs greater than BALL_COMPACTNESS numOfBlobs = blobRes.GetNumBlobs(); cout << numOfBlobs << endl; for(int i=0; i<numOfBlobs; i++) blobs[i] = blobRes.GetBlob(i); };
coord extractBlob(CBlobResult blobs, coord selectedCoord){ coord coordinate; CBlob Blob; if ( blobs.GetNumBlobs()==0 ) { coordinate.flag=false; return coordinate; } else { //!Get the blob info Blob = getNearestBlob( blobs, selectedCoord); //!Creating the coordinate struct coordinate.set( (int) Blob.MaxX(), (int) Blob.MinX(), (int) Blob.MaxY(), (int) Blob.MinY()); return coordinate; } }
CBlobResult computeWhiteMaskOtsu(Mat& imgRGBin, Mat& imgHSVIn, CBlobResult& blobs, int limitRGB, int limitHSV, double RGBratio, double HSVratio, int bmin, int bmax, int i){ waitKey(30); Mat BGRbands[3]; split(imgRGBin,BGRbands); Mat imgHSV; cvtColor(imgHSVIn,imgHSV,CV_BGR2HSV); Mat HSVbands[3]; split(imgHSV,HSVbands); Mat maskHSV, maskRGB, maskT; int otsuTRGB = getThreshVal_Otsu_8u(BGRbands[2]); do{ threshold(BGRbands[2],maskRGB,otsuTRGB,255,THRESH_BINARY); otsuTRGB++; }while(countNonZero(maskRGB)>(RGBratio*limitRGB) & otsuTRGB<=255); int otsuTHSV = getThreshVal_Otsu_8u(HSVbands[1]); do{ threshold(HSVbands[1],maskHSV,otsuTHSV,255,THRESH_BINARY_INV); otsuTHSV--; }while(countNonZero(maskHSV)>(HSVratio*limitHSV) & otsuTHSV>=0); // 0.1 bitwise_or(maskHSV,maskRGB,maskT); int blobSizeBefore = blobs.GetNumBlobs(); blobs = blobs + CBlobResult( maskT ,Mat(),8); blobs.Filter( blobs, B_EXCLUDE, CBlobGetLength(), B_GREATER, bmax ); blobs.Filter( blobs, B_EXCLUDE, CBlobGetLength(), B_LESS, bmin ); int blobSizeAfter = blobs.GetNumBlobs(); Mat newMask(maskT.size(),maskT.type()); newMask.setTo(0); for(;i<blobs.GetNumBlobs();i++){ double area = blobs.GetBlob(i)->Area(); if(area < 5000 && area > 400) blobs.GetBlob(i)->FillBlob(newMask,CV_RGB(255,255,255),0,0,true); } if(countNonZero(maskRGB)>400 && countNonZero(maskHSV)>400 && blobSizeBefore!=blobSizeAfter){ vector<Mat> BGRbands; split(imgRGBin,BGRbands); Mat maskedRGB = applyMaskBandByBand(newMask,BGRbands); bitwise_not(newMask,newMask); split(imgHSVIn,BGRbands); Mat maskedHSV = applyMaskBandByBand(newMask,BGRbands); blobs = computeWhiteMaskOtsu(maskedRGB, maskedHSV, blobs, countNonZero(maskRGB),countNonZero(maskHSV),RGBratio, HSVratio, bmin, bmax, i-1); } return blobs; }
/** - FUNCTION: + operator - FUNCTIONALITY: Joins the blobs in source with the current ones - PARAMETERS: - source: object to copy the blobs - RESULT: - object with the actual blobs and the source blobs - RESTRICTIONS: - AUTHOR: Ricard Borràs - CREATION DATE: 25-05-2005. - MODIFICATION: Date. Author. Description. */ CBlobResult CBlobResult::operator+( const CBlobResult& source ) { //creem el resultat a partir dels blobs actuals CBlobResult resultat( *this ); // reservem memòria per als nous blobs resultat.m_blobs.resize( resultat.GetNumBlobs() + source.GetNumBlobs() ); // declarem els iterador per recòrrer els blobs d'origen i desti blob_vector::const_iterator pBlobsSrc = source.m_blobs.begin(); blob_vector::iterator pBlobsDst = resultat.m_blobs.end(); // insertem els blobs de l'origen a l'actual while( pBlobsSrc != source.m_blobs.end() ) { pBlobsDst--; *pBlobsDst = new CBlob(**pBlobsSrc); pBlobsSrc++; } return resultat; }
void Auvsi_Recognize::extractShape( void ) { typedef cv::Vec<T, 1> VT; // Reduce input to two colors cv::Mat reducedColors = doClustering<T>( _image, 2 ); cv::Mat grayScaled, binary; // Make output grayscale grayScaled = convertToGray( reducedColors ); //cv::cvtColor( reducedColors, grayScaled, CV_RGB2GRAY ); // Make binary double min, max; cv::minMaxLoc( grayScaled, &min, &max ); cv::threshold( grayScaled, binary, min, 1.0, cv::THRESH_BINARY ); // ensure that background is black, image white if( binary.at<VT>(0, 0)[0] > 0.0f ) cv::threshold( grayScaled, binary, min, 1.0, cv::THRESH_BINARY_INV ); binary.convertTo( binary, CV_8U, 255.0f ); // Fill in all black regions smaller than largest black region with white CBlobResult blobs; CBlob * currentBlob; IplImage binaryIpl = binary; blobs = CBlobResult( &binaryIpl, NULL, 255 ); // Get area of biggest blob CBlob biggestBlob; blobs.GetNthBlob( CBlobGetArea(), 0, biggestBlob ); // Remove all blobs of smaller area blobs.Filter( blobs, B_EXCLUDE, CBlobGetArea(), B_GREATER_OR_EQUAL, biggestBlob.Area() ); for (int i = 0; i < blobs.GetNumBlobs(); i++ ) { currentBlob = blobs.GetBlob(i); currentBlob->FillBlob( &binaryIpl, cvScalar(255)); } // Fill in all small white regions black blobs = CBlobResult( &binaryIpl, NULL, 0 ); blobs.GetNthBlob( CBlobGetArea(), 0, biggestBlob ); blobs.Filter( blobs, B_EXCLUDE, CBlobGetArea(), B_GREATER_OR_EQUAL, biggestBlob.Area() ); for (int i = 0; i < blobs.GetNumBlobs(); i++ ) { currentBlob = blobs.GetBlob(i); currentBlob->FillBlob( &binaryIpl, cvScalar(0)); } binary = cv::Scalar(0); biggestBlob.FillBlob( &binaryIpl, cvScalar(255)); _shape = binary; }
// A Simple Camera Capture Framework int main() { CvCapture* capture = cvCaptureFromCAM( 0 ); if( !capture ) { fprintf( stderr, "ERROR: capture is NULL \n" ); return -1; } #ifdef HALF_SIZE_CAPTURE cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, 352/2); cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, 288/2); #endif // Create a window in which the captured images will be presented cvNamedWindow( "Source Image Window", CV_WINDOW_AUTOSIZE ); cvNamedWindow( "Back Projected Image", CV_WINDOW_AUTOSIZE ); cvNamedWindow( "Brightness and Contrast Window", CV_WINDOW_AUTOSIZE ); cvNamedWindow( "Blob Output Window", CV_WINDOW_AUTOSIZE ); cvNamedWindow( "Histogram Window", 0); cvNamedWindow( "Rainbow Window", CV_WINDOW_AUTOSIZE ); // Capture one frame to get image attributes: source_frame = cvQueryFrame( capture ); if( !source_frame ) { fprintf( stderr, "ERROR: frame is null...\n" ); return -1; } cvCreateTrackbar("histogram\nnormalization", "Back Projected Image", &normalization_sum, 6000, NULL); cvCreateTrackbar("brightness", "Brightness and Contrast Window", &_brightness, 200, NULL); cvCreateTrackbar("contrast", "Brightness and Contrast Window", &_contrast, 200, NULL); cvCreateTrackbar("threshold", "Blob Output Window", &blob_extraction_threshold, 255, NULL); cvCreateTrackbar("min blob size", "Blob Output Window", &min_blob_size, 2000, NULL); cvCreateTrackbar("max blob size", "Blob Output Window", &max_blob_size, source_frame->width*source_frame->height/4, NULL); inputImage = cvCreateImage(cvGetSize(source_frame), IPL_DEPTH_8U, 1); histAdjustedImage = cvCreateImage(cvGetSize(source_frame), IPL_DEPTH_8U, 1); outputImage = cvCreateImage(cvGetSize(source_frame), IPL_DEPTH_8U, 3 ); hist_image = cvCreateImage(cvSize(320,200), 8, 1); rainbowImage = cvCreateImage(cvGetSize(source_frame), IPL_DEPTH_8U, 3 ); // object that will contain blobs of inputImage CBlobResult blobs; CBlob my_enumerated_blob; cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX|CV_FONT_ITALIC, hScale, vScale, 0, lineWidth); // Some brightness/contrast stuff: bright_cont_image = cvCloneImage(inputImage); lut_mat = cvCreateMatHeader( 1, 256, CV_8UC1 ); cvSetData( lut_mat, lut, 0 ); while( 1 ) { // Get one frame source_frame = cvQueryFrame( capture ); if( !source_frame ) { fprintf( stderr, "ERROR: frame is null...\n" ); getchar(); break; } cvShowImage( "Source Image Window", source_frame ); // Do not release the frame! cvCvtColor(source_frame, inputImage, CV_RGB2GRAY); // Histogram Stuff! my_hist = cvCreateHist(1, hist_size_array, CV_HIST_ARRAY, ranges, 1); cvCalcHist( &inputImage, my_hist, 0, NULL ); cvNormalizeHist(my_hist, normalization_sum); // NOTE: First argument MUST have an ampersand, or a segmentation fault will result cvCalcBackProject(&inputImage, histAdjustedImage, my_hist); // Histogram Picture int bin_w; float max_value = 0; cvGetMinMaxHistValue( my_hist, 0, &max_value, 0, 0 ); cvScale( my_hist->bins, my_hist->bins, ((double)hist_image->height)/max_value, 0 ); cvSet( hist_image, cvScalarAll(255), 0 ); bin_w = cvRound((double)hist_image->width/hist_size); for(int i = 0; i < hist_size; i++ ) cvRectangle( hist_image, cvPoint(i*bin_w, hist_image->height), cvPoint((i+1)*bin_w, hist_image->height - cvRound(cvGetReal1D(my_hist->bins,i))), cvScalarAll(0), -1, 8, 0 ); cvShowImage( "Histogram Window", hist_image ); cvShowImage("Back Projected Image", histAdjustedImage); // Brightness/contrast loop stuff: int brightness = _brightness - 100; int contrast = _contrast - 100; /* * The algorithm is by Werner D. Streidt * (http://visca.com/ffactory/archives/5-99/msg00021.html) */ if( contrast > 0 ) { double delta = 127.*contrast/100; double a = 255./(255. - delta*2); double b = a*(brightness - delta); for(int i = 0; i < 256; i++ ) { int v = cvRound(a*i + b); if( v < 0 ) v = 0; if( v > 255 ) v = 255; lut[i] = (uchar)v; } } else { double delta = -128.*contrast/100; double a = (256.-delta*2)/255.; double b = a*brightness + delta; for(int i = 0; i < 256; i++ ) { int v = cvRound(a*i + b); if( v < 0 ) v = 0; if( v > 255 ) v = 255; lut[i] = (uchar)v; } } cvLUT( inputImage, bright_cont_image, lut_mat ); cvShowImage( "Brightness and Contrast Window", bright_cont_image); // --------------- // Blob Manipulation Code begins here: // Extract the blobs using a threshold of 100 in the image blobs = CBlobResult( bright_cont_image, NULL, blob_extraction_threshold, true ); // discard the blobs with less area than 5000 pixels // ( the criteria to filter can be any class derived from COperadorBlob ) blobs.Filter( blobs, B_INCLUDE, CBlobGetArea(), B_GREATER_OR_EQUAL, min_blob_size); blobs.Filter( blobs, B_EXCLUDE, CBlobGetArea(), B_GREATER, max_blob_size); // build an output image equal to the input but with 3 channels (to draw the coloured blobs) cvMerge( bright_cont_image, bright_cont_image, bright_cont_image, NULL, outputImage ); // plot the selected blobs in a output image for (int i=0; i < blobs.GetNumBlobs(); i++) { blobs.GetNthBlob( CBlobGetArea(), i, my_enumerated_blob ); // Color 5/6 of the color wheel (300 degrees) my_enumerated_blob.FillBlob( outputImage, cv_hsv2rgb((float)i/blobs.GetNumBlobs() * 300, 1, 1)); } // END Blob Manipulation Code // --------------- sprintf(str, "Count: %d", blobs.GetNumBlobs()); cvPutText(outputImage, str, cvPoint(50, 25), &font, cvScalar(255,0,255)); cvShowImage("Blob Output Window", outputImage); /* // Rainbow manipulation: for (int i=0; i < CV_CAP_PROP_FRAME_WIDTH; i++) { for (int j=0; j < CV_CAP_PROP_FRAME_HEIGHT; j++) { // This line is not figure out yet... // pixel_color_set = ((uchar*)(rainbowImage->imageData + rainbowImage->widthStep * j))[i * 3] ((uchar*)(rainbowImage->imageData + rainbowImage->widthStep * j))[i * 3] = 30; ((uchar*)(rainbowImage->imageData + rainbowImage->widthStep * j))[i * 3 + 1] = 30; ((uchar*)(rainbowImage->imageData + rainbowImage->widthStep * j))[i * 3 + 2] = 30; } } cvShowImage("Rainbow Window", rainbowImage); */ //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version), //remove higher bits using AND operator if( (cvWaitKey(10) & 255) == 27 ) break; } cvReleaseImage(&inputImage); cvReleaseImage(&histAdjustedImage); cvReleaseImage(&hist_image); cvReleaseImage(&bright_cont_image); cvReleaseImage(&outputImage); cvReleaseImage(&rainbowImage); // Release the capture device housekeeping cvReleaseCapture( &capture ); cvDestroyAllWindows(); return 0; }
void detect2(Mat img, vector<Mat>& regionsOfInterest,vector<Blob>& blobs){ /* Mat blurred; GaussianBlur(img, blurred, Size(), _SharpSigma, _SharpSigma); Mat lowContrastMask = abs(img - blurred) < _SharpThreshold; Mat sharpened = img*(1+_SharpAmount) + blurred*(-_SharpAmount); img.copyTo(sharpened, lowContrastMask); sharpened.copyTo(img);*/ /*************INIZIALIZZAZIONI**********/ Mat gray; Mat out = Mat::zeros(Size(WIDTH,HEIGH), CV_8U); Mat masked = Mat::zeros(Size(WIDTH,HEIGH), CV_8U); Mat morph = Mat::zeros(Size(WIDTH,HEIGH), CV_8U); Mat bwmorph = Mat::zeros(Size(WIDTH,HEIGH), CV_8U); Mat cont = Mat::zeros(Size(WIDTH,HEIGH), CV_8U); Mat maskHSV = Mat::zeros(Size(WIDTH,HEIGH), CV_8U); Mat whiteMaskMasked = Mat::zeros(Size(WIDTH,HEIGH), CV_8U); Mat whiteMaskOrig = Mat::zeros(Size(WIDTH,HEIGH), CV_8U); Mat Bands[3]; Mat noBackMask = Mat::zeros(Size(WIDTH,HEIGH), CV_8U); Mat kernelEr = getStructuringElement(MORPH_ELLIPSE,Size(5,5)); Mat thMasked; Mat thOrig; Mat bwOrig; Mat bwNoBackMask; Mat kernelOp = getStructuringElement(MORPH_ELLIPSE,Size(13,13)); vector<Mat> BGRbands; split(img,BGRbands); vector< vector<Point> > contours; /***************************************/ /*cvtColor(img,gray,CV_BGR2GRAY); gray = (gray!=0); imshow("gray",gray);*/ /*Rimozione Ombre e Background*/ // masked = applyMaskBandByBand(maskHSV,BGRbands); split(masked,BGRbands); /*Rimozione sfondo e sogliatura per videnziare esclusivamente ciò che è bianco*/ noBackMask = backgroundRemoval(img); masked = applyMaskBandByBand(noBackMask,BGRbands); /* whiteMaskOrig = computeWhiteMaskLight(img); whiteMaskOrig = whiteMaskOrig + computeWhiteMaskShadow(img); whiteMaskMasked = computeWhiteMaskLight(masked); whiteMaskMasked = whiteMaskMasked + computeWhiteMaskShadow(masked); */ CBlobResult blobsRs; blobsRs = computeWhiteMaskOtsu(img, img, blobsRs, img.rows*img.cols, img.rows*img.cols, 0.8, 0.8, 30, 200, 0); //Mat newimg(img.size(),img.type()); whiteMaskOrig.setTo(0); for(int i=0;i<blobsRs.GetNumBlobs();i++){ blobsRs.GetBlob(i)->FillBlob(whiteMaskOrig,CV_RGB(255,255,255),0,0,true); } threshold(masked,whiteMaskMasked,0,255,THRESH_BINARY); cvtColor(whiteMaskMasked,whiteMaskMasked,CV_BGR2GRAY); cout << whiteMaskMasked.type() << " " << whiteMaskOrig.type() << endl; bitwise_or(whiteMaskMasked,whiteMaskOrig,thOrig); masked = applyMaskBandByBand(thOrig,BGRbands); #if DO_MORPH /*Operazioni morfologiche per poter riempire i buchi e rimuovere i bordi frastagliati*/ dilate(masked,morph,kernelEr); erode(morph,morph,kernelEr); erode(morph,morph,kernelOp); dilate(morph,morph,kernelOp); #else morph = masked; #endif /*Ricerca componenti connesse e rimozione in base all'area*/ cvtColor(morph,bwmorph,CV_BGR2GRAY); findContours(bwmorph, contours, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE); vector<double> areas = computeArea(contours); for(int j = areas.size()-1; j>=0; j--){ if(areas.at(j)>MAX_AREA || areas.at(j)<MIN_AREA ) contours.erase(contours.begin()+j); } /*Calcolo Bounding Rectangle a partire dall'immagine con componenti connesse di interesse*/ vector<Rect> boundRect( contours.size() ); vector<vector<Point> > contours_poly( contours.size() ); vector<Point2f>center( contours.size() ); vector<float>radius( contours.size() ); /*Costruzione immagine finale ed estrazione regioni di interesse*/ for (int idx = 0; idx < contours.size(); idx++){ Blob b; b.originalImage = &img; Scalar color(255); approxPolyDP( Mat(contours[idx]), contours_poly[idx], 3, true ); boundRect[idx] = boundingRect( Mat(contours_poly[idx]) ); minEnclosingCircle( (Mat)contours_poly[idx], center[idx], radius[idx] ); // Rect tmpRect(center[idx].x-boundRect[idx].width/2,center[idx].y-boundRect[idx].height/2,boundRect[idx].width,boundRect[idx].height); Rect tmpRect(center[idx].x-radius[idx],center[idx].y-radius[idx],radius[idx]*2,radius[idx]*2); //Rect tmpRect = boundRect[idx]; Rect toPrint; tmpRect += Size(tmpRect.width*RECT_AUGMENT ,tmpRect.height*RECT_AUGMENT); // Aumenta area di RECT_ARGUMENT tmpRect -= Point((tmpRect.width*RECT_AUGMENT)/2 , (tmpRect.height*RECT_AUGMENT)/2 ); // Ricentra il rettangolo drawContours(cont, contours, idx, color, CV_FILLED, 8); if(tmpRect.x>0 && tmpRect.y>0 && tmpRect.x+tmpRect.width < morph.cols && tmpRect.y+tmpRect.height < morph.rows){ //Se il nuovo rettangolo allargato // NON esce fuori dall'immagine, accettalo regionsOfInterest.push_back(masked(tmpRect)); b.cuttedWithBack = img(tmpRect); b.cuttedImages = masked(tmpRect); b.blobsImage = cont(tmpRect); b.rectangles = tmpRect; toPrint = tmpRect; } else{ toPrint = boundRect[idx]; regionsOfInterest.push_back(masked(boundRect[idx])); b.cuttedImages = masked(boundRect[idx]); b.cuttedWithBack = img(boundRect[idx]); b.rectangles = boundRect[idx]; b.blobsImage = cont(boundRect[idx]); } Point centroid = computeCentroid(contours[idx]); b.centroid = centroid; b.area = contourArea(contours[idx]); b.distance = HEIGH - centroid.y; /*rectangle( cont, toPrint.tl(), toPrint.br(), color, 2, 8, 0 ); circle( cont, center[idx], (int)radius[idx], color, 2, 8, 0 );*/ blobs.push_back(b); } //out = out+cont; bitwise_xor(out,cont,out); /*imshow("img",img); imshow("out",out); waitKey(0);*/ }
void TabletopDetector::pcCallback(sensor_msgs::PointCloud2::ConstPtr pc_msg) { if(!pc_lock.try_lock()) return; pcl::PointCloud<pcl::PointXYZRGB> pc_full, pc_full_frame; pcl::fromROSMsg(*pc_msg, pc_full); string base_frame("/base_link"); ros::Time now = ros::Time::now(); tf_listener.waitForTransform(pc_msg->header.frame_id, base_frame, now, ros::Duration(3.0)); pcl_ros::transformPointCloud(base_frame, pc_full, pc_full_frame, tf_listener); // pc_full_frame is in torso lift frame cv::Mat cur_height_img = cv::Mat::zeros(imgx, imgy, CV_8U); BOOST_FOREACH(const pcl::PointXYZRGB& pt, pc_full_frame.points) { if(pt.x != pt.x || pt.y != pt.y || pt.z != pt.z) continue; int32_t x, y, z; x = (pt.x - minx)/(maxx-minx) * imgx; y = (pt.y - miny)/(maxy-miny) * imgy; z = (pt.z - minz)/(maxz-minz) * 256; if(x < 0 || y < 0) continue; if(x >= imgx || y >= imgy) continue; if(z < 0 || z >= 256) continue; if(cur_height_img.at<uint8_t>(x, y) == 0 || cur_height_img.at<uint8_t>(x, y) < (uint8_t) z) cur_height_img.at<uint8_t>(x, y) = (uint8_t) z; } cv::max(height_img_max, cur_height_img, height_img_max); cv::Mat cur_height_img_flt; cur_height_img.convertTo(cur_height_img_flt, CV_32F); height_img_sum += cur_height_img_flt; cv::Mat cur_count(imgx, imgy, CV_8U); cur_count = (cur_height_img > 0) / 255; cv::Mat cur_count_flt(imgx, imgy, CV_32F); cur_count.convertTo(cur_count_flt, CV_32F); height_img_count += cur_count_flt; cv::Mat height_img_avg_flt = height_img_sum / height_img_count; cv::Mat height_img_avg(imgx, imgy, CV_8U); height_img_avg_flt.convertTo(height_img_avg, CV_8U); height_img_avg = height_img_max; cv::Mat height_hist(256, 1, CV_32F, cv::Scalar(0)); for(uint32_t x=0;x<imgx;x++) for(uint32_t y=0;y<imgy;y++) { if(height_img_avg.at<uint8_t>(x,y) == 255) height_img_avg.at<uint8_t>(x,y) = 0; if(height_img_avg.at<uint8_t>(x,y) != 0) { height_hist.at<float>(height_img_avg.at<uint8_t>(x,y), 0)++; } } ////////////////////// Finding best table height ///////////////////////// uint32_t gfiltlen = 25; float stddev = 256/(maxz-minz) * 0.015; cv::Mat gauss_filt(gfiltlen, 1, CV_32F, cv::Scalar(0)); for(uint32_t i=0;i<gfiltlen;i++) gauss_filt.at<float>(i,0) = 0.39894 / stddev * std::exp(-(i-((float)gfiltlen)/2)*(i-((float)gfiltlen)/2)/(2*stddev*stddev)); //cout << gauss_filt; uint32_t maxval = 0, maxidx = 0; for(uint32_t i=0;i<256-gfiltlen;i++) { uint32_t sum = 0; for(uint32_t j=0;j<gfiltlen;j++) sum += height_hist.at<float>(i+j,0) * gauss_filt.at<float>(j,0); if(sum > maxval && i != 0) { maxval = sum; maxidx = i+gfiltlen/2; } } int32_t table_height = ((int32_t)maxidx); //printf("%d %d, ", maxval, maxidx); /////////////////////////// Getting table binary ///////////////////// cv::Mat height_img_thresh(imgx, imgy, CV_8U); height_img_thresh = height_img_avg.clone(); for(uint32_t x=0;x<imgx;x++) for(uint32_t y=0;y<imgy;y++) { if(std::fabs(table_height - ((int32_t)height_img_thresh.at<uint8_t>(x,y))) < stddev*2) height_img_thresh.at<uint8_t>(x,y) = 255; else height_img_thresh.at<uint8_t>(x,y) = 0; } ////////////////////////////////////////////////////////////////// IplImage height_img_thresh_ipl = height_img_thresh; IplConvKernel* element = cvCreateStructuringElementEx(3, 3, 1, 1, CV_SHAPE_RECT); cvMorphologyEx(&height_img_thresh_ipl, &height_img_thresh_ipl, NULL, element, CV_MOP_CLOSE, num_closes); //cvMorphologyEx(&height_img_thresh, &height_img_thresh, NULL, element, CV_MOP_OPEN, 2); cv::Mat height_img_thresh_blob = height_img_thresh.clone(); IplImage blob_img = height_img_thresh_blob; CBlobResult blobs = CBlobResult(&blob_img, NULL, 0); //blobs.Filter(blobs, B_EXCLUDE, CBlobGetArea(), B_LESS, 10); CBlob biggestblob; blobs.GetNthBlob(CBlobGetArea(), 0, biggestblob); cv::Mat table_blob(imgx, imgy, CV_8U, cv::Scalar(0)); IplImage table_blob_img = table_blob; biggestblob.FillBlob(&table_blob_img, cv::Scalar(150)); //drawCvBox2D(blob_img, table_roi, cv::Scalar(50), 1); CvBox2D table_roi = biggestblob.GetEllipse(); table_roi.angle *= CV_PI/180; cv::Mat table_hull(imgx, imgy, CV_8U, cv::Scalar(0)); IplImage hull_img = table_hull; fillCvBox2D(hull_img, table_roi, cv::Scalar(255)); //printf("Cvbox: %f, %f, %f, %f, %f\n", table_roi.center.x, table_roi.center.y, table_roi.size.width, table_roi.size.height, table_roi.angle); //cv::Mat height_morph(imgx, imgy, CV_8U, cv::Scalar(0)); //cv::Mat tmp_img(imgx, imgy, CV_8U, cv::Scalar(0)); //IplImage t1 = height_img_thresh; IplImage = height_morph; cv::Mat table_edge(imgx, imgy, CV_8U); cv::Sobel(table_blob, table_edge, CV_8U, 0, 1, 1); cv::Mat above_table(imgx, imgy, CV_8U); bitwise_and(height_img_max > table_height + stddev*2, table_hull, above_table); IplImage above_table_img = above_table; CBlobResult obj_blobs = CBlobResult(&above_table_img, NULL, 0); CBlob biggest_obj_blob; double objcentx = 0, objcenty = 0; if(obj_blobs.GetNumBlobs() > 0) { obj_blobs.GetNthBlob(CBlobGetArea(), 0, biggest_obj_blob); CvBox2D obj_box = biggest_obj_blob.GetEllipse(); objcenty = obj_box.center.x, objcentx = obj_box.center.y; } //double objcentx = 0, objcenty = 0; //cv::Mat table_edge = height_morph.clone(); //cvMorphologyEx(&height_img, &height_morph, NULL, element, CV_MOP_CLOSE); //cvFillPoly(&ipl_hull, rpts, npts, 1, cv::Scalar(255)); //cv::Mat obj_img(imgx, imgy, CV_8U, cv::Scalar(0)); //std::vector<int32_t> xfeats, yfeats, zfeats; //double sumobjx = 0, sumobjy = 0, sumobja = 0; //for(uint32_t y=0;y<imgx;y++) // for(uint32_t x=0;x<imgx;x++) // if(table_hull.at<uint8_t>(x,y) == 255 && height_morph.at<uint8_t>(x,y) == 0 // && height_img_avg.at<uint8_t>(x,y) > table_height + stddev*2) { // obj_img.at<uint8_t>(x,y) = height_img_avg.at<uint8_t>(x,y); // sumobjx += x; sumobjy += y; sumobja ++; // //xfeats.push_back(x); yfeats.push_back(y); // //zfeats.push_back(height_img.at<uint8_t>(x,y)); // } //double objcentx = sumobjx/sumobja, objcenty = sumobjy/sumobja; CvMemStorage* storage = cvCreateMemStorage(0); CvSeq* lines = 0; cv::Mat lines_img = height_img_max.clone(); IplImage lines_img_ipl = lines_img; IplImage table_edge_ipl = table_edge; cvMorphologyEx(&table_edge_ipl, &table_edge_ipl, NULL, element, CV_MOP_DILATE, num_edge_dilate); lines = cvHoughLines2(&table_edge_ipl, storage, CV_HOUGH_STANDARD, 1, degree_bins*CV_PI/180, hough_thresh, 0, 0); vector<float> theta_bins, rho_bins; vector<uint32_t> count_bins; for(uint32_t i=0; i < (uint32_t) lines->total; i++) { float* line = (float*)cvGetSeqElem(lines, i); float rho = line[0]; float theta = line[1]; bool found_same = false; for(int32_t j=theta_bins.size()-1; j >= 0; j--) { if(fabs(theta_bins[j]/count_bins[j] - theta) < theta_gran && fabs(rho_bins[j]/count_bins[j] - rho) < rho_gran) { theta_bins[j] += theta; rho_bins[j] += rho; count_bins[j]++; found_same = true; break; } } if(!found_same) { theta_bins.push_back(theta); rho_bins.push_back(rho); count_bins.push_back(1); } double a = cos(theta), b = sin(theta); double x0 = a*rho, y0 = b*rho; CvPoint pt1, pt2; a = cos(theta); b = sin(theta); //a = cos(theta+CV_PI/2); b = sin(theta+CV_PI/2); //x0 = objcenty; y0 = objcentx; pt1.x = cvRound(x0 + 1000*(-b)); pt1.y = cvRound(y0 + 1000*(a)); pt2.x = cvRound(x0 - 1000*(-b)); pt2.y = cvRound(y0 - 1000*(a)); cvLine(&lines_img_ipl, pt1, pt2, cv::Scalar(100), 2, 8 ); } //delete[] lines; for(uint32_t i=0;i<theta_bins.size();i++) { theta_bins[i] /= count_bins[i]; rho_bins[i] /= count_bins[i]; } vector<float> posesx, posesy, poses_theta, dists_obj; vector<uint32_t> pose_inds; for(uint32_t i=0;i<theta_bins.size();i++) { double theta = theta_bins[i]; double rho = rho_bins[i]; double a1 = cos(theta-CV_PI/2), b1 = sin(theta-CV_PI/2); double a2 = cos(theta-CV_PI), b2 = sin(theta-CV_PI); double vvcl = a2*b1-b2*a1, deltpx = cos(theta)*rho-objcenty, deltpy = sin(theta)*rho-objcentx; double pvcr = deltpx*b1 - deltpy*a1; double t = pvcr/vvcl; double posey = objcenty + t*a2, posex = objcentx + t*b2; printf("\naPose %d: (t: %f, %f, %f)[%f, %f](%f, %f)[%f, %f](1 %f, %f)(2 %f, %f)[theta %f, %f]\n", i, t, posex, posey, t*a2, t*b2, a1*rho, b1*rho, objcentx, objcenty, a1, b1, a2, b2, theta, rho); if(posex == posex && posey == posey && posex >= 0 && posey >= 0 && posex < imgx && posey < imgy) { posesx.push_back(posex); posesy.push_back(posey); poses_theta.push_back(theta); pose_inds.push_back(posesx.size()-1); float dist = (posex-objcentx)*(posex-objcentx)+(posey-objcenty)*(posey-objcenty); dists_obj.push_back(dist); } //lines_img.at<uint8_t>(posex, posey) } boost::function<bool(uint32_t, uint32_t)> sortind = boost::bind(&compind, _1, _2, dists_obj); sort(pose_inds.begin(), pose_inds.end(), sortind); vector<float> retposesx, retposesy, retposesr; grasp_points.poses.clear(); for(uint32_t i=0;i<pose_inds.size();i++) { float posex = posesx[pose_inds[i]], posey = posesy[pose_inds[i]]; float poser = -poses_theta[pose_inds[i]] + 3*CV_PI/2; bool same_found = false; for(int32_t j=((int)retposesx.size())-1;j>=0;j--) { if(fabs(posex - retposesx[j]) < xgran && fabs(posey - retposesy[j]) < ygran) { same_found = true; } } if(!same_found) { retposesx.push_back(posex); retposesy.push_back(posey); retposesr.push_back(poser); geometry_msgs::Pose cpose; cpose.position.x = posex/imgx*(maxx-minx) + minx; cpose.position.y = posey/imgy*(maxy-miny) + miny; cpose.position.z = table_height/256.0*(maxz-minz) + minz; btMatrix3x3 quatmat; btQuaternion quat; quatmat.setEulerZYX(poser, 0 , 0); quatmat.getRotation(quat); cpose.orientation.x = quat.x(); cpose.orientation.y = quat.y(); cpose.orientation.z = quat.z(); cpose.orientation.w = quat.w(); grasp_points.poses.push_back(cpose); } } grasp_points.header.stamp = ros::Time::now(); grasp_points.header.frame_id = base_frame; pose_arr_pub.publish(grasp_points); printf("\nCenter (%f, %f)\n", objcentx, objcenty); for(uint32_t i=0;i<retposesx.size();i++) { //for(uint32_t i=0;i<1;i++) { printf("\nPose %d: (%f, %f, r: %f)\n", i, retposesx[i], retposesy[i], retposesr[i]); //CvPoint centerpt; centerpt.x = objcenty; centerpt.y = objcentx; CvPoint centerpt; centerpt.x = retposesy[i]; centerpt.y = retposesx[i]; cvCircle(&lines_img_ipl, centerpt, 3, cv::Scalar(200), 2); } //cv::Mat obj_feats(xfeats.size(), 1, CV_32S, cv::Scalar(0)); //for(uint32_t i=0;i<xfeats.size();i++) { // obj_feats.at<uint32_t>(i,0) = xfeats[i]; obj_feats.at<uint32_t>(i,1) = yfeats[i]; obj_feats.at<uint32_t>(i,2) = zfeats[i]; //} //cvflann::KMeansIndexParams kmips; //kmips.branching = 32; //kmips.iterations = 11; //kmips.centers_init = cvflann::CENTERS_RANDOM; //kmips.cb_index = 0.2; //cv::Mat obj_centers; //CvMat obj_feats_mat = obj_feats; ////cvflann::Matrix<uint32_t> obj_feats_mat; ////cvflann::Matrix<cvflann::EUCLIDEAN> obj_centers_mat; //int num_clust = cvflann::hierarchicalClustering<CV_32S,CV_32S>(obj_feats_mat, obj_centers, kmips); //printf("\nNum clust: %d \n", num_clust); cv::Mat table_edge2 = table_edge.clone(); IplImage table_edge_ipl2 = table_edge2; cvMorphologyEx(&table_edge_ipl2, &table_edge_ipl2, NULL, element, CV_MOP_DILATE, 3); BOOST_FOREACH(const pcl::PointXYZRGB& pt, pc_full_frame.points) { if(pt.x != pt.x || pt.y != pt.y || pt.z != pt.z) continue; int32_t x, y, z; x = (pt.x - minx)/(maxx-minx) * imgx; y = (pt.y - miny)/(maxy-miny) * imgy; z = (pt.z - minz)/(maxz-minz) * 256; if(x < 0 || y < 0) continue; if(x >= imgx || y >= imgy) continue; if(z < 0 || z >= 256) continue; if(table_blob.at<uint8_t>(x,y) == 255 && std::fabs(table_height - z) < stddev*2) { uint32_t red = 0xFFFF0000; ((uint32_t*) &pt.rgb)[0] = red; } if(table_edge2.at<uint8_t>(x,y) == 255 && std::fabs(table_height - z) < stddev*4) { uint32_t blue = 0xFF0000FF; ((uint32_t*) &pt.rgb)[0] = blue; } } cv_bridge::CvImage cvb_height_img; //cvb_height_img.image = height_img_avg; //cvb_height_img.image = height_img_max; //cvb_height_img.image = height_morph; //cvb_height_img.image = obj_img; cvb_height_img.image = lines_img; //cvb_height_img.image = height_img_thresh_blob; //cvb_height_img.image = table_blob; //cvb_height_img.image = height_img_thresh; //cvb_height_img.image = above_table; //cvb_height_img.image = table_edge; cvb_height_img.header.stamp = ros::Time::now(); cvb_height_img.header.frame_id = base_frame; cvb_height_img.encoding = enc::MONO8; height_pub.publish(cvb_height_img.toImageMsg()); pc_full_frame.header.stamp = ros::Time::now(); pc_full_frame.header.frame_id = base_frame; pc_pub.publish(pc_full_frame); if(grasp_points.poses.size() > 0) grasp_points_found = true; //delete element; pc_lock.unlock(); }
void App::Update(Image &camera) { /*camera=camera.Scale(camera.m_Image->width/2, camera.m_Image->height/2); */ //cvFlip(camera.m_Image, NULL, 0); /////////////////////////////////// // dispatch from input int key=cvWaitKey(10); // usleep(500); static int t=150; static bool viewthresh=false; static bool off=false; static int spirit=0; static int crop_x=0; static int crop_y=0; static int crop_w=camera.m_Image->width; static int crop_h=camera.m_Image->height; switch (key) { case 't': viewthresh=!viewthresh; break; case 'q': t--; break; case 'w': t++; break; case 'e': t-=20; break; case 'r': t+=20; break; case 'o': off=!off; break; case 'p': spirit++; break; case 'z': crop_x+=10; break; case 'x': crop_x-=10; break; case 'c': crop_y+=10; break; case 'v': crop_y-=10; break; case 'b': crop_w+=10; break; case 'n': crop_w-=10; break; case 'm': crop_h+=10; break; case ',': crop_h-=10; break; } if (crop_x<0) crop_x=0; if (crop_x>=camera.m_Image->width) crop_x=camera.m_Image->width-1; if (crop_y<0) crop_x=0; if (crop_y>=camera.m_Image->height) crop_y=camera.m_Image->height-1; if (crop_w+crop_x>camera.m_Image->width) { crop_w=camera.m_Image->width-crop_x; } if (crop_h+crop_y>camera.m_Image->height) { crop_h=camera.m_Image->height-crop_y; } if (off) { sleep(1); cerr<<"off..."<<endl; return; } Image thresh=camera.RGB2GRAY().SubImage(crop_x,crop_y,crop_w,crop_h); cvThreshold(thresh.m_Image,thresh.m_Image,t,255,CV_THRESH_BINARY); // copy the threshold into a colour image Image tofill=thresh.GRAY2RGB(); cvFloodFill(tofill.m_Image,cvPoint(camera.m_Image->width/2, camera.m_Image->height/2), CV_RGB(0,255,0),cvScalar(0),cvScalar(255)); CBlobResult blobs; blobs = CBlobResult( thresh.m_Image, NULL, 255 ); // exclude the ones smaller than param2 value blobs.Filter( blobs, B_EXCLUDE, CBlobGetArea(), B_LESS, 10); CBlob *currentBlob; Image *out=NULL; if (key=='s') { // add the alpha channel Image src=camera.SubImage(crop_x,crop_y,crop_w,crop_h); out = new Image(src.m_Image->width, src.m_Image->height, 8, 4); for(int y=0; y<src.m_Image->height; y++) { for(int x=0; x<src.m_Image->width; x++) { CvScalar col = cvGet2D(src.m_Image,y,x); CvScalar alpha = cvGet2D(tofill.m_Image,y,x); if (alpha.val[0]==0 && alpha.val[1]==255 && alpha.val[2]==0) col.val[3]=0; else col.val[3]=255; cvSet2D(out->m_Image,y,x,col); } } } if (key=='s') { cerr<<"deleting old images in islands/"<<endl; int r=system("rm islands/*"); } list<CvRect> allrects; for (int i = 0; i < blobs.GetNumBlobs(); i++ ) { currentBlob = blobs.GetBlob(i); allrects.push_back(currentBlob->GetBoundingBox()); } list<CvRect> filteredrects=allrects; /* for (list<CvRect>::iterator i=allrects.begin(); i!=allrects.end(); ++i) { bool in=false; for (list<CvRect>::iterator j=allrects.begin(); j!=allrects.end(); ++j) { if (Inside(*i,*j)) in=true; } if (!in) filteredrects.push_back(*i); }*/ unsigned int instance = rand(); unsigned int count=0; for (list<CvRect>::iterator i=filteredrects.begin(); i!=filteredrects.end(); ++i) { CvRect rect = *i; if (key=='s') { Image island = out->SubImage(rect.x,rect.y, rect.width,rect.height); char buf[256]; sprintf(buf,"islands/island-%d-%d-%d.png",count, rect.x+rect.width/2, rect.y+rect.height/2); cerr<<"saving "<<buf<<endl; island.Save(buf); sprintf(buf,"dump/island-%d-%d-%d-%d.png", instance, count, rect.x+rect.width/2, rect.y+rect.height/2); cerr<<"saving "<<buf<<endl; island.Save(buf); } else { cvRectangle(camera.m_Image, cvPoint(crop_x+rect.x,crop_y+rect.y), cvPoint(crop_x+rect.x+rect.width, crop_y+rect.y+rect.height), colors[1]); } count++; } if (key=='s') { cerr<<"copying images to server"<<endl; //int r=system("scp -r islands [email protected]:/home/garden/GerminationX/oak/"); string path("/home/dave/code/lirec/scenarios/GerminationX/oak/public/"); path+=string(spirits[spirit%3]); string command=string("rm ")+path+string("/*.*"); int r=system(command.c_str()); string command2=string("cp islands/* ")+path; r=system(command2.c_str()); //cerr<<"finished copying...("<<r<<")"<<endl; } if (viewthresh) camera=tofill; char buf[256]; sprintf(buf,"spirit: %s thresh: %d", spirits[spirit%3], t); cvPutText(camera.m_Image, buf, cvPoint(10,20), &m_Font, colors[0]); cvRectangle(camera.m_Image, cvPoint(crop_x,crop_y), cvPoint(crop_x+crop_w,crop_y+crop_h), colors[2]); if (out!=NULL) delete out; }
// starts the auto targeting sequence void MainWindow::on_startstopbutton_clicked() { shootingstopped=false; QImage* currimage=getQImage(); n=currimage->width(); k=currimage->height(); IplImage* curriplimage=Qimage2IplImage(&currimage->convertToFormat(QImage::Format_RGB32)); IplImage* threshedimage=threshimage(curriplimage); CBlobResult blobs; CBlob* currentblob; blobs=CBlobResult(threshedimage,NULL,0); blobs.Filter( blobs, B_EXCLUDE, CBlobGetArea(), B_LESS, 150 ); int j=blobs.GetNumBlobs(); if(j==0) { QMessageBox::information(this,"No Targets","No Targets Found!"); cvReleaseImage(&threshedimage); cvReleaseImage(&curriplimage); return; } CBlobGetXCenter XCenter; CBlobGetYCenter YCenter; for(int i=0;i<blobs.GetNumBlobs();i++) { tmptargetcenter=new targetcenter; currentblob=blobs.GetBlob(i); tmptargetcenter->x=XCenter(*currentblob); tmptargetcenter->y=YCenter(*currentblob); getangles(tmptargetcenter); targets.append(tmptargetcenter); } checkformissiles(); ui->targetcountdisplay->display(targets.size()); setupautobuttons(); qApp->processEvents(); ui->timeNumber->display(0); timeshooting.start(100); turr->initAngle(); if(shootingstopped) { timeshooting.stop(); targets.clear(); return; } foreach(targetcenter* target,targets) { checkformissiles(); qApp->processEvents(); turr->setAngle(target->beta,target->betav); ui->shotcountdisplay->display(turr->currentmissilecount()); if(shootingstopped) { timeshooting.stop(); targets.clear(); delete target; return; } ui->targetcountdisplay->display(ui->targetcountdisplay->value()-1); qApp->processEvents(); delete target; }
int main(int argc, char *argv[]) { CvCapture* capture = cvCreateFileCapture( "recording_01.avi"); handOrientation rightOrientationLast = NONE, leftOrientationLast = NONE; handOrientation rightOrientationCur = NONE, leftOrientationCur = NONE; //cvNamedWindow("Input Image", CV_WINDOW_AUTOSIZE); //cvNamedWindow("Skin Pixels", CV_WINDOW_AUTOSIZE); cvNamedWindow("Skin Blobs", CV_WINDOW_AUTOSIZE); while(1){ Mat imageBGR = cvQueryFrame(capture); if(imageBGR.empty())break; //imshow("Input Image", imageBGR); // Convert the image to HSV colors. Mat imageHSV = Mat(imageBGR.size(), CV_8UC3); // Full HSV color image. cvtColor(imageBGR, imageHSV, CV_BGR2HSV); // Convert from a BGR to an HSV image. std::vector<Mat> channels(3); split(imageHSV, channels); Mat planeH = channels[0]; Mat planeS = channels[1]; Mat planeV = channels[2]; // Detect which pixels in each of the H, S and V channels are probably skin pixels. threshold(channels[0], channels[0], 150, UCHAR_MAX, CV_THRESH_BINARY_INV);//18 threshold(channels[1], channels[1], 60, UCHAR_MAX, CV_THRESH_BINARY);//50 threshold(channels[2], channels[2], 170, UCHAR_MAX, CV_THRESH_BINARY);//80 // Combine all 3 thresholded color components, so that an output pixel will only // be white if the H, S and V pixels were also white. Mat imageSkinPixels = Mat( imageBGR.size(), CV_8UC3); // Greyscale output image. bitwise_and(channels[0], channels[1], imageSkinPixels); // imageSkin = H {BITWISE_AND} S. bitwise_and(imageSkinPixels, channels[2], imageSkinPixels); // imageSkin = H {BITWISE_AND} S {BITWISE_AND} V. // Show the output image on the screen. //imshow("Skin Pixels", imageSkinPixels); IplImage ipl_imageSkinPixels = imageSkinPixels; // Find blobs in the image. CBlobResult blobs; blobs = CBlobResult(&ipl_imageSkinPixels, NULL, 0); // Use a black background color. // Ignore the blobs whose area is less than minArea. blobs.Filter(blobs, B_EXCLUDE, CBlobGetArea(), B_LESS, minBlobArea); srand (time(NULL)); // Show the large blobs. IplImage* imageSkinBlobs = cvCreateImage(imageBGR.size(), 8, 3); //Colored Output//,1); Greyscale output image. for (int i = 0; i < blobs.GetNumBlobs(); i++) { CBlob *currentBlob = blobs.GetBlob(i); currentBlob->FillBlob(imageSkinBlobs, CV_RGB(rand()%255,rand()%255,rand()%255)); // Draw the large blobs as white. cvDrawRect(imageSkinBlobs, cvPoint(currentBlob->GetBoundingBox().x,currentBlob->GetBoundingBox().y), cvPoint(currentBlob->GetBoundingBox().x + currentBlob->GetBoundingBox().width,currentBlob->GetBoundingBox().y + currentBlob->GetBoundingBox().height), cvScalar(0,0,255), 2);//Draw Bounding Boxes } cvShowImage("Skin Blobs", imageSkinBlobs); //Gestures //std::cout << "Number of Blobs: "<< blobs.GetNumBlobs() <<endl; if(blobs.GetNumBlobs() == 0){ //picture empty }else if(blobs.GetNumBlobs() == 1) { //head detected }else if(blobs.GetNumBlobs() == 2 || blobs.GetNumBlobs() == 3){ //head + one hand || head + two hands CvRect rect[3]; int indexHead = -1, indexHandLeft = -1, indexHandRight = -1; //Get Bounding Boxes for(int i = 0; i< blobs.GetNumBlobs(); i++){ rect[i] = blobs.GetBlob(i)->GetBoundingBox(); } //Detect Head and Hand indexes if(blobs.GetNumBlobs() == 2){ int indexHand = -1; if(getCenterPoint(rect[0]).y < getCenterPoint(rect[1]).y){ indexHead = 0; indexHand = 1; }else{ indexHead = 1; indexHand = 0; } if(getHandside(rect[indexHead], rect[indexHand]) == LEFT){ indexHandLeft = 1; indexHandRight = -1; }else{ // right hand indexHandLeft = -1; indexHandRight = 1; } }else{ //two hands int indexHand1 = -1; int indexHand2 = -1; if(getCenterPoint(rect[0]).y < getCenterPoint(rect[1]).y && getCenterPoint(rect[0]).y < getCenterPoint(rect[2]).y){ indexHead = 0; indexHand1 = 1; indexHand2 = 2; }else if(getCenterPoint(rect[1]).y < getCenterPoint(rect[0]).y && getCenterPoint(rect[1]).y < getCenterPoint(rect[2]).y){ indexHead = 1; indexHand1 = 0; indexHand2 = 2; }else{ indexHead = 2; indexHand1 = 0; indexHand2 = 1; } if(getHandside(rect[indexHead], rect[indexHand1]) == LEFT){ indexHandLeft = indexHand1; indexHandRight = indexHand2; }else{ indexHandLeft = indexHand2; indexHandRight = indexHand1; } } // follow the right hand if(indexHandRight > 0) { //std::cout << "right hand deteced" <<endl; if(isMoving(handRight)) { std::cout << "hand moving" <<endl; handRight.centerPrev = handRight.centerCurr; handRight.centerCurr = getCenterPoint(rect[indexHandRight]); } else { std::cout << "hand not moving" <<endl; if(handRight.centerInit.y != 0 && abs(handRight.centerInit.y - handRight.centerCurr.y) > 20) { if(handRight.centerInit.y < handRight.centerCurr.y) { // hand moved down std::cout << " hand moved down" <<endl; } else { // hand moved up std::cout << " hand moved up" <<endl; } } handRight.centerInit = getCenterPoint(rect[indexHandRight]); handRight.centerPrev = handRight.centerCurr; handRight.centerCurr = getCenterPoint(rect[indexHandRight]); } } //Get Orientations from Hand rects leftOrientationCur = (indexHandLeft != -1)?getOrientationOfRect(rect[indexHandLeft]):NONE; rightOrientationCur = (indexHandRight != -1)?getOrientationOfRect(rect[indexHandRight]):NONE; //Check Change of Left hand /*switch(detectHandStateChange(leftOrientationLast, leftOrientationCur)){ case PORTRAIT_TO_LANDSCAPE: handleGestures(LEFT_FLIP_DOWN); break; case LANDSCAPE_TO_PORTRAIT: handleGestures(LEFT_FLIP_UP); break; case NOCHANGE: default: break; } //Check Change of Right hand switch(detectHandStateChange(rightOrientationLast, rightOrientationCur)){ case PORTRAIT_TO_LANDSCAPE: handleGestures(RIGHT_FLIP_DOWN); break; case LANDSCAPE_TO_PORTRAIT: handleGestures(RIGHT_FLIP_UP); break; case NOCHANGE: default: break; }*/ }else if(blobs.GetNumBlobs() > 3){ //too much information cout<<"too much information"<<endl; } leftOrientationLast = leftOrientationCur; rightOrientationLast = rightOrientationCur; // Free all the resources. /*cvReleaseImage( &imageBGR ); cvReleaseImage( &imageHSV ); cvReleaseImage( &planeH ); cvReleaseImage( &planeS ); cvReleaseImage( &planeV ); cvReleaseImage( &imageSkinPixels ); cvReleaseImage( &imageSkinBlobs );*/ //if ESC is pressed then exit loop cvWaitKey(33); } cvWaitKey(0); return 0; }
//============================================================================== void PanTiltCameraClass::blobTracking(IplImage* hsv_mask, IplImage* pFour, IplImage* pImg) { //--- Get blobs and filter them using the blob area CBlobResult blobs; CBlob *currentBlob; //--- Create a thresholded image and display image -------------------- //--- Creates binary image IplImage* originalThr = cvCreateImage(cvGetSize(hsv_mask), IPL_DEPTH_8U,1); //--- Create 3-channel image IplImage* display = cvCreateImage(cvGetSize(hsv_mask),IPL_DEPTH_8U,3); //--- Copies the original cvMerge( hsv_mask, hsv_mask, hsv_mask, NULL, display ); //--- Makes a copy for processing cvCopy(hsv_mask,originalThr); //--- Find blobs in image --------------------------------------------- int blobThreshold = 0; bool blobFindMoments = true; blobs = CBlobResult( originalThr, originalThr, blobThreshold, blobFindMoments); //--- filters blobs according to size and radius constraints blobs.Filter( blobs, B_EXCLUDE, CBlobGetArea(), B_LESS, this->minBlobSize ); //--- display filtered blobs ------------------------------------------ //--- copies the original in (for background) cvMerge( originalThr, originalThr, originalThr, NULL, display ); CvPoint pts[this->NUMBER_OF_CIRCLES]; //--- This sequence marks all the blobs for (int i = 0; i < blobs.GetNumBlobs(); i++ ) { currentBlob = blobs.GetBlob(i); currentBlob->FillBlob( display, CV_RGB(0,0,255)); //--- Get blobs centerpoint CvPoint bcg; bcg.x = (int)(currentBlob->MinX()+((currentBlob->MaxX()-currentBlob->MinX())/2)); bcg.y = (int)(currentBlob->MinY()+((currentBlob->MaxY()-currentBlob->MinY())/2)); //--- Print the CG on the picture char blobtext[40]; for(int k=0;k<this->NUMBER_OF_CIRCLES;k++) { sprintf(blobtext,"%d",k+1); TargetReticle(display,&pts[k],blobtext,6,CV_RGB(255,0,0)); }//for }//for each blob //--- Set the ROI in the pFour image cvSetImageROI(pFour,cvRect(pImg->width,pImg->height+80,pImg->width,pImg->height)); cvCopy(display,pFour); //Reset region of interest cvResetImageROI(display); //Clean up cvReleaseImage( &originalThr ); cvReleaseImage( &display); }
int main(){ Scalar robotColor = CV_RGB(255, 0, 0); Scalar rightColor = CV_RGB(0, 255, 0); Scalar leftColor = CV_RGB(0, 0, 255); Scalar robotColor_2 = CV_RGB(0, 255, 255); Scalar rightColor_2 = CV_RGB(255, 0, 255); Scalar leftColor_2 = CV_RGB(255, 255, 0); int lowH = 0; int highH = 14; int top_cut = 120; int bot_cut = 70; int lowV = 200; int type = 0; int ticks = 0; int nb_errors = 0; int len = 150; int trace = 25; int sensitivity = 100; int area = 3000; int flip = 0; //set to 0 if no flips are needed, 1 for y axis, 2 for x axis and 3 for both namedWindow("My Window", 1); createTrackbar("lowH", "My Window", &lowH, 180); createTrackbar("highH", "My Window", &highH, 180); createTrackbar("top_cut", "My Window", &top_cut, 255); createTrackbar("bot_cut", "My Window", &bot_cut, 255); createTrackbar("lowV", "My Window", &lowV, 255); createTrackbar("LEN", "My Window", &len, 300); createTrackbar("TRACE", "My Window", &trace, 100); createTrackbar("SENSITIVITY", "My Window", &sensitivity, 200); createTrackbar("AREA", "My Window", &area, 7000); createTrackbar("FLIP", "My Window", &flip, 3); moveWindow("My Window", 0, 0); namedWindow("kalman", 1); moveWindow("kalman", 500, 0); namedWindow("Blobs Image", 1); moveWindow("Blobs Image", 500, 300); namedWindow("frame", 1); moveWindow("frame", 0, 500); namedWindow("test", WINDOW_AUTOSIZE); moveWindow("test", 0, 500); namedWindow("white", WINDOW_AUTOSIZE); moveWindow("white", 0, 500); //file of video input string filename = "testVideo_5.webm"; ofstream logs; ofstream stats; stats.open("stats.txt"); logs.open("logs.csv"); logs << "Left_x,Left_y,Left_holds,Right_x,Right_y,Right_holds,confirmed" << endl; Point center_window = Point(WIDTH/2, (HEIGHT - top_cut - bot_cut)/2); Point center_left = Point(WIDTH/4, .5*max(10, HEIGHT - top_cut - bot_cut)); Point center_right = Point(3*WIDTH/4, .5*max(10, HEIGHT - top_cut - bot_cut)); // initialize the kalman filters KalmanFilter KF_left(4, 2, 0); KalmanFilter KF_right(4, 2, 0); Mat_<float> measurement_left(2,1); measurement_left.setTo(Scalar(0)); Mat_<float> measurement_right(2,1); measurement_right.setTo(Scalar(0)); initialize_kalman(&KF_left, center_left); initialize_kalman(&KF_right, center_right); VideoCapture cap(0); // VideoCapture cap(filename); Mat kf_img(HEIGHT - top_cut - bot_cut, WIDTH, CV_8UC3); vector<Point> mousev_left,kalmanv_left; mousev_left.clear(); kalmanv_left.clear(); vector<Point> mousev_right,kalmanv_right; mousev_right.clear(); kalmanv_right.clear(); int counter = 0; int nb_confirmed = 0; int nb_total = 0; double average_left = 0; double average_right = 0; double error_left = 0; double error_right = 0; double prev_dist = norm(center_left - center_right); double new_dist = prev_dist; bool left_valid = false; bool right_valid = true; Mat temp = Mat::zeros(100,400, CV_8UC3); putText(temp, "Press any key to start", Point(50,50), FONT_HERSHEY_SIMPLEX, .5, Scalar(255,255,255)); putText(temp, "and ESC to end", Point(50, 75), FONT_HERSHEY_SIMPLEX, .5, Scalar(255,255,255)); imshow("Blobs Image", temp); waitKey(-1); int key; bool eof = false; for(;;){ Mat frame; Mat prediction_left = KF_left.predict(); Point new_left(prediction_left.at<float>(0), prediction_left.at<float>(1)); measurement_left(0) = center_left.x; measurement_left(1) = center_left.y; Mat estimated_left = KF_left.correct(measurement_left); Point statePt_left(estimated_left.at<float>(0),estimated_left.at<float>(1)); Point measPt_left(measurement_left(0),measurement_left(1)); Mat prediction_right = KF_right.predict(); Point new_right(prediction_right.at<float>(0), prediction_right.at<float>(1)); measurement_right(0) = center_right.x; measurement_right(1) = center_right.y; Mat estimated_right = KF_right.correct(measurement_right); Point statePt_right(estimated_right.at<float>(0),estimated_right.at<float>(1)); Point measPt_right(measurement_right(0),measurement_right(1)); ticks ++; error_left = norm(statePt_left - measPt_left); average_left = ((average_left * (ticks - 1)) + error_left) / ticks; error_right = norm(statePt_right - measPt_right); average_right = ((average_right * (ticks - 1)) + error_right) / ticks; imshow("kalman", kf_img); // waitKey(-1); kf_img = Scalar::all(0); mousev_left.push_back(measPt_left); kalmanv_left.push_back(statePt_left); circle(kf_img, statePt_left, 1, Scalar(255,255,255), -1); circle(kf_img, measPt_left, 1, Scalar(0,0,255), -1); int nb_mousev_left = mousev_left.size() - 1; int nb_kalmanv_left = mousev_left.size() - 1; int nb_mousev_right = mousev_left.size() - 1; int nb_kalmanv_right = mousev_left.size() - 1; for(int i = max(0, nb_mousev_left - trace); i< nb_mousev_left; i++){ line(kf_img, mousev_left[i], mousev_left[i+1], Scalar(255,255,0), 1); } for(int i = max(0, nb_kalmanv_left - trace); i< nb_kalmanv_left; i++){ line(kf_img, kalmanv_left[i], kalmanv_left[i+1], Scalar(0,0,255), 1); } mousev_right.push_back(measPt_right); kalmanv_right.push_back(statePt_right); circle(kf_img, statePt_right, 1, Scalar(255,255,255), -1); circle(kf_img, measPt_right, 1, Scalar(0,0,255), -1); for(int i = max(0, nb_mousev_right - trace); i< nb_mousev_right; i++){ line(kf_img, mousev_right[i], mousev_right[i+1], Scalar(0,255,0), 1); } for(int i = max(0, nb_kalmanv_right - trace); i< nb_kalmanv_right; i++){ line(kf_img, kalmanv_right[i], kalmanv_right[i+1], Scalar(255,0,255), 1); } Rect border(0, top_cut, WIDTH, max(10, HEIGHT - top_cut - bot_cut)); cap >> frame; if(!frame.empty()){ Mat image; int flip_type = 1; switch (flip) { case 0: break; case 1: break; case 2: flip_type = 0; break; case 3: flip_type = -1; break; } if(flip) cv::flip(frame, frame, flip_type); resize(frame, frame, Size(WIDTH, HEIGHT)); image = frame(border); imshow("frame", image); //performs the skin detection Mat converted_skin; cvtColor(image, converted_skin, CV_BGR2HSV); Mat skin_masked; inRange(converted_skin, Scalar(min(lowH, highH), 48, 80),Scalar(max(lowH, highH), 255, 255), skin_masked); imshow("test", skin_masked); //performs the robot detection Mat converted_white, white_masked, lights_masked; cvtColor(image, converted_white, CV_BGR2GRAY); inRange(converted_skin, Scalar(0, 0, 245), Scalar(180, 255, 255), lights_masked); threshold(converted_white, white_masked, lowV, 255, type); bitwise_or(white_masked, lights_masked, white_masked); imshow("white", white_masked); Mat copy(converted_skin.size(), converted_skin.type());// = converted.clone(); //detects hands as blobs CBlobResult blobs; IplImage temp = (IplImage)skin_masked; blobs = CBlobResult(&temp,NULL,1); blobs = CBlobResult(skin_masked,Mat(),NUMCORES); int numBlobs = blobs.GetNumBlobs(); if(0 == numBlobs){ cout << "can't find blobs!" << endl; continue; } // detects robot as a blob CBlobResult robot_blobs; IplImage robot_temp = (IplImage) white_masked; robot_blobs = CBlobResult(&robot_temp, NULL, 1); robot_blobs = CBlobResult(white_masked, Mat(), NUMCORES); if(0 == robot_blobs.GetNumBlobs()){ cout << "can't find robot_blobs!" << endl; continue; } CBlob *curblob; CBlob* blob_1; CBlob* blob_2; CBlob* leftBlob; CBlob* rightBlob; CBlob* robotBlob; copy.setTo(Vec3b(0,0,0)); // chooses the two largest blobs for the hands Point center_1, center_2; int max_1 = 0; int max_2 = 0; int maxArea_1 = 0; int maxArea_2 = 0; for(int i=0;i<numBlobs;i++){ int area = blobs.GetBlob(i)->Area(); if(area > maxArea_1){ maxArea_2 = maxArea_1; maxArea_1 = area; max_2 = max_1; max_1 = i; } else if(area > maxArea_2){ maxArea_2 = area; max_2 = i; } } int i_1 = max_1; int i_2 = max_2; double area_left, area_right; Rect rect_1; Rect rect_2; //determines which hand is left/right blob_1 = blobs.GetBlob(i_1); blob_2 = blobs.GetBlob(i_2); center_1 = blob_1->getCenter(); center_2 = blob_2->getCenter(); bool left_is_1 = (center_1.x < center_2.x)? true : false; leftBlob = (left_is_1)? blob_1 : blob_2; rightBlob = (left_is_1)? blob_2 : blob_1; center_left = leftBlob->getCenter(); center_right = rightBlob->getCenter(); //determine the number of valid hands //validity is decided by whether or not the hand followed a logical movement, //and if the area of the blob is large enough to be accepted int valids = 0; rect_1 = leftBlob->GetBoundingBox(); rectangle(copy, rect_1.tl(), rect_1.br(), leftColor_2, 5); error_left = norm(statePt_left - center_left); area_left = leftBlob->Area(); left_valid = error_left < sensitivity && area_left > area; if(left_valid){ leftBlob->FillBlob(copy,leftColor, true); valids ++; } circle(copy, center_left, 5, leftColor_2, -1); rect_2 = rightBlob->GetBoundingBox(); rectangle(copy, rect_2.tl(), rect_2.br(), rightColor_2, 5); error_right = norm(statePt_right - center_right); area_right = rightBlob->Area(); right_valid = error_right < sensitivity && area_right > area; if(right_valid){ rightBlob->FillBlob(copy,rightColor, true); valids ++; } circle(copy, center_right, 5, rightColor_2, -1); //finds the blob representing the robot //we could add a restriction to only choose a blob between the two hands //in terms of x-coordinate //a Kalman check can easily be done for the robot Point robot_center; maxArea_1 = 0; max_1 = 0; numBlobs = robot_blobs.GetNumBlobs(); if(0 < numBlobs){ for(int i=0;i<numBlobs;i++){ curblob = robot_blobs.GetBlob(i); robot_center = curblob->getCenter(); double dist_1 = norm(center_1 - robot_center); double dist_2 = norm(center_2 - robot_center); if(dist_1 < len || dist_2 < len){ double area = robot_blobs.GetBlob(i)->Area(); if(area > maxArea_1){ max_1 = i; maxArea_1 = area; } } } int i_3 = max_1; curblob = robot_blobs.GetBlob(i_3); curblob->FillBlob(copy,robotColor, true); robot_center = curblob->getCenter(); circle(copy, robot_center, 5, robotColor_2, -1); Rect rect_3 = curblob->GetBoundingBox(); rectangle(copy, rect_3.tl(), rect_3.br(), robotColor_2, 5); // determines which hand is controlling the robot // by cheching the position of the 3 blobs // an additional check could be done by verifying if //the center of the robot is moving in the same direction //as the center of the hand moving it bool is_left = false; bool is_right = false; bool confirmed = false; double dist_left = norm(center_left - robot_center); double dist_right = norm(center_right - robot_center); double dist_both = norm(center_left - center_right); Point robot_tl = rect_3.tl(); Point robot_br = rect_3.br(); int left_count = 0; int right_count = 0; if(rect_1.contains(robot_tl)) left_count++; if(rect_1.contains(robot_br)) left_count++; if(rect_1.contains(robot_center)) left_count++; if(rect_2.contains(robot_tl)) right_count++; if(rect_2.contains(robot_br)) right_count++; if(rect_2.contains(robot_center)) right_count++; switch(valids){ case 0: break; case 1:{ int area_sum = area_left + area_right; if(dist_left > 2* dist_right || dist_right > 2*dist_left){ if(area_sum > 2 * area && (area_left > 2*area_right || area_right > 2*area_left) && ((left_valid && left_count > 0)||(right_valid && right_count > 0))){ is_left = true; is_right = true; if(left_count > 2 || right_count > 2) confirmed = true; } } if(left_valid && left_count > 1) { is_left = true; if(left_count > 2) confirmed = true; } if(right_valid && right_count > 1) { is_right = true; if(right_count > 2) confirmed = true; } //if just one hand is on screen if(area_right < area/2){ if(center_left.x > robot_center.x){ is_left = true; } else{ is_right = true; } } else if (area_left < area/2){ if(center_right.x < robot_center.x){ is_right = true; } else{ is_left = true; } } break;} case 2:{ int moreLeft = left_count - right_count; int moreRight = right_count - left_count; int countSum = left_count + right_count; switch (countSum) { case 3:{ switch (left_count) { case 3: is_left = true; confirmed = true; break; case 2: case 1: is_left = true; is_right = true; confirmed= true; break; case 0: is_right = true; confirmed = true; break; } } case 2:{ switch (left_count) { case 2: is_left = true; confirmed = true; break; case 1: is_left = true; is_right = true; break; case 0: is_right = true; confirmed = true; break; } } case 1:{ switch (left_count) { case 1: is_left = true; break; case 0: is_right = true; break; } } case 0:{ break; } } break;} } bool found = false; for(size_t i = robot_tl.x; i<= robot_br.x && !found; i++){ for(size_t j = robot_tl.y; j<= robot_br.y && !found; j++){ int color1 = 0; int color2 = 255; Vec3b colour = copy.at<Vec3b>(Point(i, j)); if(colour[1] == color1 && colour[0] == color2){ found = true; is_left = true; } if(colour[1] == color2 && colour[0] == color1){ found = true; is_right = true; } } } if (found) confirmed = true; if(!is_left && !is_right){ cout << "-- none!"; if(left_count == 0 && right_count == 0) confirmed = true; } else if(is_left && is_right){ cout << "-- both!"; } else { if (is_left){ cout << " -- left!"; } else { cout << " -- right!"; } } imshow("kalman", kf_img); // up till here if(confirmed){ nb_confirmed ++; cout << " -- confirmed" << endl; } else { cout << endl; } csv(&logs, center_left.x, center_left.y, is_left, center_right.x, center_right.y, is_right, confirmed); } nb_total ++; // // //displayOverlay("Blobs Image","Multi Thread"); new_dist = norm(center_left - center_right); // don't throw errors in the first 10 frames if(ticks > 10){ if(error_left > 20 && error_right > 20 /*&& new_dist < prev_dist*/){ circle(copy, Point(WIDTH/2, HEIGHT/2), 100, Scalar(0, 0, 255), 30); nb_errors ++; } } prev_dist = new_dist; imshow("Blobs Image",copy); key = waitKey(10); } else{ eof = true; } if(27 == key || 1048603 == key || eof){ double kalman_error_percentage = (nb_errors*100.0)/ticks; double confirm_percentage = (nb_confirmed*100.0/nb_total); stats << "kalman error frequency: " << kalman_error_percentage << "\%" << endl; stats << "confirmed: " << confirm_percentage << "\%" << endl; logs.close(); stats.close(); return 0; } } }
void Auvsi_Recognize::extractLetter( void ) { typedef cv::Vec<unsigned char, 1> VT_binary; #ifdef TWO_CHANNEL typedef cv::Vec<T, 2> VT; #else typedef cv::Vec<T, 3> VT; #endif typedef cv::Vec<int, 1> IT; // Erode input slightly cv::Mat input; cv::erode( _shape, input, cv::Mat() ); // Remove any small white blobs left over CBlobResult blobs; CBlob * currentBlob; CBlob biggestBlob; IplImage binaryIpl = input; blobs = CBlobResult( &binaryIpl, NULL, 0 ); blobs.GetNthBlob( CBlobGetArea(), 0, biggestBlob ); blobs.Filter( blobs, B_EXCLUDE, CBlobGetArea(), B_GREATER_OR_EQUAL, biggestBlob.Area() ); for (int i = 0; i < blobs.GetNumBlobs(); i++ ) { currentBlob = blobs.GetBlob(i); currentBlob->FillBlob( &binaryIpl, cvScalar(0)); } // Perform k-means on this region only int areaLetter = (int)biggestBlob.Area(); cv::Mat kMeansInput = cv::Mat( areaLetter, 1, _image.type() ); // Discard if we couldn't extract a letter if( areaLetter <= 0 ) { _letter = cv::Mat( _shape ); _letter = cv::Scalar(0); return; } cv::MatIterator_<VT_binary> binaryIterator = input.begin<VT_binary>(); cv::MatIterator_<VT_binary> binaryEnd = input.end<VT_binary>(); cv::MatIterator_<VT> kMeansIterator = kMeansInput.begin<VT>(); for( ; binaryIterator != binaryEnd; ++binaryIterator ) { if( (*binaryIterator)[0] > 0 ) { (*kMeansIterator) = _image.at<VT>( binaryIterator.pos() ); ++kMeansIterator; } } // Get k-means labels cv::Mat labels = doClustering<T>( kMeansInput, 2, false ); int numZeros = areaLetter - cv::countNonZero( labels ); bool useZeros = numZeros < cv::countNonZero( labels ); // Reshape into original form _letter = cv::Mat( _shape.size(), _shape.type() ); _letter = cv::Scalar(0); binaryIterator = input.begin<VT_binary>(); binaryEnd = input.end<VT_binary>(); cv::MatIterator_<IT> labelsIterator = labels.begin<IT>(); for( int index = 0; binaryIterator != binaryEnd; ++binaryIterator ) { if( (*binaryIterator)[0] > 0 ) { // Whichever label was the minority, we make that value white and all other values black unsigned char value = (*labelsIterator)[0]; if( useZeros ) if( value ) value = 0; else value = 255; else if( value ) value = 255; else value = 0; _letter.at<VT_binary>( binaryIterator.pos() ) = VT_binary( value ); ++labelsIterator; } } }
/* arg1: Width of each frame arg2: Height of each frame arg3: Target frames per second of the program arg4: Maximum number of blobs to track. Each blob MAY corresspond to a person in front of the camera */ int main(int argc, char* argv[]) { if (argc < 5) { cout << "Too few arguments to the program. Exiting...\n"; return 0; } int width, height, fps, numberOfBlobs; try { //Read the arguments width = atoi(argv[1]); height = atoi(argv[2]); fps = atoi(argv[3]); numberOfBlobs = atoi(argv[4]); //Done reading arguments } catch(...) { cout << "One or more arguments are invalid!. Exiting...\n"; return 0; } /* int width = 320; int height = 240; int fps = 10; int numberOfBlobs = 2; */ tempImageV4L = cvCreateImage(cvSize(width, height), 8, 3); frameNumber = 0; //Beginning initialising cameras rightCamera = new Camera("/dev/video0", width, height, fps); leftCamera = new Camera("/dev/video1", width, height, fps); //leftCamera = rightCamera; //If only one camera is available, uncomment this line and comment the line above this. //Done initialising cameras //Waste some frames so as to get the cameras running in full flow WasteNFrames(10); //Beginning capturing background backImageRight = GetNextCameraShot(rightCamera); backImageLeft = GetNextCameraShot(leftCamera); frameNumber++; cvtColor(backImageRight, backImageRight, CV_BGR2HSV); cvtColor(backImageLeft, backImageLeft, CV_BGR2HSV); //Done capturing background //General Stuff Mat motionImageRight(backImageRight.rows, backImageRight.cols, CV_8UC1); Mat motionImageLeft(backImageLeft.rows, backImageLeft.cols, CV_8UC1); Mat HSVImageRight, HSVImageLeft; Mat displayImageRight, displayImageLeft; //End of General Stuff while (1) //The infinite loop { //Beginning getting camera shots rightImage = GetNextCameraShot(rightCamera); leftImage = GetNextCameraShot(leftCamera); frameNumber++; //Done getting camera shots //Beginning getting motion images HSVImageRight = rightImage.clone(); cvtColor(HSVImageRight, HSVImageRight, CV_BGR2HSV); CompareWithBackground(HSVImageRight, backImageRight, motionImageRight); medianBlur(motionImageRight, motionImageRight, 3); HSVImageLeft = leftImage.clone(); cvtColor(HSVImageLeft, HSVImageLeft, CV_BGR2HSV); CompareWithBackground(HSVImageLeft, backImageLeft, motionImageLeft); medianBlur(motionImageLeft, motionImageLeft, 3); //Ended getting motion images cout << "\nFor frame #" << frameNumber << " :\n"; //Beginning Getting Blobs IplImage imageblobPixels = motionImageRight; CBlobResult blobs; blobs = CBlobResult(&imageblobPixels, NULL, 0); // Use a black background color. int minArea = 100 / ((640 / width) * (640 / width)); blobs.Filter(blobs, B_EXCLUDE, CBlobGetArea(), B_LESS, minArea); int foundBlobs = blobs.GetNumBlobs(); //Ended Getting Blobs cout << "Found " << foundBlobs << " motion blobs\n"; //Creating copies of original images for modifying and displaying displayImageRight = rightImage.clone(); displayImageLeft = leftImage.clone(); //Done creating copies //Cycling through the blobs for (int blobIndex = 0; blobIndex < blobs.GetNumBlobs() && blobIndex < numberOfBlobs; blobIndex++) { cout << "Blob #" << blobIndex << " : "; //Getting blob details CBlob * blob = blobs.GetBlob(blobIndex); int x = blob->GetBoundingBox().x; int y = blob->GetBoundingBox().y; int w = blob->GetBoundingBox().width; int h = blob->GetBoundingBox().height; //Done getting blob details int sep = 0; //The point for which we want to find depth PixPoint inP = {x + w/2, y + h/2}, oP = {0, 0}; cout << "inPoint = {" << inP.x << ", " << inP.y << "} "; //Initialing the rectangle in which the corressponding point is likely in Rectangle rect; rect.location.x = -1; rect.location.y = inP.y - 5; rect.size.x = rightImage.cols; rect.size.y = 11; //Done initialising the target rectangle //Find the corressponding point and calculate the sepertion oP = PointCorresponder::correspondPoint(rightImage, leftImage, inP, rect, motionImageLeft); sep = inP.x - oP.x; cout << "foundPoint = {" << oP.x << ", " << oP.y << "} "; //Just for visual presentation DrawRect(displayImageRight, x, y, w, h); cv::circle(displayImageRight, Point(inP.x, inP.y), 10, Scalar(0), 3); cv::circle(displayImageLeft, Point(oP.x, oP.y), 10, Scalar(0), 3); //Done decoration //The thing we were looking for... how can we forget to print this? :P cout << "seperation = " << sep << "\n"; } //Show the windows cv::namedWindow("RIGHT"); cv::namedWindow("thresh"); cv::namedWindow("LEFT"); imshow("LEFT", displayImageLeft); imshow("RIGHT", displayImageRight); imshow("thresh", motionImageRight); //End of code for showing windows //The loop terminating condition if (waitKey(27) >= 0) break; } //Mission Successful!! :D :) return 0; }
void TabletopSegmentor::pcCallback(sensor_msgs::PointCloud2::ConstPtr pc_msg) { if(!pc_lock.try_lock()) return; pcl::PointCloud<pcl::PointXYZRGB> pc_full, pc_full_frame; pcl::fromROSMsg(*pc_msg, pc_full); string base_frame("/base_link"); ros::Time now = ros::Time::now(); tf_listener.waitForTransform(pc_msg->header.frame_id, base_frame, now, ros::Duration(3.0)); pcl_ros::transformPointCloud(base_frame, pc_full, pc_full_frame, tf_listener); // pc_full_frame is in torso lift frame cv::Mat cur_height_img = cv::Mat::zeros(imgx, imgy, CV_8U); BOOST_FOREACH(const pcl::PointXYZRGB& pt, pc_full_frame.points) { if(pt.x != pt.x || pt.y != pt.y || pt.z != pt.z) continue; int32_t x, y, z; x = (pt.x - minx)/(maxx-minx) * imgx; y = (pt.y - miny)/(maxy-miny) * imgy; z = (pt.z - minz)/(maxz-minz) * 256; if(x < 0 || y < 0) continue; if(x >= imgx || y >= imgy) continue; if(z < 0 || z >= 256) continue; if(cur_height_img.at<uint8_t>(x, y) == 0 || cur_height_img.at<uint8_t>(x, y) < (uint8_t) z) cur_height_img.at<uint8_t>(x, y) = (uint8_t) z; } cv::max(height_img_max, cur_height_img, height_img_max); cv::Mat cur_height_img_flt; cur_height_img.convertTo(cur_height_img_flt, CV_32F); height_img_sum += cur_height_img_flt; cv::Mat cur_count(imgx, imgy, CV_8U); cur_count = (cur_height_img > 0) / 255; cv::Mat cur_count_flt(imgx, imgy, CV_32F); cur_count.convertTo(cur_count_flt, CV_32F); height_img_count += cur_count_flt; cv::Mat height_img_avg_flt = height_img_sum / height_img_count; cv::Mat height_img_avg(imgx, imgy, CV_8U); height_img_avg_flt.convertTo(height_img_avg, CV_8U); height_img_avg = height_img_max; cv::Mat height_hist(256, 1, CV_32F, cv::Scalar(0)); for(uint32_t x=0;x<imgx;x++) for(uint32_t y=0;y<imgy;y++) { if(height_img_avg.at<uint8_t>(x,y) == 255) height_img_avg.at<uint8_t>(x,y) = 0; if(height_img_avg.at<uint8_t>(x,y) != 0) { height_hist.at<float>(height_img_avg.at<uint8_t>(x,y), 0)++; } } ////////////////////// Finding best table height ///////////////////////// uint32_t gfiltlen = 25; float stddev = 256/(maxz-minz) * 0.015; cv::Mat gauss_filt(gfiltlen, 1, CV_32F, cv::Scalar(0)); for(uint32_t i=0;i<gfiltlen;i++) gauss_filt.at<float>(i,0) = 0.39894 / stddev * std::exp(-(i-((float)gfiltlen)/2)*(i-((float)gfiltlen)/2)/(2*stddev*stddev)); //cout << gauss_filt; uint32_t maxval = 0, maxidx = 0; for(uint32_t i=0;i<256-gfiltlen;i++) { uint32_t sum = 0; for(uint32_t j=0;j<gfiltlen;j++) sum += height_hist.at<float>(i+j,0) * gauss_filt.at<float>(j,0); if(sum > maxval && i != 0) { maxval = sum; maxidx = i+gfiltlen/2; } } int32_t table_height = ((int32_t)maxidx); //printf("%d %d, ", maxval, maxidx); /////////////////////////// Getting table binary ///////////////////// cv::Mat height_img_thresh(imgx, imgy, CV_8U); height_img_thresh = height_img_avg.clone(); for(uint32_t x=0;x<imgx;x++) for(uint32_t y=0;y<imgy;y++) { if(std::fabs(table_height - ((int32_t)height_img_thresh.at<uint8_t>(x,y))) < stddev*2) height_img_thresh.at<uint8_t>(x,y) = 255; else height_img_thresh.at<uint8_t>(x,y) = 0; } ////////////////////////////////////////////////////////////////// IplImage height_img_thresh_ipl = height_img_thresh; IplConvKernel* element = cvCreateStructuringElementEx(3, 3, 1, 1, CV_SHAPE_RECT); cvMorphologyEx(&height_img_thresh_ipl, &height_img_thresh_ipl, NULL, element, CV_MOP_CLOSE, num_closes); //cvMorphologyEx(&height_img_thresh, &height_img_thresh, NULL, element, CV_MOP_OPEN, 2); cv::Mat height_img_thresh_blob = height_img_thresh.clone(); IplImage blob_img = height_img_thresh_blob; CBlobResult blobs = CBlobResult(&blob_img, NULL, 0); //blobs.Filter(blobs, B_EXCLUDE, CBlobGetArea(), B_LESS, 10); CBlob biggestblob; blobs.GetNthBlob(CBlobGetArea(), 0, biggestblob); cv::Mat table_blob(imgx, imgy, CV_8U, cv::Scalar(0)); IplImage table_blob_img = table_blob; biggestblob.FillBlob(&table_blob_img, cv::Scalar(150)); //drawCvBox2D(blob_img, table_roi, cv::Scalar(50), 1); CvBox2D table_roi = biggestblob.GetEllipse(); table_roi.angle *= CV_PI/180; cv::Mat table_hull(imgx, imgy, CV_8U, cv::Scalar(0)); IplImage hull_img = table_hull; fillCvBox2D(hull_img, table_roi, cv::Scalar(255)); //printf("Cvbox: %f, %f, %f, %f, %f\n", table_roi.center.x, table_roi.center.y, table_roi.size.width, table_roi.size.height, table_roi.angle); //cv::Mat height_morph(imgx, imgy, CV_8U, cv::Scalar(0)); //cv::Mat tmp_img(imgx, imgy, CV_8U, cv::Scalar(0)); //IplImage t1 = height_img_thresh; IplImage = height_morph; cv::Mat above_table(imgx, imgy, CV_8U); bitwise_and(height_img_max > table_height + stddev*2, table_hull, above_table); IplImage above_table_img = above_table; CBlobResult obj_blobs = CBlobResult(&above_table_img, NULL, 0); obj_blobs.Filter(obj_blobs, B_EXCLUDE, CBlobGetArea(), B_LESS, obj_min_area); CBlob cur_obj_blob; vector<float> obj_cents_x, obj_cents_y, obj_cents_r, obj_areas, obj_dists; vector<uint32_t> obj_inds; for(int i=0;i<obj_blobs.GetNumBlobs();i++) { obj_blobs.GetNthBlob(CBlobGetArea(), i, cur_obj_blob); CvBox2D obj_box = cur_obj_blob.GetEllipse(); obj_cents_x.push_back(obj_box.center.x); obj_cents_y.push_back(obj_box.center.y); obj_cents_r.push_back(obj_box.angle * CV_PI/180); obj_areas.push_back(cur_obj_blob.Area()); obj_dists.push_back((obj_box.center.x-imgx/2)*(obj_box.center.x-imgx/2)+obj_box.center.y*obj_box.center.y); obj_inds.push_back(i); } boost::function<bool(uint32_t, uint32_t)> sortind = boost::bind(&compind, _1, _2, obj_dists); sort(obj_inds.begin(), obj_inds.end(), sortind); obj_poses.poses.clear(); for(uint32_t i=0;i<obj_inds.size();i++) { float posey = obj_cents_x[obj_inds[i]], posex = obj_cents_y[obj_inds[i]]; float poser = -obj_cents_r[obj_inds[i]] + 3*CV_PI/2; geometry_msgs::Pose cpose; cpose.position.x = posex/imgx*(maxx-minx) + minx; cpose.position.y = posey/imgy*(maxy-miny) + miny; cpose.position.z = table_height/256.0*(maxz-minz) + minz; btMatrix3x3 quatmat; btQuaternion quat; quatmat.setEulerZYX(poser, 0 , 0); quatmat.getRotation(quat); cpose.orientation.x = quat.x(); cpose.orientation.y = quat.y(); cpose.orientation.z = quat.z(); cpose.orientation.w = quat.w(); CvPoint centerpt; centerpt.y = posex; centerpt.x = posey; printf("[%d](%f, %f, area: %f)\n", i, posex, posey, obj_areas[obj_inds[i]]); IplImage height_img_max_ipl = height_img_max; cvCircle(&height_img_max_ipl, centerpt, 3, cv::Scalar(200), 2); obj_poses.poses.push_back(cpose); } obj_poses.header.stamp = ros::Time::now(); obj_poses.header.frame_id = base_frame; obj_arr_pub.publish(obj_poses); cv_bridge::CvImage cvb_height_img; //cvb_height_img.image = height_img_avg; //cvb_height_img.image = height_img_max; //cvb_height_img.image = height_morph; //cvb_height_img.image = obj_img; //cvb_height_img.image = height_img_thresh_blob; //cvb_height_img.image = table_blob; //cvb_height_img.image = height_img_thresh; cvb_height_img.image = above_table; //cvb_height_img.image = table_edge; cvb_height_img.header.stamp = ros::Time::now(); cvb_height_img.header.frame_id = base_frame; cvb_height_img.encoding = enc::MONO8; height_pub.publish(cvb_height_img.toImageMsg()); pc_full_frame.header.stamp = ros::Time::now(); pc_full_frame.header.frame_id = base_frame; //pc_pub.publish(pc_full_frame); if(obj_poses.poses.size() > 0) obj_poses_found = true; //delete element; pc_lock.unlock(); }
int main() { CvPoint pt1,pt2; CvRect regt; CvPoint cir_center; CvPoint frame_center; CvPoint A,B,C,D; CvPoint temp; double angle,spinsize; int cir_radius=1; int frame_width=160, frame_height=120; CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY ); if ( !capture ) { fprintf(stderr, "ERROR: capture is NULL \n" ); getchar(); return -1; } cvSetCaptureProperty(capture,CV_CAP_PROP_FRAME_WIDTH,frame_width);// 120x160 cvSetCaptureProperty(capture,CV_CAP_PROP_FRAME_HEIGHT,frame_height); //cvSetCaptureProperty(capture, CV_CAP_PROP_FPS,10); // cvSetCaptureProperty(capture,CV_CAP_PROP_POS_FRAMES,5); // Create a window in which the captured images will be presented cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE ); // Show the image captured from the camera in the window and repeat while ( 1 ) { // Get one frame IplImage* frame = cvQueryFrame( capture ); if ( !frame ) { fprintf( stderr, "ERROR: frame is null...\n" ); getchar(); break; } int modfheight, modfwidth; modfheight = frame->height; modfwidth = frame->width; // create modified frame with 1/4th the original size IplImage* modframe = cvCreateImage(cvSize((int)(modfwidth/4),(int)(modfheight/4)),frame->depth,frame->nChannels); //cvCreateImage(size of frame, depth, noofchannels) cvResize(frame, modframe,CV_INTER_LINEAR); // create HSV(Hue, Saturation, Value) frame IplImage* hsvframe = cvCreateImage(cvGetSize(modframe),8, 3); cvCvtColor(modframe, hsvframe, CV_BGR2HSV); //cvCvtColor(input frame,outputframe,method) // create a frame within threshold. IplImage* threshframe = cvCreateImage(cvGetSize(hsvframe),8,1); cvInRangeS(hsvframe,cvScalar(15, 100, 100),cvScalar(60, 220, 220),threshframe); //cvInRangeS(input frame, cvScalar(min range),cvScalar(max range),output frame) // created dilated image IplImage* dilframe = cvCreateImage(cvGetSize(threshframe),8,1); cvDilate(threshframe,dilframe,NULL,2); //cvDilate(input frame, output frame, mask, number of times to dilate) CBlobResult blobs; blobs = CBlobResult(dilframe,NULL,0); // CBlobresult(inputframe, mask, threshold) Will filter all white parts of image blobs.Filter(blobs,B_EXCLUDE,CBlobGetArea(),B_LESS,50);//blobs.Filter(input, cond, criteria, cond, const) Filter all images whose area is less than 50 pixels CBlob biggestblob; blobs.GetNthBlob(CBlobGetArea(),0,biggestblob); //GetNthBlob(criteria, number, output) Get only the largest blob based on CblobGetArea() // get 4 points to define the rectangle pt1.x = biggestblob.MinX()*4; pt1.y = biggestblob.MinY()*4; pt2.x = biggestblob.MaxX()*4; pt2.y = biggestblob.MaxY()*4; cir_center.x=(pt1.x+pt2.x)/2; cir_center.y=(pt1.y+pt2.y)/2; frame_center.x=frame_width/2; frame_center.y=frame_height/2; A.x=frame_center.x-4; A.y=frame_center.y; B.x=frame_center.x+4; B.y=frame_center.y; C.y=frame_center.y-4; C.x=frame_center.x; D.y=frame_center.y+4; D.x=frame_center.x; cvRectangle(frame,pt1,pt2,cvScalar(255,0,0),1,8,0); // draw rectangle around the biggest blob cvCircle( frame, cir_center, cir_radius, cvScalar(0,255,255), 1, 8, 0 ); // center point of the rectangle cvLine(frame, A, B,cvScalar(255,0,255),2,8,0); cvLine(frame, C, D,cvScalar(255,0,255),2,8,0); if (cir_center.x!=0&&cir_center.y!=0){ spinsize=sqrt((cir_center.x-frame_center.x)*(cir_center.x-frame_center.x) +(cir_center.y-frame_center.y)*(cir_center.y-frame_center.y)); angle = atan2((double)cir_center.y-frame_center.y,(double)cir_center.x-frame_center.x); temp.x=(int)(frame_center.x+spinsize/5*cos(angle+3.1416/4)); temp.y=(int)(frame_center.y+spinsize/5*sin(angle+3.1415/4)); cvLine(frame, temp, frame_center,cvScalar(0,255,0),1,8,0); temp.x=(int)(frame_center.x+spinsize/5*cos(angle-3.1416/4)); temp.y=(int)(frame_center.y+spinsize/5*sin(angle-3.1415/4)); cvLine(frame, temp, frame_center,cvScalar(0,255,0),1,8,0); cvLine(frame, cir_center, frame_center,cvScalar(0,255,0),1,8,0); //cvCircle( frame, frame_center, cir_radius, cvScalar(0,255,255), 2, 8, 0 ); } cvShowImage( "mywindow", frame); // show output image // Do not release the frame! //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version), //remove higher bits using AND operator if ( (cvWaitKey(10) & 255) == 27 ) break; } // Release the capture device housekeeping cvReleaseCapture( &capture ); cvDestroyWindow( "mywindow" ); return 0; }