vector<Rect> CountourEx(Mat image){ Mat threshold_output; vector<vector<Point> > contours; vector<Vec4i> hierarchy; /// ʹÓÃThreshold¼ì²â±ßÔµ threshold( src_gray, threshold_output, thresh, 255, THRESH_BINARY ); /// ÕÒµ½ÂÖÀª findContours( threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) ); /// ¶à±ßÐαƽüÂÖÀª + »ñÈ¡¾ØÐκÍÔ²Ðα߽ç¿ò vector<vector<Point> > contours_poly( contours.size() ); vector<Rect> boundRect( contours.size() ); vector<Point2f>center( contours.size() ); vector<float>radius( contours.size() ); for( int i = 0; i < contours.size(); i++ ) boundRect[i] = boundingRect( Mat(contours_poly[i]) ); /// »¶à±ßÐÎÂÖÀª + °üΧµÄ¾ØÐοò + Ô²Ðοò Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 ); for( int i = 0; i< contours.size(); i++ ) rectangle( drawing, boundRect[i].tl(), boundRect[i].br(), Scalar(0,0,255), 2 ); /// ÏÔʾÔÚÒ»¸ö´°¿Ú namedWindow( "Contours", CV_WINDOW_AUTOSIZE ); imshow( "Contours", drawing ); return boundRect; }
float ScenRectangle::intersect(Node *node) { //////////////// if(nodeOverlap(*node)) { Point2D pt(node->getPosition()); //Trasformo il punto nelle coordinate locali del rettangolo pt.translate(-this->p.x,-this->p.y); pt.rotate(-rotAngle); Vector2D lv; lv=node->getVelocity(); lv.rotate(-rotAngle); Vector2D shift=calcShift(pt,lv,node->getRadius()+0.01f); node->setPosition(shift); //return 0; } //////////////////////////// float radius=node->getRadius(); ScenRectangle boundRect(Point2D(p.x-radius,p.y-radius), width+radius*2,height+radius*2); //Creo il raggio e lo trasformo nelle coodinate locali del rettangolo Ray2D ray=transformRay(Ray2D(node->getPosition(),node->getVelocity())); //Lo devo traslare perchè calcolo //l'intersezione con un rettangolo allargato ray.o.x+=radius; ray.o.y+=radius; //Calcolo l'intersezione return boundRect.rayLocalIntersect(ray); }//Fine intersect
void MotionDetection::BackGroundDetection(Mat fgMaskMOG, Mat mask, double ScaleFactor) { int min_size = 1, max_size = 10000; vector<vector<Point> > contours; erode(fgMaskMOG,fgMaskMOG,Mat()); dilate(fgMaskMOG,fgMaskMOG,Mat()); findContours( fgMaskMOG, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); // findContours( tempMog, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE,Point(0, 0)); vector<Rect> boundRect( contours.size() ); vector<vector<Point> > contours_poly( contours.size() ); // double smallest_area = contourArea( contours[0],false); for( int i = 0; i< contours.size(); i++ ) { approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true ); boundRect[i] = boundingRect( Mat(contours_poly[i]) ); // rectangle( drawing, boundRect[i].tl(), boundRect[i].br(), Scalar( 255,255,255), -1, 8, 0 ); //drawContours( drawing, contours, i, Scalar(255,255,255), CV_FILLED, 8, hierarchy); } Mat drawing = Mat::zeros( fgMaskMOG.size(), CV_8UC1); for( int i = 0; i< contours.size(); i++ ) { //need to make sure what's the exact min_size and max_size if(boundRect[i].area() < min_size * ScaleFactor * ScaleFactor || boundRect[i].area() > max_size * ScaleFactor * ScaleFactor) { continue; } rectangle( drawing, boundRect[i].tl(), boundRect[i].br(), Scalar(255,255,255), -1, 8, 0); } resize(drawing, drawing, Size(drawing.cols / ScaleFactor, drawing.rows / ScaleFactor), 0, 0, INTER_NEAREST); if(option_str == "-b") { static int counter = 0; String output_str = outPut_Mask_Path + NumberToString(++counter) + ".png"; imwrite(output_str, drawing); } for(int i = 0; i < drawing.cols; i++) { for(int j = 0; j < drawing.rows; j++) { Point p(i,j); if(drawing.at<uchar>(p) == 255) { mask.at<uchar>(p) += mask_add_step; } } } }
void imageProcess::filterWhiteAreas() { std::vector<std::vector<cv::Point>> contours; // Vector for storing contour of large white pixels areas cv::Mat temp; //Temp Mat to not change the original std::vector<cv::Vec4i> hierarchy; frame->copyTo(temp); //find contours will change the src image, so we use a copy to find large white cluster of pixels cv::findContours(temp, contours, hierarchy, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE); std::vector<std::vector<cv::Point>> contours_poly(contours.size()); std::vector<cv::RotatedRect> boundRect(contours.size()); for (size_t i = 0; i < contours.size(); i++) { double a = contourArea(contours[i], false); if ((a > whiteAreaMaxLimit)) { approxPolyDP(cv::Mat(contours[i]), contours_poly[i], 8, true); drawContours(*frame, contours_poly, i, cv::Scalar(0, 0, 0), -1, 8, hierarchy, 0, cv::Point(11, 11)); } } }
/* * Function: addBoundingBox * Usage: addBoundingBox(image, mask,draw); * ---------------------------------------- * Displays the bounding boxes from mask on the image. Returns vector of * <code>Rect's</code> */ vector<Rect> addBoundingBox(Mat& frame, Mat& mask, bool draw) { vector< vector< cv::Point> > contours; Mat threshout = mask.clone(); //cvtColor(threshout, threshout, COLOR_BGR2GRAY); //threshold(threshout, threshout, 1, 255, CV_THRESH_BINARY); //findContours will get the outer contours. any holes are not unique contours. findContours(threshout, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0)); //Use in Release mode only!!! /// Approximate contours to polygons + get bounding rects and circles vector<vector<Point> > contours_poly(contours.size()); vector<Rect> boundRect(contours.size()); for (int i = 0; i < contours.size(); i++) { //approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true); boundRect[i] = boundingRect(Mat(contours[i])); } /// Draw polygonal contour + bonding rects + circles //cvtColor(frame, frame, CV_GRAY2RGB); if (draw) { RNG rng(12345); for (int i = 0; i < contours.size(); i++) { Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)); rectangle(frame, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0); } } return boundRect; }
void KnnMDMethod::detect(const cv::Mat& input) { std::vector<cv::Vec4i> hierarchy; cv::Mat estForeground, //mog2 result estBackground, //mog2 result contoursMat, //tmp required to show all steps dilated, //after dilatation tmp,tmpF; input.copyTo(tmp); _knn->apply(input, estForeground); _knn->getBackgroundImage(estBackground); display(ConfigManager::VIEW_KNN_BACKGROUND, estBackground); display(ConfigManager::VIEW_KNN_FOREGROUND, estForeground); dilate(estForeground,dilated, dilateElement); display(ConfigManager::VIEW_KNN_DILATATION, dilated); dilated.copyTo(contoursMat); dilated.copyTo(tmpF); std::vector<std::vector<cv::Point> > contours; if(TimeManager::getTimeManager().time() % ConfigManager::getConfigManager().get<int>(ConfigManager::MD_DETECTION_STEP) == 0) { findContours( contoursMat, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0) ); std::vector<cv::Rect> boundRect(contours.size()); std::vector<std::vector<cv::Point> > contoursPoly(contours.size()); for( unsigned long i = 0; i< contours.size(); i++ ) { approxPolyDP(cv::Mat(contours[i]),contoursPoly[i],10,false); boundRect[i] = boundingRect(cv::Mat(contoursPoly[i])); rectangle(tmp, boundRect[i].tl(), boundRect[i].br(), cv::Scalar(0,255,0), 2,8,0); std::vector<cv::Rect> found, found_filtered; cv::Mat img1 = tmp(boundRect[i]); cv::Mat imgF = tmpF(boundRect[i]); cv::Mat img, imgFF; resize(img1,img,cv::Size((img1.cols/(double)img1.rows)*ConfigManager::getConfigManager().get<int>(ConfigManager::MD_GROUP_SIZE_FIX),ConfigManager::getConfigManager().get<int>(ConfigManager::MD_GROUP_SIZE_FIX))); resize(imgF,imgFF,cv::Size((imgF.cols/(double)imgF.rows)*ConfigManager::getConfigManager().get<int>(ConfigManager::MD_GROUP_SIZE_FIX),ConfigManager::getConfigManager().get<int>(ConfigManager::MD_GROUP_SIZE_FIX))); if(!(boundRect[i].width > ConfigManager::getConfigManager().get<double>(ConfigManager::MD_GROUP_WINDOW_TRESH) * input.rows || boundRect[i].width > ConfigManager::getConfigManager().get<double>(ConfigManager::MD_GROUP_WINDOW_TRESH) * input.rows)) { Group group(img1.cols/(double)img.cols, img1.rows/(double)img.rows,boundRect[i].x, boundRect[i].y,img, imgFF, boundRect[i]); DataManager::getDataManager().addGroup(group); } } } display(ConfigManager::VIEW_KNN_RESULT, tmp); }
/* * Finds and draws blobs by looking at the contours * The image pointer needs to be an binary image * Rectangles are drawn on the original with objects * This function calculates the center as well */ void Locator::findAndDrawBlobs(cv::Mat &blobs ) { blobs = lastStableBackground.clone(); cv::vector<cv::vector<cv::Point> > contours; cv::vector<cv::Vec4i> hierarchy; cv::Mat gray; cv::cvtColor(detectedDifference, gray, CV_BGR2GRAY); /// Find contours cv::findContours( gray, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0) ); /// Approximate contours to polygons + get bounding rects and circles cv::vector<cv::vector<cv::Point> > contours_poly( contours.size() ); cv::vector<cv::Rect> boundRect( contours.size() ); cv::vector<cv::Point2f>center( contours.size() ); cv::vector<float>radius( contours.size() ); for( uint i = 0; i < contours.size(); i++ ) { cv::approxPolyDP( cv::Mat(contours[i]), contours_poly[i], 3, true ); boundRect[i] = cv::boundingRect( cv::Mat(contours_poly[i]) ); cv::minEnclosingCircle( contours_poly[i], center[i], radius[i] ); } objectLocations.clear(); /// Draw polygonal contour + bonding rects for( uint i = 0; i< contours.size(); i++ ) { if(contours[i].size() > (uint)minContourSize) { objectLocations.push_back(boundRect[i]); rectangle( blobs, boundRect[i].tl(), boundRect[i].br(), cv::Scalar(0,0,255), 2, 8, 0 ); rectangle( blobs, cv::Point(center[i].x - 3, center[i].y - 3), cv::Point(center[i].x + 3, center[i].y + 3), cv::Scalar(0,255,255), 2, 8, 0 ); } }; gray.release(); }
vector<Rect> AvatarDetector::findAvatarsBoundRect(const cv::Mat & image, int thresh) { Mat gray = image.clone(); if (gray.channels() == 4) { cvtColor(gray, gray, CV_BGRA2GRAY); } Mat thresholdOutput; vector<vector<Point> > contours; vector<Vec4i> hierarchy; threshold( gray, thresholdOutput, thresh, 255, THRESH_BINARY_INV); findContours( thresholdOutput, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0)); vector<vector<Point> > contours_poly(contours.size()); vector<Rect> boundRect(contours.size()); for(int i = 0; i < contours.size(); i++) { approxPolyDP( cv::Mat(contours[i]), contours_poly[i], 3, true); boundRect[i] = boundingRect( cv::Mat(contours_poly[i])); } return boundRect; }
std::string OCR_DORSAL<T>::ReconocerDorsal(cv::Mat &imgBinaria, std::vector<std::vector<cv::Point>> &contornoCaracteres) { if ( carga_OCR() ) { size_t numCaracteres = contornoCaracteres.size(); OCR_DORSAL::vecNumber numero; std::vector<cv::Rect> boundRect(numCaracteres); std::string digit_numero; int maxNumCharFault = (double)numCaracteres * RATECHARFAULT; for (size_t i = 0; i < numCaracteres; i++ ) { boundRect[i] = cv::boundingRect(cv::Mat(contornoCaracteres[i])); cv::Mat newImagen = cv::Mat::zeros( imgBinaria.size(), CV_8UC1 ); cv::drawContours( newImagen, contornoCaracteres, i, Scalar(255,255,255), -1, 8, std::vector<Vec4i>(), 0, Point() ); cv::Mat segmentacion = newImagen & imgBinaria; cv::Mat imagenSegment( segmentacion, boundRect[i]); cv::copyMakeBorder(imagenSegment.clone(), imagenSegment,15,15,15,15,BORDER_CONSTANT,Scalar(0)); NumberRecognition(imagenSegment, digit_numero); numero.add(digit_numero, boundRect[i].x); if ( numero.charFault > maxNumCharFault ) { return std::string(); } } numero.ordenar(); std::string Num = numero.get(); std::cout<<"DORSAL: "<<Num<<std::endl; return Num; } else { return std::string(); } }
/* Back ground detection*/ void BackGroundDetection(Mat frame, Mat mask,BackgroundSubtractorMOG2 *pMog) { //copy and resize Mat frame_copy = frame.clone(),fore; if(BG_scale_factor != 1.0f) resize(frame_copy, frame_copy, Size(), BG_scale_factor, BG_scale_factor, INTER_AREA); pMog->operator ()(frame_copy,fore); vector<vector<Point> > contours; erode(fore,fore,Mat()); dilate(fore,fore,Mat()); findContours(fore, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE) ; vector<Rect> boundRect( contours.size() ); vector<vector<Point> > contours_poly( contours.size() ); vector<Vec4i> hierarchy; Mat drawing = Mat::zeros(fore.size(),CV_8UC1); // double smallest_area = contourArea( contours[0],false); for( int i = 0; i< contours.size(); i++ ) { approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true ); boundRect[i] = boundingRect( Mat(contours_poly[i]) ); if( (contourArea( contours[i],false) >= 100) && (contourArea( contours[i],false) < video_size.area() * 0.95)) { drawContours( drawing, contours, i, Scalar(255,255,255), CV_FILLED, 8, hierarchy); } //the min_size and max_size here should be fixed // if(boundRect[i].area() >= 100 && boundRect[i].area() < video_size.area() * 0.95) // { // // rectangle( drawing, boundRect[i].tl(), boundRect[i].br(), Scalar(255,255,255), -1, 8, 0); // drawContours( drawing, contours, i, Scalar(255,255,255), CV_FILLED, 8, hierarchy); // } } // imshow("drawing-scale",drawing); if(BG_scale_factor != 1.0f) resize(drawing, drawing, Size(video_size.width,video_size.height), 0,0, INTER_NEAREST); // imshow("drawing-original",drawing); for(int i = 0; i < mask.cols; i++) { for(int j = 0; j < mask.rows; j++) { Point p = Point(i,j); if(drawing.at<uchar>(p) == 255) mask.at<uchar>(p) += mask_add_step; } } }
double FillingRate(Mat src, int size) { Mat src_gray; cvtColor(src, src_gray, CV_BGR2GRAY); blur(src_gray, src_gray, Size(3, 3)); Mat threshold_output; vector<vector<Point> > contours; vector<Vec4i> hierarchy; /// Detect edges using Threshold threshold(src_gray, threshold_output, 253, 255, THRESH_BINARY); //imshow("", threshold_output); waitKey(0); /// Find contours findContours(threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0)); /// Approximate contours to polygons + get bounding rects and circles vector<vector<Point> > contours_poly(contours.size()); vector<Rect> boundRect(contours.size()); vector<Point2f>center(contours.size()); vector<float>radius(contours.size()); for (int i = 0; i < contours.size(); i++) { approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true); minEnclosingCircle((Mat)contours_poly[i], center[i], radius[i]); } /// Draw polygonal contour + bonding rects + circles Mat drawing = Mat::zeros(threshold_output.size(), CV_8UC3); for (int i = 0; i< contours.size(); i++) { //cout << arcLength(contours[i], true) << endl; Scalar color = Scalar(255, 255, 255); drawContours(drawing, contours_poly, i, color, 1, 8, vector<Vec4i>(), 0, Point()); circle(drawing, center[i], (int)radius[i], color, 2, 8, 0); } size = windowSize*windowSize - countNonZero(threshold_output); /// Show in a window //namedWindow("Contours", CV_WINDOW_AUTOSIZE); imshow("Contours", drawing); waitKey(0); cout << double(size) / (double(pow(radius[1], 2))*3.14); return double(size) / (double(pow(radius[1], 2))*3.14); }
void TwBoxLayout::layoutWidgets() { TwRect<int> rect = m_hostWidget->localRect(); TwMargin<int> contentMargin = m_hostWidget->contentMargin(); int childWidgetSpacing = m_hostWidget->childSpacing(); int x = rect.left() + contentMargin.left(); int y = rect.top() + contentMargin.top(); rect.moveTo(x, y); rect.resize(rect.width() - contentMargin.left() - contentMargin.right(), rect.height() - contentMargin.top() - contentMargin.bottom()); //TODO for(int i = 0; i < m_hostWidget->widgetCount(); ++i) { TwWidget* child = m_hostWidget->widgetAt(i); if(child->isVisible()) { TwRect<int> boundRect(x, y, rect.right(), rect.bottom()); TwSize<int> childSize = child->calcLayoutSize(); if (m_orientation == Tw::Horizontal) { boundRect.setWidth(childSize.width()); // boundRect.setHeight(twMin(boundRect.height(), childSize.height())); x += childSize.width() + childWidgetSpacing; } else { boundRect.setHeight(childSize.height()); // boundRect.setWidth(twMin(boundRect.width(), childSize.width())); y += childSize.height() + childWidgetSpacing; } child->setBoundRect(boundRect); } } }
cv::Mat ColorDetect::detect(const cv::Mat &img) { cv::Mat in, cvtImg, thrImg, eImg, dImg; cv::Mat element(5, 5, CV_8U, cv::Scalar(1)); std::vector<std::vector<cv::Point> > contours; in = img; cv::cvtColor(in, cvtImg, CV_BGR2HLS, 0); thrImg = getThresh(cvtImg); cv::dilate(thrImg, dImg, element); cv::dilate(dImg, eImg, element); for (int h = 0; h>4; h++) { cv::erode(eImg, eImg, element); } cv::dilate(eImg, dImg, element); cv::dilate(dImg, dImg, element); cv::findContours(dImg, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); std::vector<std::vector<cv::Point> > contours_poly(contours.size()); std::vector<cv::Rect> boundRect(contours.size()); for (unsigned int i = 0; i < contours.size(); i++) { cv::approxPolyDP(cv::Mat(contours[i]), contours_poly[i], 25, true); boundRect[i] = cv::boundingRect(cv::Mat(contours_poly[i])); } for (unsigned int j = 0; j < contours.size(); j++) { cv::rectangle(in, boundRect[j], cv::Scalar(0, 0, 255), 2); } return in; }
void SubPlotWnd::DrawString(Graphics * g) { Color fontColor; COLORREF color; bool succ = this->StrategyGetColor.Call(this, 0, color); ASSERT(succ); fontColor.SetFromCOLORREF(color); SolidBrush fontBrush(fontColor); StringFormat format; format.SetAlignment(StringAlignmentNear); Gdiplus::Font font(L"Arial", 10); PointF pointF(5, 2); CRect rect; this->GetClientRect(&rect); RectF boundRect( (Gdiplus::REAL)rect.left, (Gdiplus::REAL)rect.top, (Gdiplus::REAL)rect.right, (Gdiplus::REAL)rect.bottom ); //int frameCount = 1; //int stringLen = 0; //int validFrameCount = 0; //int frameIdx = plotWndProp->replayReadPos; //while( (frameIdx > 0) && (validFrameCount < frameCount) ) //{ // frameIdx--; // int frameDataCount = seriesProp->replayBuffer[frameIdx].len / sizeof(char); // if (frameDataCount > 0) // { // validFrameCount++; // } // stringLen += frameDataCount; // if (stringLen > 4*1024) // break; //} //char * string = new char[stringLen+1]; //char * dstPtr = string; //while( frameIdx < plotWndProp->replayReadPos ) //{ // int frameDataCount = seriesProp->replayBuffer[frameIdx].len / sizeof(char); // if (frameDataCount > 0) // { // memcpy(dstPtr, seriesProp->replayBuffer[frameIdx].data, frameDataCount * sizeof(char)); // dstPtr += frameDataCount; // } // frameIdx++; //} //*dstPtr = 0; CString stringToDraw(string); g->DrawString(stringToDraw, -1, &font, boundRect, &format, &fontBrush); //delete [] string; }
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);*/ }
int main( int argc, char** argv ) { /// Load an image cv::Mat src, greyIm, histeqIm; src = cv::imread( argv[1] ); if( !src.data ) { printf("Input file? No? ouuuupsss thooooorryyyyy\n"); return -1; } cv::Size s = src.size(); int rows = s.height; int cols = s.width; // Setup a rectangle to define your region of interest cv::Rect myROI(0, rows/2, cols, rows/2); // Crop the full image to that image contained by the rectangle myROI // Note that this doesn't copy the data cv::Mat croppedImage = src(myROI); cv::imwrite("output/1_low_half.jpg", croppedImage); cv::cvtColor(croppedImage, greyIm, cv::COLOR_BGR2GRAY); cv::Size crop_size = croppedImage.size(); int crop_rows = crop_size.height; int crop_cols = crop_size.width; cv::imwrite("output/2_grey_scale.jpg", greyIm); cv::equalizeHist( greyIm, histeqIm ); cv::imwrite("output/3_hist_eq.jpg", histeqIm); std::vector<std::vector<cv::Point> > contours; std::vector<cv::Vec4i> hierarchy; // Reduce noise with kernel 3x3 cv::Mat blurIm; blur(histeqIm, blurIm, cv::Size(3,3)); cv::imwrite("output/4_blur.jpg", blurIm); // Canny detector cv::Mat edgesIm; Canny(blurIm, edgesIm, thresh, thresh*ratio, kernel_size); cv::imwrite("output/5_edge.jpg", edgesIm); // Find contours cv::findContours(edgesIm, contours, hierarchy, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE, cv::Point(0,0)); // Approximate contours to polygons + get bounding rects and circles std::vector<std::vector<cv::Point> > contours_poly(contours.size()); std::vector<cv::Rect> boundRect(contours.size()); std::vector<cv::Point2f>center(contours.size()); std::vector<float>radius(contours.size()); for (int i = 0; i < contours.size(); i++) { cv::approxPolyDP(cv::Mat(contours[i]), contours_poly[i], 3, true); boundRect[i] = cv::boundingRect(cv::Mat(contours_poly[i])); cv::minEnclosingCircle((cv::Mat)contours_poly[i], center[i], radius[i]); } // Draw contours int j=0; cv::Mat drawing = cv::Mat::zeros(edgesIm.size(), CV_8UC3); cv::Mat piece[5], hsvIm[5]; for (int i = 0; i < contours.size(); i++) { if (!((boundRect[i].height >= boundRect[i].width/5) && (boundRect[i].height <= boundRect[i].width/2) && boundRect[i].height<=crop_rows/4 && boundRect[i].width<=crop_cols/2 && boundRect[i].height>=crop_rows/10 && boundRect[i].width>=crop_cols/6)) continue; cv::Rect roi = boundRect[i]; piece[j] = croppedImage(roi); imwrite("output/contour"+std::to_string(j)+".jpg", piece[j]); j++; cv::Scalar color = cv::Scalar(rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255)); cv::drawContours(drawing, contours, i, color, 2, 8, hierarchy, 0, cv::Point()); cv::rectangle(drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0); //circle(drawing, center[i], (int)radius[i], color, 2, 8, 0); } imwrite("output/6_contours.jpg", drawing); int h_bins = 50; int s_bins = 60; int histSize[] = { h_bins, s_bins }; float h_ranges[] = { 0, 180 }; float s_ranges[] = { 0, 256 }; const float* ranges[] = { h_ranges, s_ranges }; int channels[] = { 0, 1 }; cv::Mat hist[5]; for (int i=0; i<j; i++){ cvtColor(piece[i], hsvIm[i], cv::COLOR_BGR2HSV); imwrite("output/hsvIm"+std::to_string(i)+".jpg", hsvIm[i]); calcHist( &hsvIm[i], 1, channels, cv::Mat(), hist[i], 2, histSize, ranges, true, false ); //normalize( hsvIm[i], hsvIm[i], 0, 1, cv::NORM_MINMAX, -1, cv::Mat() ); } return 0; }
void TrackShirt::ImageCallback(const sensor_msgs::ImageConstPtr& msg) { cv_bridge::CvImagePtr cv_ptr; try { cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8); } catch (cv_bridge::Exception& e) { ROS_ERROR("cv_bridge exception: %s", e.what()); return; } frame = cv_ptr->image; char key = (char)cvWaitKey(10); if (key ==27 ) { ros::requestShutdown(); } else if ( key =='z' ) { IMSHOW = true; //namedWindow(OPENCV_WINDOW); } else if (key == 'x') { IMSHOW = false; cvDestroyAllWindows() ; //namedWindow(OPENCV_WINDOW); } if (trackObject == -1) { //Initial stage, before selecting object. Do nothing. Camera view shown as is. } else if (trackObject == 0) { rectangle(frame, Point(selection.x,selection.y),Point(selection.x+selection.width,selection.y+selection.height),Scalar(0,0,255),1); } else if (PerFoRoMode == 3) { Mat imgHSV, imgThresh, binFrame; int contSize; cvtColor(frame, imgHSV, CV_BGR2HSV); //Get binary image using HSV threshold inRange(imgHSV, mLowerBound, mUpperBound, imgThresh); //Morphological operations to get smoother blobs with reduced noise dilate( imgThresh, imgThresh, elemDilate ); erode( imgThresh, imgThresh, elemErode ); dilate( imgThresh, imgThresh, elemDilate ); erode( imgThresh, imgThresh, elemErode ); morphologyEx(imgThresh, imgThresh, MORPH_OPEN, structure_elem); imgThresh.copyTo(binFrame); vector<vector<Point> > contours; vector<Vec4i> hierarchy; /// Find contours findContours( binFrame, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) ); contSize = contours.size(); //cout<<"contours size "<<contSize<<endl; //If no contours if (contSize==0) { navX = 0; navY = 0; if (IMSHOW) { imshow(OPENCV_WINDOW, frame); //imshow("Binary Image with Detected Object", imgThresh); } return; } /// Approximate contours to polygons + get bounding rects vector<vector<Point> > contours_poly( contSize ); vector<Rect> boundRect( contSize ); /// Get the moments vector<Moments> mu(contSize ); cv::Mat contArea = Mat::zeros(contSize,1,CV_32FC1); for( int i = 0; i < contSize; i++ ) { approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true ); boundRect[i] = boundingRect( Mat(contours_poly[i]) ); mu[i] = moments( contours[i], false ); contArea.at<float>(i) = contourArea(contours[i]); } /// Get the mass centers: vector<Point2f> mc( contSize ); for( int i = 0; i < contSize; i++ ) { mc[i] = Point2f( mu[i].m10/mu[i].m00 , mu[i].m01/mu[i].m00 ); } ///Nearest centroid to previous position cv::Mat dist = Mat::zeros(contSize,1,CV_32FC1); cv::Mat normDist = Mat::zeros(contSize,1,CV_32FC1); for( int i = 0; i < contSize; i++ ) { dist.at<float>(i) = abs(mc[i].x - selectCentroid.x) + abs(mc[i].y - selectCentroid.y); normDist.at<float>(i) = maxDistance - dist.at<float>(i); } cv::Mat normSelect= Mat::zeros(contSize,1,CV_32FC1); normSelect = contArea + normDist; // cv::Mat sortedSelect = Mat::zeros(contSize,1,CV_32FC1); cv::sortIdx(normSelect, sortedSelect, CV_SORT_EVERY_COLUMN+CV_SORT_DESCENDING); Point selectPt = mc[sortedSelect.at<int>(0)]; //If first tracked frame, initialze Kalman if (trackObject == 1) { initTracker(); trackObject = 2; } //Kalman estimate based on previous state and measurement kalmanEstimatePt = kalmanTracker(selectPt); ///Distance of object position estimate from previous position distPrevCurrent = abs(kalmanEstimatePt.x - selectCentroid.x) + abs(kalmanEstimatePt.y - selectCentroid.y); distPrevCurrent = distPrevCurrent / maxDistance; if (missCount > 5) { distThresh*=1.5; } else { distThresh = minDistThresh; } /// ///////////////////////////////////////////////////////////// ///Threshold the detected centroid's distance from prev/////////////// if (distPrevCurrent < distThresh && contArea.at<float>(sortedSelect.at<int>(0)) >= 10) { //Final object position estimate using kalman selectCentroid = kalmanEstimatePt; if (IMSHOW) { rectangle( frame, boundRect[sortedSelect.at<int>(0)], Scalar(255,255,255), 2, 8, 0 ); } shirt_msg.x = selectCentroid.x; shirt_msg.y = selectCentroid.y; shirt_msg.area = boundRect[sortedSelect.at<int>(0)].width * boundRect[sortedSelect.at<int>(0)].height; track_shirt_pub_.publish(shirt_msg); //cout<<"X="<<navX<<"Y="<<navY<<endl; missCount = 0; drawArrow(frame, cv::Point(frame.cols/2, frame.rows/2), selectCentroid, Scalar(255,0,0)); } else { missCount++; navX = 0.0; navY = 0.0; } } // Update GUI Window //if (IMSHOW) { // imshow(OPENCV_WINDOW, frame); ///imshow("Binary Image with Detected Object", imgThresh); //} //cv::waitKey(3); // Output modified video stream image_shirt_pub_.publish(cv_ptr->toImageMsg()); }
void Vision_Objects::Main() { //Gui::getInstance(); //pluginTab = new Tab("Vision_Objects"); std::string action; cout<<"Intialization of ObjectRecognition"<<endl; cv::Mat kinect_rgb; cv::Mat kinect_gray; cv::Mat kinect_depth; cv::Mat contours_img; cv::Mat cutout; std::stringstream ss; //cout<<sharedMemory->getInstance().requestedObject->c_str()<<endl; //printf(sharedMemory->getInstance().requestedObject->c_str()); // sharedMemory->getInstance().setAction("reconocer_objeto"); int conta=0; OrbRecognizer OrbObject; OrbObject.setThreshold(0.8); OrbObject.setRecognitionTreshold(5); OrbObject.setMachine(3); // es SIFT. OrbObject.setDistance(1); bool newobject=true; for (;;) { action=sharedMemory->getInstance().getAction(); if (action=="recognizeObject") { cout<<"Starting: "<< action << " STATE in ObjectRecognition"<<endl; //TODO revisar posición de esta instrucción //Se indica a memoria compartida la orden que se esta atendiendo if(newobject) { ss.str(""); if (sharedMemory->getInstance().getTestRunning()=="CocktailParty") { sharedMemory->getInstance().setLastObjective(sharedMemory->getInstance().getUser(userOrder)); cout<< "***USER: "******" Name: "<< sharedMemory->getInstance().getUser(userOrder).getName()<<" Order: "<< sharedMemory->getInstance().getUser(userOrder).getOrder()<<endl; ss << "../data/Objects/" << sharedMemory->getInstance().getLastObjective().getOrder() << "/"; } else ss << "../data/Objects/" << sharedMemory->getInstance().getRequestedObject() << "/"; cout<< ss.str().c_str()<<endl; OrbObject.TrainingSet(ss.str().c_str()); newobject=false; } cv::RNG rng(12345); kinect_depth=sharedMemory->getInstance().kinectInfo->get_depth(); kinect_rgb = sharedMemory->getInstance().kinectInfo->get_RGB(); int threshold=50; cv::threshold(kinect_depth, kinect_depth, threshold, 255, cv::THRESH_TOZERO_INV ); cv::threshold(kinect_depth, kinect_depth, threshold-5, 255, cv::THRESH_TOZERO_INV); cv::Mat destination; cv::resize(kinect_depth, destination, kinect_rgb.size(), 0, 0, cv::INTER_CUBIC); // cv::blur( kinect_depth, kinect_depth, cv::Size(3,3) ); cv::namedWindow("threshold", CV_WINDOW_AUTOSIZE); cv::imshow("threshold", destination); vector<vector<cv::Point> > contours; vector<cv::Vec4i> hirarchy; cv::findContours(destination, contours, hirarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cv::Point(0,0)); vector<vector<cv::Point> >contours_poly(contours.size()); vector<cv::Rect> boundRect (contours.size()); vector<cv::Point2f> center(contours.size()); vector<float>radius(contours.size()); for(int i=0; i< contours.size(); i++) { cv::approxPolyDP( cv::Mat(contours[i]), contours_poly[i], 3, true ); boundRect[i]=cv::boundingRect( cv::Mat(contours_poly[i]) ); cv::minEnclosingCircle( (cv::Mat)contours_poly[i], center[i], radius[i] ); } cvtColor( kinect_rgb, kinect_gray, CV_RGB2GRAY ); cv::GaussianBlur( kinect_gray, kinect_gray, cv::Size( 3, 3 ), 0, 0 ); contours_img= cv::Mat::zeros( destination.size(), CV_8UC3); for (int i=0; i < contours.size(); i++) { cv::Scalar color= cv::Scalar(rng.uniform(0,255), rng.uniform(0,255), rng.uniform(0,255) ); cv::drawContours( destination, contours_poly, i, color, 1, 8, vector<cv::Vec4i>(), 0, cv::Point() ); cv::rectangle(contours_img, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0); cv::circle(contours_img, center[i], (int)radius[i], color, 2, 8, 0); if( boundRect[i].height>64 && boundRect[i].height<512 && boundRect[i].width>64 && boundRect[i].width<512 ) { cutout=kinect_gray(boundRect[i]); cv::imshow("cutout",cutout); std::cout << "ultima altura " << boundRect[i].height << "ultimo ancho " << boundRect[i].width << std::endl; cv::waitKey(100); std::cout << "despues de WAIT"<< std::endl; if(OrbObject.evalWindow(cutout)) { std::cout << "ENCONTRE" << std::endl; cv::rectangle(kinect_rgb, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0); string phrase; if (sharedMemory->getInstance().getTestRunning()=="Emergency") phrase = "I found the "+ sharedMemory->getInstance().getRequestedObject(); else phrase = "I found the "+ sharedMemory->getInstance().getLastObjective().getOrder(); sharedMemory->getInstance().sintetizer.set_Phrase(phrase); //sharedMemory->getInstance().setAction(cambiar_estado("objeto_reconocido","si")); std::cout << "El objeto esta en los pixeles " << center[i].x << center[i].y << std::endl; sharedMemory->getInstance().setObjectPositionX(center[i].x/2); sharedMemory->getInstance().setObjectPositionY(center[i].y/2.1333333); newobject=true; // cv::destroyAllWindows(); //lastMoment i= contours.size(); userOrder++; // std::stringstream pp; // pp<< "../data/EmergencyReport/EmergencyObjectRequested.png"; // // conta++; cv::imwrite("../data/EmergencyReport/EmergencyObjectRequested.png", cutout); // std::stringstream cc; // cc<< "../data/SI/Kine" << conta << ".png"; // conta++; // cv::imwrite(cc.str().c_str(), kinect_rgb); sharedMemory->getInstance().setAction("computePoint"); } else { std::cout << "NO ENCONTRE" << std::endl; // std::stringstream pp; // pp<< "../data/Objects/negatives/" << conta << ".png"; // conta++; // cv::imwrite(pp.str().c_str(), cutout); } } } } } }
void Projector::showRectangle(bool gpuView) { indices.resize(480); for (int i = 0; i < 480; i++) indices[i].resize(640); libfreenect2::Registration* registration = new libfreenect2::Registration(_dev->getIrCameraParams(), _dev->getColorCameraParams()); libfreenect2::Frame undistorted(512, 424, 4), registered(512, 424, 4); libfreenect2::FrameMap frames; SimpleViewer viewer; bool shutdown = false; cv::Mat board(480, 640, CV_8UC4, cv::Scalar::all(255)); if (!gpuView) { cv::namedWindow("reprojection", CV_WINDOW_NORMAL); cv::moveWindow("reprojection", 0, 0); //setWindowProperty("reprojection", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN); } else { viewer.setSize(480, 640); // TO-DO change resolution viewer.initialize(); libfreenect2::Frame b(640, 480, 4); b.data = board.data; viewer.addFrame("RGB", &b); shutdown = shutdown || viewer.render(); } while (!shutdown) { board = cv::Mat(480, 640, CV_8UC4, cv::Scalar::all(255)); std::vector<cv::Point3f> wrldSrc; std::vector<cv::Point3f> plnSrc; if (!gpuView) cv::imshow("reprojection", board); (_listener)->waitForNewFrame(frames); libfreenect2::Frame *rgb = frames[libfreenect2::Frame::Color]; libfreenect2::Frame *depth = frames[libfreenect2::Frame::Depth]; registration->apply(rgb, depth, &undistorted, ®istered, true, NULL, NULL); for (int i = 0; i<512; i++) { for (int j = 0; j<424; j++) { float x = 0, y = 0, z = 0, color = 0; registration->getPointXYZRGB(&undistorted, ®istered, i, j, x, y, z, color); if (z>0.5 && z<2.1) { x = static_cast<float>(x + right / ((double)640.0)); //////////TO-DO fix that y = static_cast<float>(y + up / ((double)480.0)); x -= 0.5; y -= 0.5; double PI = 3.14159265; x = static_cast<float>(std::cos(rotX * PI / 180) * x - std::sin(rotX * PI / 180) * y); y = static_cast<float>(std::sin(rotX * PI / 180) * x + std::cos(rotX * PI / 180) * y); x += 0.5; y += 0.5; wrldSrc.push_back(cv::Point3f(x * 100, y * 100, z * 100)); } } } PlaneData pln = findRectangle(registration, &undistorted, ®istered); if (wrldSrc.size() > 0) { std::vector<cv::Point2f> projected = projectPoints(wrldSrc); for (int i = 0; i < projected.size(); i++) { if (480 - projected[i].x >0 && projected[i].y > 0 && 480 - projected[i].x < 475 && projected[i].y < 630) { cv::Mat ROI = board(cv::Rect(static_cast<int>(projected[i].y), static_cast<int>(480 - projected[i].x), 2, 2)); ROI.setTo(cv::Scalar(100, 100, 150, 100)); } } if (pln.points.size() > 0) { projected = projectPoints(pln.points); cv::Mat cont = cv::Mat(480, 640, CV_8UC1, cv::Scalar::all(0)); for (int i = 0; i < projected.size(); i++) { if (480 - projected[i].x >0 && projected[i].y > 0 && 480 - projected[i].x < 475 && projected[i].y < 630) { cv::Mat ROI = board(cv::Rect(static_cast<int>(projected[i].y), static_cast<int>(480 - projected[i].x), 2, 2)); ROI.setTo(cv::Scalar(250, 100, 100, 100)); cont.at<uchar>(static_cast<int>(480 - projected[i].x), static_cast<int>(projected[i].y), 0) = 255; indices[static_cast<int>(480 - projected[i].x)][static_cast<int>(projected[i].y)] = i; } } vector<vector<cv::Point> > contours; vector<cv::Vec4i> hierarchy; cv::GaussianBlur(cont, cont, cv::Size(7, 7), 5, 11); findContours(cont, contours, hierarchy, cv::RETR_CCOMP, cv::CHAIN_APPROX_NONE, cv::Point(0, 0)); vector<vector<cv::Point> > contours_poly(contours.size()); vector<cv::Rect> boundRect(contours.size()); vector<cv::Point2f>center(contours.size()); vector<float>radius(contours.size()); int nPoly; for (int i = 0; i < contours.size(); i++) { cv::approxPolyDP(cv::Mat(contours[i]), contours_poly[i], 10, true); nPoly = contours_poly[i].size(); } for (int i = 0; i< contours.size(); i++) { drawContours(board, contours_poly, 0, cv::Scalar(0, 255, 0), 5); } } if (!gpuView) imshow("reprojection", board); else { libfreenect2::Frame b(640, 480, 4); b.data = board.data; viewer.addFrame("RGB", &b); shutdown = shutdown || viewer.render(); } } (_listener)->release(frames); if (!gpuView) { int op = cv::waitKey(50); if (op == 100 || (char)(op) == 'd') right -= 1; if (op == 115 || (char)(op) == 's') up += 1; if (op == 97 || (char)(op) == 'a') right += 1; if (op == 119 || (char)(op) == 'w') up -= 1; if (op == 114 || (char)(op) == 'r') rotX -= 0.5; if (op == 102 || (char)(op) == 'f') rotX += 0.5; if (op == 1113997 || op == 1048586 || op == 1048608 || op == 10 || op == 32) { std::cout << "right = " << right << ";\nup = " << up << ";\nrotX = " << rotX << ";\n"; break; } } else { right = 0; up = 0; rotX = 0; right = viewer.offsetX; up = viewer.offsetY; rotX = viewer.rot; } } if (!gpuView) cv::destroyWindow("reprojection"); else { viewer.stopWindow(); } }
void Projector::objProjectionOffline(std::string objPath, std::string objName, bool gpuView) { std::cout << "Camera init: "; objObject obj(objPath, objName); obj.loadData(); cout << "DONE\n"; cv::namedWindow("objTest", CV_WINDOW_NORMAL); cv::moveWindow("objTest", 0, 0); indices.resize(480); for (int i = 0; i < 480; i++) indices[i].resize(640); libfreenect2::Registration* registration = new libfreenect2::Registration(_dev->getIrCameraParams(), _dev->getColorCameraParams()); libfreenect2::Frame undistorted(512, 424, 4), registered(512, 424, 4); libfreenect2::FrameMap frames; SimpleViewer viewer; bool shutdown = false; cv::Mat board(480, 640, CV_8UC4, cv::Scalar::all(255)); cv::Vec3f prevNormal(-1, -1, -1); if (!gpuView) { cv::namedWindow("reprojection", CV_WINDOW_NORMAL); cv::moveWindow("reprojection", 200, 200); //setWindowProperty("reprojection", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN); } else { viewer.setSize(480, 640); // TO-DO change resolution viewer.initialize(); libfreenect2::Frame b(640, 480, 4); b.data = board.data; viewer.addFrame("RGB", &b); shutdown = shutdown || viewer.render(); } while (!shutdown) { board = cv::Mat(480, 640, CV_8UC4, cv::Scalar::all(255)); std::vector<cv::Point3f> plnSrc; if (!gpuView) cv::imshow("reprojection", board); (_listener)->waitForNewFrame(frames); libfreenect2::Frame *rgb = frames[libfreenect2::Frame::Color]; libfreenect2::Frame *depth = frames[libfreenect2::Frame::Depth]; registration->apply(rgb, depth, &undistorted, ®istered, true, NULL, NULL); PlaneData pln = findRectangle(registration, &undistorted, ®istered); if (pln.points.size() > 0) { std::vector<cv::Point2f> projected = projectPoints(pln.points); cv::Mat cont = cv::Mat(480, 640, CV_8UC1, cv::Scalar::all(0)); for (int i = 0; i < projected.size(); i++) { if (480 - projected[i].x >0 && projected[i].y > 0 && 480 - projected[i].x < 475 && projected[i].y < 630) { cv::Mat ROI = board(cv::Rect(static_cast<int>(projected[i].y), static_cast<int>(480 - projected[i].x), 2, 2)); ROI.setTo(cv::Scalar(250, 100, 100, 100)); cont.at<uchar>(static_cast<int>(480 - projected[i].x), static_cast<int>(projected[i].y), 0) = 255; plnSrc.push_back(pln.points[i]); } } vector<vector<cv::Point> > contours; vector<cv::Vec4i> hierarchy; cv::GaussianBlur(cont, cont, cv::Size(7, 7), 5, 11); findContours(cont, contours, hierarchy, cv::RETR_CCOMP, cv::CHAIN_APPROX_NONE, cv::Point(0, 0)); vector<vector<cv::Point> > contours_poly(contours.size()); vector<cv::Rect> boundRect(contours.size()); vector<cv::Point2f>center(contours.size()); vector<float>radius(contours.size()); for (int i = 0; i < contours.size(); i++) { cv::approxPolyDP(cv::Mat(contours[i]), contours_poly[i], 10, true); } for (int i = 0; i < contours.size(); i++) { drawContours(board, contours_poly, 0, cv::Scalar(0, 255, 0), 5); } cv::Mat data_pts = cv::Mat(300, 3, CV_64FC1); cv::Vec3f normal(0, 0, 0); int jump = plnSrc.size() / 300; for (int i = 0; i < 100; i++) { data_pts.at<double>(i, 0) = plnSrc[i*jump].x; data_pts.at<double>(i, 1) = plnSrc[i*jump].y; data_pts.at<double>(i, 2) = plnSrc[i*jump].z; data_pts.at<double>(i + 100, 0) = plnSrc[(i + 100)*jump].x; data_pts.at<double>(i + 100, 1) = plnSrc[(i + 100)*jump].y; data_pts.at<double>(i + 100, 2) = plnSrc[(i + 100)*jump].z; data_pts.at<double>(i + 200, 0) = plnSrc[(i + 200) *jump].x; data_pts.at<double>(i + 200, 1) = plnSrc[(i + 200)*jump].y; data_pts.at<double>(i + 200, 2) = plnSrc[(i + 200)*jump].z; } cv::PCA pca_analysis(data_pts, cv::Mat(), CV_PCA_DATA_AS_ROW); cv::Vec3f cntr = cv::Vec3f((pca_analysis.mean.at<double>(0, 0)), (pca_analysis.mean.at<double>(0, 1)), (pca_analysis.mean.at<double>(0, 2))); vector<cv::Point3f> eigen_vecs(2); vector<double> eigen_val(2); for (int i = 0; i < 2; ++i) { eigen_vecs[i] = cv::Point3f(pca_analysis.eigenvectors.at<double>(i, 0), pca_analysis.eigenvectors.at<double>(i, 1), pca_analysis.eigenvectors.at<double>(i, 2)); eigen_val[i] = pca_analysis.eigenvalues.at<double>(0, i); } cv::Vec3f p1 = cv::Vec3f((eigen_vecs[0].x * eigen_val[0]), (eigen_vecs[0].y * eigen_val[0]), (eigen_vecs[0].z * eigen_val[0])); cv::Vec3f p2 = cv::Vec3f((eigen_vecs[1].x * eigen_val[1]), (eigen_vecs[1].y * eigen_val[1]), (eigen_vecs[1].z * eigen_val[1])); normal = p1.cross(p2); normal = cv::normalize(normal); //pln.center = cntr; pln.normal = normal; obj.setCamera(cv::Point3f(pln.center.x, -pln.center.y, -pln.center.z + 150), cv::Vec3f(pln.normal[0], pln.normal[1], pln.normal[2])); if (!gpuView) imshow("reprojection", board); else { libfreenect2::Frame b(640, 480, 4); b.data = board.data; viewer.addFrame("RGB", &b); shutdown = shutdown || viewer.render(); } } cv::Mat im = obj.render(); cv::imshow("objTest", im); //} (_listener)->release(frames); if (!gpuView) { int op = cv::waitKey(50); if (op == 100 || (char)(op) == 'd') right -= 1; if (op == 115 || (char)(op) == 's') up += 1; if (op == 97 || (char)(op) == 'a') right += 1; if (op == 119 || (char)(op) == 'w') up -= 1; if (op == 114 || (char)(op) == 'r') rotX -= 0.5; if (op == 102 || (char)(op) == 'f') rotX += 0.5; if (op == 1113997 || op == 1048586 || op == 1048608 || op == 10 || op == 32) { std::cout << "right = " << right << ";\nup = " << up << ";\nrotX = " << rotX << ";\n"; break; } } else { //right = 0; //up = 0; //rotX = 0; right = viewer.offsetX; up = viewer.offsetY; rotX = viewer.rot; } } if (!gpuView) cv::destroyWindow("reprojection"); else { viewer.stopWindow(); } cv::destroyWindow("objTest"); }
vector<Point> Camera::Follow() //prima void { //float* contour=new float[height*width](); float* output=new float[height*width](); vector<float> temp = ToArray(Mat::zeros(height, width, CV_8UC1 )); output = &temp[0]; cout << "[START] Active Contour " << endl; // Follow the camera cout << "Following the camera ..."; cout << flush; Point center(-1,-1); Vec3b hsv; Mat mask, gray, HSV; Scalar lowerb, upperb; int erosion_size = 2, dilation_size = 10; Mat erodeElement = getStructuringElement(MORPH_RECT, Size(2 * erosion_size + 1, 2 * erosion_size + 1), Point(erosion_size, erosion_size) ); Mat dilateElement = getStructuringElement(MORPH_RECT, Size(2 * dilation_size + 1, 2 * dilation_size + 1), Point(dilation_size, dilation_size) ); vector<float> frameArray, maskArray; Mat ROI = Mat::zeros( height, width, CV_8UC1 ); int count = 0; double sum = 0; //while(waitKey(1) == -1) {//da togliere /*if (capture.read(frame) == NULL) { cout << "[ERROR] frame not read" << endl; return; }*/ //frame già settato clock_t startTime = clock(); // compute the time cvtColor(frame, gray, COLOR_RGB2GRAY); cvtColor(frame, HSV, COLOR_RGB2HSV); setMouseCallback("Frame", onMouse); if( drawing_box ) draw_box(&frame, roi); if(clicked) { // Init mask if(!haveMask) { // Take hsv from mouse center = initCenter; hsv = HSV.at<Vec3b>(center.y,center.x); haveMask = true; //cout << "HSV: " << hsv << endl; lowerb = Scalar(hsv.val[0] - 30, hsv.val[1] - 50, hsv.val[2] - 50); upperb = Scalar(hsv.val[0] + 30, hsv.val[1] + 50, hsv.val[2] + 50); //cout << "lowerb: " << lowerb << endl; //cout << "upperb: " << upperb << endl; ROI = Mat::zeros( height, width, CV_8UC1 ); rectangle( ROI, roi.tl(), roi.br(), Scalar(255), -1); sum = 0; count = 0; //benchmark } // Create the mask inRange(HSV, lowerb , upperb, mask); dilate(mask, mask, dilateElement); mask = mask.mul(ROI); //imshow("mask", mask); frameArray = ToArray(gray); maskArray = ToArray(mask); ActiveContour(&frameArray[0], output, contour, &maskArray[0], width, height, roi.br().y); Mat OUT = ToMat(output, height, width); OUT.convertTo(OUT, CV_8UC1); //imshow("Output", OUT); vector<vector<Point> > contours; vector<Vec4i> hierarchy; findContours(OUT, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0)); vector<Rect> boundRect( contours.size() ); for( int i = 0; i < contours.size(); i++ ) { boundRect[i] = boundingRect( Mat(contours[i]) ); } /// Draw polygonal contour + bonding rects + circles Mat drawing = Mat::zeros( height, width, CV_8UC3 ); circle(frame, center, 5, Scalar(0,0,255), 5); for( int i = 0; i< contours.size(); i++ ) { if(boundRect[i].contains(center)) { drawContours( frame, contours, i, Scalar(255,255,255), 1, 8, vector<Vec4i>(), 0, Point() ); rectangle( frame, boundRect[i].tl(), boundRect[i].br(), Scalar(0,255,0), 2, 8, 0 ); // Center center.x = boundRect[i].tl().x + boundRect[i].size().width/2; center.y = boundRect[i].tl().y + boundRect[i].size().height/2; int x = boundRect[i].size().width; int y = boundRect[i].size().height; int v = (int)(sqrt((x/(y+EPS))*area)); int h = (int)(area/v); int deltax = (int)((h-y)/2); int deltay = (int)((v-x)/2); int tlx = boundRect[i].tl().x -deltax; int tly = boundRect[i].tl().y -deltay; int brx = boundRect[i].br().x +deltax; int bry = boundRect[i].br().y +deltay; tlx = (tlx < 0) ? 0 : tlx; brx = (brx > width) ? width : brx; tly = (tly < 0) ? 0 : tly; bry = (bry > height) ? height : bry; roi = Rect(Point(tlx,tly),Point(brx,bry)); ROI = Mat::zeros( height, width, CV_8UC1 ); rectangle( ROI, roi.tl(), roi.br(), Scalar(255), -1); rectangle( frame, roi.tl(), roi.br(), Scalar(0,0,255), 2, 8, 0 ); //imshow("ROI", ROI); break; } } } imshow("Frame", frame); sum += double( clock() - startTime ) / (double)CLOCKS_PER_SEC; count++; // } cout << sum / count << endl << flush; return contours.front(); }
void getContours(const char* filename) { cv::Mat img = cv::imread(filename, 0); /* //Apply blur to smooth edges and use adapative thresholding cv::Size size(3,3); cv::GaussianBlur(img,img,size,0); adaptiveThreshold(img, img,255,CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY,75,10); cv::bitwise_not(img, img); */ cv::Mat tmp; cv::GaussianBlur(img, tmp, cv::Size(25,25), 25); cv::addWeighted(img, 1.5, tmp, -0.5, 0, img); adaptiveThreshold(img, img,255,CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY,55,60); cv::bitwise_not(img, img); cv::Mat img2 = img.clone(); std::vector<cv::Point> points; cv::Mat_<uchar>::iterator it = img.begin<uchar>(); cv::Mat_<uchar>::iterator end = img.end<uchar>(); for (; it != end; ++it) if (*it) points.push_back(it.pos()); cv::RotatedRect box = cv::minAreaRect(cv::Mat(points)); double angle = box.angle; if (angle < -45.) angle += 90.; cv::Point2f vertices[4]; box.points(vertices); for(int i = 0; i < 4; ++i) cv::line(img, vertices[i], vertices[(i + 1) % 4], cv::Scalar(255, 0, 0), 1, CV_AA); cv::Mat rot_mat = cv::getRotationMatrix2D(box.center, angle, 1); cv::Mat rotated; cv::warpAffine(img2, rotated, rot_mat, img.size(), cv::INTER_CUBIC); cv::Size box_size = box.size; if (box.angle < -45.) std::swap(box_size.width, box_size.height); cv::Mat cropped; cv::getRectSubPix(rotated, box_size, box.center, cropped); cv::imshow("Cropped", cropped); // imwrite("example5.jpg",cropped); Mat cropped2=cropped.clone(); cvtColor(cropped2,cropped2,CV_GRAY2RGB); Mat cropped3 = cropped.clone(); cvtColor(cropped3,cropped3,CV_GRAY2RGB); vector<vector<Point> > contours; vector<Vec4i> hierarchy; /// Find contours cv:: findContours( cropped, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_TC89_KCOS, Point(0, 0) ); /// Approximate contours to polygons + get bounding rects and circles vector<vector<Point> > contours_poly( contours.size() ); vector<Rect> boundRect( contours.size() ); vector<Point2f>center( contours.size() ); vector<float>radius( contours.size() ); //Get poly contours for( int i = 0; i < contours.size(); i++ ) { approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true ); } //Get only important contours, merge contours that are within another vector<vector<Point> > validContours; for (int i=0;i<contours_poly.size();i++){ Rect r = boundingRect(Mat(contours_poly[i])); if(r.area()<100)continue; bool inside = false; for(int j=0;j<contours_poly.size();j++){ if(j==i)continue; Rect r2 = boundingRect(Mat(contours_poly[j])); if(r2.area()<100||r2.area()<r.area())continue; if(r.x>r2.x&&r.x+r.width<r2.x+r2.width&& r.y>r2.y&&r.y+r.height<r2.y+r2.height){ inside = true; } } if(inside)continue; validContours.push_back(contours_poly[i]); } //Get bounding rects for(int i=0;i<validContours.size();i++){ boundRect[i] = boundingRect( Mat(validContours[i]) ); } //Display Scalar color = Scalar(0,255,0); for( int i = 0; i< validContours.size(); i++ ) { if(boundRect[i].area()<100)continue; drawContours( cropped2, validContours, i, color, 1, 8, vector<Vec4i>(), 0, Point() ); rectangle( cropped2, boundRect[i].tl(), boundRect[i].br(),color, 2, 8, 0 ); } //imwrite("example6.jpg",cropped2); imshow("Contours",cropped2); extractContours(cropped3,validContours); cv::waitKey(0); }
void Dimage::salient_detection() { Mat hsl,h,s,l,hg,lg, blkWhte, grad, img; vector<vector<Point> > contours; Mat src = this->image; Mat cropped; string buffer; GaussianBlur(this->image, img, Size(3, 3), 0, 0, BORDER_DEFAULT); cout << "blurred" << endl; vector<Mat> channels(3); cvtColor(img, hsl, CV_RGB2HLS); split(hsl, channels); cout << "splitted" << endl; h = channels[1]; s = channels[0]; l = channels[2]; hg = gradient(h); lg = gradient(l); grad = h + l; grad = CannyThreshold(grad,grad); h = CannyThreshold(h,h); cout << "thresholded" << endl; grad = (grad & h); /// Find contours findContours(grad, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0)); vector<vector<Point> > contours_poly(contours.size()); vector<Rect> boundRect(contours.size()); /// Approximate contours to polygons + get bounding rects and circles cout << "countours found" << endl; cout << contours.size() << endl; for (int i = 0; i < contours.size(); i++) { cout << "i: " << i << endl; if (contourArea(contours.at(i)) < 600) { cout << "in da if" << endl; approxPolyDP(Mat(contours.at(i)), contours_poly.at(i), 3, true); cout << "at roi" << endl; //boundingRect(Mat(contours[i])); Rect roi = boundingRect(Mat(contours.at(i))); /*cropped = src(roi); imshow("Cropped image", cropped); cvWaitKey(0);*/ this->ROI.push_back(roi); cout << "size of victor: " << this->ROI.size() << endl; } } cout << "bounding rect found" << endl; };