/////////////////////////////////////////////////////////////////// // Panel::CannyDetection() // Description: This function is called by DetectEdges() and it // is the Canny edge detection function which does not contain // debugging statements. We run the image through several different // image processing functions to prepare the image before edge // detection. After detection the edges we run Hough lines which // approximates lines of minimum length as specified in // Settings.xml. We find all intersections of the Hough lines // then make a minimum area rectangle around the intersections to // approximate the edges of the panel which we are trying to // measure. From there we use the unit conversion calculated // in DetectFeatures() to find a length and width of the current // panel. We report the length and width with a message box. /////////////////////////////////////////////////////////////////// Mat Panel::CannyDetection(Mat image, bool showImg) { Mat greyImage; cvtColor(image, greyImage, CV_BGR2GRAY); Mat eroded, dilated, thresh, blurredThresh, edges, edgesGray; vector<Vec2f> lines; threshold(greyImage, thresh, m_lowCannyThreshold, 255, THRESH_BINARY); erode(thresh, eroded, Mat()); dilate(eroded, dilated, Mat()); GaussianBlur(thresh, blurredThresh, Size(7, 7), m_sigmaX, m_sigmaY); Canny(blurredThresh, edges, m_cannyLow, m_cannyLow*m_ratio, 3); HoughLines(edges, lines, 1, CV_PI / 180, m_houghLength, 0, 0); cvtColor(edges, edgesGray, CV_GRAY2BGR); for (size_t i = 0; i < lines.size(); i++) { float rho = lines[i][0], theta = lines[i][1]; Point pt1, pt2; double a = cos(theta), b = sin(theta); double x0 = a*rho, y0 = b*rho; pt1.x = cvRound(x0 + 1000 * (-b)); pt1.y = cvRound(y0 + 1000 * (a)); pt2.x = cvRound(x0 - 1000 * (-b)); pt2.y = cvRound(y0 - 1000 * (a)); line(edgesGray, pt1, pt2, Scalar(0, 0, 255), 3, CV_AA); } //////////////////////////////////////////////////////// // Compute the intersection from the lines detected //////////////////////////////////////////////////////// vector<Point2f> intersections; for (size_t i = 0; i < lines.size(); i++) { for (size_t j = 0; j < lines.size(); j++) { Vec2f line1 = lines[i]; Vec2f line2 = lines[j]; if (acceptLinePair(line1, line2, (float)CV_PI / 32)) { Point2f intersection = computeIntersect(line1, line2); if (intersection.x >= 0 && intersection.y >= 0) intersections.push_back(intersection); } } } if (intersections.size() > 0) { vector<Point2f>::iterator i; for (i = intersections.begin(); i != intersections.end(); ++i) { cout << "Intersection is " << i->x << ", " << i->y << endl; circle(image, *i, 2, Scalar(0, 255, 0), 3); } // Find the minimum bounding rectangle RotatedRect rect; Point2f rectPoints[4]; Scalar color = Scalar(255, 0, 0); if (intersections.size() == 4) { // TODO } rect = minAreaRect(intersections); rect.points(rectPoints); int j = 0; for (j; j < 4; j++) line(image, rectPoints[j], rectPoints[(j + 1) % 4], color, 5, 8); float topLength = (float)norm(rectPoints[1] - rectPoints[0]); float botLength = (float)norm(rectPoints[3] - rectPoints[2]); float panelWidthPixels = topLength < botLength ? topLength : botLength; float leftHeight = (float)norm(rectPoints[3] - rectPoints[0]); float rightHeight = (float)norm(rectPoints[2] - rectPoints[1]); float panelHeightPixels = leftHeight < rightHeight ? leftHeight : rightHeight; string dimensionDisplayPixels = "Pixels:\nWidth: " + to_string(panelWidthPixels) + " pixels\nHeight: " + to_string(panelHeightPixels) + " pixels"; // ShowMessage(dimensionDisplayPixels); if (m_conversionRate) { float panelWidthReal = panelWidthPixels / m_conversionRate; float panelHeightReal = panelHeightPixels / m_conversionRate; string dimensionDisplayActual = "Actual:\nWidth: " + to_string(panelWidthReal) + " cm\nHeight: " + to_string(panelHeightReal) + " cm"; ShowMessage(dimensionDisplayActual); } } if (showImg){ namedWindow("Intersections", CV_WINDOW_KEEPRATIO); imshow("Intersections", image); } ///////////////////////////////////////////////////////////// // End of Computing the intersection from the lines detected ///////////////////////////////////////////////////////////// return edges; }
void CHuman::human_detect(Mat &morph,Mat &displayframe) { int centroidX = 0; int centroidY = 0; int label = 0 ; int label1 = 0; for(unsigned int i = 0;i < m_BlobRects.size();i++)//while(pos) { Rect rt = m_BlobRects[i]; if(rt.x + rt.width < morph.cols && rt.y + rt.height < morph.rows) { if(rt.width * rt.height<500) continue; centroidX = rt.x + rt.width/2; centroidY = rt.y + rt.height/2; blobdata.x = rt.x + rt.width/2; blobdata.y = rt.y + rt.height/2; blobdata.w = rt.width; blobdata.h = rt.height; blobdata.id = label1; Rect monitorzone =Rect(60, 0,displayframe.cols-60, displayframe.cols); if((centroidX > monitorzone.x)&&(centroidX < monitorzone.x +monitorzone.width)&&(centroidY>monitorzone.y)&&( centroidY < monitorzone.y +monitorzone.height) ) { label1++; humanlist.push_back(blobdata); } blobdata.x = rt.x + rt.width/2; blobdata.y = rt.y + rt.height/2; blobdata.w = rt.width; blobdata.h = rt.height; blobdata.id = label; for(unsigned int k=0;k < MonitorZoneRects.size() ;k++) { if((centroidX > MonitorZoneRects[k].x)\ &&(centroidX < MonitorZoneRects[k].x + MonitorZoneRects[k].width)\ &&(centroidY > MonitorZoneRects[k].y)\ &&(centroidY < MonitorZoneRects[k].y + MonitorZoneRects[k].height)) { label++; object.push_back(cv::Point(centroidX,centroidY)); //humanlist.push_back(blobdata); rectangle(displayframe, rt, Scalar( 255, 0, 0 ), 2, 8, 0); rectangle(displayframe,cvPoint(centroidX-1,centroidY-1),cvPoint(centroidX+1,centroidY+1),cvScalar(123,123,0),3,8,0); //alarm = 1; char idstr[100]; sprintf(idstr, "%d,%d",blobdata.id,blobdata.w*blobdata.h); putText(displayframe,idstr,cvPoint(blobdata.x,blobdata.y),CV_FONT_HERSHEY_COMPLEX, 1, cvScalar(0,0,255)); } } } }//for end }
//found the human body in the frame and return the foundRect as vector //The parameter Mat frame will be drawen on several rectangles void MotionDetection::PeopleDetectByHOG( Mat frame , Mat mask, double ScaleFactor, HOGDescriptor hog) { Mat frameCopy = frame.clone(); resize(frameCopy, frameCopy, Size(), ScaleFactor, ScaleFactor, INTER_AREA); cout << frameCopy.size() << endl; // imshow("test1",frameCopy); vector<Rect> found, found_filtered; //do detection //if the size of src image is too small, the program will be error as assertion fault hog.detectMultiScale(frameCopy, found, 0, Size(8,8), Size(32,32), 1.05, 2); // hog.detectMultiScale(frameCopy, found); //remove nested rectangle size_t i, j; for( i = 0; i < found.size(); i++ ) { Rect r = found[i]; for( j = 0; j < found.size(); j++ ) if( j != i && (r & found[j]) == r) break; if( j == found.size() ) found_filtered.push_back(r); } Mat drawing = Mat::zeros(frameCopy.size(),CV_8UC1); for( i = 0; i < found_filtered.size(); i++ ) { Rect r = found_filtered[i]; // the HOG detector returns slightly larger rectangles than the real objects. // so we slightly shrink the rectangles to get a nicer output. // r.x += cvRound(r.width*0.1); // r.width = cvRound(r.width*0.8); // r.y += cvRound(r.height*0.07); // r.height = cvRound(r.height*0.8); r = Rect_AdjustSizeAroundCenter(r,0.55,0.8); //rectangle(frame, r.tl(), r.br(), cv::Scalar(0,255,0), 1); //rectangle(img, r.tl(), r.br(), cv::Scalar(0,255,0), 1); // int tlx = r.tl().x, tly = r.tl().y; // for(int j = tlx; j < (tlx + r.width) ; j++) // { // for(int ii = tly; ii < (tly + r.height); ii++) // { // frameCopy_mask.at<uchar>(Point(j,ii)) = 255; // } // } rectangle( drawing, r.tl(), r.br(), Scalar(255,255,255), -1, 8, 0); } // imshow("test2",drawing); //recover to the original size resize(drawing, drawing, Size(), 1/ScaleFactor, 1/ScaleFactor, INTER_NEAREST); if(option_str == "-h") { static int counter = 0; String output_str = outPut_Mask_Path + NumberToString(++counter) + ".png"; imwrite(output_str, drawing); } //add the new mask on the final mask //!!problem: the convert between float and int will affect the size of video resolution //After doing the convert from float to int, the new video resolution must be smaller than //the orginal one, which is beacuse we always do the downScale; therefore, we use the smaller //size as the counter in order not to encounter the array problem for(int i = 0; i < drawing.cols; i ++) { for(int j = 0; j < drawing.rows; j++) { Point p = Point(i,j); if(drawing.at<uchar>(p) == 255) mask.at<uchar>(p) += mask_add_step; } } // imshow("test3",mask); }
void camera_contours_display(int num, Straightener & straight) { int c; IplImage* color_img; CvCapture* cv_cap = cvCaptureFromCAM(num); cvNamedWindow("Video", 0); // create window resizeWindow("Video", 700,700); for(;;) { color_img = cvQueryFrame(cv_cap); // get frame if(color_img != 0) { Mat cam_mat(color_img); Mat result; cam_mat.copyTo(result); if(straight.doAll(cam_mat, result)) { ///Apply blur blur(result, result, Size(3,3)); ///Apply Canny to destination Matrix Canny(result, result, 50, 50, 3); /// Vectors for storing contours vector<vector<Point> > contours; //contours of the paper sheet vector<vector<Point> > approx_contours; //approx contours of the paper sheet vector<Vec4i> hierarchy; int erosion_type = 2; int erosion_size = 3; Mat element = getStructuringElement(erosion_type, Size( 2*erosion_size + 1, 2*erosion_size+1), Point( erosion_size, erosion_size)); dilate(result, result, element); /// Cut 20 px from each side to avoid paper borders detection result = result(Rect(10, 10, result.cols-20, result.rows-20)); findContours(result, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE, Point(0, 0)); /// Draw contours Mat drawing = Mat::zeros( result.size(), CV_8UC3 ); /// https://github.com/Itseez/opencv/blob/master/samples/cpp/contours2.cpp // approx_contours.resize(contours.size()); for(unsigned int i = 0; i < contours.size(); i++) { /// Area of more than 20 and no parent if(contourArea(contours[i]) > 20 && hierarchy[i][3] == -1) { vector<Point> tmp_contour; approxPolyDP(Mat(contours[i]), tmp_contour, 3, true); approx_contours.push_back(tmp_contour); } } for(unsigned int i=0; i < approx_contours.size(); i++) { Scalar color; if(approx_contours[i].size() == 4) { color = Scalar( 255, 255, 255); drawContours( drawing, approx_contours, i, color, 1, 8, NULL, 0, Point() ); } else { color = Scalar( 0, 255, 0); drawContours( drawing, approx_contours, i, color, 1, 8, NULL, 0, Point() ); } } imshow("Video", drawing); } } c = cvWaitKey(10); // wait 10 ms or for key stroke if(c == 27) break; // if ESC, break and quit } /* clean up */ cvReleaseCapture( &cv_cap ); cvDestroyWindow("Video"); }
void drawBall(Mat draw_img, Scalar color = Scalar(0,255,255) ){ line(draw_img, Point(plast[0], plast[1]), Point(pnow[0], pnow[1]), color, 20); circle(draw_img, Point(pnow[0], pnow[1]), 20, color, -1); }
void MouseNavigationTool::display(GLContextData& contextData) const { bool gotoScreenCoords=factory->showMouseCursor||(factory->showScreenCenter&&navigationMode!=IDLE&&navigationMode!=WIDGETING); const VRScreen* screen=0; ONTransform screenT; if(gotoScreenCoords) { /* Get a pointer to the screen the mouse is on: */ if(mouseAdapter!=0&&mouseAdapter->getWindow()!=0) screen=mouseAdapter->getWindow()->getVRScreen(); else screen=getMainScreen(); screenT=screen->getScreenTransformation(); /* Save and set up OpenGL state: */ glPushAttrib(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_ENABLE_BIT|GL_LINE_BIT|GL_TEXTURE_BIT); glDisable(GL_LIGHTING); /* Go to screen coordinates: */ glPushMatrix(); glMultMatrix(screenT); } if(factory->showScreenCenter&&navigationMode!=IDLE&&navigationMode!=WIDGETING) { /* Determine the screen containing the input device and find its center: */ Scalar centerPos[2]; if(mouseAdapter!=0) mouseAdapter->getWindow()->getWindowCenterPos(centerPos); else { centerPos[0]=getMainScreen()->getWidth()*Scalar(0.5); centerPos[1]=getMainScreen()->getHeight()*Scalar(0.5); } /* Calculate the endpoints of the screen's crosshair lines in screen coordinates: */ Point l=Point(Scalar(0),centerPos[1],Scalar(0)); Point r=Point(screen->getWidth(),centerPos[1],Scalar(0)); Point b=Point(centerPos[0],Scalar(0),Scalar(0)); Point t=Point(centerPos[0],screen->getHeight(),Scalar(0)); /* Determine the crosshair colors: */ Color bgColor=getBackgroundColor(); Color fgColor; for(int i=0; i<3; ++i) fgColor[i]=1.0f-bgColor[i]; fgColor[3]=bgColor[3]; /* Draw the screen crosshairs: */ glDepthFunc(GL_LEQUAL); glLineWidth(3.0f); glColor(bgColor); glBegin(GL_LINES); glVertex(l); glVertex(r); glVertex(b); glVertex(t); glEnd(); glLineWidth(1.0f); glColor(fgColor); glBegin(GL_LINES); glVertex(l); glVertex(r); glVertex(b); glVertex(t); glEnd(); } if(factory->showMouseCursor) { /* Get the data item: */ DataItem* dataItem=contextData.retrieveDataItem<DataItem>(this); /* Calculate the mouse position: */ Point mousePos=screenT.inverseTransform(currentPos); for(int i=0; i<2; ++i) mousePos[i]-=factory->mouseCursorHotspot[i]*factory->mouseCursorSize[i]; /* Draw the mouse cursor: */ glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D,dataItem->textureObjectId); glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE); glEnable(GL_ALPHA_TEST); glAlphaFunc(GL_GEQUAL,0.5f); glBegin(GL_QUADS); glTexCoord(mouseCursorTexCoordBox.getVertex(0)); glVertex(mousePos[0],mousePos[1]); glTexCoord(mouseCursorTexCoordBox.getVertex(1)); glVertex(mousePos[0]+factory->mouseCursorSize[0],mousePos[1]); glTexCoord(mouseCursorTexCoordBox.getVertex(3)); glVertex(mousePos[0]+factory->mouseCursorSize[0],mousePos[1]+factory->mouseCursorSize[1]); glTexCoord(mouseCursorTexCoordBox.getVertex(2)); glVertex(mousePos[0],mousePos[1]+factory->mouseCursorSize[1]); glEnd(); glBindTexture(GL_TEXTURE_2D,0); } if(gotoScreenCoords) { /* Go back to physical coordinates: */ glPopMatrix(); /* Restore OpenGL state: */ glPopAttrib(); } }
bool doTrain(int startStep, OutputArray logLikelihoods, OutputArray labels, OutputArray probs) { int dim = trainSamples.cols; // Precompute the empty initial train data in the cases of START_E_STEP and START_AUTO_STEP if(startStep != START_M_STEP) { if(covs.empty()) { CV_Assert(weights.empty()); clusterTrainSamples(); } } if(!covs.empty() && covsEigenValues.empty() ) { CV_Assert(invCovsEigenValues.empty()); decomposeCovs(); } if(startStep == START_M_STEP) mStep(); double trainLogLikelihood, prevTrainLogLikelihood = 0.; int maxIters = (termCrit.type & TermCriteria::MAX_ITER) ? termCrit.maxCount : DEFAULT_MAX_ITERS; double epsilon = (termCrit.type & TermCriteria::EPS) ? termCrit.epsilon : 0.; for(int iter = 0; ; iter++) { eStep(); trainLogLikelihood = sum(trainLogLikelihoods)[0]; if(iter >= maxIters - 1) break; double trainLogLikelihoodDelta = trainLogLikelihood - prevTrainLogLikelihood; if( iter != 0 && (trainLogLikelihoodDelta < -DBL_EPSILON || trainLogLikelihoodDelta < epsilon * std::fabs(trainLogLikelihood))) break; mStep(); prevTrainLogLikelihood = trainLogLikelihood; } if( trainLogLikelihood <= -DBL_MAX/10000. ) { clear(); return false; } // postprocess covs covs.resize(nclusters); for(int clusterIndex = 0; clusterIndex < nclusters; clusterIndex++) { if(covMatType == COV_MAT_SPHERICAL) { covs[clusterIndex].create(dim, dim, CV_64FC1); setIdentity(covs[clusterIndex], Scalar(covsEigenValues[clusterIndex].at<double>(0))); } else if(covMatType == COV_MAT_DIAGONAL) { covs[clusterIndex] = Mat::diag(covsEigenValues[clusterIndex]); } } if(labels.needed()) trainLabels.copyTo(labels); if(probs.needed()) trainProbs.copyTo(probs); if(logLikelihoods.needed()) trainLogLikelihoods.copyTo(logLikelihoods); trainSamples.release(); trainProbs.release(); trainLabels.release(); trainLogLikelihoods.release(); return true; }
void MouseDialogNavigationTool::frame(void) { /* Update the current mouse position: */ currentPos=calcScreenPos(); /* Act depending on this tool's current state: */ if(isActive()) { if(spinning) { /* Calculate incremental rotation: */ rotation.leftMultiply(NavTrackerState::rotate(NavTrackerState::Rotation::rotateScaledAxis(spinAngularVelocity*getFrameTime()))); NavTrackerState t=preScale; t*=rotation; t*=postScale; setNavigationTransformation(t); } else { switch(navigationMode) { case ROTATING: { /* Calculate the rotation position: */ Vector offset=(lastRotationPos-screenCenter)+rotateOffset; /* Calculate mouse displacement vector: */ Point rotationPos=currentPos; Vector delta=rotationPos-lastRotationPos; lastRotationPos=rotationPos; /* Calculate incremental rotation: */ Vector axis=Geometry::cross(offset,delta); Scalar angle=Geometry::mag(delta)/factory->rotateFactor; if(angle!=Scalar(0)) rotation.leftMultiply(NavTrackerState::rotate(NavTrackerState::Rotation::rotateAxis(axis,angle))); NavTrackerState t=preScale; t*=rotation; t*=postScale; setNavigationTransformation(t); break; } case PANNING: { /* Update the navigation transformation: */ NavTrackerState t=NavTrackerState::translate(currentPos-motionStart); t*=preScale; setNavigationTransformation(t); break; } case DOLLYING: { /* Calculate the current dollying direction: */ Vector dollyingDirection; if(mouseAdapter!=0) dollyingDirection=mouseAdapter->getWindow()->getVRScreen()->getScreenTransformation().transform(factory->screenDollyingDirection); else dollyingDirection=getMainScreen()->getScreenTransformation().transform(factory->screenDollyingDirection); /* Update the navigation transformation: */ Scalar dollyDist=((currentPos-motionStart)*dollyingDirection)/factory->dollyFactor; NavTrackerState t=NavTrackerState::translate(dollyDirection*dollyDist); t*=preScale; setNavigationTransformation(t); break; } case SCALING: { /* Calculate the current scaling direction: */ Vector scalingDirection; if(mouseAdapter!=0) scalingDirection=mouseAdapter->getWindow()->getVRScreen()->getScreenTransformation().transform(factory->screenScalingDirection); else scalingDirection=getMainScreen()->getScreenTransformation().transform(factory->screenScalingDirection); /* Update the navigation transformation: */ Scalar scale=((currentPos-motionStart)*scalingDirection)/factory->scaleFactor; NavTrackerState t=preScale; t*=NavTrackerState::scale(Math::exp(scale)); t*=postScale; setNavigationTransformation(t); break; } } } } }
void MouseDialogNavigationTool::display(GLContextData& contextData) const { if(showScreenCenter) { /* Save and set up OpenGL state: */ glPushAttrib(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_ENABLE_BIT|GL_LINE_BIT|GL_TEXTURE_BIT); glDisable(GL_LIGHTING); /* Get a pointer to the screen the mouse is on: */ const VRScreen* screen; if(mouseAdapter!=0&&mouseAdapter->getWindow()!=0) screen=mouseAdapter->getWindow()->getVRScreen(); else screen=getMainScreen(); ONTransform screenT=screen->getScreenTransformation(); /* Go to screen coordinates: */ glPushMatrix(); glMultMatrix(screenT); /* Determine the screen containing the input device and find its center: */ Scalar centerPos[2]; if(mouseAdapter!=0) mouseAdapter->getWindow()->getWindowCenterPos(centerPos); else { centerPos[0]=getMainScreen()->getWidth()*Scalar(0.5); centerPos[1]=getMainScreen()->getHeight()*Scalar(0.5); } /* Calculate the endpoints of the screen's crosshair lines in screen coordinates: */ Point l=Point(Scalar(0),centerPos[1],Scalar(0)); Point r=Point(screen->getWidth(),centerPos[1],Scalar(0)); Point b=Point(centerPos[0],Scalar(0),Scalar(0)); Point t=Point(centerPos[0],screen->getHeight(),Scalar(0)); /* Determine the crosshair colors: */ Color bgColor=getBackgroundColor(); Color fgColor; for(int i=0;i<3;++i) fgColor[i]=1.0f-bgColor[i]; fgColor[3]=bgColor[3]; /* Draw the screen crosshairs: */ glDepthFunc(GL_LEQUAL); glLineWidth(3.0f); glColor(bgColor); glBegin(GL_LINES); glVertex(l); glVertex(r); glVertex(b); glVertex(t); glEnd(); glLineWidth(1.0f); glColor(fgColor); glBegin(GL_LINES); glVertex(l); glVertex(r); glVertex(b); glVertex(t); glEnd(); /* Go back to physical coordinates: */ glPopMatrix(); /* Restore OpenGL state: */ glPopAttrib(); } }
void cv::Laplacian( InputArray _src, OutputArray _dst, int ddepth, int ksize, double scale, double delta, int borderType ) { Mat src = _src.getMat(); if (ddepth < 0) ddepth = src.depth(); _dst.create( src.size(), CV_MAKETYPE(ddepth, src.channels()) ); Mat dst = _dst.getMat(); #ifdef HAVE_TEGRA_OPTIMIZATION if (scale == 1.0 && delta == 0) { if (ksize == 1 && tegra::laplace1(src, dst, borderType)) return; if (ksize == 3 && tegra::laplace3(src, dst, borderType)) return; if (ksize == 5 && tegra::laplace5(src, dst, borderType)) return; } #endif if( ksize == 1 || ksize == 3 ) { float K[2][9] = {{0, 1, 0, 1, -4, 1, 0, 1, 0}, {2, 0, 2, 0, -8, 0, 2, 0, 2}}; Mat kernel(3, 3, CV_32F, K[ksize == 3]); if( scale != 1 ) kernel *= scale; filter2D( src, dst, ddepth, kernel, Point(-1,-1), delta, borderType ); } else { const size_t STRIPE_SIZE = 1 << 14; int depth = src.depth(); int ktype = std::max(CV_32F, std::max(ddepth, depth)); int wdepth = depth == CV_8U && ksize <= 5 ? CV_16S : depth <= CV_32F ? CV_32F : CV_64F; int wtype = CV_MAKETYPE(wdepth, src.channels()); Mat kd, ks; getSobelKernels( kd, ks, 2, 0, ksize, false, ktype ); if( ddepth < 0 ) ddepth = src.depth(); int dtype = CV_MAKETYPE(ddepth, src.channels()); int dy0 = std::min(std::max((int)(STRIPE_SIZE/(getElemSize(src.type())*src.cols)), 1), src.rows); Ptr<FilterEngine> fx = createSeparableLinearFilter(src.type(), wtype, kd, ks, Point(-1,-1), 0, borderType, borderType, Scalar() ); Ptr<FilterEngine> fy = createSeparableLinearFilter(src.type(), wtype, ks, kd, Point(-1,-1), 0, borderType, borderType, Scalar() ); int y = fx->start(src), dsty = 0, dy = 0; fy->start(src); const uchar* sptr = src.data + y*src.step; Mat d2x( dy0 + kd.rows - 1, src.cols, wtype ); Mat d2y( dy0 + kd.rows - 1, src.cols, wtype ); for( ; dsty < src.rows; sptr += dy0*src.step, dsty += dy ) { fx->proceed( sptr, (int)src.step, dy0, d2x.data, (int)d2x.step ); dy = fy->proceed( sptr, (int)src.step, dy0, d2y.data, (int)d2y.step ); if( dy > 0 ) { Mat dstripe = dst.rowRange(dsty, dsty + dy); d2x.rows = d2y.rows = dy; // modify the headers, which should work d2x += d2y; d2x.convertTo( dstripe, dtype, scale, delta ); } } } }
void MouseDialogNavigationTool::buttonCallback(int,int buttonIndex,InputDevice::ButtonCallbackData* cbData) { if(cbData->newButtonState) // Button has just been pressed { /* Deactivate spinning: */ spinning=false; /* Start navigating according to the current navigation mode: */ switch(navigationMode) { case ROTATING: if(activate()) startRotating(); break; case PANNING: if(activate()) startPanning(); break; case DOLLYING: if(activate()) startDollying(); break; case SCALING: if(activate()) startScaling(); break; } } else // Button has just been released { /* Check for spinning if currently in rotating mode: */ if(navigationMode==ROTATING) { /* Check if the input device is still moving: */ Point currentPos=calcScreenPos(); Vector delta=currentPos-lastRotationPos; if(Geometry::mag(delta)>factory->spinThreshold) { /* Calculate spinning angular velocity: */ Vector offset=(lastRotationPos-screenCenter)+rotateOffset; Vector axis=Geometry::cross(offset,delta); Scalar angularVelocity=Geometry::mag(delta)/(factory->rotateFactor*getFrameTime()); spinAngularVelocity=axis*(Scalar(0.5)*angularVelocity/axis.mag()); /* Enable spinning: */ spinning=true; } else { /* Deactivate the tool: */ deactivate(); } } else { /* Deactivate the tool: */ deactivate(); } } }
vector<bool> CharacterAnalysis::filterBetweenLines(Mat img, vector<vector<Point> > contours, vector<Vec4i> hierarchy, vector<Point> outerPolygon, vector<bool> goodIndices) { static float MIN_AREA_PERCENT_WITHIN_LINES = 0.88; vector<bool> includedIndices(contours.size()); for (int j = 0; j < contours.size(); j++) includedIndices[j] = false; if (outerPolygon.size() == 0) return includedIndices; vector<Point> validPoints; // Figure out the line height LineSegment topLine(outerPolygon[0].x, outerPolygon[0].y, outerPolygon[1].x, outerPolygon[1].y); LineSegment bottomLine(outerPolygon[3].x, outerPolygon[3].y, outerPolygon[2].x, outerPolygon[2].y); float x = ((float) img.cols) / 2; Point midpoint = Point(x, bottomLine.getPointAt(x)); Point acrossFromMidpoint = topLine.closestPointOnSegmentTo(midpoint); float lineHeight = distanceBetweenPoints(midpoint, acrossFromMidpoint); // Create a white mask for the area inside the polygon Mat outerMask = Mat::zeros(img.size(), CV_8U); Mat innerArea = Mat::zeros(img.size(), CV_8U); fillConvexPoly(outerMask, outerPolygon.data(), outerPolygon.size(), Scalar(255,255,255)); for (int i = 0; i < contours.size(); i++) { if (goodIndices[i] == false) continue; // get rid of the outline by drawing a 1 pixel width black line drawContours(innerArea, contours, i, // draw this contour cv::Scalar(255,255,255), // in CV_FILLED, 8, hierarchy, 0 ); bitwise_and(innerArea, outerMask, innerArea); vector<vector<Point> > tempContours; findContours(innerArea, tempContours, CV_RETR_EXTERNAL, // retrieve the external contours CV_CHAIN_APPROX_SIMPLE ); // all pixels of each contours ); double totalArea = contourArea(contours[i]); double areaBetweenLines = 0; for (int tempContourIdx = 0; tempContourIdx < tempContours.size(); tempContourIdx++) { areaBetweenLines += contourArea(tempContours[tempContourIdx]); } if (areaBetweenLines / totalArea >= MIN_AREA_PERCENT_WITHIN_LINES) { includedIndices[i] = true; } innerArea.setTo(Scalar(0,0,0)); } return includedIndices; }
void Importer::resolveSceneUrls(Node& root, const Url& baseUrl) { auto base = baseUrl; if (isZipArchiveUrl(baseUrl)) { base = getBaseUrlForZipArchive(baseUrl); } // Resolve global texture URLs. Node textures = root["textures"]; if (textures.IsDefined()) { for (auto texture : textures) { if (Node textureUrlNode = texture.second["url"]) { if (nodeIsPotentialUrl(textureUrlNode)) { textureUrlNode = Url(textureUrlNode.Scalar()).resolved(base).string(); } } } } // Resolve inline texture URLs. if (Node styles = root["styles"]) { for (auto entry : styles) { Node style = entry.second; if (!style.IsMap()) { continue; } //style->texture if (Node texture = style["texture"]) { if (nodeIsTextureUrl(texture, textures)) { texture = Url(texture.Scalar()).resolved(base).string(); } } //style->material->texture if (Node material = style["material"]) { if (!material.IsMap()) { continue; } for (auto& prop : {"emission", "ambient", "diffuse", "specular", "normal"}) { if (Node propNode = material[prop]) { if (!propNode.IsMap()) { continue; } if (Node matTexture = propNode["texture"]) { if (nodeIsTextureUrl(matTexture, textures)) { matTexture = Url(matTexture.Scalar()).resolved(base).string(); } } } } } //style->shader->uniforms->texture if (Node shaders = style["shaders"]) { if (!shaders.IsMap()) { continue; } if (Node uniforms = shaders["uniforms"]) { for (auto uniformEntry : uniforms) { Node uniformValue = uniformEntry.second; if (nodeIsTextureUrl(uniformValue, textures)) { uniformValue = Url(uniformValue.Scalar()).resolved(base).string(); } else if (uniformValue.IsSequence()) { for (Node u : uniformValue) { if (nodeIsTextureUrl(u, textures)) { u = Url(u.Scalar()).resolved(base).string(); } } } } } } } } // Resolve data source URLs. if (Node sources = root["sources"]) { for (auto source : sources) { if (!source.second.IsMap()) { continue; } if (Node sourceUrl = source.second["url"]) { if (nodeIsPotentialUrl(sourceUrl)) { sourceUrl = Url(sourceUrl.Scalar()).resolved(base).string(); } } } } // Resolve font URLs. if (Node fonts = root["fonts"]) { if (fonts.IsMap()) { for (const auto& font : fonts) { if (font.second.IsMap()) { auto urlNode = font.second["url"]; if (nodeIsPotentialUrl(urlNode)) { urlNode = Url(urlNode.Scalar()).resolved(base).string(); } } else if (font.second.IsSequence()) { for (auto& fontNode : font.second) { auto urlNode = fontNode["url"]; if (nodeIsPotentialUrl(urlNode)) { urlNode = Url(urlNode.Scalar()).resolved(base).string(); } } } } } } }
void GrabCutMF::Demo(CStr &wkDir, float w1, float w2, float w3, float alpha, float beta, float gama, float mu) { CStr imgDir = wkDir + "Imgs/", salDir = wkDir + "Sal4N/", iluDir = wkDir + "Ilu4N/"; vecS namesNE; int imgNum = CmFile::GetNamesNE(imgDir + "*.jpg", namesNE); CmFile::MkDir(salDir); CmFile::MkDir(iluDir); printf("w1 = %g, w2 = %g, w3 = %g, alpha = %g, beta = %g, gama = %g, mu = %g\n", w1, w2, w3, alpha, beta, gama, mu); // Number of labels //const int M = 2; CmTimer tm("Time"), tmIni("TimeIni"), tmRef("TimeRef"); double maxWeight = 2; // 2: 0.958119, 1: 0.953818, tm.Start(); #pragma omp parallel for for (int i = 0; i < imgNum; i++){ printf("Processing %d/%d: %s%s.jpg%20s\r\n", i, imgNum, _S(imgDir), _S(namesNE[i]), ""); CmFile::Copy(imgDir + namesNE[i] + ".jpg", salDir + namesNE[i] + ".jpg"); //CmFile::Copy(imgDir + namesNE[i] + ".png", salDir + namesNE[i] + "_GT.png"); Mat _imMat3u = imread(imgDir + namesNE[i] + ".jpg"), imMat3f, imMat3u, gt1u; Mat _gt1u = imread(imgDir + namesNE[i] + ".png", CV_LOAD_IMAGE_GRAYSCALE); if(_gt1u.rows == 0 && _gt1u.cols == 0) { cout<<"Error: unable to open "<<(imgDir + namesNE[i] + ".png")<<endl; continue; } blur(_gt1u, _gt1u, Size(3,3)); Mat _res1u = Mat::zeros(_imMat3u.size(), CV_8U); Rect wkRect = CmCv::GetMaskRange(_gt1u, 30, 200); _imMat3u(wkRect).copyTo(imMat3u); _gt1u(wkRect).copyTo(gt1u); imMat3u.convertTo(imMat3f, CV_32FC3, 1/255.0); Rect rect = CmCv::GetMaskRange(gt1u, 5, 128); Mat edge1u; // Use an edge map to expand the background mask in flat (no edge) region CmCv::CannySimpleRGB(imMat3u, edge1u, 120, 1200, 5); dilate(edge1u, edge1u, Mat(), Point(-1, -1), 3); Mat borderMask1u(imMat3u.size(), CV_8U), tmpMask; memset(borderMask1u.data, 255, borderMask1u.step.p[0] * borderMask1u.rows); borderMask1u(rect) = Scalar(0); getGrabMask(edge1u, borderMask1u); //* The Mean field based GrabCut //tmIni.Start(); GrabCutMF cutMF(imMat3f, imMat3u, salDir + namesNE[i], w1, w2, w3, alpha, beta, gama, mu); //Mat borderMask1u = CmCv::getGrabMask(imMat3u, rect), tmpMask; imwrite(salDir + namesNE[i] + "_BM.png", borderMask1u); imwrite(salDir + namesNE[i] + ".jpg", imMat3u); //imwrite(salDir + namesNE[i] + "_GT.png", gt1u); cutMF.initialize(rect, borderMask1u, (float)maxWeight, true); //cutMF.setGrabReg(rect, CmCv::getGrabMask(imMat3u, rect)); //tmIni.Stop(); //tmRef.Start(); cutMF.refine(); //tmRef.Stop(); Mat res1u = cutMF.drawResult(), invRes1u; res1u.copyTo(_res1u(wkRect)); imwrite(salDir + namesNE[i] + "_GCMF1.png", _res1u); //if (sum(res1u).val[0] < EPS){ // printf("%s.jpg don't contains a salient object\n", _S(namesNE[i])); // continue; //} dilate(res1u(rect), tmpMask, Mat(), Point(-1, -1), 10); bitwise_not(tmpMask, borderMask1u(rect)); getGrabMask(edge1u, borderMask1u); //blur(res1u, invRes1u, Size(3, 3)); // //PointSeti hullPnts; //convexHullOfMask(invRes1u, hullPnts); //fillConvexPoly(invRes1u, hullPnts, 255); //bitwise_not(invRes1u, invRes1u); //bitwise_or(invRes1u, borderMask1u, borderMask1u); imwrite(salDir + namesNE[i] + "_MB2.png", borderMask1u); //double w = maxWeight - (maxWeight-1)*sum(res1u).val[0]/(borderMask1u.rows*borderMask1u.cols*255 - sum(borderMask1u).val[0]); cutMF.initialize(rect, borderMask1u, 2); cutMF.refine(); //printf("weight = %g\n", w); //imshow("Result", res1u); //imshow("Possible", borderMask1u); //imshow("Image", imMat3f); //waitKey(0); res1u = cutMF.drawResult(); Rect rectRes = CmCv::GetMaskRange(res1u, 5, 128); if (rectRes.width * 1.1 < rect.width || rectRes.height * 1.1 < rect.height){ // Too short result printf("%s.jpg contains a small object\n", _S(namesNE[i])); memset(borderMask1u.data, 255, borderMask1u.step.p[0] * borderMask1u.rows); borderMask1u(rect) = Scalar(0); cutMF.initialize(rect, borderMask1u, 2); cutMF.refine(); res1u = cutMF.drawResult(); imwrite(salDir + namesNE[i] + "_MB2.png", borderMask1u); CmFile::Copy2Dir(salDir + namesNE[i] + "*.*", iluDir); imwrite(iluDir + namesNE[i] + "_GCMF.png", _res1u); } res1u.copyTo(_res1u(wkRect)); imwrite(salDir + namesNE[i] + "_GCMF.png", _res1u); } tm.Stop(); double avgTime = tm.TimeInSeconds()/imgNum; printf("Speed: %gs, %gfps\t\t\n", avgTime, 1/avgTime); //tmIni.Report(); //tmRef.Report(); //CmEvaluation::EvalueMask(imgDir + "*.png", salDir, ".png", "_GC.png"); char* pDes[] = { "GCMF1", "GCMF"}; //, "CudaG4", "Onecut", "GC", "CudaH", vecS des = charPointers2StrVec (pDes); CStr rootDir = CmFile::GetFatherFolder(wkDir), dbName = CmFile::GetNameNE(wkDir.substr(0, wkDir.size() - 1)); CmEvaluation::EvalueMask(imgDir + "*.png", salDir, des, wkDir.substr(0, wkDir.size() - 1) + "Res.m", 0.3, false, "", dbName); }
void MouseNavigationTool::valuatorCallback(int,int,InputDevice::ValuatorCallbackData* cbData) { currentValue=Scalar(cbData->newValuatorValue); if(currentValue!=Scalar(0)) { /* Act depending on this tool's current state: */ switch(navigationMode) { case IDLE: case SPINNING: /* Try activating this tool: */ if(navigationMode==SPINNING||activate()) if(dolly) { /* Calculate the dollying direction: */ if(mouseAdapter!=0) dollyDirection=mouseAdapter->getWindow()->getViewer()->getHeadPosition()-calcScreenCenter(); else dollyDirection=getMainViewer()->getHeadPosition()-calcScreenCenter(); dollyDirection.normalize(); /* Initialize the wheel dollying factor: */ currentWheelScale=Scalar(1); preScale=getNavigationTransformation(); /* Go to wheel dollying mode: */ navigationMode=DOLLYING_WHEEL; } else { /* Calculate the scaling center: */ screenCenter=calcScreenCenter(); /* Initialize the wheel scaling factor: */ currentWheelScale=Scalar(1); preScale=NavTrackerState::translateFromOriginTo(screenCenter); postScale=NavTrackerState::translateToOriginFrom(screenCenter); postScale*=getNavigationTransformation(); /* Go to wheel scaling mode: */ navigationMode=SCALING_WHEEL; } break; default: /* This can definitely happen; just ignore the event */ break; } } else { /* Act depending on this tool's current state: */ switch(navigationMode) { case DOLLYING_WHEEL: case SCALING_WHEEL: /* Deactivate this tool: */ deactivate(); /* Go to idle mode: */ navigationMode=IDLE; break; default: /* This can definitely happen; just ignore the event */ break; } } }
//得到所有的连通分支 void ComputeConnectedParts(_in_ Mat * Mask1,_in_ int dims1,_in_ int dims2,_in_ int dims3,_out_ vector<vector<Point3i> >& connectedMap ) { connectedMap.clear(); //std::cout<<"hahhahah"<<dims1<<std::endl; dim1=dims1; dim2=dims2; dim3=dims3;//这个地方好像没有必要 Mask=new Mat[dim3]; for (int i=0;i<dim3;i++) { Mask[i].create(dim2,dim1,CV_8UC1); Mask[i].setTo(Scalar(0)); } for(int k=0;k<dim3;k++) { for(int j=0;j<dim2;j++) { for(int i=0;i<dim1;i++) { Mask[k].at<uchar>(j,i)=Mask1[k].at<uchar>(j,i); } } } Mat* FloodFillTmp=new Mat[dim3]; for (int i=0;i<dim3;i++) { FloodFillTmp[i].create(dim2,dim1,CV_8UC1); FloodFillTmp[i].setTo(Scalar(0)); } for (int z=0;z<dim3;z++) { for (int y=0;y<dim2;y++) { for (int x=0;x<dim1;x++) { if (Mask[z].at<uchar>(y,x)>0) { islandPoints myIslandPoints; Point3i point_tmp(x,y,z); connectivity::GetIsland(point_tmp,myIslandPoints,FloodFillTmp);//注意这边(x,y)是矩阵坐标,换成点是(y,x) for (int k=0;k<myIslandPoints.islandVolume;k++) { Mask[myIslandPoints.pointsInIsland[k].z].at<uchar> (myIslandPoints.pointsInIsland[k].y,myIslandPoints.pointsInIsland[k].x)=0; } vector<Point3i> newPart; newPart.clear(); for ( unsigned int i=0;i<myIslandPoints.pointsInIsland.size();i++) { newPart.push_back(myIslandPoints.pointsInIsland[i]); } newPart.resize(newPart.size()); connectedMap.push_back(newPart); for (int k=0;k<myIslandPoints.islandVolume;k++) { FloodFillTmp[myIslandPoints.pointsInIsland[k].z].at<uchar> (myIslandPoints.pointsInIsland[k].y,myIslandPoints.pointsInIsland[k].x)=0; } } } } } delete [] FloodFillTmp; std::cout<<"In Total We Have "<<connectedMap.size()<<" Parts"<<std::endl; //Mask=NULL; }
void MouseNavigationTool::frame(void) { /* Update the current mouse position: */ currentPos=calcScreenPos(); /* Act depending on this tool's current state: */ switch(navigationMode) { case IDLE: /* Do nothing */ break; case WIDGETING: { /* Deliver the event: */ GLMotif::Event event(true); event.setWorldLocation(calcSelectionRay()); getWidgetManager()->pointerMotion(event); if(draggedWidget!=0) { /* Update the dragged widget's transformation: */ NavTrackerState current=NavTrackerState::translateFromOriginTo(calcScreenPos()); current*=preScale; getWidgetManager()->setPrimaryWidgetTransformation(draggedWidget,GLMotif::WidgetManager::Transformation(current)); } break; } case ROTATING: { /* Calculate the rotation position: */ Vector offset=(lastRotationPos-screenCenter)+rotateOffset; /* Calculate mouse displacement vector: */ Point rotationPos=currentPos; Vector delta=rotationPos-lastRotationPos; lastRotationPos=rotationPos; /* Calculate incremental rotation: */ Vector axis=Geometry::cross(offset,delta); Scalar angle=Geometry::mag(delta)/factory->rotateFactor; if(angle!=Scalar(0)) rotation.leftMultiply(NavTrackerState::rotate(NavTrackerState::Rotation::rotateAxis(axis,angle))); NavTrackerState t=preScale; t*=rotation; t*=postScale; setNavigationTransformation(t); break; } case SPINNING: { /* Calculate incremental rotation: */ rotation.leftMultiply(NavTrackerState::rotate(NavTrackerState::Rotation::rotateScaledAxis(spinAngularVelocity*getCurrentFrameTime()))); NavTrackerState t=preScale; t*=rotation; t*=postScale; setNavigationTransformation(t); break; } case PANNING: { /* Update the navigation transformation: */ NavTrackerState t=NavTrackerState::translate(currentPos-motionStart); t*=preScale; setNavigationTransformation(t); break; } case DOLLYING: { /* Calculate the current dollying direction: */ Vector dollyingDirection; if(mouseAdapter!=0) dollyingDirection=mouseAdapter->getWindow()->getVRScreen()->getScreenTransformation().transform(factory->screenDollyingDirection); else dollyingDirection=getMainScreen()->getScreenTransformation().transform(factory->screenDollyingDirection); /* Update the navigation transformation: */ Scalar dollyDist=((currentPos-motionStart)*dollyingDirection)/factory->dollyFactor; NavTrackerState t=NavTrackerState::translate(dollyDirection*dollyDist); t*=preScale; setNavigationTransformation(t); break; } case SCALING: { /* Calculate the current scaling direction: */ Vector scalingDirection; if(mouseAdapter!=0) scalingDirection=mouseAdapter->getWindow()->getVRScreen()->getScreenTransformation().transform(factory->screenScalingDirection); else scalingDirection=getMainScreen()->getScreenTransformation().transform(factory->screenScalingDirection); /* Update the navigation transformation: */ Scalar scale=((currentPos-motionStart)*scalingDirection)/factory->scaleFactor; NavTrackerState t=preScale; t*=NavTrackerState::scale(Math::exp(scale)); t*=postScale; setNavigationTransformation(t); break; } case DOLLYING_WHEEL: { /* Update the navigation transformation: */ Scalar scale=currentValue; currentWheelScale+=factory->wheelDollyFactor*scale; NavTrackerState t=NavTrackerState::translate(dollyDirection*currentWheelScale); t*=preScale; setNavigationTransformation(t); break; } case SCALING_WHEEL: { /* Update the navigation transformation: */ Scalar scale=currentValue; currentWheelScale*=Math::pow(factory->wheelScaleFactor,scale); NavTrackerState t=preScale; t*=NavTrackerState::scale(currentWheelScale); t*=postScale; setNavigationTransformation(t); break; } } }
Vector OdeErrControl( Method &method, const Scalar &ti , const Scalar &tf , const Vector &xi , const Scalar &smin , const Scalar &smax , Scalar &scur , const Vector &eabs , const Scalar &erel , Vector &ef , Vector &maxabs, size_t &nstep ) { // check simple vector class specifications CheckSimpleVector<Scalar, Vector>(); size_t n = xi.size(); CPPAD_ASSERT_KNOWN( smin <= smax, "Error in OdeErrControl: smin > smax" ); CPPAD_ASSERT_KNOWN( eabs.size() == n, "Error in OdeErrControl: size of eabs is not equal to n" ); CPPAD_ASSERT_KNOWN( maxabs.size() == n, "Error in OdeErrControl: size of maxabs is not equal to n" ); size_t m = method.order(); CPPAD_ASSERT_KNOWN( m > 1, "Error in OdeErrControl: m is less than or equal one" ); bool ok; bool minimum_step; size_t i; Vector xa(n), xb(n), eb(n), nan_vec(n); // initialization Scalar zero(0); Scalar one(1); Scalar two(2); Scalar three(3); Scalar m1(m-1); Scalar ta = ti; for(i = 0; i < n; i++) { nan_vec[i] = nan(zero); ef[i] = zero; xa[i] = xi[i]; if( zero <= xi[i] ) maxabs[i] = xi[i]; else maxabs[i] = - xi[i]; } nstep = 0; Scalar tb, step, lambda, axbi, a, r, root; while( ! (ta == tf) ) { // start with value suggested by error criteria step = scur; // check maximum if( smax <= step ) step = smax; // check minimum minimum_step = step <= smin; if( minimum_step ) step = smin; // check if near the end if( tf <= ta + step * three / two ) tb = tf; else tb = ta + step; // try using this step size nstep++; method.step(ta, tb, xa, xb, eb); step = tb - ta; // check if this steps error estimate is ok ok = ! (hasnan(xb) || hasnan(eb)); if( (! ok) && minimum_step ) { ef = nan_vec; return nan_vec; } // compute value of lambda for this step lambda = Scalar(10) * scur / step; for(i = 0; i < n; i++) { if( zero <= xb[i] ) axbi = xb[i]; else axbi = - xb[i]; a = eabs[i] + erel * axbi; if( ! (eb[i] == zero) ) { r = ( a / eb[i] ) * step / (tf - ti); root = exp( log(r) / m1 ); if( root <= lambda ) lambda = root; } } if( ok && ( one <= lambda || step <= smin * three / two) ) { // this step is within error limits or // close to the minimum size ta = tb; for(i = 0; i < n; i++) { xa[i] = xb[i]; ef[i] = ef[i] + eb[i]; if( zero <= xb[i] ) axbi = xb[i]; else axbi = - xb[i]; if( axbi > maxabs[i] ) maxabs[i] = axbi; } } if( ! ok ) { // decrease step an see if method will work this time scur = step / two; } else if( ! (ta == tf) ) { // step suggested by the error criteria is not used // on the last step because it may be very small. scur = lambda * step / two; } } return xa; }
int main(int argc, char* argv[]) { int width = SIZE * WIDTH; int height = SIZE * HEIGHT; int size = SIZE; Mat img(height, width, CV_8UC3, Scalar(0)); namedWindow("Display Image", WINDOW_AUTOSIZE); initialPlate(plate); while (true) { LBlock b(Point2f(WIDTH / 2, 3), plate); Block& block = b; Point2f point(0, 0); time_t tick, tock; time(&tick); while (true) { //image initialize by black img = Mat::zeros(height, width, CV_8UC3); //draw whole plate for (int y = 0; y < HEIGHT; y++) { for (int x = 0; x < WIDTH; x++) { int temp = y * WIDTH + x; if (plate[temp] != 0) { Point2f tempPoint1(x * size, y * size); Point2f tempPoint2(x * size + size - 1, y * size + size - 1); Rect rect(tempPoint1, tempPoint2); rectangle(img, rect, Scalar(255, 255, 255), -1); } } } //get input int input = cvWaitKey(1); if (input == 65361) { //left block.move(-1); } else if (input == 65362) { //up block.rotate(); } else if (input == 65363) { //right block.move(1); } else if (input == 65364) { //down block.move(0); } else if (input == 32) { //space block.moveFullDown(); checkLines(plate); break; } else if (input == 27) //esc return 0;; time(&tock); if (tock - tick > 0) { int deadOrNot = block.move(0); tick = tock; if (deadOrNot == -1) { break; } } imshow("Display Image", img); } } destroyAllWindows(); cvWaitKey(); return 0; }
bool Scraper::FindAllBMPs(const searchType type, HBITMAP hBmp, const double threshold, const int maxMatch, vector<MATCHPOINTS> &matches) { // Convert HBITMAP to Mat unique_ptr<Gdiplus::Bitmap> pBitmap; pBitmap.reset(Gdiplus::Bitmap::FromHBITMAP(hBmp, NULL)); Mat img = CGdiPlus::CopyBmpToMat(pBitmap.get()); pBitmap.reset(); cvtColor( img, img, CV_BGRA2BGR ); // Find right image group imageType iType = type==searchGoldStorage ? goldStorage : type==searchElixStorage ? elixStorage : type==searchDarkStorage ? darkStorage : type==searchLootCollector ? collector : type==searchLootBubble ? lootBubble : type==searchDonateButton ? donateButton : (imageType) 0; int iTypeIndex = -1; for (int i = 0; i < (int) imageGroups.size(); i++) if (imageGroups[i].iType == iType) iTypeIndex = i; if (iTypeIndex == -1) return false; // Scan through each Mat in this image group int count = 0; for (int i = 0; i < (int) imageGroups[iTypeIndex].mats.size(); i++) { // Get matches for this image Mat result( FindMatch(img, imageGroups[iTypeIndex].mats[i]) ); // Parse through matches in result set while (count < maxMatch) { double minVal, maxVal; Point minLoc, maxLoc; minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc); // Fill haystack with pure green so we don't match this same location rectangle(img, maxLoc, cv::Point(maxLoc.x + imageGroups[iTypeIndex].mats[i].cols, maxLoc.y + imageGroups[iTypeIndex].mats[i].rows), CV_RGB(0,255,0), 2); // Fill results array with lo vals, so we don't match this same location floodFill(result, maxLoc, 0, 0, Scalar(0.1), Scalar(1.0)); if (maxVal >= threshold && maxVal > 0) { // Check if this point is within 10 pixels of an existing match to avoid dupes bool alreadyFound = false; for (int k=0; k<count; k++) { if (DistanceBetweenTwoPoints((double) maxLoc.x, (double) maxLoc.y, (double) matches.at(k).x, (double) matches.at(k).y) < 10.0) { alreadyFound = true; break; } } // Add matched location to the vector if (alreadyFound == false) { MATCHPOINTS match; match.val = maxVal; match.x = maxLoc.x; match.y = maxLoc.y; matches.push_back(match); count++; } } else { break; } } if (count >= maxMatch) break; } return true; }
void mStep() { // Update means_k, covs_k and weights_k from probs_ik int dim = trainSamples.cols; // Update weights // not normalized first reduce(trainProbs, weights, 0, CV_REDUCE_SUM); // Update means means.create(nclusters, dim, CV_64FC1); means = Scalar(0); const double minPosWeight = trainSamples.rows * DBL_EPSILON; double minWeight = DBL_MAX; int minWeightClusterIndex = -1; for(int clusterIndex = 0; clusterIndex < nclusters; clusterIndex++) { if(weights.at<double>(clusterIndex) <= minPosWeight) continue; if(weights.at<double>(clusterIndex) < minWeight) { minWeight = weights.at<double>(clusterIndex); minWeightClusterIndex = clusterIndex; } Mat clusterMean = means.row(clusterIndex); for(int sampleIndex = 0; sampleIndex < trainSamples.rows; sampleIndex++) clusterMean += trainProbs.at<double>(sampleIndex, clusterIndex) * trainSamples.row(sampleIndex); clusterMean /= weights.at<double>(clusterIndex); } // Update covsEigenValues and invCovsEigenValues covs.resize(nclusters); covsEigenValues.resize(nclusters); if(covMatType == COV_MAT_GENERIC) covsRotateMats.resize(nclusters); invCovsEigenValues.resize(nclusters); for(int clusterIndex = 0; clusterIndex < nclusters; clusterIndex++) { if(weights.at<double>(clusterIndex) <= minPosWeight) continue; if(covMatType != COV_MAT_SPHERICAL) covsEigenValues[clusterIndex].create(1, dim, CV_64FC1); else covsEigenValues[clusterIndex].create(1, 1, CV_64FC1); if(covMatType == COV_MAT_GENERIC) covs[clusterIndex].create(dim, dim, CV_64FC1); Mat clusterCov = covMatType != COV_MAT_GENERIC ? covsEigenValues[clusterIndex] : covs[clusterIndex]; clusterCov = Scalar(0); Mat centeredSample; for(int sampleIndex = 0; sampleIndex < trainSamples.rows; sampleIndex++) { centeredSample = trainSamples.row(sampleIndex) - means.row(clusterIndex); if(covMatType == COV_MAT_GENERIC) clusterCov += trainProbs.at<double>(sampleIndex, clusterIndex) * centeredSample.t() * centeredSample; else { double p = trainProbs.at<double>(sampleIndex, clusterIndex); for(int di = 0; di < dim; di++ ) { double val = centeredSample.at<double>(di); clusterCov.at<double>(covMatType != COV_MAT_SPHERICAL ? di : 0) += p*val*val; } } } if(covMatType == COV_MAT_SPHERICAL) clusterCov /= dim; clusterCov /= weights.at<double>(clusterIndex); // Update covsRotateMats for COV_MAT_GENERIC only if(covMatType == COV_MAT_GENERIC) { SVD svd(covs[clusterIndex], SVD::MODIFY_A + SVD::FULL_UV); covsEigenValues[clusterIndex] = svd.w; covsRotateMats[clusterIndex] = svd.u; } max(covsEigenValues[clusterIndex], minEigenValue, covsEigenValues[clusterIndex]); // update invCovsEigenValues invCovsEigenValues[clusterIndex] = 1./covsEigenValues[clusterIndex]; } for(int clusterIndex = 0; clusterIndex < nclusters; clusterIndex++) { if(weights.at<double>(clusterIndex) <= minPosWeight) { Mat clusterMean = means.row(clusterIndex); means.row(minWeightClusterIndex).copyTo(clusterMean); covs[minWeightClusterIndex].copyTo(covs[clusterIndex]); covsEigenValues[minWeightClusterIndex].copyTo(covsEigenValues[clusterIndex]); if(covMatType == COV_MAT_GENERIC) covsRotateMats[minWeightClusterIndex].copyTo(covsRotateMats[clusterIndex]); invCovsEigenValues[minWeightClusterIndex].copyTo(invCovsEigenValues[clusterIndex]); } } // Normalize weights weights /= trainSamples.rows; }
/** @function doHarris */ int Panorama::doHarris() { Mat R; Mat Rtemp; // Corner Response function Mat Ix, Iy, Ixy, Ix2, Iy2; // the second moment eigenvalues function double maxValtemp, minValtemp; double minVal; double maxVal; int sigma = 3; // Gaussian sigma float k = 0.04; // the alpha of Response function int aperture_size =3, block_size = 3; double scale = 1.0; // parameters of sobel first order derivative. char* window = "Harris."; // the name of Harris result ///////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////// cout << "Processing Harris Corner Detector..." << endl; /* Initialize the corner response function and the temp mat */ R = Mat::zeros( srcGray.size(), CV_32FC1 ); Rtemp = Mat::zeros( srcGray.size(), CV_32FC1 ); /* Use Sobel function to calculate the first order derivative of both x and y */ Sobel( srcGray, Ix, CV_32F, 1, 0, aperture_size, scale, 0, BORDER_DEFAULT ); Sobel( srcGray, Iy, CV_32F, 0, 1, aperture_size, scale, 0, BORDER_DEFAULT ); /* Calculate the Gaussian Derivative */ GaussianBlur(Iy, Iy, Size(block_size, block_size), sigma, 0 ,BORDER_DEFAULT); GaussianBlur(Ix, Ix, Size(block_size, block_size), sigma, 0 ,BORDER_DEFAULT); /* Calculate the square of each intensity */ pow(Ix,2,Ix2); pow(Iy,2,Iy2); /* Calculate the Gaussian Derivative */ GaussianBlur(Iy2, Iy2, Size(block_size, block_size), sigma, 0 ,BORDER_DEFAULT); GaussianBlur(Ix2, Ix2, Size(block_size, block_size), sigma, 0 ,BORDER_DEFAULT); /* Calculate the Corner Response function */ for( int j = 0; j < srcGray.rows; j++ ) { for( int i = 0; i < srcGray.cols; i++ ) { float lambda_1 = Ix2.at<float>( j, i, 0); float lambda_2 = Iy2.at<float>( j, i, 0); Rtemp.at<float>(j, i, 0) = lambda_1 * lambda_2 - k * pow( ( lambda_1 + lambda_2 ), 2 ); } } minMaxLoc( Rtemp, &minValtemp, &maxValtemp, 0, 0, Mat() ); /* Normalize Corner Response function as the maxium value is 1 */ for( int j = 0; j < srcGray.rows; j++ ) { for( int i = 0; i < srcGray.cols; i++ ) { R.at<float>(j, i) = 1 / maxValtemp * Rtemp.at<float>(j, i, 0); } } /* Find local maxima of response function (nonmaximum suppression)*/ minMaxLoc( R, &minVal, &maxVal, 0, 0, Mat() ); /* Create Window */ namedWindow( window, CV_WINDOW_AUTOSIZE ); int currentLevel = 5; int maxLevel = 100; double threshold = ( maxVal - minVal ) * currentLevel/maxLevel ; for( int j = 0; j < srcGray.rows; j++ ) { for( int i = 0; i < srcGray.cols; i++ ) { if( R.at<float>(j,i) > threshold) { circle( srcCopy, Point(i,j), 4, Scalar(255,255,255), 0, 0, 0 ); } } } imshow( window, srcCopy ); /* delete &minVal; delete &maxVal; delete &maxValtemp; delete &minValtemp; delete &R; delete &Rtemp; delete &Ix; delete &Iy; delete &Ix2; delete &Iy2; delete &Ixy; */ return(0); }
/** * @brief Processed a search request. * @param request the request to proceed. */ u_int32_t ORBSearcher::searchImage(SearchRequest &request) { timeval t[5]; gettimeofday(&t[0], NULL); cout << "Loading the image and extracting the ORBs." << endl; Mat img; u_int32_t i_ret = ImageLoader::loadImage(request.imageData.size(), request.imageData.data(), img); if (i_ret != OK) return i_ret; vector<KeyPoint> keypoints; Mat descriptors; ORB(1000, 1.02, 100)(img, noArray(), keypoints, descriptors); gettimeofday(&t[1], NULL); cout << "time: " << getTimeDiff(t[0], t[1]) << " ms." << endl; cout << "Looking for the visual words. " << endl; unordered_map<u_int32_t, list<Hit> > imageReqHits; // key: visual word, value: the found angles for (unsigned i = 0; i < keypoints.size(); ++i) { #define NB_NEIGHBORS 1 vector<int> indices(NB_NEIGHBORS); vector<int> dists(NB_NEIGHBORS); wordIndex->knnSearch(descriptors.row(i), indices, dists, NB_NEIGHBORS); for (unsigned j = 0; j < indices.size(); ++j) { const unsigned i_wordId = indices[j]; if (imageReqHits.find(i_wordId) == imageReqHits.end()) { // Convert the angle to a 16 bit integer. Hit hit; hit.i_imageId = 0; hit.i_angle = keypoints[i].angle / 360 * (1 << 16); hit.x = keypoints[i].pt.x; hit.y = keypoints[i].pt.y; imageReqHits[i_wordId].push_back(hit); } } } cout << imageReqHits.size() << " visual words kept for the request." << endl; const unsigned i_nbTotalIndexedImages = index->getTotalNbIndexedImages(); cout << i_nbTotalIndexedImages << " images indexed in the index." << endl; unordered_map<u_int32_t, vector<Hit> > indexHits; // key: visual word id, values: index hits. index->getImagesWithVisualWords(imageReqHits, indexHits); gettimeofday(&t[2], NULL); cout << "time: " << getTimeDiff(t[1], t[2]) << " ms." << endl; cout << "Ranking the images." << endl; unordered_map<u_int32_t, float> weights; // key: image id, value: image score. for (unordered_map<u_int32_t, vector<Hit> >::const_iterator it = indexHits.begin(); it != indexHits.end(); ++it) { const vector<Hit> &hits = it->second; const float f_weight = log((float)i_nbTotalIndexedImages / hits.size()); for (vector<Hit>::const_iterator it2 = hits.begin(); it2 != hits.end(); ++it2) { /* TF-IDF according to the paper "Video Google: * A Text Retrieval Approach to Object Matching in Videos" */ unsigned i_totalNbWords = index->countTotalNbWord(it2->i_imageId); weights[it2->i_imageId] += f_weight / i_totalNbWords; } } priority_queue<SearchResult> rankedResults; for (tr1::unordered_map<unsigned, float>::const_iterator it = weights.begin(); it != weights.end(); ++it) rankedResults.push(SearchResult(it->second, it->first)); gettimeofday(&t[3], NULL); cout << "time: " << getTimeDiff(t[2], t[3]) << " ms." << endl; cout << "Reranking 300 among " << rankedResults.size() << " images." << endl; priority_queue<SearchResult> rerankedResults; reranker.rerank(imageReqHits, indexHits, rankedResults, rerankedResults, 300); gettimeofday(&t[4], NULL); cout << "time: " << getTimeDiff(t[3], t[4]) << " ms." << endl; cout << "Returning the results. " << endl; returnResults(rerankedResults, request, 100); #if 0 // Draw keypoints and ellipses. Mat img_res; drawKeypoints(img, cleanKeypoints, img_res, Scalar::all(-1), DrawMatchesFlags::DEFAULT); for (unsigned i = 0; i < ellipses.size(); ++i) ellipse( img_res, ellipses[i], Scalar(0, 0, 255), 1); // Show the image. imshow("Keypoints 1", img_res); #endif return SEARCH_RESULTS; }
void Build_Vocabulary::extract(String filename, double threshold ,double thresholdCorner){ clock_t start, finish; double elapsed_time; string dir = "Validation/" + filename, filepath; DIR *dp; struct dirent *dirp; struct stat filestat; dp = opendir( dir.c_str() ); // detecting keypoints SiftFeatureDetector detector(threshold, thresholdCorner); vector<KeyPoint> keypoints; // computing descriptors //SiftDescriptorExtractor() extractor; Ptr<DescriptorExtractor > extractor( new OpponentColorDescriptorExtractor( Ptr<DescriptorExtractor>(new SiftDescriptorExtractor()) ) ); Mat descriptors; Mat training_descriptors; Mat img; cout << "------- Build Vocabulary ---------\n"; int count = 0; cout << "Extract descriptors.."<<endl; start=time(NULL); while ((dirp = readdir( dp ))) { filepath = dir + "/" + dirp->d_name; // If the file is a directory (or is in some way invalid) we'll skip it if (stat( filepath.c_str(), &filestat )) continue; if (S_ISDIR( filestat.st_mode )) continue; img = imread(filepath); if (!img.data) { continue; } detector.detect(img, keypoints); { Mat out; img.copyTo(out); drawKeypoints(img, keypoints, out, Scalar(255)); imshow("fg",img); imshow("keypoints", out); finish=time(NULL); elapsed_time = finish-start; int hours = (int) elapsed_time / 3600; int minutes = (int) (elapsed_time - hours * 3600) / 60; int seconds = (int) elapsed_time - hours * 3600 - minutes * 60; cout << "Elapsed Time: " << hours << ":" << minutes << ":" << seconds << endl; waitKey(0); } extractor->compute(img, keypoints, descriptors); training_descriptors.push_back(descriptors); cout << "."; count++; } cout << endl; closedir( dp ); cout << "Total Images Input: " << count << endl; cout << "Total descriptors: " << training_descriptors.rows << endl; finish=time(NULL); elapsed_time = finish-start; int hours = (int) elapsed_time / 3600; int minutes = (int) (elapsed_time - hours * 3600) / 60; int seconds = (int) elapsed_time - hours * 3600 - minutes * 60; cout << "Elapsed Time for Extracting: " << hours << ":" << minutes << ":" << seconds << endl << endl; FileStorage fs("training_descriptors_" + filename + "_color.txt", FileStorage::WRITE); fs << "training_descriptors" << training_descriptors; fs.release(); }
void CHuman::blobdeal(Mat &displayframe) { vector<dismin> blobdis; dismin vdis; dismin res; vector<dismin> dis; uint32 i=0; uint32 j =0; uint32 k =0; int id =0; int del_id = 0; int leftid = 0; int id_min = 0; int idmin = 0; int flag = 0; //Frame.human.track = Mat(Frame.variable.frame.rows, Frame.variable.frame.cols, CV_8UC3); //track =displayframe.clone() ; nikola humanlistpro = humanlist; if(humanlist.empty()) { //printf("no objects\n"); for(int lineNum =0;lineNum < DirectionLines.size();lineNum++) { // humanstatis.jishu[lineNum] ++; for( i=0; i < humanstatis.maxsize[lineNum];i++) { //cout<<"humanstatis.maxsize[lineNum]==="<<humanstatis.maxsize[lineNum]<<endl; //cout<<"humanstatis.objdispear[lineNum][i]==="<<humanstatis.objdispear[lineNum][i]<<endl; humanstatis.objdispear[lineNum][i]++; } if(humanstatis.maxsize[lineNum]) { for(unsigned int i=0;i<DOORFILTERNUM;i++)//Frame.human.humanlistpro.size() { flag = 0; //for(unsigned int j=0;j<DOORFILTERLEVEAL1;j++) //{ if(humanstatis.objdispear[lineNum][i] <25*4) flag = 1; /* }*/ if(!flag)//if(total == out) { humanstatis.doorout[lineNum] ++; humanstatis.maxsize[lineNum]-=1; /* for(unsigned int j=0;j<DOORFILTERLEVEAL1;j++) {*/ //Frame.humanstatis.dispear[lineNum][i][j] = in; humanstatis.objdispear[lineNum][i] = 0; /*}*/ } } } /* if(humanstatis.jishu[lineNum] >= 80) { humanstatis.doorout[lineNum] = humanstatis.doorin[lineNum]; humanstatis.jishu[lineNum] = 100; humanstatis.maxsize[lineNum] = 0; } */ for(int i=0;i<DOORFILTERNUM;i++) for(int j=0;j<DOORFILTERLEVEAL;j++) humanstatis.doorfilter[lineNum][i][j]=0; } return; } //memset(humanstatis.jishu,0,sizeof(humanstatis.jishu)); if( humanlist.size() > humanlistpre.size()) { for( i=0;i<humanlist.size();i++) { for(j=0;j<humanlistpre.size();j++) { vdis.dis=caldis(humanlist[i].x,humanlistpre[j].x,humanlist[i].y,humanlistpre[j].y); vdis.id = humanlistpre[j].id; blobdis.push_back(vdis); } if(!blobdis.empty ()) { res = seekvectormin(blobdis); //Frame.human.humanlist[i].id = id; id = humanlistpro[i].id; humanlistpro[i].id = res.id; res.id = id; dis.push_back(res); blobdis.clear(); //blobdis.resize(0); } } if(!dis.empty()) { if(humanlist.size() == (humanlistpre.size() +1)) { res = seekvectormax(dis); del_id = getid(dis,humanlistpre); humanlistpro[abs(res.id)].id = del_id;// 2 Frame.human.humanlist.size() - 1; } else if( humanlist.size()>=humanlistpre.size()+2)//have seperate { //int *index = new int[Frame.human.humanlistpre.size()]; for( i=0;i<dis.size();i++) { for( j=0;j<humanlistpro.size();j++) { if(dis[i].id == humanlistpro[j].id) { //*index++ = dis[i].id;// //dis[i].id = 127; dis.erase(dis.begin() + i); break; } } } id_min = dis[0].id; for(i=0;i<dis.size();i++) { if(id_min >= dis[i].id) { id_min =dis[i].id; idmin = i; } } for( k=0;k<dis.size();k++) { humanlistpro[dis[k].id].id = id_min; } } if(!dis.empty()) dis.clear(); } //std::sort(dis.begin(),dis.end()); //dis.back() } else//now < pre { for( i=0;i<humanlist.size();i++) { for( j=0;j<humanlistpre.size();j++) { vdis.dis=caldis(humanlist[i].x,humanlistpre[j].x,humanlist[i].y,humanlistpre[j].y); vdis.id = humanlistpre[j].id; blobdis.push_back(vdis); } if(!blobdis.empty()) { res = seekvectormin(blobdis); humanlistpro[i].id = res.id; blobdis.clear(); //blobdis.resize(0); } } for(int lineNum =0;lineNum < DirectionLines.size();lineNum++) { for( i=0;i<humanstatis.maxsize[lineNum];i++)//Frame.human.humanlistpre.size() { flag = 0; for(int j=0;j<humanlistpro.size();j++) { if(humanstatis.maxsize[lineNum] == humanlistpro[j].id) flag = 1; } if(!flag) { humanstatis.objdispear[lineNum][i]++; } } } } for(int lineNum =0;lineNum < DirectionLines.size();lineNum++) { for( i = 0;i<humanlistpro.size();i++) { id = humanlistpro[i].id; humanlistpro[i].pos[lineNum] = pointToline(DirectionLines[lineNum],Point2i(humanlistpro[i].x,humanlistpro[i].y)); for(int j=0;j<DOORFILTERLEVEAL-1;j++) humanstatis.doorfilter[lineNum][id][j] = humanstatis.doorfilter[lineNum][id][j+1]; humanstatis.doorfilter[lineNum][id][DOORFILTERLEVEAL-1] = humanlistpro[i].pos[lineNum]; if(humanlistpro[i].pos[lineNum] == in) { //Frame.humanstatis.dispear[lineNum][id][DOORFILTERLEVEAL1-1] = in; humanstatis.objdispear[lineNum][i] = 0; } } doorwaydetect(lineNum); } humanlistpre = humanlistpro; for(int lineNum =0;lineNum < DirectionLines.size();lineNum++) { if(humanstatis.maxsize[lineNum]<humanstatis.doorin[lineNum] - humanstatis.doorout[lineNum]) humanstatis.maxsize[lineNum] = humanstatis.doorin[lineNum] - humanstatis.doorout[lineNum]; } for(unsigned int n=0;n<humanlistpro.size();n++) { Rect rt=Rect(humanlistpro[n].x-humanlistpro[n].w/2,humanlistpro[n].y-humanlistpro[n].h/2,humanlistpro[n].w,humanlistpro[n].h); rectangle(displayframe, rt, Scalar( 0, 255, 255 ), 2, 8, 0); } }
void MouseNavigationTool::buttonCallback(int,int buttonIndex,InputDevice::ButtonCallbackData* cbData) { /* Process based on which button was pressed: */ switch(buttonIndex) { case 0: if(cbData->newButtonState) // Button has just been pressed { /* Act depending on this tool's current state: */ switch(navigationMode) { case IDLE: case SPINNING: if(factory->interactWithWidgets) { /* Check if the mouse pointer is over a GLMotif widget: */ GLMotif::Event event(false); event.setWorldLocation(calcSelectionRay()); if(getWidgetManager()->pointerButtonDown(event)) { if(navigationMode==SPINNING) { /* Deactivate this tool: */ deactivate(); } /* Go to widget interaction mode: */ navigationMode=WIDGETING; /* Drag the entire root widget if the event's target widget is a title bar: */ if(dynamic_cast<GLMotif::TitleBar*>(event.getTargetWidget())!=0) { /* Start dragging: */ draggedWidget=event.getTargetWidget(); /* Calculate the dragging transformation: */ NavTrackerState initialTracker=NavTrackerState::translateFromOriginTo(calcScreenPos()); preScale=Geometry::invert(initialTracker); GLMotif::WidgetManager::Transformation initialWidget=getWidgetManager()->calcWidgetTransformation(draggedWidget); preScale*=NavTrackerState(initialWidget); } else draggedWidget=0; } else { /* Try activating this tool: */ if(navigationMode==SPINNING||activate()) startRotating(); } } else { /* Try activating this tool: */ if(navigationMode==SPINNING||activate()) startRotating(); } break; case PANNING: if(dolly) startDollying(); else startScaling(); break; default: /* This shouldn't happen; just ignore the event */ break; } } else // Button has just been released { /* Act depending on this tool's current state: */ switch(navigationMode) { case WIDGETING: { /* Deliver the event: */ GLMotif::Event event(true); event.setWorldLocation(calcSelectionRay()); getWidgetManager()->pointerButtonUp(event); /* Deactivate this tool: */ navigationMode=IDLE; draggedWidget=0; break; } case ROTATING: { /* Check if the input device is still moving: */ Point currentPos=calcScreenPos(); Vector delta=currentPos-lastRotationPos; if(Geometry::mag(delta)>factory->spinThreshold) { /* Calculate spinning angular velocity: */ Vector offset=(lastRotationPos-screenCenter)+rotateOffset; Vector axis=Geometry::cross(offset,delta); Scalar angularVelocity=Geometry::mag(delta)/(factory->rotateFactor*getCurrentFrameTime()); spinAngularVelocity=axis*(Scalar(0.5)*angularVelocity/axis.mag()); /* Go to spinning mode: */ navigationMode=SPINNING; } else { /* Deactivate this tool: */ deactivate(); /* Go to idle mode: */ navigationMode=IDLE; } break; } case DOLLYING: case SCALING: startPanning(); break; default: /* This shouldn't happen; just ignore the event */ break; } } break; case 1: if(cbData->newButtonState) // Button has just been pressed { /* Act depending on this tool's current state: */ switch(navigationMode) { case IDLE: case SPINNING: /* Try activating this tool: */ if(navigationMode==SPINNING||activate()) startPanning(); break; case ROTATING: if(dolly) startDollying(); else startScaling(); break; default: /* This shouldn't happen; just ignore the event */ break; } } else // Button has just been released { /* Act depending on this tool's current state: */ switch(navigationMode) { case PANNING: /* Deactivate this tool: */ deactivate(); /* Go to idle mode: */ navigationMode=IDLE; break; case DOLLYING: case SCALING: startRotating(); break; default: /* This shouldn't happen; just ignore the event */ break; } } break; case 2: /* Set the dolly flag: */ dolly=cbData->newButtonState; if(factory->invertDolly) dolly=!dolly; if(dolly) // Dollying has just been enabled { /* Act depending on this tool's current state: */ switch(navigationMode) { case SCALING: startDollying(); break; default: /* Nothing to do */ break; } } else { /* Act depending on this tool's current state: */ switch(navigationMode) { case DOLLYING: startScaling(); break; default: /* Nothing to do */ break; } } break; } }
int CHuman::HumanDetectRun(Mat &displayframe) { //int time_use=0; //struct timeval start; //struct timeval end; //gettimeofday(&start,NULL); Mat tmpframe; //Mat blobdealFrame; vector< vector<Point> > contours; Rect contoursRect; alarm =0; displayframe.copyTo(tmpframe); //displayframe.copyTo(blobdealFrame); vector<blobnode>().swap(humanlistpro); m_zoomRows = tmpframe.rows /m_rowsZoomRate; m_zoomCols = tmpframe.cols /m_colsZoomRate; w_Rate = (float)tmpframe.cols / m_zoomCols; h_Rate = (float)tmpframe.rows / m_zoomRows; Mat morph = Mat(tmpframe.rows ,tmpframe.cols,CV_8UC1); mog(tmpframe,foregrondframe,0.001); // 0.001 frameindex++; if(frameindex<250) return 2; if(frameindex >= 250) frameindex =250; foregrondframe.copyTo(mask); threshold(mask, mask, 200, 255, THRESH_BINARY); cv::erode(mask, mask, cv::Mat()); cv::dilate(mask, mask, cv::Mat()); algorithmMorphology_Operations(mask, mask); findContours(mask, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE); mask.release(); //nikola m_BlobRects.clear(); for(int i=0;i<contours.size();i++) { contoursRect = boundingRect(contours[i]); if(fabs(contourArea(contours[i])) > 600.0) { //rectangle(displayframe, contoursRect,color_rect, 2, 8, 0); m_BlobRects.push_back(contoursRect); } } if((m_Flag & 0x02) == 0x02){ for(int i=0;i<DirectionLines.size();i++) { line(displayframe,DirectionLines[i].Start,DirectionLines[i].End,Scalar(255)); } } if((m_Flag & 0x01) == 1){ for(int ii=0;ii<MonitorZoneRects.size();ii++) { rectangle(displayframe, MonitorZoneRects[ii], Scalar( 255, 0, 0 ), 2, 8, 0);//�� } } human_detect(morph,displayframe); if((m_Flag & 0x01) == 1){ census(displayframe);// for human statistics } if((m_Flag & 0x02) == 0x02){ blobdeal(displayframe); } if(humanstatis.numAll<(humanstatis.doorin[0]+humanstatis.doorin[1]-humanstatis.doorout[0]-humanstatis.doorout[1])) humanstatis.numAll = humanstatis.doorin[0]+humanstatis.doorin[1]-humanstatis.doorout[0]-humanstatis.doorout[1]; //dbgprint("door1:in=%d,out=%d door2:in=%d,out=%d\n",humanstatis.doorin[0],humanstatis.doorout[0],humanstatis.doorin[1],humanstatis.doorout[1]); if(humanstatis.numAll >= MaxNum){ //printf("humanstatis.numAll is %d\n",humanstatis.numAll); alarm =1; } char dstr[100]; sprintf(dstr, "door1:in=%d,out=%d door2:in=%d,out=%d",humanstatis.doorin[0],humanstatis.doorout[0],humanstatis.doorin[1],humanstatis.doorout[1]); putText(displayframe,dstr,cvPoint(200,25),CV_FONT_HERSHEY_COMPLEX, 0.5, cvScalar(0,0,255)); //printf("humanstatis Num:%d, humanstatisIn:%d,humanstatisOut:%d\n",humanstatis.numAll,humanstatis.inAll,humanstatis.outAll); //char dstr[100]; //sprintf(dstr, "in=%d,out=%d",humanstatis.doorin,humanstatis.doorout); //putText(displayframe,dstr,cvPoint(25,25),CV_FONT_HERSHEY_COMPLEX, 1, cvScalar(0,0,255)); //printf("doorin=%d,doorout=%d\n",humanstatis.doorin,humanstatis.doorout); tmpframe.release(); //nikola morph.release(); vector<Point>().swap(object); //vector<Point> vector<blobnode>().swap(humanlist); //gettimeofday(&end,NULL); //time_use=(end.tv_sec-start.tv_sec)*1000+(end.tv_usec-start.tv_usec)/1000;//�� //printf("time_use is %d\n",time_use); foregrondframe.release(); return 0; }
MouseNavigationToolFactory::MouseNavigationToolFactory(ToolManager& toolManager) :ToolFactory("MouseNavigationTool",toolManager), rotatePlaneOffset(getInchFactor()*Scalar(12)), rotateFactor(getInchFactor()*Scalar(12)), invertDolly(false), screenDollyingDirection(0,1,0), screenScalingDirection(0,1,0), dollyFactor(getInchFactor()*Scalar(12)), scaleFactor(getInchFactor()*Scalar(12)), wheelDollyFactor(getInchFactor()*Scalar(-12)), wheelScaleFactor(Scalar(0.5)), spinThreshold(Scalar(0)), showScreenCenter(false), interactWithWidgets(true), showMouseCursor(false), mouseCursorSize(Scalar(0.5),Scalar(0.5),Scalar(0.0)), mouseCursorHotspot(Scalar(0.0),Scalar(1.0),Scalar(0.0)), mouseCursorImageFileName(DEFAULTMOUSECURSORIMAGEFILENAME), mouseCursorNominalSize(24) { /* Initialize tool layout: */ layout.setNumDevices(1); layout.setNumButtons(0,3); layout.setNumValuators(0,1); /* Insert class into class hierarchy: */ ToolFactory* navigationToolFactory=toolManager.loadClass("NavigationTool"); navigationToolFactory->addChildClass(this); addParentClass(navigationToolFactory); /* Load class settings: */ Misc::ConfigurationFileSection cfs=toolManager.getToolClassSection(getClassName()); rotatePlaneOffset=cfs.retrieveValue<Scalar>("./rotatePlaneOffset",rotatePlaneOffset); rotateFactor=cfs.retrieveValue<Scalar>("./rotateFactor",rotateFactor); invertDolly=cfs.retrieveValue<bool>("./invertDolly",invertDolly); screenDollyingDirection=cfs.retrieveValue<Vector>("./screenDollyingDirection",screenDollyingDirection); screenScalingDirection=cfs.retrieveValue<Vector>("./screenScalingDirection",screenScalingDirection); dollyFactor=cfs.retrieveValue<Scalar>("./dollyFactor",dollyFactor); scaleFactor=cfs.retrieveValue<Scalar>("./scaleFactor",scaleFactor); wheelDollyFactor=cfs.retrieveValue<Scalar>("./wheelDollyFactor",wheelDollyFactor); wheelScaleFactor=cfs.retrieveValue<Scalar>("./wheelScaleFactor",wheelScaleFactor); spinThreshold=cfs.retrieveValue<Scalar>("./spinThreshold",spinThreshold); showScreenCenter=cfs.retrieveValue<bool>("./showScreenCenter",showScreenCenter); interactWithWidgets=cfs.retrieveValue<bool>("./interactWithWidgets",interactWithWidgets); showMouseCursor=cfs.retrieveValue<bool>("./showMouseCursor",showMouseCursor); mouseCursorSize=cfs.retrieveValue<Size>("./mouseCursorSize",mouseCursorSize); mouseCursorHotspot=cfs.retrieveValue<Vector>("./mouseCursorHotspot",mouseCursorHotspot); mouseCursorImageFileName=cfs.retrieveString("./mouseCursorImageFileName",mouseCursorImageFileName); mouseCursorNominalSize=cfs.retrieveValue<unsigned int>("./mouseCursorNominalSize",mouseCursorNominalSize); /* Set tool class' factory pointer: */ MouseNavigationTool::factory=this; }
void MultiCursorAppCpp::detectHeadPosition(CvBlobs blobs) { // Initialize userData userData.clear(); // 前フレームのラベルを見て最も投票数の多かったラベルを前フレームの対応領域とする for (CvBlobs::const_iterator it = blobs.begin(); it != blobs.end(); ++it) { if (!preLabelMat.empty()) { // 前フレームのラベルを投票 vector<Point2i> checkLabel; checkLabel.push_back(Point2i(0, 0)); // init checkLabel; for (int y = it->second->miny; y < it->second->maxy; ++y) { for (int x = it->second->minx; x < it->second->maxx; ++x) { unsigned long preLabel = preLabelMat.at<unsigned long>(y, x); if (it->first == labelMat.at<unsigned long>(y, x) && preLabel != 0) { bool isFoundLabel = false; for (int i = 0; i < checkLabel.size(); ++i) { if (checkLabel[i].x == preLabel) { ++checkLabel[i].y; isFoundLabel = true; break; } } if (!isFoundLabel) { Point2i newPoint(preLabel, 1); // (labelID, # of label) checkLabel.push_back(newPoint); } } } } // 最も投票数の多いラベルを探す Point2i mainLabel(0, 0); for (int i = 0; i < checkLabel.size(); ++i) { if (checkLabel[i].y > mainLabel.y) { mainLabel.x = checkLabel[i].x; mainLabel.y = checkLabel[i].y; } } // 前フレームのデータと対応付けて現フレームのデータを作る UserData newUserData; newUserData.isDataFound = false; if (mainLabel.y > it->second->area / 2) // 半分以上を占める領域がないときは無視 { for (vector<UserData>::iterator p = preUserData.begin(); p != preUserData.end(); ++p) { if (p->labelID == mainLabel.x) { p->isDataFound = true; newUserData.isDataFound = true; newUserData.headInfo.height = p->headInfo.height; newUserData.headInfo.depthPoint.x = p->headInfo.depthPoint.x; newUserData.headInfo.depthPoint.y = p->headInfo.depthPoint.y; break; } } } newUserData.labelID = it->first; newUserData.centroid.x = it->second->centroid.x; newUserData.centroid.y = it->second->centroid.y; userData.push_back(newUserData); } else { UserData newUserData; newUserData.isDataFound = false; newUserData.labelID = it->first; newUserData.centroid.x = it->second->centroid.x; newUserData.centroid.y = it->second->centroid.y; userData.push_back(newUserData); } } // Update preLabel preLabelMat = labelMat; // Find the highest point of each user area USHORT* headHeights = new USHORT[blobs.size()]; Point2i* newHighestPositions = new Point2i[blobs.size()]; int blobID = 0; for (CvBlobs::const_iterator it = blobs.begin(); it != blobs.end(); it++) { headHeights[blobID] = 0; newHighestPositions[blobID].x = 0; newHighestPositions[blobID].y = 0; for (int y = it->second->miny; y <= it->second->maxy; y++) { for (int x = it->second->minx; x <= it->second->maxx; x++) { if (0 <= blobID && blobID < blobs.size()) { USHORT height = *heightMatrix.ptr<USHORT>(y, x); if (headHeights[blobID] < height && height < HEAD_HEIGHT_MAX) { headHeights[blobID] = height; newHighestPositions[blobID].x = x; newHighestPositions[blobID].y = y; } } } } // Debug: Show the highest point of each users // circle(userAreaMat, Point(newHighestPositions[blobID].x, newHighestPositions[blobID].y), 5, Scalar(255, 0, 255), 3); blobID++; } // Define users' head positions Point2i* newHeadPositions = new Point2i[blobs.size()]; INT* numHeadPoints = new INT[blobs.size()]; blobID = 0; for (CvBlobs::const_iterator it = blobs.begin(); it != blobs.end(); ++it) { // Set the highest position as a base point of searching userData[blobID].headInfo.depthPoint.x = newHighestPositions[blobID].x; userData[blobID].headInfo.depthPoint.y = newHighestPositions[blobID].y; userData[blobID].headInfo.height = headHeights[blobID]; if (userData[blobID].isDataFound) { // Check distance between 2d positions in current frame and in preframe float distance = sqrt( pow(userData[blobID].headInfo.depthPoint.x - preUserData[blobID].headInfo.depthPoint.x, 2) + pow(userData[blobID].headInfo.depthPoint.y - preUserData[blobID].headInfo.depthPoint.y, 2) ); float distanceZ = abs(heightMatrix.at<float>(userData[blobID].headInfo.depthPoint.y, userData[blobID].headInfo.depthPoint.x) - preUserData[blobID].headInfo.height); //cout << distance << endl; // If the point is far from predata, just use pre-data / もし前回のフレームより大きく頭の位置がずれていたら前回の値を使う if (distance > 80.0f || distanceZ > 1000.0f) { userData[blobID].headInfo.height = preUserData[blobID].headInfo.height; userData[blobID].headInfo.depthPoint.x = preUserData[blobID].headInfo.depthPoint.x; userData[blobID].headInfo.depthPoint.y = preUserData[blobID].headInfo.depthPoint.y; } } // Estimate exact head positions (Get average) numHeadPoints[blobID] = 0; newHeadPositions[blobID].x = 0; newHeadPositions[blobID].y = 0; #if 1 int offset_head = 40; // ミーティングルーム用 #else int offset_head = 100; // 作業場用 #endif int minY = userData[blobID].headInfo.depthPoint.y - offset_head; if (minY < 0) minY = 0; int maxY = userData[blobID].headInfo.depthPoint.y + offset_head; if (maxY > kinectBasics.heightDepth) maxY = kinectBasics.heightDepth; int minX = userData[blobID].headInfo.depthPoint.x - offset_head; if (minX < 0) minX = 0; int maxX = userData[blobID].headInfo.depthPoint.x + offset_head; if (maxX > kinectBasics.widthDepth) maxX = kinectBasics.widthDepth; for (int y = minY; y <= maxY; y++) { for (int x = minX; x <= maxX; x++) { USHORT height = *heightMatrix.ptr<USHORT>(y, x); //cout << it->first << ", " << labelMat.at<unsigned long>(y, x) << endl; if ((userData[blobID].headInfo.height - HEAD_LENGTH) < height && height < HEAD_HEIGHT_MAX && it->first == labelMat.at<unsigned long>(y, x) // 同じblob内のみ探索 ) { newHeadPositions[blobID].x += x; newHeadPositions[blobID].y += y; numHeadPoints[blobID]++; } } } blobID++; } // Make avarage pixel value of each head positions the head positions of users for (int i = 0; i < blobs.size(); i++) { if (numHeadPoints[i] != 0) { // Calculate head position in 2D pixel userData[i].headInfo.depthPoint.x = newHeadPositions[i].x / numHeadPoints[i]; userData[i].headInfo.depthPoint.y = newHeadPositions[i].y / numHeadPoints[i]; } else { // 点が見つからなかった場合は最も高い点を頭にする userData[i].headInfo.depthPoint.x = newHighestPositions[i].x; userData[i].headInfo.depthPoint.y = newHighestPositions[i].y; } // Calculate head position in 3D point float* headPosition = point3fMatrix.ptr<float>(userData[i].headInfo.depthPoint.y, userData[i].headInfo.depthPoint.x); userData[i].headInfo.cameraPoint.x = headPosition[0]; userData[i].headInfo.cameraPoint.y = headPosition[1]; userData[i].headInfo.cameraPoint.z = headPosition[2] + 0.2; // Debug: Show the head point circle(userAreaMat, Point(userData[i].headInfo.depthPoint.x, userData[i].headInfo.depthPoint.y), 7, Scalar(255, 0, 0), 3); } }
/////////////////////////////////////////////////////////////////// // Panel::MaskWithColor() // Description: /////////////////////////////////////////////////////////////////// void Panel::MaskWithColor(string sImgPath, string color, bool debug) { Mat HSV, Mask, Mask1, Mask2, MaskResult; // Show the original image if(!ShowImage(sImgPath, "Original")) return; Mat image; if (m_roi.width && m_roi.width <= m_Image.cols && m_roi.height && m_roi.height <= m_Image.rows) image = m_pPanel->m_Image(m_roi); else image = m_pPanel->m_Image; // Convert the color HSV Format cvtColor(image, HSV, CV_BGR2HSV); // The inRange() function will create a mask with // only the pixels of color in the range specified if (color == "Blue") { inRange(HSV, Scalar(105, 100, 30), Scalar(131, 255, 255), Mask); } else if (color == "Red") { inRange(HSV, Scalar(0, 100, 30), Scalar(6, 255, 255), Mask1); inRange(HSV, Scalar(165, 100, 30), Scalar(179, 255, 255), Mask2); bitwise_or(Mask1, Mask2, Mask); } else if (color == "panel") { // test code int h1Lo = 0, s1Lo = 55, v1Lo = 115; int h1Hi = 20, s1Hi = 120, v1Hi = 210; int h2Lo = 0, s2Lo = 0, v2Lo = 0; int h2Hi = 0, s2Hi = 0, v2Hi = 0; namedWindow("InRange Tester", CV_WINDOW_NORMAL); cvCreateTrackbar("Hue Lo 1:", "InRange Tester", &h1Lo, 180); cvCreateTrackbar("Hue Hi 1:", "InRange Tester", &h1Hi, 180); cvCreateTrackbar("Sat Lo 1", "InRange Tester", &s1Lo, 255); cvCreateTrackbar("Sat Hi 1", "InRange Tester", &s1Hi, 255); cvCreateTrackbar("Val Lo 1", "InRange Tester", &v1Lo, 255); cvCreateTrackbar("Val Hi 1", "InRange Tester", &v1Hi, 255); cvCreateTrackbar("Hue Lo 2:", "InRange Tester", &h2Lo, 180); cvCreateTrackbar("Hue Hi 2:", "InRange Tester", &h2Hi, 180); cvCreateTrackbar("Sat Lo 2", "InRange Tester", &s2Lo, 255); cvCreateTrackbar("Sat Hi 2", "InRange Tester", &s2Hi, 255); cvCreateTrackbar("Val Lo 2", "InRange Tester", &v2Lo, 255); cvCreateTrackbar("Val Hi 2", "InRange Tester", &v2Hi, 255); while (true) { inRange(HSV, Scalar(h1Lo, s1Lo, v1Lo), Scalar(h1Hi, s1Hi, v1Hi), Mask1); inRange(HSV, Scalar(h2Lo, s2Lo, v2Lo), Scalar(h2Hi, s2Hi, v2Hi), Mask2); bitwise_or(Mask1, Mask2, Mask); image.copyTo(MaskResult, Mask); namedWindow("Mask Result", CV_WINDOW_NORMAL); imshow("Mask Result", MaskResult); if (waitKey(50) == 27) break; } } image.copyTo(MaskResult, Mask); if (color == "Red" || color == "Blue") { // Use the below lines to debug red or blue color mask // namedWindow("Mask Result", CV_WINDOW_AUTOSIZE); // imshow("Mask Result", MaskResult); DetectBlob(MaskResult, debug); } }