void StereoCalib(const vector<string>& imagelist, Size boardSize, bool useCalibrated=true, bool showRectified=true) { if( imagelist.size() % 2 != 0 ) { cout << "Error: the image list contains odd (non-even) number of elements\n"; return; } printf("board size: %d x %d", boardSize.width, boardSize.height); bool displayCorners = true; const int maxScale = 2; const float squareSize = 1.f; // Set this to your actual square size // ARRAY AND VECTOR STORAGE: vector<vector<Point2f> > imagePoints[2]; vector<vector<Point3f> > objectPoints; Size imageSize; int i, j, k, nimages = (int)imagelist.size()/2; imagePoints[0].resize(nimages); imagePoints[1].resize(nimages); vector<string> goodImageList; for( i = j = 0; i < nimages; i++ ) { for( k = 0; k < 2; k++ ) { const string& filename = imagelist[i*2+k]; Mat img = imread(filename, 0); if(img.empty()) break; if( imageSize == Size() ) imageSize = img.size(); else if( img.size() != imageSize ) { cout << "The image " << filename << " has the size different from the first image size. Skipping the pair\n"; break; } bool found = false; vector<Point2f>& corners = imagePoints[k][j]; for( int scale = 1; scale <= maxScale; scale++ ) { Mat timg; if( scale == 1 ) timg = img; else resize(img, timg, Size(), scale, scale); found = findChessboardCorners(timg, boardSize, corners, CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_NORMALIZE_IMAGE); if( found ) { if( scale > 1 ) { Mat cornersMat(corners); cornersMat *= 1./scale; } break; } } if( displayCorners ) { cout << filename << endl; Mat cimg, cimg1; cvtColor(img, cimg, CV_GRAY2BGR); drawChessboardCorners(cimg, boardSize, corners, found); double sf = 640./MAX(img.rows, img.cols); resize(cimg, cimg1, Size(), sf, sf); imshow("corners", cimg1); char c = (char)waitKey(500); if( c == 27 || c == 'q' || c == 'Q' ) //Allow ESC to quit exit(-1); } else putchar('.'); if( !found ) break; cornerSubPix(img, corners, Size(11,11), Size(-1,-1), TermCriteria(CV_TERMCRIT_ITER+CV_TERMCRIT_EPS, 30, 0.01)); } if( k == 2 ) { goodImageList.push_back(imagelist[i*2]); goodImageList.push_back(imagelist[i*2+1]); j++; } } cout << j << " pairs have been successfully detected.\n"; nimages = j; if( nimages < 2 ) { cout << "Error: too little pairs to run the calibration\n"; return; } imagePoints[0].resize(nimages); imagePoints[1].resize(nimages); objectPoints.resize(nimages); for( i = 0; i < nimages; i++ ) { for( j = 0; j < boardSize.height; j++ ) for( k = 0; k < boardSize.width; k++ ) objectPoints[i].push_back(Point3f(j*squareSize, k*squareSize, 0)); } cout << "Running stereo calibration ...\n"; Mat cameraMatrix[2], distCoeffs[2]; cameraMatrix[0] = Mat::eye(3, 3, CV_64F); cameraMatrix[1] = Mat::eye(3, 3, CV_64F); Mat R, T, E, F; double rms = stereoCalibrate(objectPoints, imagePoints[0], imagePoints[1], cameraMatrix[0], distCoeffs[0], cameraMatrix[1], distCoeffs[1], imageSize, R, T, E, F, TermCriteria(CV_TERMCRIT_ITER+CV_TERMCRIT_EPS, 100, 1e-5), CV_CALIB_FIX_ASPECT_RATIO + CV_CALIB_ZERO_TANGENT_DIST + //CV_CALIB_SAME_FOCAL_LENGTH + CV_CALIB_RATIONAL_MODEL + CV_CALIB_FIX_K3 + CV_CALIB_FIX_K4 + CV_CALIB_FIX_K5); cout << "done with RMS error=" << rms << endl; // CALIBRATION QUALITY CHECK // because the output fundamental matrix implicitly // includes all the output information, // we can check the quality of calibration using the // epipolar geometry constraint: m2^t*F*m1=0 double err = 0; int npoints = 0; vector<Vec3f> lines[2]; for( i = 0; i < nimages; i++ ) { int npt = (int)imagePoints[0][i].size(); Mat imgpt[2]; for( k = 0; k < 2; k++ ) { imgpt[k] = Mat(imagePoints[k][i]); undistortPoints(imgpt[k], imgpt[k], cameraMatrix[k], distCoeffs[k], Mat(), cameraMatrix[k]); computeCorrespondEpilines(imgpt[k], k+1, F, lines[k]); } for( j = 0; j < npt; j++ ) { double errij = fabs(imagePoints[0][i][j].x*lines[1][j][0] + imagePoints[0][i][j].y*lines[1][j][1] + lines[1][j][2]) + fabs(imagePoints[1][i][j].x*lines[0][j][0] + imagePoints[1][i][j].y*lines[0][j][1] + lines[0][j][2]); err += errij; } npoints += npt; } cout << "average reprojection err = " << err/npoints << endl; // save intrinsic parameters FileStorage fs("calib/intrinsics.yml", CV_STORAGE_WRITE); if( fs.isOpened() ) { fs << "M1" << cameraMatrix[0] << "D1" << distCoeffs[0] << "M2" << cameraMatrix[1] << "D2" << distCoeffs[1]; fs.release(); } else cout << "Error: can not save the intrinsic parameters\n"; Mat R1, R2, P1, P2, Q; Rect validRoi[2]; stereoRectify(cameraMatrix[0], distCoeffs[0], cameraMatrix[1], distCoeffs[1], imageSize, R, T, R1, R2, P1, P2, Q, CALIB_ZERO_DISPARITY, 1, imageSize, &validRoi[0], &validRoi[1]); fs.open("calib/extrinsics.yml", CV_STORAGE_WRITE); if( fs.isOpened() ) { fs << "R" << R << "T" << T << "R1" << R1 << "R2" << R2 << "P1" << P1 << "P2" << P2 << "Q" << Q; fs.release(); } else cout << "Error: can not save the intrinsic parameters\n"; // OpenCV can handle left-right // or up-down camera arrangements bool isVerticalStereo = fabs(P2.at<double>(1, 3)) > fabs(P2.at<double>(0, 3)); // COMPUTE AND DISPLAY RECTIFICATION if( !showRectified ) return; Mat rmap[2][2]; // IF BY CALIBRATED (BOUGUET'S METHOD) if( useCalibrated ) { // we already computed everything } // OR ELSE HARTLEY'S METHOD else // use intrinsic parameters of each camera, but // compute the rectification transformation directly // from the fundamental matrix { vector<Point2f> allimgpt[2]; for( k = 0; k < 2; k++ ) { for( i = 0; i < nimages; i++ ) std::copy(imagePoints[k][i].begin(), imagePoints[k][i].end(), back_inserter(allimgpt[k])); } F = findFundamentalMat(Mat(allimgpt[0]), Mat(allimgpt[1]), FM_8POINT, 0, 0); Mat H1, H2; stereoRectifyUncalibrated(Mat(allimgpt[0]), Mat(allimgpt[1]), F, imageSize, H1, H2, 3); R1 = cameraMatrix[0].inv()*H1*cameraMatrix[0]; R2 = cameraMatrix[1].inv()*H2*cameraMatrix[1]; P1 = cameraMatrix[0]; P2 = cameraMatrix[1]; } //Precompute maps for cv::remap() initUndistortRectifyMap(cameraMatrix[0], distCoeffs[0], R1, P1, imageSize, CV_16SC2, rmap[0][0], rmap[0][1]); initUndistortRectifyMap(cameraMatrix[1], distCoeffs[1], R2, P2, imageSize, CV_16SC2, rmap[1][0], rmap[1][1]); Mat canvas; double sf; int w, h; if( !isVerticalStereo ) { sf = 600./MAX(imageSize.width, imageSize.height); w = cvRound(imageSize.width*sf); h = cvRound(imageSize.height*sf); canvas.create(h, w*2, CV_8UC3); } else { sf = 300./MAX(imageSize.width, imageSize.height); w = cvRound(imageSize.width*sf); h = cvRound(imageSize.height*sf); canvas.create(h*2, w, CV_8UC3); } for( i = 0; i < nimages; i++ ) { for( k = 0; k < 2; k++ ) { Mat img = imread(goodImageList[i*2+k], 0), rimg, cimg; remap(img, rimg, rmap[k][0], rmap[k][1], CV_INTER_LINEAR); cvtColor(rimg, cimg, CV_GRAY2BGR); Mat canvasPart = !isVerticalStereo ? canvas(Rect(w*k, 0, w, h)) : canvas(Rect(0, h*k, w, h)); resize(cimg, canvasPart, canvasPart.size(), 0, 0, CV_INTER_AREA); if( useCalibrated ) { Rect vroi(cvRound(validRoi[k].x*sf), cvRound(validRoi[k].y*sf), cvRound(validRoi[k].width*sf), cvRound(validRoi[k].height*sf)); rectangle(canvasPart, vroi, Scalar(0,0,255), 3, 8); } } if( !isVerticalStereo ) for( j = 0; j < canvas.rows; j += 16 ) line(canvas, Point(0, j), Point(canvas.cols, j), Scalar(0, 255, 0), 1, 8); else for( j = 0; j < canvas.cols; j += 16 ) line(canvas, Point(j, 0), Point(j, canvas.rows), Scalar(0, 255, 0), 1, 8); imshow("rectified", canvas); char c = (char)waitKey(); if( c == 27 || c == 'q' || c == 'Q' ) break; } }
KDvoid CornerSubPix ( KDint nIdx ) { string sMsg; KDchar szStr [ 256 ]; Mat tSrc; Mat tDst; Mat tGray; KDint nThresh; RNG tRng ( 12345 ); nThresh = 205; // Load source image and convert it to gray tSrc = imread ( "/res/image/apple.png" ); cvtColor ( tSrc, tGray, CV_BGR2GRAY ); // // Apply Shi-Tomasi corner detector // // Parameters for Shi-Tomasi algorithm vector<Point2f> aCorners; KDdouble dQualityLevel = 0.01; KDdouble dMinDistance = 10; KDint nMaxCorners = 4; KDint nBlockSize = 3; bool bUseHarrisDetector = false; KDdouble dK = 0.04; // Copy the source image tDst = tSrc.clone ( ); // Apply corner detection goodFeaturesToTrack ( tGray, aCorners, nMaxCorners, dQualityLevel, dMinDistance, Mat ( ), nBlockSize, bUseHarrisDetector, dK ); // Draw corners detected kdSprintfKHR ( szStr, "** Number of corners detected: %d\n", aCorners.size ( ) ); sMsg = szStr; KDint nR = 4; for ( KDuint i = 0; i < aCorners.size ( ); i++ ) { circle ( tDst, aCorners [ i ], nR, Scalar ( tRng.uniform ( 0, 255 ), tRng.uniform ( 0, 255 ), tRng.uniform ( 0, 255 ) ), -1, 8, 0 ); } // Set the neeed parameters to find the refined corners Size tWinSize = Size ( 5, 5 ); Size tZeroZone = Size ( -1, -1 ); TermCriteria tCriteria = TermCriteria ( CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 40, 0.001 ); // Calculate the refined corner locations cornerSubPix ( tGray, aCorners, tWinSize, tZeroZone, tCriteria ); // Write them down for ( KDuint i = 0; i < aCorners.size ( ); i++ ) { kdSprintfKHR ( szStr, " -- Refined Corner [%d] ( %.3f, %.3f )\n", i, aCorners [ i ].x, aCorners [ i ].y ); sMsg += szStr; } g_pController->setFrame ( 1, tSrc ); g_pController->setFrame ( 2, tDst ); g_pController->setMessage ( sMsg.c_str ( ) ); }
/** * Find a list of candidate marker from a given scene * * @param current frame, in grayscale 8UC1 format * @return a list of marker candidates **/ vector<Marker> MarkerDetector::findMarkerCandidates( Mat& frame ) { vector<Marker> candidates; /* Do some thresholding, in fact you should tune the parameters here a bit */ Mat thresholded; threshold( frame, thresholded, 50.0, 255.0, CV_THRESH_BINARY ); /* Find contours */ vector<vector<Point>> contours; findContours( thresholded.clone(), contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE ); for( vector<Point> contour: contours ) { /* Approximate polygons out of these contours */ vector<Point> approxed; approxPolyDP( contour, approxed, contour.size() * 0.05, true ); /* Make sure it passes our first candidate check */ if( !checkPoints( approxed ) ) continue; /* Do some perspective transformation on the candidate marker to a predetermined square */ Marker marker; marker.matrix = Mat( markerHeight, markerWidth, CV_8UC1 ); std::copy( approxed.begin(), approxed.end(), back_inserter( marker.poly ) ); /* Apply sub pixel search */ cornerSubPix( thresholded, marker.poly, Size(5, 5), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 40, 0.001) ); /* Projection target */ const static vector<Point2f> target_corners = { Point2f( -0.5f, -0.5f ), Point2f( +5.5f, -0.5f ), Point2f( +5.5f, +5.5f ), Point2f( -0.5f, +5.5f ), }; /* Apply perspective transformation, to project our 3D marker to a predefined 2D coords */ Mat projection = getPerspectiveTransform( marker.poly, target_corners ); warpPerspective( thresholded, marker.matrix, projection, marker.matrix.size() ); /* Ignore those region that's fully black, or not surrounded by black bars */ if( sum(marker.matrix) == Scalar(0) || countNonZero( marker.matrix.row(0)) != 0 || countNonZero( marker.matrix.row(markerHeight - 1)) != 0 || countNonZero( marker.matrix.col(0)) != 0 || countNonZero( marker.matrix.col(markerWidth - 1)) != 0 ) { continue; } /* Find the rotation that has the smallest hex value */ pair<unsigned int, unsigned int> minimum = { numeric_limits<unsigned int>::max(), 0 }; vector<unsigned int> codes(markerHeight); unsigned int power = 1 << (markerWidth - 3); /* Rotate the marker 4 times, store the hex code upon each rotation */ for( int rotation = 0; rotation < 4; rotation++ ) { stringstream ss; codes[rotation] = 0; for( int i = 1; i < markerHeight - 1; i++ ) { unsigned int code = 0; for ( int j = 1; j < markerWidth - 1; j++ ){ int value = static_cast<int>(marker.matrix.at<uchar>(i, j)); if( value == 0 ) code = code + ( power >> j ); } ss << hex << code; } ss >> codes[rotation]; if( minimum.first > codes[rotation] ) { minimum.first = codes[rotation]; minimum.second = rotation; } flip( marker.matrix, marker.matrix, 1 ); marker.matrix = marker.matrix.t(); } rotate( marker.poly.begin(), marker.poly.begin() + ((minimum.second + 2) % 4), marker.poly.end() ); for( int i = 0; i < minimum.second; i++ ) { flip( marker.matrix, marker.matrix, 1 ); marker.matrix = marker.matrix.t(); } marker.code = minimum.first; candidates.push_back( marker ); } return candidates; }
AppTemplate::AppTemplate(const Mat* frame_set, const Rect iniWin,int ID) :ID(ID)//bgr,hsv,lab { //get roi out of frame set Rect body_win=scaleWin(iniWin,1/TRACKING_TO_BODYSIZE_RATIO); Rect roi_win(body_win.x-body_win.width,body_win.y-body_win.width,3*body_win.width,2*body_win.width+body_win.height); body_win= body_win&Rect(0,0,frame_set[0].cols,frame_set[0].rows); roi_win=roi_win&Rect(0,0,frame_set[0].cols,frame_set[0].rows); Mat roi_set[]={Mat(frame_set[0],roi_win),Mat(frame_set[1],roi_win),Mat(frame_set[2],roi_win)}; Rect iniWin_roi=iniWin-Point(roi_win.x,roi_win.y); //scores for each channel list<ChannelScore> channel_score; Mat mask_roi(roi_set[0].rows,roi_set[0].cols,CV_8UC1,Scalar(0)); rectangle(mask_roi,iniWin_roi,Scalar(255),-1); Mat inv_mask_roi(roi_set[0].rows,roi_set[0].cols,CV_8UC1,Scalar(255)); rectangle(inv_mask_roi,body_win-Point(roi_win.x,roi_win.y),Scalar(0),-1); //calculate score for each channel Mat temp_hist; Mat temp_bp; int hist_size[]={BIN_NUMBER}; for (int i=0;i<9;i++) { float range1[]={0,255}; if (i==3) { range1[1]=179; } const float* hist_range[]={range1}; calcHist(roi_set,3,&i,inv_mask_roi,temp_hist,1,hist_size,hist_range); normalize(temp_hist,temp_hist,255,0.0,NORM_L1);//scale to 255 for display calcBackProject(roi_set,3,&i,temp_hist,temp_bp,hist_range); int c[]={0}; int hs[]={BIN_NUMBER}; float hr[]={0,255}; const float* hrr[]={hr}; Mat hist_fore; Mat hist_back; calcHist(&temp_bp,1,c,mask_roi,hist_fore,1,hs,hrr); calcHist(&temp_bp,1,c,inv_mask_roi,hist_back,1,hs,hrr); normalize(hist_fore,hist_fore,1.0,0.0,NORM_L1); normalize(hist_back,hist_back,1.0,0.0,NORM_L1); //deal with gray image to get rid of #IND double score=getVR(hist_back,hist_fore); score=score==score ? score:0; channel_score.push_back(ChannelScore(i,score)); } //choose the 2 highest scored channels channel_score.sort(compareChannel); channels[0]=channel_score.back().idx; channel_score.pop_back(); channels[1]=channel_score.back().idx; //using 2 best channel to calculate histogram for (int i=0;i<2;++i) { _hRang[i][0]=0; if (channels[i]==3) _hRang[i][1]=179; else _hRang[i][1]=255; hRange[i]=_hRang[i]; } calcHist(roi_set,3,channels,inv_mask_roi,temp_hist,2,hSize,hRange); normalize(temp_hist,temp_hist,255,0,NORM_L1); Mat final_mask;//mask for sampling calcBackProject(roi_set,3,channels,temp_hist,final_mask,hRange); threshold(final_mask,final_mask,5,255,CV_THRESH_BINARY_INV); final_mask=min(final_mask,mask_roi); //choose the best two feature space for foreground**************** Mat hist_fore,hist_back; channel_score.clear(); double sum_score=0; for (int i=0;i<9;i++) { float range1[]={0,255}; if (i==3) { range1[1]=179; } const float* hist_range[]={range1}; Mat temp_hist_neg; calcHist(roi_set,3,&i,final_mask,temp_hist,1,hist_size,hist_range); normalize(temp_hist,temp_hist,255,0,NORM_L1); calcHist(roi_set,3,&i,inv_mask_roi,temp_hist_neg,1,hist_size,hist_range); normalize(temp_hist_neg,temp_hist_neg,255,0,NORM_L1); log(temp_hist,temp_hist); log(temp_hist_neg,temp_hist_neg); temp_hist=temp_hist-temp_hist_neg; threshold(temp_hist,temp_hist,0,255,CV_THRESH_TOZERO); normalize(temp_hist,temp_hist,255,0.0,NORM_L1);//scale to 255 for display calcBackProject(roi_set,3,&i,temp_hist,temp_bp,hist_range); int c[]={0}; int hs[]={BIN_NUMBER}; float hr[]={0,255}; const float* hrr[]={hr}; calcHist(&temp_bp,1,c,final_mask,hist_fore,1,hs,hrr); calcHist(&temp_bp,1,c,inv_mask_roi,hist_back,1,hs,hrr); normalize(hist_fore,hist_fore,1.0,0.0,NORM_L1); normalize(hist_back,hist_back,1.0,0.0,NORM_L1); double score=getVR(hist_back,hist_fore); score=score==score ? score:0; channel_score.push_back(ChannelScore(i,score)); sum_score+=exp(score); } channel_score.sort(compareChannel); channels[0]=channel_score.back().idx; channel_score.pop_back(); channels[1]=channel_score.back().idx; for (int i=0;i<2;++i) { _hRang[i][0]=0; if (channels[i]==3) _hRang[i][1]=179; else _hRang[i][1]=255; hRange[i]=_hRang[i]; } calcHist(roi_set,3,channels,final_mask,hist,2,hSize,hRange);/////////////////// normalize(hist,hist,255,0,NORM_L1); //recover the shift_vector Mat backPro; calcBackProject(roi_set,3,channels,hist,backPro,hRange); iniWin_roi=iniWin-Point(roi_win.x,roi_win.y); Point2f origin_point_roi((float)(iniWin_roi.x+0.5*iniWin_roi.width),(float)(iniWin_roi.y+0.5*iniWin_roi.height)); meanShift(backPro,iniWin_roi,TermCriteria( CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1 )); Point2f shift_point_roi((float)(iniWin_roi.x+0.5*iniWin_roi.width),(float)(iniWin_roi.y+0.5*iniWin_roi.height)); shift_vector=(shift_point_roi-origin_point_roi)*(1/(float)iniWin.width); }
Mat TableObjectDetector::clusterObjects(Mat P, int K, bool removeOutliers) { Mat L; int attempts = 5; P.convertTo(P, CV_32F); kmeans(P, K, L, TermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS, 10000, 0.0001), attempts, KMEANS_PP_CENTERS); // We remove outliers that are outside a number of standard deviations away // from the object centroid. We do this by just setting their cluster label // to -1 if (removeOutliers) { float numStdDevs = 3; float maxDist = 0.1; // Caluclate centroids vector<Mat> clusterData; Mat clusterCentroids(K, 3, CV_32F); for (int k=0; k<K; k++) { Mat D = Mat(0, 3, CV_64F); for (int i=0; i<L.rows; i++) { if (L.at<int>(i)==k) { D.push_back(P.row(i)); } } clusterData.push_back(D); reduce(D, clusterCentroids.row(k), 0, CV_REDUCE_AVG); } // Now calculate distances of each point, and the std. devs. of each // cluster Mat Dist = Mat::zeros(P.rows, 1, CV_32F); vector<Mat> centroidDistances; for (int k=0; k<K; k++) { centroidDistances.push_back(Mat(0, 1, CV_32F)); } for (int i=0; i<L.rows; i++) { Mat centroid = clusterCentroids.row(L.at<int>(i)); Mat pt = P.row(i); int k = L.at<int>(i); float d = std::sqrt( std::pow(pt.at<float>(0) - centroid.at<float>(0), 2) + std::pow(pt.at<float>(1) - centroid.at<float>(1), 2) + std::pow(pt.at<float>(2) - centroid.at<float>(2), 2) ); Dist.at<float>(i) = d; centroidDistances.at(k).push_back(d); } for (int k=0; k<K; k++) { Mat ignore; Mat std_dev; meanStdDev(centroidDistances.at(k), ignore, std_dev); float k_std = std_dev.at<Scalar>(0)(0); for (int i=0; i<P.rows; i++) { if (L.at<int>(i) == k) { //if (Dist.at<float>(i) > numStdDevs*k_std) { if (Dist.at<float>(i) > maxDist) { L.at<int>(i) = -1; } } } } // Now compute standard deviations for all clusters //Mat centroidStdDevs = Mat::zeros(K, 1); //for (int k=0; k<K; k++) { //} } return L; }
/** * Run hierarchical k-means with a distance threshold of 0.15 meters */ Mat TableObjectDetector::clusterObjectsHierarchical(cv::Mat P, int max_clusters) { Mat L = Mat::zeros (P.rows, 1, CV_32S); double dthresh = 0.15; int currentClusterNum = 0; int num_iter = 30; std::stack<Cluster*> c_stack; // Initialize with a single cluster containing all datapoints Cluster* C = new Cluster(); Mat D = Mat(P.rows, 4, CV_64F); P.copyTo(D.colRange(0, 3)); // ID each point so we return them in the same order for (int i=0; i<D.rows; i++) { D.at<double>(i, 3) = i; } C->setData(D); c_stack.push(C); // Run hierarchical k-means for (int t=0; t<num_iter; t++) { if (currentClusterNum == max_clusters) { return L; } if (c_stack.empty()) { return L; } Cluster* C = (Cluster*)c_stack.top(); c_stack.pop(); Mat D = C->getData(); // Calculate cluster centroid Mat Cmean; reduce(D, Cmean, 0, CV_REDUCE_AVG); // Calculate max distance double maxDist = 0; for (int i=0; i<D.rows; i++) { double dx = D.at<double>(i, 0) - Cmean.at<double>(0); double dy = D.at<double>(i, 1) - Cmean.at<double>(1); double dz = D.at<double>(i, 2) - Cmean.at<double>(2); double dist = sqrt(dx*dx + dy*dy + dz*dz); if (dist > maxDist) { maxDist = dist; } } // Check to see if this cluster satisfies the conditions if (maxDist < dthresh) { for (int i=0; i<D.rows; i++) { int idx = (int)D.at<double>(i, 3); L.at<int>(idx) = currentClusterNum; } currentClusterNum++; } else { Mat L_iter; int attempts = 5; Mat D32 = Mat(D.rows, 3, CV_64F); D.colRange(0, 3).copyTo(D32); D32.convertTo(D32, CV_32F); kmeans(D32, 2, L_iter, TermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS, 10000, 0.0001), attempts, KMEANS_PP_CENTERS); Mat D0 = Mat(0, 3, CV_64F); Mat D1 = Mat(0, 3, CV_64F); for (int i=0; i<L_iter.rows; i++) { if (L_iter.at<int>(i) == 0) { D0.push_back(D.row(i)); } else { D1.push_back(D.row(i)); } } Cluster* C0 = new Cluster(); C0->setData(D0); Cluster* C1 = new Cluster(); C1->setData(D1); c_stack.push(C0); c_stack.push(C1); } } return L; }
* \f$R_i, T_i\f$ are concatenated 1x3 vectors. * @param perViewErrors Output vector of average re-projection errors estimated for each pattern view. * @param flags flags Different flags for the calibration process (see #calibrateCamera for details). * @param criteria Termination criteria for the iterative optimization algorithm. * * This function calibrates a camera using a set of corners of a Charuco Board. The function * receives a list of detected corners and its identifiers from several views of the Board. * The function returns the final re-projection error. */ CV_EXPORTS_AS(calibrateCameraCharucoExtended) double calibrateCameraCharuco( InputArrayOfArrays charucoCorners, InputArrayOfArrays charucoIds, const Ptr<CharucoBoard> &board, Size imageSize, InputOutputArray cameraMatrix, InputOutputArray distCoeffs, OutputArrayOfArrays rvecs, OutputArrayOfArrays tvecs, OutputArray stdDeviationsIntrinsics, OutputArray stdDeviationsExtrinsics, OutputArray perViewErrors, int flags = 0, TermCriteria criteria = TermCriteria(TermCriteria::COUNT + TermCriteria::EPS, 30, DBL_EPSILON)); /** @brief It's the same function as #calibrateCameraCharuco but without calibration error estimation. */ CV_EXPORTS_W double calibrateCameraCharuco( InputArrayOfArrays charucoCorners, InputArrayOfArrays charucoIds, const Ptr<CharucoBoard> &board, Size imageSize, InputOutputArray cameraMatrix, InputOutputArray distCoeffs, OutputArrayOfArrays rvecs = noArray(), OutputArrayOfArrays tvecs = noArray(), int flags = 0, TermCriteria criteria = TermCriteria(TermCriteria::COUNT + TermCriteria::EPS, 30, DBL_EPSILON)); /** * @brief Detect ChArUco Diamond markers * * @param image input image necessary for corner subpixel.
/////////////////////////////////////////////////////// // Panel::CalibrateCamera() Description /////////////////////////////////////////////////////// void Panel::CalibrateCamera(string sFilePath) { help(); //! [file_read] Settings s; const string inputSettingsFile = sFilePath; FileStorage fs(inputSettingsFile, FileStorage::READ); // Read the settings if (!fs.isOpened()) { cout << "Could not open the configuration file: \"" << inputSettingsFile << "\"" << endl; // return -1; } fs["Settings"] >> s; fs.release(); // close Settings file //! [file_read] //FileStorage fout("settings.yml", FileStorage::WRITE); // write config as YAML //fout << "Settings" << s; if (!s.goodInput) { cout << "Invalid input detected. Application stopping. " << endl; // return -1; } vector<vector<Point2f> > imagePoints; Mat cameraMatrix, distCoeffs; Size imageSize; int mode = s.inputType == Settings::IMAGE_LIST ? CAPTURING : DETECTION; clock_t prevTimestamp = 0; const Scalar RED(0, 0, 255), GREEN(0, 255, 0); const char ESC_KEY = 27; int counter = 1; //! [get_input] for (;;) { Mat view; bool blinkOutput = false; view = s.nextImage(); //----- If no more image, or got enough, then stop calibration and show result ------------- if (mode == CAPTURING && imagePoints.size() >= (size_t)s.nrFrames) { if (runCalibrationAndSave(s, imageSize, cameraMatrix, distCoeffs, imagePoints)) mode = CALIBRATED; else mode = DETECTION; } if (view.empty()) // If there are no more images stop the loop { // if calibration threshold was not reached yet, calibrate now if (mode != CALIBRATED && !imagePoints.empty()) runCalibrationAndSave(s, imageSize, cameraMatrix, distCoeffs, imagePoints); break; } //! [get_input] imageSize = view.size(); // Format input image. if (s.flipVertical) flip(view, view, 0); //! [find_pattern] vector<Point2f> pointBuf; bool found; switch (s.calibrationPattern) // Find feature points on the input format { case Settings::CHESSBOARD: found = findChessboardCorners(view, s.boardSize, pointBuf, CALIB_CB_ADAPTIVE_THRESH | CALIB_CB_FAST_CHECK | CALIB_CB_NORMALIZE_IMAGE); break; case Settings::CIRCLES_GRID: found = findCirclesGrid(view, s.boardSize, pointBuf); break; case Settings::ASYMMETRIC_CIRCLES_GRID: found = findCirclesGrid(view, s.boardSize, pointBuf, CALIB_CB_ASYMMETRIC_GRID); break; default: found = false; break; } //! [find_pattern] //! [pattern_found] if (found) // If done with success, { // improve the found corners' coordinate accuracy for chessboard if (s.calibrationPattern == Settings::CHESSBOARD) { Mat viewGray; cvtColor(view, viewGray, COLOR_BGR2GRAY); cornerSubPix(viewGray, pointBuf, Size(11, 11), Size(-1, -1), TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 30, 0.1)); } if (mode == CAPTURING && // For camera only take new samples after delay time (!s.inputCapture.isOpened() || clock() - prevTimestamp > s.delay*1e-3*CLOCKS_PER_SEC)) { imagePoints.push_back(pointBuf); prevTimestamp = clock(); blinkOutput = s.inputCapture.isOpened(); } // Draw the corners. drawChessboardCorners(view, s.boardSize, Mat(pointBuf), found); } //! [pattern_found] //----------------------------- Output Text ------------------------------------------------ //! [output_text] string msg = (mode == CAPTURING) ? "100/100" : mode == CALIBRATED ? "Calibrated" : "Press 'g' to start"; int baseLine = 0; Size textSize = getTextSize(msg, 1, 1, 1, &baseLine); Point textOrigin(view.cols - 2 * textSize.width - 10, view.rows - 2 * baseLine - 10); if (mode == CAPTURING) { if (s.showUndistorsed) msg = format("%d/%d Undist", (int)imagePoints.size(), s.nrFrames); else msg = format("%d/%d", (int)imagePoints.size(), s.nrFrames); } putText(view, msg, textOrigin, 1, 1, mode == CALIBRATED ? GREEN : RED); if (blinkOutput) bitwise_not(view, view); //! [output_text] //------------------------- Video capture output undistorted ------------------------------ //! [output_undistorted] if (mode == CALIBRATED && s.showUndistorsed) { Mat temp = view.clone(); undistort(temp, view, cameraMatrix, distCoeffs); } //! [output_undistorted] //------------------------------ Show image and check for input commands ------------------- //! [await_input] namedWindow("Image View" + to_string(counter), WINDOW_NORMAL); resizeWindow("Image View" + to_string(counter), 640, 480); imshow("Image View" + to_string(counter), view); char key = (char)waitKey(s.inputCapture.isOpened() ? 50 : s.delay); cout << "Image " << to_string(counter) << " Completed" << endl; counter++; if (key == ESC_KEY) break; if (key == 'u' && mode == CALIBRATED) s.showUndistorsed = !s.showUndistorsed; if (s.inputCapture.isOpened() && key == 'g') { mode = CAPTURING; imagePoints.clear(); } //! [await_input] } // -----------------------Show the undistorted image for the image list ------------------------ //! [show_results] if (s.inputType == Settings::IMAGE_LIST && s.showUndistorsed) { Mat view, rview, map1, map2; initUndistortRectifyMap(cameraMatrix, distCoeffs, Mat(), getOptimalNewCameraMatrix(cameraMatrix, distCoeffs, imageSize, 1, imageSize, 0), imageSize, CV_16SC2, map1, map2); m_mainMap1 = map1; m_mainMap2 = map2; for (size_t i = 0; i < s.imageList.size(); i++) { view = imread(s.imageList[i], 1); if (view.empty()) continue; remap(view, rview, map1, map2, INTER_LINEAR); imshow("Image View", rview); char c = (char)waitKey(); if (c == ESC_KEY || c == 'q' || c == 'Q') break; } } //! [show_results] // return 0; }
EMImpl() { nclusters = DEFAULT_NCLUSTERS; covMatType=EM::COV_MAT_DIAGONAL; termCrit = TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, EM::DEFAULT_MAX_ITERS, 1e-6); }
int main(int argc, char** argv) { ofstream f1; f1.open("result.txt"); size_t i,j; Point2f cp; cv::initModule_nonfree(); vector<Point2f> MP1,MP2; vector<int> trainIdxs, queryIdxs; //Read Video File VideoCapture cap("video1.avi"); if( !cap.isOpened() ) { cout << "Could not initialize capturing...\n"; return 0;} VideoWriter writer("ms_tracking.avi",CV_FOURCC('D','I','V','3'), 10,cvSize(640,480),1); cv::SURF mySURF; mySURF.extended = 0; Ptr<DescriptorMatcher> descriptorMatcher = DescriptorMatcher::create( "FlannBased" ); int mactherFilterType = getMatcherFilterType( "CrossCheckFilter" ); Mat frame,img1,img2; cap >> frame; if( frame.empty() ) return -1; img1 = frame.clone() ; Mat temp,temp1; if(img1.empty()) cout << "Exiting as the input image is empty" << endl; const char* name = "Initiate_ROI"; box = cvRect(-1,-1,0,0); cvNamedWindow( name,1); cvSetMouseCallback( name, my_mouse_callback2); // Main loop while( 1 ) { img1.copyTo(temp); if( drawing_poly) { for ( i=0; i < polyPoints.size(); i++) circle(temp, polyPoints[i], 2, Scalar(0,255,0), -1,8); } cv::imshow(name,temp) ; char c = (char)waitKey(10); if( c == '\x1b' ) // esc break; if(poly_drawn) break; } //Read the polygon points from a text file FILE *f11; polyPoints.clear(); IpolyPoints.clear(); f11 = fopen("points.txt","r"); Point a; for(int j=0;j<37;j++) { fscanf(f11,"%d",&(a.x)); fscanf(f11,"%d",&(a.y)); polyPoints.push_back(a); IpolyPoints.push_back(a); } fclose(f11); // Drawing Polygon Point pointArr[polyPoints.size()]; for (i=0; i< polyPoints.size(); i++) pointArr[i] = polyPoints[i]; const Point* pointsArray[1] = {pointArr}; int nCurvePts[1] = { polyPoints.size() }; polylines(temp, pointsArray, nCurvePts, 1, 1, Scalar(0,255,0), 1); cout << polyPoints.size() << endl; box= boundingRect(polyPoints); //boxOrg = Rect(box.x-15, box.y-15, box.width+30, box.height+30); boxOuter = Rect(box.x-30, box.y-30, box.width+60, box.height+60); //box =boxOrg; // storing the initial selected Box, as "box" variable changes in consecutive matching boxP=box; Mat img1ROI, labels1, clusters1, descriptors,roidescriptors, descriptors1,bdescriptors, bmdescriptors; vector<int> reprojections; // number of reprojections per KP, size same as KP(incresing) vector<Point2f> points,points1,points2, Mpoints1,Mpoints2,bpoints,npoints1,npoints2; //bmpoints,tpoints; vector<KeyPoint> roikeypoints, bkeypoints,keypoints,keypoints1, keypoints2; draw_box(temp, box ); //Show InnerBox - This is used by the Mean-Shift Tracker draw_box(temp,boxOuter); //Show OuterBox - This is used for removing background points bpoints.clear(); //calculating keypoints and descriptors of the selected polygon in image roi //==============================================================================================// for(i=0;i<polyPoints.size();i++) { // cout << polyPoints[i] << endl; // polyPoints[i].x = polyPoints[i].x -boxOuter.x; polyPoints[i].y = polyPoints[i].y- boxOuter.y; } img1ROI = img1(boxOuter); points1.clear(); mySURF.detect(img1ROI, roikeypoints); KeyPoint::convert(roikeypoints, points); mySURF.compute(img1ROI, roikeypoints, roidescriptors); bdescriptors.release();bkeypoints.clear(); bcategorizePoints( points, bpoints,polyPoints, roikeypoints, roidescriptors, bkeypoints, bdescriptors); shiftPoints(bpoints,boxOuter); for(i=0;i<bpoints.size();i++) circle(temp, bpoints[i], 2, Scalar(0,255,0),2); vector<KeyPoint> tpkeypoints; Mat tpdescriptors; categorizePoints( points, points1,polyPoints, roikeypoints, roidescriptors, tpkeypoints, tpdescriptors); shiftPoints(points1, boxOuter); for(i=0;i<points1.size();i++) circle(temp, points1[i], 2, Scalar(0,0,255),2); //====================================================================================================// points1.clear(); Mat img2ROI; // tpkeypoints = keypoints1; tpdescriptors = descriptors1; cv::imshow(name,temp) ; imwrite("a.jpg",temp); cout << "BD_SIZE \t" << bdescriptors.rows << "\t" << "FD_SIZE \t" << tpdescriptors.rows << endl; // Mat newimg = img1ROI.clone(); // KeyPoint::convert(tpkeypoints, points1); // for(size_t i=0;i<points1.size();i++) // circle(newimg, points1[i], 2, Scalar(255,0,255),2); // imshow( "newimg", newimg ); // points1.clear(); waitKey(0); cvDestroyWindow( name ); int FG_mp, FG, BG_mp, BG, FG_BG, msI ; //Foreground matching points struct timeval t1, t2; for(int l=0;;l++) { gettimeofday(&t1, NULL); cv::kmeans(tpdescriptors, NOC, labels1, TermCriteria( CV_TERMCRIT_ITER + CV_TERMCRIT_EPS, 50, 1.0 ), 1, KMEANS_RANDOM_CENTERS, clusters1); cap >> frame; img2 = frame.clone() ; temp1 =frame.clone() ; if(img2.empty() ) { cout<< "Could not open image: " << endl ; break;} int flag=1; Mpoints1.clear(); Mat descriptors2; msI=0; meanShift(img1, img2, descriptorMatcher, mactherFilterType, tpkeypoints, tpdescriptors,keypoints2,descriptors2, clusters1, cp, flag, MP1,img2ROI,bkeypoints, bdescriptors, temp1,FG_mp, FG, BG_mp, BG, FG_BG,msI); //==========scaling================= float scale=1; // cout <<"MP1size \t" << MP1.size() <<endl; if(APPLY_SCALING) { vector<DMatch> filteredMatches; if(descriptors1.rows > 4 && descriptors2.rows > 4) { crossCheckMatching( descriptorMatcher, descriptors1, descriptors2, filteredMatches, 1 ); trainIdxs.clear(); queryIdxs.clear(); for( i = 0; i < filteredMatches.size(); i++ ) { queryIdxs.push_back(filteredMatches[i].queryIdx); trainIdxs.push_back(filteredMatches[i].trainIdx); } points1.clear(); points2.clear(); KeyPoint::convert(keypoints1, points1, queryIdxs); KeyPoint::convert(keypoints2, points2, trainIdxs); // cout << "point2size" << points2.size() << endl; //homography npoints1.clear();npoints2.clear(); Mpoints1.clear();Mpoints2.clear(); Mat H12, points1t; double ransacReprojThreshold = 10; if( ransacReprojThreshold >= 0 && points1.size() > 4) H12 = findHomography( Mat(points1), Mat(points2), CV_RANSAC, ransacReprojThreshold ); vector<char> matchesMask( filteredMatches.size(), 0 );// NONmatchesMask( filteredMatches.size(), 0 ); if( !H12.empty() ) { perspectiveTransform(Mat(points1), points1t, H12); double maxInlierDist = 10;//ransacReprojThreshold < 0 ? 3 : ransacReprojThreshold; for(i = 0; i < points1.size(); i++ ) { if( norm(points2[i] - points1t.at<Point2f>((int)i,0)) <= 5)// maxInlierDist ) // inlier { matchesMask[i] = 1; npoints2.push_back(points2[i]); npoints1.push_back(points1[i]); } } for(i=0; i<npoints2.size();i++) { for(j=0;j<MP1.size();j++) { double dist = norm(npoints2[i]-MP1[j]); // cout <<"dist \t" <<dist << endl; // waitKey(0); if(dist < 0.1) { Mpoints2.push_back(npoints2[i]); Mpoints1.push_back(npoints1[i]); break; } } } } Mat drawImg; drawMatches( img1ROI, keypoints1, img2ROI, keypoints2, filteredMatches, drawImg, CV_RGB(0, 255, 0), CV_RGB(0, 0, 255), matchesMask #if DRAW_RICH_KEYPOINTS_MODE , DrawMatchesFlags::DRAW_RICH_KEYPOINTS #endif ); imshow( "correspondance", drawImg ); cout << "npoints1.size \t" << Mpoints1.size() << "\t" << Mpoints2.size() << endl; if(Mpoints1.size() > 8) weightScalingAspect(Mpoints1,Mpoints2,&scale); } } img1=img2; img1ROI = img2ROI; boxOrg =box; keypoints1 = keypoints2; descriptors1 =descriptors2; box.x += box.width/2; box.y += box.height/2; box.height = round(boxOrg.height *scale); box.width = round(( float(boxOrg.width)/float(boxOrg.height) ) * box.height); box.x -= box.width/2; box.y -= box.height/2; boundaryCheckRect(box); cout <<"SCALE \t" << scale << endl; gettimeofday(&t2, NULL); double diff = (float)((t2.tv_sec * 1000000 + t2.tv_usec) - (t1.tv_sec * 1000000 + t1.tv_usec)); diff = diff/1000; cout <<"Time taken in mili sec \t" << diff<< endl; // cout << tpdescriptors.rows << endl; //cout <<"BD \t" << bdescriptors.rows << endl; f1 << l << "\t" << FG_mp << "\t" << BG_mp << "\t" << FG << "\t"<< msI << "\n"; cout << "l \t" << l << "\t" <<" msI \t"<< msI << endl; imshow("img2",temp1); writer << temp1; waitKey(0); // boxOrg = eBox; char c = (char)waitKey(10); if( c == '\x1b' ) // esc { cout << "Exiting ..." << endl; break; } } trajectory.close(); return 0; }
//--------------------------------------------------------- Ptr<StatModel> COpenCV_ML_ANN::Get_Model(void) { Ptr<ANN_MLP> Model = ANN_MLP::create(); //----------------------------------------------------- Mat layer_sizes(1, 2 + Parameters("ANN_LAYERS")->asInt(), CV_32SC1); layer_sizes.at<int>(0) = Get_Feature_Count(); // The first layer needs the same size (number of neurons) as the number of columns in the training data for(int i=1; i<layer_sizes.cols-1; i++) { layer_sizes.at<int>(i) = Parameters("ANN_NEURONS")->asInt(); } layer_sizes.at<int>(layer_sizes.cols-1) = Get_Class_Count(); // The last layer needs the same size (number of neurons) as the number of output columns Model->setLayerSizes(layer_sizes); //----------------------------------------------------- switch( Parameters("ANN_ACTIVATION")->asInt() ) { case 0: // Identity Model->setActivationFunction(ANN_MLP::IDENTITY); break; default: // Sigmoid Model->setActivationFunction(ANN_MLP::SIGMOID_SYM, Parameters("ANN_ACT_ALPHA")->asDouble(), Parameters("ANN_ACT_BETA" )->asDouble() ); break; case 2: // Gaussian Model->setActivationFunction(ANN_MLP::GAUSSIAN, Parameters("ANN_ACT_ALPHA")->asDouble(), Parameters("ANN_ACT_BETA" )->asDouble() ); break; } //----------------------------------------------------- Model->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, Parameters("ANN_MAXITER")->asInt (), Parameters("ANN_EPSILON")->asDouble() )); //----------------------------------------------------- switch( Parameters("ANN_PROPAGATION")->asInt() ) { case 0: // resilient propagation Model->setTrainMethod(ANN_MLP::RPROP); Model->setRpropDW0 (Parameters("ANN_RP_DW0" )->asDouble()); Model->setRpropDWPlus (Parameters("ANN_RP_DW_PLUS" )->asDouble()); Model->setRpropDWMinus (Parameters("ANN_RP_DW_MINUS")->asDouble()); Model->setRpropDWMin (Parameters("ANN_RP_DW_MIN" )->asDouble()); Model->setRpropDWMax (Parameters("ANN_RP_DW_MAX" )->asDouble()); break; default: Model->setTrainMethod(ANN_MLP::BACKPROP); Model->setBackpropMomentumScale(Parameters("ANN_BP_MOMENT" )->asDouble()); Model->setBackpropWeightScale (Parameters("ANN_BP_DW" )->asDouble()); break; } //----------------------------------------------------- return( Model ); }
TEST_P(ML_ANN_METHOD, Test) { int methodType = get<0>(GetParam()); string methodName = get<1>(GetParam()); int N = get<2>(GetParam()); String folder = string(cvtest::TS::ptr()->get_data_path()); String original_path = folder + "waveform.data"; String dataname = folder + "waveform" + '_' + methodName; Ptr<TrainData> tdata2 = TrainData::loadFromCSV(original_path, 0); Mat samples = tdata2->getSamples()(Range(0, N), Range::all()); Mat responses(N, 3, CV_32FC1, Scalar(0)); for (int i = 0; i < N; i++) responses.at<float>(i, static_cast<int>(tdata2->getResponses().at<float>(i, 0))) = 1; Ptr<TrainData> tdata = TrainData::create(samples, ml::ROW_SAMPLE, responses); ASSERT_FALSE(tdata.empty()) << "Could not find test data file : " << original_path; RNG& rng = theRNG(); rng.state = 0; tdata->setTrainTestSplitRatio(0.8); Mat testSamples = tdata->getTestSamples(); #ifdef GENERATE_TESTDATA { Ptr<ml::ANN_MLP> xx = ml::ANN_MLP_ANNEAL::create(); Mat_<int> layerSizesXX(1, 4); layerSizesXX(0, 0) = tdata->getNVars(); layerSizesXX(0, 1) = 30; layerSizesXX(0, 2) = 30; layerSizesXX(0, 3) = tdata->getResponses().cols; xx->setLayerSizes(layerSizesXX); xx->setActivationFunction(ml::ANN_MLP::SIGMOID_SYM); xx->setTrainMethod(ml::ANN_MLP::RPROP); xx->setTermCriteria(TermCriteria(TermCriteria::COUNT, 1, 0.01)); xx->train(tdata, ml::ANN_MLP::NO_OUTPUT_SCALE + ml::ANN_MLP::NO_INPUT_SCALE); FileStorage fs; fs.open(dataname + "_init_weight.yml.gz", FileStorage::WRITE + FileStorage::BASE64); xx->write(fs); fs.release(); } #endif { FileStorage fs; fs.open(dataname + "_init_weight.yml.gz", FileStorage::READ); Ptr<ml::ANN_MLP> x = ml::ANN_MLP_ANNEAL::create(); x->read(fs.root()); x->setTrainMethod(methodType); if (methodType == ml::ANN_MLP::ANNEAL) { x->setAnnealEnergyRNG(RNG(CV_BIG_INT(0xffffffff))); x->setAnnealInitialT(12); x->setAnnealFinalT(0.15); x->setAnnealCoolingRatio(0.96); x->setAnnealItePerStep(11); } x->setTermCriteria(TermCriteria(TermCriteria::COUNT, 100, 0.01)); x->train(tdata, ml::ANN_MLP::NO_OUTPUT_SCALE + ml::ANN_MLP::NO_INPUT_SCALE + ml::ANN_MLP::UPDATE_WEIGHTS); ASSERT_TRUE(x->isTrained()) << "Could not train networks with " << methodName; string filename = dataname + ".yml.gz"; Mat r_gold; #ifdef GENERATE_TESTDATA x->save(filename); x->predict(testSamples, r_gold); { FileStorage fs_response(dataname + "_response.yml.gz", FileStorage::WRITE + FileStorage::BASE64); fs_response << "response" << r_gold; } #else { FileStorage fs_response(dataname + "_response.yml.gz", FileStorage::READ); fs_response["response"] >> r_gold; } #endif ASSERT_FALSE(r_gold.empty()); Ptr<ml::ANN_MLP> y = Algorithm::load<ANN_MLP>(filename); ASSERT_TRUE(y != NULL) << "Could not load " << filename; Mat rx, ry; for (int j = 0; j < 4; j++) { rx = x->getWeights(j); ry = y->getWeights(j); double n = cvtest::norm(rx, ry, NORM_INF); EXPECT_LT(n, FLT_EPSILON) << "Weights are not equal for layer: " << j; } x->predict(testSamples, rx); y->predict(testSamples, ry); double n = cvtest::norm(ry, rx, NORM_INF); EXPECT_LT(n, FLT_EPSILON) << "Predict are not equal to result of the saved model"; n = cvtest::norm(r_gold, rx, NORM_INF); EXPECT_LT(n, FLT_EPSILON) << "Predict are not equal to 'gold' response"; } }
Calib::Calib() { Size boardSize(6,5); // Chessboard' size in corners with both color (nb of squares -1) int widthSquare = 40; // Width of a square in mm int heightSquare = 27; vector <Mat> images; // Getting the four images of the chessboard string imageFileName = "../src/mire1.jpg"; images.push_back(imread(imageFileName, 1)); imageFileName = "../src/mire2.jpg"; images.push_back(imread(imageFileName, 1)); imageFileName = "../src/mire3.jpg"; images.push_back(imread(imageFileName, 1)); imageFileName = "../src/mire4.jpg"; images.push_back(imread(imageFileName, 1)); Size imageSize = images.at(0).size(); // Find chessboard's corners in the scene for the 4 images vector<vector<Point2f> > cornersScene(1); vector<Mat> imagesGray; imagesGray.resize(4); for (int i=0; i<4; i++) { if(images.at(i).empty()) { cerr << "Image not read correctly!" << endl; exit(-1); } bool patternFound = findChessboardCorners(images.at(i), boardSize, cornersScene[0]); if(!patternFound) { cerr << "Could not find chess board!" << endl; exit(-1); } // Improve corner's coordinate accuracy cvtColor(images.at(i), imagesGray.at(i), CV_RGB2GRAY); cornerSubPix(imagesGray.at(i), cornersScene[0], Size(3,2), Size(-1,-1), TermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 30, 0.1)); // Drawing the corners drawChessboardCorners(images.at(i), boardSize, Mat(cornersScene[0]), patternFound ); imshow("Corners find", images.at(i)); int keyPressed; /*do { keyPressed = waitKey(0); } while (keyPressed != 27);*/ } // Getting the chessboard's corners on the mire's image vector<vector<Point3f> > cornersMire(1); for( int y = 0; y < boardSize.height; y++ ) { for( int x = 0; x < boardSize.width; x++ ) { cornersMire[0].push_back(cv::Point3f(float(x*widthSquare), float(y*heightSquare), 0)); } } // Getting the camera's parameters Mat distortionCoefficients = Mat::zeros(8, 1, CV_64F); Mat cameraMatrix = Mat::eye(3, 3, CV_64F); calibrateCamera(cornersMire, cornersScene, imageSize, cameraMatrix, distortionCoefficients, rotationVectors, translationVectors); //cout << "Camera matrix: " << cameraMatrix << endl; //cout << "Distortion _coefficients: " << distortionCoefficients << endl; cout << rotationVectors.at(0) << endl; cout << translationVectors.at(0) << endl; }
OpticalFlow::OpticalFlow() { data.termcrit = TermCriteria(TermCriteria::COUNT | TermCriteria::EPS, 40, 0.03); data.subPixWinSize = Size(10, 10); data.winSize = Size(7, 7); data.blocked = false; }
TrackerSamplerPF::TrackerSamplerPF(const Mat& chosenRect,const TrackerSamplerPF::Params ¶meters): params( parameters ),_function(new TrackingFunctionPF(chosenRect)){ className="PF"; _solver=createPFSolver(_function,parameters.std,TermCriteria(TermCriteria::MAX_ITER,parameters.iterationNum,0.0), parameters.particlesNum,parameters.alpha); }
Mat temp2; Mat temp3; Mat temp4; Mat temp5; }; enum { OPTFLOW_USE_INITIAL_FLOW=4, OPTFLOW_FARNEBACK_GAUSSIAN=256 }; //! computes sparse optical flow using multi-scale Lucas-Kanade algorithm CV_EXPORTS_W void calcOpticalFlowPyrLK( InputArray prevImg, InputArray nextImg, InputArray prevPts, CV_OUT InputOutputArray nextPts, OutputArray status, OutputArray err, Size winSize=Size(15,15), int maxLevel=3, TermCriteria criteria=TermCriteria( TermCriteria::COUNT+TermCriteria::EPS, 30, 0.01), double derivLambda=0.5, int flags=0 ); //! computes dense optical flow using Farneback algorithm CV_EXPORTS_W void calcOpticalFlowFarneback( InputArray prev, InputArray next, CV_OUT InputOutputArray flow, double pyr_scale, int levels, int winsize, int iterations, int poly_n, double poly_sigma, int flags ); //! estimates the best-fit Euqcidean, similarity, affine or perspective transformation // that maps one 2D point set to another or one image to another. CV_EXPORTS_W Mat estimateRigidTransform( InputArray src, InputArray dst, bool fullAffine); }
/////////////////////////////////////////////////////// // Panel::PixelsToLength() // Description: Not currently used: Calculates the // ratio of cm to pixels from one checkerboard image /////////////////////////////////////////////////////// void Panel::PixelsToLength(string sImgPath) { cout << "Pixels to Length" << endl << endl; m_pPanel->m_Image = imread(sImgPath); Point2f corner1; Point2f corner2; Point2f corner3; Point2f corner4; bool found = false; found = findChessboardCorners(m_pPanel->m_Image, Size(9, 6), m_pPanel->corners, CALIB_CB_ADAPTIVE_THRESH | CALIB_CB_FAST_CHECK | CALIB_CB_NORMALIZE_IMAGE); Mat imgGray; cvtColor(m_pPanel->m_Image, imgGray, COLOR_BGR2GRAY); cornerSubPix(imgGray, m_pPanel->corners, Size(11, 11), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 30, 0.1)); corner1.x = m_pPanel->corners[45].x; corner1.y = m_pPanel->corners[45].y; corner2.x = m_pPanel->corners[0].x; corner2.y = m_pPanel->corners[0].y; corner3.x = m_pPanel->corners[8].x; corner3.y = m_pPanel->corners[8].y; corner4.x = m_pPanel->corners[53].x; corner4.y = m_pPanel->corners[53].y; // Draw rectangle around checkerboard line(m_pPanel->m_Image, corner1, corner2, CV_RGB(0, 0, 255), 2); line(m_pPanel->m_Image, corner2, corner3, CV_RGB(0, 0, 255), 2); line(m_pPanel->m_Image, corner3, corner4, CV_RGB(0, 0, 255), 2); line(m_pPanel->m_Image, corner4, corner1, CV_RGB(0, 0, 255), 2); circle(m_pPanel->m_Image, corner1, 10, CV_RGB(0, 0, 255), 2); circle(m_pPanel->m_Image, corner2, 10, CV_RGB(0, 255, 0), 2); circle(m_pPanel->m_Image, corner3, 10, CV_RGB(255, 0, 0), 2); circle(m_pPanel->m_Image, corner4, 10, CV_RGB(100, 100, 100), 2); double pixel_length = 0; double pixel_width = 0; double pixel_width1 = norm(corner1 - corner2); double pixel_length1 = norm(corner2 - corner3); double pixel_width2 = norm(corner3 - corner4); double pixel_length2 = norm(corner4 - corner1); if (pixel_length1 >= pixel_length2) pixel_length = pixel_length1; else pixel_length = pixel_length2; if (pixel_width1 >= pixel_width2) pixel_width = pixel_width1; else pixel_width = pixel_width2; double ratio = (m_pPanel->m_boardLength - 1.0) / (m_pPanel->m_boardWidth - 1.0); if (pixel_length >= (pixel_width * ratio)){ m_pPanel->m_cmPerPixel = (m_pPanel->m_squareSize * (float)(m_pPanel->m_boardLength - 1)) / pixel_length; } else m_pPanel->m_cmPerPixel = (m_pPanel->m_squareSize * (float)(m_pPanel->m_boardWidth - 1)) / pixel_width; cout << "cm per pixel : " << m_pPanel->m_cmPerPixel << endl; // Perspective Transform // double ratio = 8.0 / 5.0; // double length = ratio * pixel_width; // // vector<Point2f> panel_pts; // vector<Point2f> rect_pts; // panel_pts.push_back(corner1); // panel_pts.push_back(corner2); // panel_pts.push_back(corner3); // panel_pts.push_back(corner4); // rect_pts.push_back(Point2f(0, 0)); // rect_pts.push_back(Point2f((float)width, 0)); // rect_pts.push_back(Point2f((float)width, (float)length)); // rect_pts.push_back(Point2f(0, (float)length)); // // // Draw new rectangle // line(m_pPanel->m_Image, rect_pts[0], rect_pts[1], CV_RGB(255, 0, 0), 2); // line(m_pPanel->m_Image, rect_pts[1], rect_pts[2], CV_RGB(255, 0, 0), 2); // line(m_pPanel->m_Image, rect_pts[2], rect_pts[3], CV_RGB(255, 0, 0), 2); // line(m_pPanel->m_Image, rect_pts[3], rect_pts[0], CV_RGB(255, 0, 0), 2); // // // Perspective Transorm // Mat transmtx = getPerspectiveTransform(panel_pts, rect_pts); // int offsetSize = 500; // Mat transformed = Mat::zeros(m_pPanel->m_Image.cols + offsetSize, m_pPanel->m_Image.rows + offsetSize, CV_8UC3); // warpPerspective(m_pPanel->m_Image, transformed, transmtx, transformed.size()); // //// Mat subImg(transformed, Rect(corner1.x, corner1.y, width, length)); // // namedWindow("Original", WINDOW_NORMAL); // imshow("Original", m_pPanel->m_Image); // namedWindow("Warped", WINDOW_AUTOSIZE); // imshow("Warped", transformed); }