bool ImageSegmenter::Process() { // Read input image m_docImage = imread(m_document_image_path.c_str(), CV_LOAD_IMAGE_GRAYSCALE); // converts to grayscale if required PreprocessImage(m_docImage); SplitIntoLines(m_docImage); if (m_output_level == "LINES") { SaveOutput(m_output_level); return true; } else { SplitLinesIntoWords(m_docImage); SaveOutput(m_output_level); } return true; }
void CVisionPipeline::TrackMotion (CIplImage &image, float &xVel, float &yVel) { CvTermCriteria term; CvRect box; static TAnalisysMatrix velXMatrix, velYMatrix, velModulusMatrix; crvColorToGray (image.ptr(), m_imgCurr.ptr()); // Prepare ROI's m_imgPrev.PushROI (); m_imgCurr.PushROI (); m_imgVelX.PushROI (); m_imgVelY.PushROI (); m_imgCurrProc.PushROI (); m_trackArea.GetBoxImg (&image, box); m_imgPrev.SetROI (box); m_imgCurr.SetROI (box); m_imgPrevProc.SetROI (box); // Mutex is not needed m_imgCurrProc.SetROI (box); m_imgVelX.SetROI (box); m_imgVelY.SetROI (box); // Pre-processing PreprocessImage (); // Compute optical flow term.type= CV_TERMCRIT_ITER; term.max_iter= 6; cvCalcOpticalFlowHS (m_imgPrevProc.ptr(), m_imgCurrProc.ptr(), 0, m_imgVelX.ptr(), m_imgVelY.ptr(), 0.001, term); MatrixMeanImageCells (&m_imgVelX, velXMatrix); MatrixMeanImageCells (&m_imgVelY, velYMatrix); int x, y; float velModulusMax= 0; // Compute modulus for every motion cell for (x= 0; x< COMP_MATRIX_WIDTH; ++x) { for (y= 0; y< COMP_MATRIX_HEIGHT; ++y) { velModulusMatrix[x][y]= (velXMatrix[x][y] * velXMatrix[x][y] + velYMatrix[x][y] * velYMatrix[x][y]); if (velModulusMax< velModulusMatrix[x][y]) velModulusMax= velModulusMatrix[x][y]; } } // Select valid cells (i.e. those with enough motion) int validCells= 0; xVel= yVel= 0; for (x= 0; x< COMP_MATRIX_WIDTH; ++x) { for (y= 0; y< COMP_MATRIX_HEIGHT; ++y) { if (velModulusMatrix[x][y]> (0.05 * velModulusMax) ) { ++validCells; xVel+= velXMatrix[x][y]; yVel+= velYMatrix[x][y]; } } } // Ensure minimal area to avoid extremely high values int cellArea= (box.width * box.height) / (COMP_MATRIX_WIDTH * COMP_MATRIX_HEIGHT); if (cellArea== 0) cellArea= 1; int minValidCells= (3000 / cellArea); if (validCells< minValidCells) validCells= minValidCells; // Compute speed xVel= - (xVel / (float) validCells) * 40; yVel= (yVel / (float) validCells) * 80; // Restore ROI's m_imgCurrProc.PopROI (); m_imgPrev.PopROI (); m_imgCurr.PopROI (); m_imgVelX.PopROI (); m_imgVelY.PopROI (); }