bool ObjPatchMatcher::MatchViewPatch(const Mat& cimg, const Mat& dmap_raw) { // compute feature map Mat gray_img, gray_img_float; cvtColor(cimg, gray_img, CV_BGR2GRAY); gray_img.convertTo(gray_img_float, CV_32F); waitKey(10); // gradient Mat grad_x, grad_y, grad_mag; Sobel(gray_img_float, grad_x, CV_32F, 1, 0); Sobel(gray_img_float, grad_y, CV_32F, 0, 1); magnitude(grad_x, grad_y, grad_mag); // depth // do search Mat score_map = Mat::zeros(cimg.rows, cimg.cols, CV_32F); Mat feat; int topK = 1; map<float, ImgWin> top_det; cout<<"Start match..."<<endl; float max_dist = 0; double start_t = getTickCount(); //#pragma omp parallel for for(int r=patch_size.height/2; r<gray_img.rows-patch_size.height/2; r++) { for(int c=patch_size.width/2; c<gray_img.cols-patch_size.width/2; c++) { Rect box(c-patch_size.width/2, r-patch_size.height/2, patch_size.width, patch_size.height); MatFeatureSet featset; grad_mag(box).copyTo(featset["gradient"]); ComputePatchFeat(featset, feat); vector<DMatch> matches; MatchPatch(feat, topK, matches); for(size_t i=0; i<topK; i++) { score_map.at<float>(r,c) += matches[i].distance; } score_map.at<float>(r,c) /= topK; ImgWin best_match = box; best_match.score = matches[0].trainIdx; top_det[matches[0].distance] = best_match; max_dist = MAX(max_dist, score_map.at<float>(r,c)); } } cv::imshow("color", cimg); ImgVisualizer::DrawFloatImg("scoremap", score_map); cv::waitKey(0); int cnt = 0; Mat disp_img = cimg.clone(); for(auto pi=top_det.begin(); pi!=top_det.end(); pi++) { cout<<pi->first<<endl; rectangle(disp_img, pi->second, CV_RGB(255,0,0), 2); Mat db_view = imread(patch_meta.objects[pi->second.score].meta_data.img_path); resize(db_view, db_view, patch_size); Mat test_win; resize(cimg(pi->second), test_win, patch_size); ImgVisualizer::DrawFloatImg("db grad", patch_meta.objects[pi->second.score].visual_data.img_desc); ImgVisualizer::DrawFloatImg("test grad", grad_mag(pi->second)); imshow("db view", db_view); imshow("color", disp_img); waitKey(0); if(++cnt == 100) break; } return true; }
//------------------------------------------------------------------ LRESULT CALLBACK WndMainProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int i, notif; Tsquare *t; switch(message){ case WM_PAINT: { static PAINTSTRUCT ps; BeginPaint(hWnd, &ps); paint(ps.hdc, &ps.rcPaint); EndPaint(hWnd, &ps); } break; case WM_LBUTTONDOWN: lbutton(lParam); break; case WM_RBUTTONDOWN: rbutton(lParam); break; case WM_LBUTTONUP: if(inserting){ ReleaseCapture(); inserting=false; insertGroup(); resetSolution(); } break; case WM_MOUSEMOVE: if(inserting){ t= hitTest(lParam); if(t && t!=insSquares[insLen-1] && insLen<Nsymbol){ insSquares[insLen++]=t; } } #ifdef _DEBUGM mousemove(lParam); #endif break; case WM_TIMER: if(!IsIconic(hWin)){ playtime++; statusTime(); checkShowErr(false); } break; case WM_KEYDOWN: key(wParam); break; case WM_GETMINMAXINFO: { LPMINMAXINFO lpmm = (LPMINMAXINFO)lParam; lpmm->ptMinTrackSize.x = 250; lpmm->ptMinTrackSize.y = 200+toolH; break; } case WM_SIZE: width=LOWORD(lParam); height=HIWORD(lParam); SendMessage(toolbar, TB_AUTOSIZE, 0, 0); SendMessage(statusbar, WM_SIZE, 0, 0); onMoved(); invalidate(); break; case WM_MOVE: onMoved(); break; case WM_CLOSE: SendMessage(hWin, WM_COMMAND, ID_EXIT, 0); break; case WM_QUERYENDSESSION: writeini(); return TRUE; case WM_DESTROY: PostQuitMessage(0); break; case WM_COMMAND: notif=HIWORD(wParam); wParam=LOWORD(wParam); if(setLang(wParam)) break; if(wParam>=ID_SYMBOL && wParam<unsigned(ID_SYMBOL+size)){ select(wParam-ID_SYMBOL); break; } if(wParam>=ID_SIZE+4 && wParam<=ID_SIZE+Msize){ if(askNew()) break; size=wParam-ID_SIZE; newGameFormat(); numButtons(); break; } if(wParam>=ID_MULTI && wParam<ID_MULTI+sizeA(gameTypeA)-1){ if(askNew()) break; gameType=wParam-ID_MULTI; newGameFormat(); break; } switch(wParam){ case ID_CLEAR: noScore=true; init(false); invalidate(); break; case ID_CLEAR_ALL: noScore=true; initSquare(false); invalidate(); break; case ID_EDITOR: if(!editor){ if((undoPos==0 || done==Nsquare) && isGenerated()){ initSquare(false); } editor=true; playtime=0; noScore=true; editorChanged(); } break; case ID_EDITOR_END: if(editor){ endEditor(); editor=false; editorChanged(); } break; case ID_SOLVE: case ID_SOLVE1: if(testTotal()) break; noScore=true; if(done<Nsquare){ waitOn(); #ifdef _DEBUG DWORD time=getTickCount(); #endif Nsolution=0; curSolution=-1; //find all solutions (up to Msolution) undoAllPos=undoPos; if(wParam==ID_SOLVE1) resolve1(); else resolve(); freeGroups(); #ifdef _DEBUG status(4, _T("%d ms"), getTickCount()-time); #endif waitOff(); } if(Nsolution>0){ i=curSolution; curSolution++; if(curSolution>=Nsolution) curSolution=0; if(Nsolution>1){ if(i<0){ status(4, _T("%d %s"), Nsolution, lng(662, "solutions")); } else{ status(4, _T("%d/%d"), curSolution+1, Nsolution); } } rdSolution(); } else{ //easy solution (without recurse) or not solvable curSolution=0; status(4, _T("")); } checkErr(); invalidate(); break; case ID_CHEAT: noScore=true; if(errTime<0){ waitOn(); hint(); waitOff(); } checkShowErr(true); break; case ID_UNDO: undo(); checkErr(); break; case ID_REDO: redo(); checkErr(); break; case ID_UNDO_SYMBOL: undoSymbol(); checkErr(); break; case ID_REDO_SYMBOL: redoSymbol(); checkErr(); break; case ID_UNDO_ALL: undoAll(); checkErr(); break; case ID_REDO_ALL: while(redo()); checkErr(); break; case ID_DEL: select(-1); break; case ID_INS: select(-2); break; case ID_SIGN: select(-3); break; case ID_CONS: select(-4); break; case ID_EVEN: select(-5); break; case ID_EXIT: writeini(); DestroyWindow(hWin); break; case ID_DIAGONAL: if(askNew()) break; diag=!diag; newGameFormat(); break; case ID_SYMETRIC: if(askNew()) break; symetric=!symetric; newGameFormat(); break; case ID_LEVEL: if(DialogBox(inst, MAKEINTRESOURCE(IDD_LEVEL), hWnd, (DLGPROC)LevelProc)){ if(!editor) newGame(); } break; case ID_SHOWERR: DialogBox(inst, MAKEINTRESOURCE(IDD_ERRTIME), hWnd, (DLGPROC)ShowErrProc); break; case ID_KILLER: if(askNew()) break; killer=!killer; newGameFormat(); numButtons(); break; case ID_GREATER: if(askNew()) break; greater=!greater; newGameFormat(); break; case ID_CONSECUTIVE: if(askNew()) break; consecutive=!consecutive; newGameFormat(); if(selectedNum==-4 && !consecutive) select(-1); break; case ID_ODDEVEN: if(askNew()) break; oddeven=!oddeven; newGameFormat(); if(selectedNum==-5 && !oddeven) select(-1); break; case ID_DIGITS: case ID_LETTERS: case ID_COLORS: symbol0=wParam-350; checkMenus(); invalidate(); numButtons(); break; case ID_NEWGAME: if(editor) SendMessage(hWnd, WM_COMMAND, ID_EDITOR_END, 0); else newGame(); break; case ID_DELINI: delreg=true; break; case ID_DELHISCORE: if(MessageBox(hWnd, lng(799, "Do you really want to delete all hiscores ?"), title, MB_YESNO|MB_ICONQUESTION|MB_DEFBUTTON2) == IDYES){ for(TscoreTab *tab=score; tab;){ TscoreTab *t1= tab->next; delete tab; tab=t1; } score=0; writeScore(); } break; case ID_BEST_SCORES: DialogBox(inst, MAKEINTRESOURCE(IDD_HISCORE), hWnd, (DLGPROC)ScoreProc); break; case ID_COLORDLG: DialogBox(inst, MAKEINTRESOURCE(IDD_COLORS), hWin, (DLGPROC)ColorProc); break; case ID_ABOUT: DialogBox(inst, MAKEINTRESOURCE(IDD_ABOUT), hWnd, (DLGPROC)AboutProc); break; case ID_HELP_README: { TCHAR *buf=(TCHAR*)_alloca(2*MAX_PATH); getExeDir(buf, lng(13, "readme.txt")); if(ShellExecute(0, _T("open"), buf, 0, 0, SW_SHOWNORMAL)==(HINSTANCE)ERROR_FILE_NOT_FOUND){ msglng(730, "Cannot open %s", buf); } } break; case ID_WRBMP: if(saveFileDlg(&bmpOfn, hWnd, 0)){ wrBmp(bmpFn, bmpOfn.nFilterIndex); } break; case ID_SAVE: if(saveFileDlg(&gameOfn, hWnd, OFN_OVERWRITEPROMPT)){ save(gameFn); } break; case ID_OPEN: if(openFileDlg(&gameOfn, hWnd, OFN_FILEMUSTEXIST|OFN_HIDEREADONLY)){ open(gameFn); checkErr(); } break; case ID_CLEAR_GRP: resetSolution(); for(i=0; i<Ngroup; i++){ delGroup(&group[i]); } invalidate(); break; case ID_CLEAR_SGN: case ID_CLEAR_CONS: resetSolution(); for(i=0; i<Nboard; i++){ if(wParam==ID_CLEAR_SGN){ putSign(0, &board[i], 0); putSign(0, &board[i], 1); } if(wParam==ID_CLEAR_CONS){ putCons(false, &board[i], 0); putCons(false, &board[i], 1); } } invalidate(); break; case ID_MARKS: noScore=true; showMarks(); break; case ID_DELMARKS: delAllMarks(); break; case ID_PDF: if(askNew()) break; DialogBox(inst, MAKEINTRESOURCE(IDD_PDF), hWnd, (DLGPROC)PdfProc); numButtons(); break; } break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; }
bool CCapturador::CapturePatternsUndisorted(Mat& CameraMatrix,Mat& DistMatrix,int time) { m_vCaptures.clear(); VideoCapture cap(0); // open the default camera if (!cap.isOpened()) // check if we succeeded return -1; bool bMakeCapture = false; int nPatterns = 0; namedWindow("Camera", 1); namedWindow("Patrones"); /* HWND win_handle = FindWindow(0, L"Patrones"); if (!win_handle) { printf("Failed FindWindow\n"); } // Resize unsigned int flags = (SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER); flags &= ~SWP_NOSIZE; unsigned int x = 0; unsigned int y = 0; unsigned int w = m_Options->m_nWidth; unsigned int h = m_Options->m_nHeight; SetWindowPos(win_handle, HWND_NOTOPMOST, x, y, w, h, flags); // Borderless SetWindowLong(win_handle, GWL_STYLE, GetWindowLong(win_handle, GWL_EXSTYLE) | WS_EX_TOPMOST); ShowWindow(win_handle, SW_SHOW); cvMoveWindow("Patrones", 0, 0); */ long A = getTickCount(); long B = getTickCount(); bool start = false; for (int i = 0;;) { imshow("Patrones", m_vPatterns[i]); Mat frame; cap >> frame; if (frame.empty()) return false; Mat view, rview, map1, map2; initUndistortRectifyMap(CameraMatrix, DistMatrix, Mat(), getOptimalNewCameraMatrix(CameraMatrix, DistMatrix, frame.size(), 1, frame.size(), 0), frame.size(), CV_16SC2, map1, map2); remap(frame, rview, map1, map2, INTER_LINEAR); imshow("Camera", rview); B = getTickCount(); int C = B - A; if ((C>time&&start) || waitKey(30) >= 0) { start = true; cout << "time = " << C << endl; A = getTickCount(); i++; Mat capture = frame.clone(); Mat gray; cv::cvtColor(capture, gray, CV_BGR2GRAY); m_vCaptures.push_back(gray); if (++nPatterns >= m_nPatterns) break; }; } cout << "Patrones capturados." << endl; cvDestroyWindow("Patrones"); return true; }
qint64 QElapsedTimer::elapsed() const { qint64 elapsed = getTickCount() - t1; return ticksToNanoseconds(elapsed) / 1000000; }
//Execution timing functions double IMCSEngine::getTimingStart(void) { return (double)getTickCount(); //Get timing starting point }
Stitcher::Status Stitcher::composePanorama(InputArray images, OutputArray pano) { LOGLN("Warping images (auxiliary)... "); vector<Mat> imgs; images.getMatVector(imgs); if (!imgs.empty()) { CV_Assert(imgs.size() == imgs_.size()); Mat img; seam_est_imgs_.resize(imgs.size()); for (size_t i = 0; i < imgs.size(); ++i) { imgs_[i] = imgs[i]; resize(imgs[i], img, Size(), seam_scale_, seam_scale_); seam_est_imgs_[i] = img.clone(); } vector<Mat> seam_est_imgs_subset; vector<Mat> imgs_subset; for (size_t i = 0; i < indices_.size(); ++i) { imgs_subset.push_back(imgs_[indices_[i]]); seam_est_imgs_subset.push_back(seam_est_imgs_[indices_[i]]); } seam_est_imgs_ = seam_est_imgs_subset; imgs_ = imgs_subset; } Mat &pano_ = pano.getMatRef(); #if ENABLE_LOG int64 t = getTickCount(); #endif vector<Point> corners(imgs_.size()); vector<Mat> masks_warped(imgs_.size()); vector<Mat> images_warped(imgs_.size()); vector<Size> sizes(imgs_.size()); vector<Mat> masks(imgs_.size()); // Prepare image masks for (size_t i = 0; i < imgs_.size(); ++i) { masks[i].create(seam_est_imgs_[i].size(), CV_8U); masks[i].setTo(Scalar::all(255)); } // Warp images and their masks Ptr<detail::RotationWarper> w = warper_->create(float(warped_image_scale_ * seam_work_aspect_)); for (size_t i = 0; i < imgs_.size(); ++i) { Mat_<float> K; cameras_[i].K().convertTo(K, CV_32F); K(0,0) *= (float)seam_work_aspect_; K(0,2) *= (float)seam_work_aspect_; K(1,1) *= (float)seam_work_aspect_; K(1,2) *= (float)seam_work_aspect_; corners[i] = w->warp(seam_est_imgs_[i], K, cameras_[i].R, INTER_LINEAR, BORDER_REFLECT, images_warped[i]); sizes[i] = images_warped[i].size(); w->warp(masks[i], K, cameras_[i].R, INTER_NEAREST, BORDER_CONSTANT, masks_warped[i]); } vector<Mat> images_warped_f(imgs_.size()); for (size_t i = 0; i < imgs_.size(); ++i) images_warped[i].convertTo(images_warped_f[i], CV_32F); LOGLN("Warping images, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec"); // Find seams exposure_comp_->feed(corners, images_warped, masks_warped); seam_finder_->find(images_warped_f, corners, masks_warped); // Release unused memory seam_est_imgs_.clear(); images_warped.clear(); images_warped_f.clear(); masks.clear(); LOGLN("Compositing..."); #if ENABLE_LOG t = getTickCount(); #endif Mat img_warped, img_warped_s; Mat dilated_mask, seam_mask, mask, mask_warped; //double compose_seam_aspect = 1; double compose_work_aspect = 1; bool is_blender_prepared = false; double compose_scale = 1; bool is_compose_scale_set = false; Mat full_img, img; for (size_t img_idx = 0; img_idx < imgs_.size(); ++img_idx) { LOGLN("Compositing image #" << indices_[img_idx] + 1); // Read image and resize it if necessary full_img = imgs_[img_idx]; if (!is_compose_scale_set) { if (compose_resol_ > 0) compose_scale = min(1.0, sqrt(compose_resol_ * 1e6 / full_img.size().area())); is_compose_scale_set = true; // Compute relative scales //compose_seam_aspect = compose_scale / seam_scale_; compose_work_aspect = compose_scale / work_scale_; // Update warped image scale warped_image_scale_ *= static_cast<float>(compose_work_aspect); w = warper_->create((float)warped_image_scale_); // Update corners and sizes for (size_t i = 0; i < imgs_.size(); ++i) { // Update intrinsics cameras_[i].focal *= compose_work_aspect; cameras_[i].ppx *= compose_work_aspect; cameras_[i].ppy *= compose_work_aspect; // Update corner and size Size sz = full_img_sizes_[i]; if (std::abs(compose_scale - 1) > 1e-1) { sz.width = cvRound(full_img_sizes_[i].width * compose_scale); sz.height = cvRound(full_img_sizes_[i].height * compose_scale); } Mat K; cameras_[i].K().convertTo(K, CV_32F); Rect roi = w->warpRoi(sz, K, cameras_[i].R); corners[i] = roi.tl(); sizes[i] = roi.size(); } } if (std::abs(compose_scale - 1) > 1e-1) resize(full_img, img, Size(), compose_scale, compose_scale); else img = full_img; full_img.release(); Size img_size = img.size(); Mat K; cameras_[img_idx].K().convertTo(K, CV_32F); // Warp the current image w->warp(img, K, cameras_[img_idx].R, INTER_LINEAR, BORDER_REFLECT, img_warped); // Warp the current image mask mask.create(img_size, CV_8U); mask.setTo(Scalar::all(255)); w->warp(mask, K, cameras_[img_idx].R, INTER_NEAREST, BORDER_CONSTANT, mask_warped); // Compensate exposure exposure_comp_->apply((int)img_idx, corners[img_idx], img_warped, mask_warped); img_warped.convertTo(img_warped_s, CV_16S); img_warped.release(); img.release(); mask.release(); // Make sure seam mask has proper size dilate(masks_warped[img_idx], dilated_mask, Mat()); resize(dilated_mask, seam_mask, mask_warped.size()); mask_warped = seam_mask & mask_warped; if (!is_blender_prepared) { blender_->prepare(corners, sizes); is_blender_prepared = true; } // Blend the current image blender_->feed(img_warped_s, mask_warped, corners[img_idx]); } Mat result, result_mask; blender_->blend(result, result_mask); LOGLN("Compositing, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec"); // Preliminary result is in CV_16SC3 format, but all values are in [0,255] range, // so convert it to avoid user confusing result.convertTo(pano_, CV_8U); return OK; }
/** * This is the main entry point of a native application that is using * android_native_app_glue. It runs in its own thread, with its own * event loop for receiving input events and doing other things. */ void android_main(struct android_app* state) { //struct engine engine; const int FRAMES_PER_SECOND = 60; const int SKIP_TICKS = 1000 / FRAMES_PER_SECOND; // Make sure glue isn't stripped. app_dummy(); memset(&engine, 0, sizeof(engine)); state->userData = &engine; state->onAppCmd = engine_handle_cmd; state->onInputEvent = engine_handle_input; engine.app = state; createSoundEngine(); createBufferQueueAudioPlayer(); // Prepare to monitor accelerometer /*engine.sensorManager = ASensorManager_getInstance(); engine.accelerometerSensor = ASensorManager_getDefaultSensor(engine.sensorManager, ASENSOR_TYPE_ACCELEROMETER); engine.sensorEventQueue = ASensorManager_createEventQueue(engine.sensorManager, state->looper, LOOPER_ID_USER, NULL, NULL);*/ if (state->savedState != NULL) { // We are starting with a previous saved state; restore from it. // engine.state = *(struct saved_state*)state->savedState; } int32_t next_game_tick = getTickCount(); int sleep_time = 0; //http://www.koonsolo.com/news/dewitters-gameloop/ // loop waiting for stuff to do. while (1) { // Read all pending events. int ident; int events; struct android_poll_source* source; // If not animating, we will block forever waiting for events. // If animating, we loop until all events are read, then continue // to draw the next frame of animation. if ((ident = ALooper_pollAll(0, NULL, &events, (void**)&source)) >= 0) { // Process this event. if (source != NULL) { source->process(state, source); } } engine_draw_frame(&engine); next_game_tick += SKIP_TICKS; sleep_time = next_game_tick - now_ms(); if( sleep_time >= 0 ) { sleep( sleep_time ); } } shutdownAudio(); }
double ParallelOptimize::optimize(stereo_base::Depth &result, const int max_iter) const { typedef CompactLabelSpace Space; typedef ParallelFusionPipeline<Space> Pipeline; bool victorMethod = true; result.initialize(width, height, -1); const int kFusionSize = 4; ParallelFusionOption pipelineOption; pipelineOption.num_threads = num_threads + 1; pipelineOption.max_iteration = model->nLabel / num_threads / kFusionSize * max_iter; const int kLabelPerThread = model->nLabel / pipelineOption.num_threads; Pipeline::GeneratorSet generators((size_t)pipelineOption.num_threads); Pipeline::SolverSet solvers((size_t)pipelineOption.num_threads); vector<Space> initials((size_t)pipelineOption.num_threads); vector<ThreadOption> threadOptions((size_t)pipelineOption.num_threads); vector<vector<int> > labelSubSpace; splitLabel(labelSubSpace); const int kPix = model->width * model->height; //slave threads const int kOtherThread = std::min(num_threads-1, 1); for(auto i=0; i<pipelineOption.num_threads - 1; ++i){ const int startid = labelSubSpace[i].front(); initials[i].init(kPix, vector<int>(1, startid)); threadOptions[i].kTotal = kFusionSize + kOtherThread; threadOptions[i].kOtherThread = kOtherThread; threadOptions[i].solution_exchange_interval = 1; if(multiway) { generators[i] = shared_ptr<ProposalGenerator<Space> >(new MultiwayStereoGenerator(kPix, labelSubSpace[i])); solvers[i] = shared_ptr<FusionSolver<Space> >(new MultiwayStereoSolver(model)); }else{ generators[i] = shared_ptr<ProposalGenerator<Space> >(new SimpleStereoGenerator(kPix, labelSubSpace[i])); solvers[i] = shared_ptr<FusionSolver<Space> >(new SimpleStereoSolver(model)); } printf("Initial energy on thread %d: %.5f\n", i, solvers[i]->evaluateEnergy(initials[i])); } //monitor thread threadOptions.back().is_monitor = true; solvers.back() = shared_ptr<FusionSolver<Space> >(new SimpleStereoMonitor(model)); generators.back() = shared_ptr<ProposalGenerator<Space> >(new DummyGenerator()); StereoPipeline parallelFusionPipeline(pipelineOption); float t = (float)getTickCount(); printf("Start runing parallel optimization\n"); parallelFusionPipeline.runParallelFusion(initials, generators, solvers, threadOptions); t = ((float)getTickCount() - t) / (float)getTickFrequency(); SolutionType<Space > solution; parallelFusionPipeline.getBestLabeling(solution); printf("Done! Final energy: %.5f, running time: %.3fs\n", solution.first, t); std::dynamic_pointer_cast<SimpleStereoMonitor>(solvers.back())->dumpData(file_io.getDirectory() + "/temp"); dumpOutData(parallelFusionPipeline, file_io.getDirectory()+"/temp/plot_"+method); for(auto i=0; i<model->width * model->height; ++i){ result.setDepthAtInd(i, solution.second(i,0)); } return solution.first; }
Mat faceDetection(Mat &videoFrame, double scaleFactor, int scaledWidth, bool eyesDetect, bool mouthDetect, CascadeClassifier &mFaceDetector, CascadeClassifier &mMouthDetector, CascadeClassifier &mEyeDetector, QString personName ) { Mat defaultFace; Mat grayscaleFace; Mat equalizedFace; Mat resizedFace; Mat savedFace; const double CHANGE_IMAGE = 0.5; //0.3; // Change in face from last time const double CHANGE_TIME = 0.5;//1.0; // Time change old - new /*grayscale image*/ videoFrame.copyTo( defaultFace ); if (defaultFace.channels() == 3) { cvtColor(defaultFace, grayscaleFace, CV_BGR2GRAY); } else if (defaultFace.channels() == 4) { cvtColor(defaultFace, grayscaleFace, CV_BGRA2GRAY); } else { grayscaleFace = defaultFace; } /*shrinked image*/ float scale = defaultFace.cols / (float)scaledWidth; if (defaultFace.cols > scaledWidth) { int scaledHeight = cvRound(defaultFace.rows / scale); resize(grayscaleFace, resizedFace, Size(scaledWidth, scaledHeight)); } else { resizedFace = grayscaleFace; } /**/ vector< cv::Rect > faceVec; mFaceDetector.detectMultiScale( resizedFace, faceVec, scaleFactor ); for( size_t i=0; i<faceVec.size(); i++ ) { cv::rectangle( defaultFace, faceVec[i], CV_RGB(255,200,0), 2 ); cv::Mat face = resizedFace( faceVec[i] ); // EyesDetection if( eyesDetect == 1 ) { vector< cv::Rect > eyeVec; mEyeDetector.detectMultiScale(face, eyeVec); for( size_t j=0; j<eyeVec.size(); j++ ) { cv::Rect rect = eyeVec[j]; rect.x += faceVec[i].x; rect.y += faceVec[i].y; //saveFace(face); cv::rectangle(resizedFace, rect, CV_RGB(0,255,0), 2 ); } } // MouthDetection /*if mouth was detected on second part of face, face is equalized, normalized by size and saved, if it is not as the face saved last time.*/ if( mouthDetect == 1 ) { vector< cv::Rect > mouthVec; Rect halfRect = faceVec[i]; halfRect.height /= 2; halfRect.y += halfRect.height; Mat halfFace = videoFrame( halfRect ); mMouthDetector.detectMultiScale( halfFace, mouthVec, 3 ); for( size_t j=0; j<mouthVec.size(); j++ ) { cv::Rect rect = mouthVec[j]; rect.x += halfRect.x; rect.y += halfRect.y; Mat equalizedImg; equalizeHist(face, equalizedImg); Mat face_resized; resize(equalizedImg, face_resized, Size(faceSize, faceSize), 1.0, 1.0, INTER_CUBIC); double imageDiff = 10000000000.0; if (oldFace.data) { imageDiff = norm(face_resized, oldFace, NORM_L2)/(double)(face_resized.rows * face_resized.cols); } double current_time = (double)getTickCount(); double timeDiff_seconds = (current_time - oldTime)/getTickFrequency(); cout << "imageDiff " << imageDiff << " and timeDiff " << timeDiff_seconds << " and oldtime " << oldTime << " and currentTime " <<current_time << "\n"; if ((imageDiff > CHANGE_IMAGE) && (timeDiff_seconds > CHANGE_TIME)) { saveFace(face_resized, personName); qDebug() << "Photo was saved"; cv::rectangle( defaultFace, rect, CV_RGB(255,255,255), 2 ); Mat displayedFaceRegion = defaultFace( faceVec[i]); displayedFaceRegion += CV_RGB(90,90,90); oldFace = face_resized.clone(); oldTime = current_time; } } } return defaultFace; } }
int main( int argc, char** argv ) { printf( "Scale Space Cost Aggregation\n" ); if( argc != 11 ) { printf( "Usage: [CC_METHOD] [CA_METHOD] [PP_METHOD] [C_ALPHA] [lImg] [rImg] [lDis] [rDis] [maxDis] [disSc]\n" ); printf( "\nPress any key to continue...\n" ); getchar(); return -1; } string ccName = argv[ 1 ]; string caName = argv[ 2 ]; string ppName = argv[ 3 ]; double costAlpha = atof( argv[ 4 ] ); string lFn = argv[ 5 ]; string rFn = argv[ 6 ]; string lDisFn = argv[ 7 ]; string rDisFn = argv[ 8 ]; int maxDis = atoi( argv[ 9 ] ); int disSc = atoi( argv[ 10 ] ); // // Load left right image // printf( "\n--------------------------------------------------------\n" ); printf( "Load Image: (%s) (%s)\n", argv[ 5 ], argv[ 6 ] ); printf( "--------------------------------------------------------\n" ); Mat lImg = imread( lFn, CV_LOAD_IMAGE_COLOR ); Mat rImg = imread( rFn, CV_LOAD_IMAGE_COLOR ); if( !lImg.data || !rImg.data ) { printf( "Error: can not open image\n" ); printf( "\nPress any key to continue...\n" ); getchar(); return -1; } // set image format cvtColor( lImg, lImg, CV_BGR2RGB ); cvtColor( rImg, rImg, CV_BGR2RGB ); lImg.convertTo( lImg, CV_64F, 1 / 255.0f ); rImg.convertTo( rImg, CV_64F, 1 / 255.0f ); // time double duration; duration = static_cast<double>(getTickCount()); // // Stereo Match at each pyramid // int PY_LVL = 5; // build pyramid and cost volume Mat lP = lImg.clone(); Mat rP = rImg.clone(); SSCA** smPyr = new SSCA*[ PY_LVL ]; CCMethod* ccMtd = getCCType( ccName ); CAMethod* caMtd = getCAType( caName ); PPMethod* ppMtd = getPPType( ppName ); for( int p = 0; p < PY_LVL; p ++ ) { if( maxDis < 5 ) { PY_LVL = p; break; } printf( "\n\tPyramid: %d:", p ); smPyr[ p ] = new SSCA( lP, rP, maxDis, disSc ); smPyr[ p ]->CostCompute( ccMtd ); smPyr[ p ]->CostAggre( caMtd ); // pyramid downsample maxDis = maxDis / 2 + 1; disSc *= 2; pyrDown( lP, lP ); pyrDown( rP, rP ); } printf( "\n--------------------------------------------------------\n" ); printf( "\n Cost Aggregation in Scale Space\n" ); printf( "\n--------------------------------------------------------\n" ); // new method SolveAll( smPyr, PY_LVL, costAlpha ); // old method //for( int p = PY_LVL - 2 ; p >= 0; p -- ) { // smPyr[ p ]->AddPyrCostVol( smPyr[ p + 1 ], costAlpha ); //} // // Match + Postprocess // smPyr[ 0 ]->Match(); smPyr[ 0 ]->PostProcess( ppMtd ); Mat lDis = smPyr[ 0 ]->getLDis(); Mat rDis = smPyr[ 0 ]->getRDis(); #ifdef _DEBUG for( int s = 0; s < PY_LVL; s ++ ) { smPyr[ s ]->Match(); Mat sDis = smPyr[ s ]->getLDis(); ostringstream sStr; sStr << s; string sFn = sStr.str( ) + "_ld.png"; imwrite( sFn, sDis ); } saveOnePixCost( smPyr, PY_LVL ); #endif #ifdef USE_MEDIAN_FILTER // // Median Filter Output // MeanFilter( lDis, lDis, 3 ); #endif duration = static_cast<double>(getTickCount())-duration; duration /= cv::getTickFrequency(); // the elapsed time in sec printf( "\n--------------------------------------------------------\n" ); printf( "Total Time: %.2lf s\n", duration ); printf( "--------------------------------------------------------\n" ); // // Save Output // imwrite( lDisFn, lDis ); imwrite( rDisFn, rDis ); delete [] smPyr; delete ccMtd; delete caMtd; delete ppMtd; return 0; }
quint64 qt_msectime() { return ticksToNanoseconds(getTickCount()) / 1000000; }
void VideoDemos( VideoCapture& surveillance_video, int starting_frame, bool clean_binary_images ) { Mat previous_gray_frame, optical_flow, optical_flow_display; Mat current_frame, thresholded_image, closed_image, first_frame; Mat current_frame_gray, running_average_background; Mat temp_running_average_background, running_average_difference; Mat running_average_foreground_mask, running_average_foreground_image; Mat selective_running_average_background; Mat temp_selective_running_average_background, selective_running_average_difference; Mat selective_running_average_foreground_mask, selective_running_average_background_mask, selective_running_average_foreground_image; double running_average_learning_rate = 0.01; surveillance_video.set(CV_CAP_PROP_POS_FRAMES,starting_frame); surveillance_video >> current_frame; first_frame = current_frame.clone(); cvtColor(current_frame, current_frame_gray, CV_BGR2GRAY); current_frame.convertTo(running_average_background, CV_32F); selective_running_average_background = running_average_background.clone(); int rad = running_average_background.depth(); MedianBackground median_background( current_frame, (float) 1.005, 1 ); Mat median_background_image, median_foreground_image; int codec = static_cast<int>(surveillance_video.get(CV_CAP_PROP_FOURCC)); // V3.0.0 update on next line. OLD CODE was BackgroundSubtractorMOG2 gmm; //(50,16,true); Ptr<BackgroundSubtractorMOG2> gmm = createBackgroundSubtractorMOG2(); Mat foreground_mask, foreground_image = Mat::zeros(current_frame.size(), CV_8UC3); double frame_rate = surveillance_video.get(CV_CAP_PROP_FPS); double time_between_frames = 1000.0/frame_rate; Timestamper* timer = new Timestamper(); int frame_count = 0; while ((!current_frame.empty()) && (frame_count++ < 1000))//1800)) { double duration = static_cast<double>(getTickCount()); vector<Mat> input_planes(3); split(current_frame,input_planes); cvtColor(current_frame, current_frame_gray, CV_BGR2GRAY); if (frame_count%2 == 0) // Skip every second frame so the flow is greater. { if ( previous_gray_frame.data ) { Mat lucas_kanade_flow; timer->ignoreTimeSinceLastRecorded(); LucasKanadeOpticalFlow(previous_gray_frame, current_frame_gray, lucas_kanade_flow); timer->recordTime("Lucas Kanade Optical Flow"); calcOpticalFlowFarneback(previous_gray_frame, current_frame_gray, optical_flow, 0.5, 3, 15, 3, 5, 1.2, 0); cvtColor(previous_gray_frame, optical_flow_display, CV_GRAY2BGR); drawOpticalFlow(optical_flow, optical_flow_display, 8, Scalar(0, 255, 0), Scalar(0, 0, 255)); timer->recordTime("Farneback Optical Flow"); char frame_str[100]; sprintf( frame_str, "Frame = %d", frame_count); Mat temp_output = JoinImagesHorizontally( current_frame, frame_str, optical_flow_display, "Farneback Optical Flow", 4 ); Mat optical_flow_output = JoinImagesHorizontally( temp_output, "", lucas_kanade_flow, "Lucas Kanade Optical Flow", 4 ); imshow("Optical Flow", optical_flow_output ); } std::swap(previous_gray_frame, current_frame_gray); } // Static background image Mat difference_frame, binary_difference; Mat structuring_element(3,3,CV_8U,Scalar(1)); timer->ignoreTimeSinceLastRecorded(); absdiff(current_frame,first_frame,difference_frame); cvtColor(difference_frame, thresholded_image, CV_BGR2GRAY); threshold(thresholded_image,thresholded_image,30,255,THRESH_BINARY); if (clean_binary_images) { morphologyEx(thresholded_image,closed_image,MORPH_CLOSE,structuring_element); morphologyEx(closed_image,binary_difference,MORPH_OPEN,structuring_element); current_frame.copyTo(binary_difference, thresholded_image); } else { binary_difference.setTo(Scalar(0,0,0)); current_frame.copyTo(binary_difference, thresholded_image); } timer->recordTime("Static difference"); // Running Average (three channel version) vector<Mat> running_average_planes(3); split(running_average_background,running_average_planes); accumulateWeighted(input_planes[0], running_average_planes[0], running_average_learning_rate); accumulateWeighted(input_planes[1], running_average_planes[1], running_average_learning_rate); accumulateWeighted(input_planes[2], running_average_planes[2], running_average_learning_rate); merge(running_average_planes,running_average_background); running_average_background.convertTo(temp_running_average_background,CV_8U); absdiff(temp_running_average_background,current_frame,running_average_difference); split(running_average_difference,running_average_planes); // Determine foreground points as any point with a difference of more than 30 on any one channel: threshold(running_average_difference,running_average_foreground_mask,30,255,THRESH_BINARY); split(running_average_foreground_mask,running_average_planes); bitwise_or( running_average_planes[0], running_average_planes[1], running_average_foreground_mask ); bitwise_or( running_average_planes[2], running_average_foreground_mask, running_average_foreground_mask ); if (clean_binary_images) { morphologyEx(running_average_foreground_mask,closed_image,MORPH_CLOSE,structuring_element); morphologyEx(closed_image,running_average_foreground_mask,MORPH_OPEN,structuring_element); } running_average_foreground_image.setTo(Scalar(0,0,0)); current_frame.copyTo(running_average_foreground_image, running_average_foreground_mask); timer->recordTime("Running Average"); // Running Average with selective update vector<Mat> selective_running_average_planes(3); // Find Foreground mask selective_running_average_background.convertTo(temp_selective_running_average_background,CV_8U); absdiff(temp_selective_running_average_background,current_frame,selective_running_average_difference); split(selective_running_average_difference,selective_running_average_planes); // Determine foreground points as any point with an average difference of more than 30 over all channels: Mat temp_sum = (selective_running_average_planes[0]/3 + selective_running_average_planes[1]/3 + selective_running_average_planes[2]/3); threshold(temp_sum,selective_running_average_foreground_mask,30,255,THRESH_BINARY_INV); // Update background split(selective_running_average_background,selective_running_average_planes); accumulateWeighted(input_planes[0], selective_running_average_planes[0], running_average_learning_rate,selective_running_average_foreground_mask); accumulateWeighted(input_planes[1], selective_running_average_planes[1], running_average_learning_rate,selective_running_average_foreground_mask); accumulateWeighted(input_planes[2], selective_running_average_planes[2], running_average_learning_rate,selective_running_average_foreground_mask); invertImage(selective_running_average_foreground_mask,selective_running_average_foreground_mask); accumulateWeighted(input_planes[0], selective_running_average_planes[0], running_average_learning_rate/3.0,selective_running_average_foreground_mask); accumulateWeighted(input_planes[1], selective_running_average_planes[1], running_average_learning_rate/3.0,selective_running_average_foreground_mask); accumulateWeighted(input_planes[2], selective_running_average_planes[2], running_average_learning_rate/3.0,selective_running_average_foreground_mask); merge(selective_running_average_planes,selective_running_average_background); if (clean_binary_images) { morphologyEx(selective_running_average_foreground_mask,closed_image,MORPH_CLOSE,structuring_element); morphologyEx(closed_image,selective_running_average_foreground_mask,MORPH_OPEN,structuring_element); } selective_running_average_foreground_image.setTo(Scalar(0,0,0)); current_frame.copyTo(selective_running_average_foreground_image, selective_running_average_foreground_mask); timer->recordTime("Selective Running Average"); // Median background timer->ignoreTimeSinceLastRecorded(); median_background.UpdateBackground( current_frame ); timer->recordTime("Median"); median_background_image = median_background.GetBackgroundImage(); Mat median_difference; absdiff(median_background_image,current_frame,median_difference); cvtColor(median_difference, median_difference, CV_BGR2GRAY); threshold(median_difference,median_difference,30,255,THRESH_BINARY); median_foreground_image.setTo(Scalar(0,0,0)); current_frame.copyTo(median_foreground_image, median_difference); // Update the Gaussian Mixture Model // V3.0.0 update on next line. OLD CODE was gmm(current_frame, foreground_mask); gmm->apply(current_frame, foreground_mask); // Clean the resultant binary (moving pixel) mask using an opening. threshold(foreground_mask,thresholded_image,150,255,THRESH_BINARY); Mat moving_incl_shadows, shadow_points; threshold(foreground_mask,moving_incl_shadows,50,255,THRESH_BINARY); absdiff( thresholded_image, moving_incl_shadows, shadow_points ); Mat cleaned_foreground_mask; if (clean_binary_images) { morphologyEx(thresholded_image,closed_image,MORPH_CLOSE,structuring_element); morphologyEx(closed_image,cleaned_foreground_mask,MORPH_OPEN,structuring_element); } else cleaned_foreground_mask = thresholded_image.clone(); foreground_image.setTo(Scalar(0,0,0)); current_frame.copyTo(foreground_image, cleaned_foreground_mask); timer->recordTime("Gaussian Mixture Model"); // Create an average background image (just for information) Mat mean_background_image; timer->ignoreTimeSinceLastRecorded(); // V3.0.0 update on next line. OLD CODE was gmm.getBackgroundImage(mean_background_image); gmm->getBackgroundImage(mean_background_image); duration = static_cast<double>(getTickCount())-duration; duration /= getTickFrequency()/1000.0; int delay = (time_between_frames>duration) ? ((int) (time_between_frames-duration)) : 1; char c = cvWaitKey(delay); char frame_str[100]; sprintf( frame_str, "Frame = %d", frame_count); Mat temp_static_output = JoinImagesHorizontally( current_frame, frame_str, first_frame, "Static Background", 4 ); Mat static_output = JoinImagesHorizontally( temp_static_output, "", binary_difference, "Foreground", 4 ); imshow("Static Background Model", static_output ); Mat temp_running_output = JoinImagesHorizontally( current_frame, frame_str, temp_running_average_background, "Running Average Background", 4 ); Mat running_output = JoinImagesHorizontally( temp_running_output, "", running_average_foreground_image, "Foreground", 4 ); imshow("Running Average Background Model", running_output ); Mat temp_selective_output = JoinImagesHorizontally( current_frame, frame_str, temp_selective_running_average_background, "Selective Running Average Background", 4 ); Mat selective_output = JoinImagesHorizontally( temp_selective_output, "", selective_running_average_foreground_image, "Foreground", 4 ); imshow("Selective Running Average Background Model", selective_output ); Mat temp_median_output = JoinImagesHorizontally( current_frame, frame_str, median_background_image, "Median Background", 4 ); Mat median_output = JoinImagesHorizontally( temp_median_output, "", median_foreground_image, "Foreground", 4 ); imshow("Median Background Model", median_output ); Mat temp_gaussian_output = JoinImagesHorizontally( current_frame, frame_str, mean_background_image, "GMM Background", 4 ); Mat gaussian_output = JoinImagesHorizontally( temp_gaussian_output, "", foreground_image, "Foreground", 4 ); imshow("Gaussian Mixture Model", gaussian_output ); timer->putTimes( current_frame ); imshow( "Computation Times", current_frame ); surveillance_video >> current_frame; } cvDestroyAllWindows(); }
//Calls the Feedback low-level command numIterations times and calculates the //time per iteration. int allIO(HANDLE hDevice, u6CalibrationInfo *caliInfo) { uint8 *sendBuff, *recBuff; uint16 checksumTotal, bits16; uint32 bits32; int sendChars, recChars, i, j, sendSize, recSize; double valueAIN[14]; long time; int ret = 0; for( i = 0; i < 14; i++ ) valueAIN[i] = 9999; //Setting up a Feedback command that will set CIO0-3 as input, and setting //DAC0 voltage sendBuff = (uint8 *)malloc(18*sizeof(uint8)); //Creating an array of size 18 recBuff = (uint8 *)malloc(10*sizeof(uint8)); //Creating an array of size 10 sendBuff[1] = (uint8)(0xF8); //Command byte sendBuff[2] = 6; //Number of data words (.5 word for echo, 5.5 //words for IOTypes and data) sendBuff[3] = (uint8)(0x00); //Extended command number sendBuff[6] = 0; //Echo sendBuff[7] = 29; //IOType is PortDirWrite sendBuff[8] = 0; //FIO Writemask sendBuff[9] = 0; //EIO Writemask sendBuff[10] = 15; //CIO Writemask sendBuff[11] = 0; //FIO Direction sendBuff[12] = 0; //EIO Direction sendBuff[13] = 0; //CIO Direction //Setting DAC0 with 2.5 volt output sendBuff[14] = 38; //IOType is DAC0(16-bit) //Value is 2.5 volts (in binary) getDacBinVoltCalibrated16Bit(caliInfo, 0, 2.5, &bits16); sendBuff[15] = (uint8)(bits16&255); sendBuff[16] = (uint8)(bits16/256); sendBuff[17] = 0; //extra padding byte extendedChecksum(sendBuff, 18); //Sending command to U6 if( (sendChars = LJUSB_Write(hDevice, sendBuff, 18)) < 18 ) { if(sendChars == 0) printf("Feedback (CIO input) error : write failed\n"); else printf("Feedback (CIO input) error : did not write all of the buffer\n"); ret = -1; goto cleanmem; } //Reading response from U6 if( (recChars = LJUSB_Read(hDevice, recBuff, 10)) < 10 ) { if( recChars == 0 ) { printf("Feedback (CIO input) error : read failed\n"); ret = -1; goto cleanmem; } else printf("Feedback (CIO input) error : did not read all of the buffer\n"); } checksumTotal = extendedChecksum16(recBuff, 10); if( (uint8)((checksumTotal / 256) & 0xff) != recBuff[5] ) { printf("Feedback (CIO input) error : read buffer has bad checksum16(MSB)\n"); ret = -1; goto cleanmem; } if( (uint8)(checksumTotal & 0xff) != recBuff[4] ) { printf("Feedback (CIO input) error : read buffer has bad checksum16(LBS)\n"); ret = -1; goto cleanmem; } if( extendedChecksum8(recBuff) != recBuff[0] ) { printf("Feedback (CIO input) error : read buffer has bad checksum8\n"); ret = -1; goto cleanmem; } if( recBuff[1] != (uint8)(0xF8) || recBuff[3] != (uint8)(0x00) ) { printf("Feedback (CIO input) error : read buffer has wrong command bytes \n"); ret = -1; goto cleanmem; } if( recBuff[6] != 0 ) { printf("Feedback (CIO input) error : received errorcode %d for frame %d in Feedback response. \n", recBuff[6], recBuff[7]); ret = -1; goto cleanmem; } free(sendBuff); free(recBuff); //Setting up Feedback command that will run numIterations times if( ((sendSize = 7+numChannels*4) % 2) != 0 ) sendSize++; //Need an extra byte sendBuff = (uint8 *)malloc(sendSize*sizeof(uint8)); //Creating an array of size sendSize if( ((recSize = 9+numChannels*3) % 2) != 0 ) recSize++; //Need an extra byte recBuff = (uint8 *)malloc(recSize*sizeof(uint8)); //Creating an array of size recSize sendBuff[1] = (uint8)(0xF8); //Command byte sendBuff[2] = (sendSize - 6)/2; //Number of data words sendBuff[3] = (uint8)(0x00); //Extended command number sendBuff[6] = 0; //Echo sendBuff[sendSize - 1] = 0; //Setting last byte to zero in case it is the extra padding byte //Setting AIN read commands for( j = 0; j < numChannels; j++ ) { sendBuff[7 + j*4] = 2; //IOType is AIN24 //Positive Channel (bits 0 - 4), LongSettling (bit 6) and QuickSample (bit 7) sendBuff[8 + j*4] = j; //Positive Channel sendBuff[9 + j*4] = (uint8)(resolution&15) + (uint8)((gainIndex&15)*16); //ResolutionIndex (Bits 0-3), GainIndex (Bits 4-7) sendBuff[10 + j*4] = (uint8)(settlingFactor&7); //SettlingFactor (Bits 0-2) if( j%2 == 0 ) sendBuff[10 + j*4] += (uint8)((differential&1)*128); //Differential (Bits 7) } extendedChecksum(sendBuff, sendSize); time = getTickCount(); for( i = 0; i < numIterations; i++ ) { //Sending command to U6 if( (sendChars = LJUSB_Write(hDevice, sendBuff, sendSize)) < sendSize ) { if(sendChars == 0) printf("Feedback error (Iteration %d): write failed\n", i); else printf("Feedback error (Iteration %d): did not write all of the buffer\n", i); ret = -1; goto cleanmem; } //Reading response from U6 if( (recChars = LJUSB_Read(hDevice, recBuff, recSize)) < recSize ) { if( recChars == 0 ) { printf("Feedback error (Iteration %d): read failed\n", i); ret = -1; goto cleanmem; } } checksumTotal = extendedChecksum16(recBuff, recChars); if( (uint8)((checksumTotal / 256) & 0xff) != recBuff[5] ) { printf("Feedback error (Iteration %d): read buffer has bad checksum16(MSB)\n", i); ret = -1; goto cleanmem; } if( (uint8)(checksumTotal & 0xff) != recBuff[4] ) { printf("Feedback error (Iteration %d): read buffer has bad checksum16(LBS)\n", i); ret = -1; goto cleanmem; } if( extendedChecksum8(recBuff) != recBuff[0] ) { printf("Feedback error (Iteration %d): read buffer has bad checksum8\n", i); ret = -1; goto cleanmem; } if( recBuff[1] != (uint8)(0xF8) || recBuff[3] != (uint8)(0x00) ) { printf("Feedback error (Iteration %d): read buffer has wrong command bytes \n", i); ret = -1; goto cleanmem; } if( recBuff[6] != 0 ) { printf("Feedback error (Iteration %d): received errorcode %d for frame %d in Feedback response. \n", i, recBuff[6], recBuff[7]); ret = -1; goto cleanmem; } if( recChars != recSize ) { printf("Feedback error (Iteration %d): received packet if %d size when expecting %d\n", i, recChars, recSize); ret = -1; goto cleanmem; } //Getting AIN voltages for(j = 0; j < numChannels; j++) { bits32 = recBuff[9+j*3] + recBuff[10+j*3]*256 + recBuff[11+j*3]*65536; getAinVoltCalibrated(caliInfo, resolution, gainIndex, 1, bits32, &valueAIN[j]); } } time = getTickCount() - time; printf("Milliseconds per iteration = %.3f\n", (double)time / (double)numIterations); printf("\nAIN readings from last iteration:\n"); for( j = 0; j < numChannels; j++ ) printf("%.3f\n", valueAIN[j]); cleanmem: free(sendBuff); free(recBuff); sendBuff = NULL; recBuff = NULL; return ret; }
void ObjectTester::TestObjectRanking(const DatasetName& dbname) { // load dataset FileInfos img_fns; FileInfos dmap_fns; map<string, vector<ImgWin>> rawgtwins; NYUDepth2DataMan nyudata; Berkeley3DDataManager berkeleydata; if(dbname == DB_NYU2_RGBD) { nyudata.GetImageList(img_fns); nyudata.GetDepthmapList(dmap_fns); if(img_fns.size() != dmap_fns.size()) return; nyudata.LoadGTWins(img_fns, rawgtwins); } if(dbname == DB_BERKELEY3D) { berkeleydata.GetImageList(img_fns); berkeleydata.GetDepthmapList(dmap_fns); if(img_fns.size() != dmap_fns.size()) return; berkeleydata.LoadGTWins(img_fns, rawgtwins); } GenericObjectDetector detector; if( !detector.InitBingObjectness() ) return; SalientRegionDetector saldet; SalientRGBDRegionDetector salrgbd; DepthSaliency depth_sal; vector<vector<ImgWin>> objdetwins(img_fns.size()), saldetwins(img_fns.size()), depthdetwins; vector<vector<ImgWin>> gtwins(img_fns.size()); #pragma omp parallel for for (int i=0; i<img_fns.size(); i++) { // read image Mat curimg = imread(img_fns[i].filepath); if(curimg.empty()) continue; // read depth Mat curdmap; if(dbname == DB_NYU2_RGBD) if( !nyudata.LoadDepthData(dmap_fns[i].filepath, curdmap) ) continue; if(dbname == DB_BERKELEY3D) if( !berkeleydata.LoadDepthData(dmap_fns[i].filepath, curdmap) ) continue; //#define VERBOSE #ifdef VERBOSE // show gt visualsearch::ImgVisualizer::DrawImgWins("gt", curimg, rawgtwins[img_fns[i].filename]); visualsearch::ImgVisualizer::DrawFloatImg("dmap", curdmap, Mat()); #endif // normalize to image //normalize(curdmap, curdmap, 0, 255, NORM_MINMAX); //curdmap.convertTo(curdmap, CV_8U); //cvtColor(curdmap, curdmap, CV_GRAY2BGR); //imshow("depthimg", curdmap); //waitKey(10); //visualsearch::ImgVisualizer::DrawImgWins("b3d", curimg, rawgtwins[img_fns[i].filename]); //waitKey(0); // resize Size newsz; //ToolFactory::compute_downsample_ratio(Size(curimg.cols, curimg.rows), 300, newsz); //resize(curimg, curimg, newsz); double start_t = getTickCount(); // get objectness windows vector<ImgWin> objboxes; detector.GetObjectsFromBing(curimg, objboxes, 1000); //visualsearch::ImgVisualizer::DrawImgWins("objectness", curimg, objboxes); cout<<"objectness: "<<(double)(getTickCount()-start_t) / getTickFrequency()<<endl; start_t = getTickCount(); // rank normalize(curdmap, curdmap, 0, 255, NORM_MINMAX); vector<ImgWin> salboxes = objboxes; int saltype = SAL_COLOR | SAL_DEPTH; salrgbd.Init(saltype, curimg, curdmap); salrgbd.RankWins(salboxes); //depth_sal.RankWins(curdmap, salboxes); cout<<"Depth ranking: "<<(double)(getTickCount()-start_t) / getTickFrequency()<<endl; #ifdef VERBOSE vector<Mat> imgs(50); for (int i=0; i<50; i++) { imgs[i] = curimg(salboxes[i]); } Mat dispimg; visualsearch::ImgVisualizer::DrawImgCollection("objectness", imgs, 50, 15, dispimg); imshow("objectness", dispimg); visualsearch::ImgVisualizer::DrawImgWins("saldet", curimg, salboxes); waitKey(0); #endif /*saldet.g_para.segThresholdK = 200; saldet.Init(curdmap); saldet.RankWins(salboxes);*/ //visualsearch::ImgVisualizer::DrawImgWins("sal", curimg, salboxes); //waitKey(0); // add to collection objdetwins[i] = objboxes; saldetwins[i] = salboxes; gtwins[i] = rawgtwins[img_fns[i].filename]; cout<<"Finish detection on "<<i<<"/"<<img_fns.size()<<endl; } // evaluation WindowEvaluator eval; vector<Point2f> objprvals, salprvals, depthprvals; int topnum[] = {1, 5, 10, 50, 100, 200, 500, 800, 1000}; for(int i=0; i<9; i++) { Point2f curpr = eval.ComputePR(objdetwins, gtwins, topnum[i]); objprvals.push_back(curpr); curpr = eval.ComputePR(saldetwins, gtwins, topnum[i]); salprvals.push_back(curpr); } // save to file ofstream out1("nyu_objpr.txt"); for (size_t i=0; i<objprvals.size(); i++) out1<<objprvals[i].x<<" "<<objprvals[i].y<<endl; ofstream out2("nyu_rgbdpr.txt"); for (size_t i=0; i<salprvals.size(); i++) out2<<salprvals[i].x<<" "<<salprvals[i].y<<endl; cout<<"Finish evaluation"<<endl; }
bool GenericObjectDetector::RunVOC() { FileInfos imglist; db_man.Init(DB_VOC07); if( !db_man.GetImageList(imglist) ) return false; for(size_t id=0; id<10; id++) { double start_t = getTickCount(); img = imread(imglist[id].filepath); // preprocessing Preprocess(img); segmentor.DoSegmentation(img); const vector<visualsearch::SuperPixel>& sps = segmentor.superPixels; vector<ImgWin> det_wins; // loop all segment for (size_t sel_id=0; sel_id<sps.size(); sel_id++) { // test all window settings for(size_t win_id=0; win_id<winconfs.size(); win_id++) { // compute adjust range Point minpt, maxpt; if( !WinLocRange(sps[sel_id].box, winconfs[win_id], minpt, maxpt) ) continue; Point curpt(sps[sel_id].box.x+sps[sel_id].box.width/2, sps[sel_id].box.y+sps[sel_id].box.height/2); double bestscore = 0; ImgWin bestWin; // do shifting for(int i=0; i<10; i++) { // generate locations vector<ImgWin> wins; SampleWinLocs(curpt, winconfs[win_id], minpt, maxpt, 6, wins); if(wins.empty()) continue; for(size_t j=0; j<wins.size(); j++) wins[j].score = ComputeCenterSurroundMeanColorDiff(wins[j]); // sort sort(wins.begin(), wins.end()); const ImgWin selWin = wins[wins.size()-1]; //cout<<"Best score: "<<selWin.score<<endl; if(selWin.score > bestscore) { // shift to max point curpt.x = selWin.x + selWin.width/2; curpt.y = selWin.y + selWin.height/2; // update bestWin = selWin; } } det_wins.push_back(bestWin); } } sort(det_wins.begin(), det_wins.end()); cout<<"Time for image "<<id<<": "<<(getTickCount()-start_t) / getTickFrequency()<<"s"<<endl; // visualize final results reverse(det_wins.begin(), det_wins.end()); visualsearch::ImgVisualizer::DrawImgWins("final", img, det_wins); waitKey(0); } return true; }
void RecognitionDemos( Mat& full_image, Mat& template1, Mat& template2, Mat& template1locations, Mat& template2locations, VideoCapture& bicycle_video, Mat& bicycle_background, Mat& bicycle_model, VideoCapture& people_video, CascadeClassifier& cascade, Mat& numbers, Mat& good_orings, Mat& bad_orings, Mat& unknown_orings ) { Timestamper* timer = new Timestamper(); // Principal Components Analysis PCASimpleExample(); char ch = cvWaitKey(); cvDestroyAllWindows(); PCAFaceRecognition(); ch = cvWaitKey(); cvDestroyAllWindows(); // Statistical Pattern Recognition Mat gray_numbers,binary_numbers; cvtColor(numbers, gray_numbers, CV_BGR2GRAY); threshold(gray_numbers,binary_numbers,128,255,THRESH_BINARY_INV); vector<vector<Point>> contours; vector<Vec4i> hierarchy; findContours(binary_numbers,contours,hierarchy,CV_RETR_TREE,CV_CHAIN_APPROX_NONE); Mat contours_image = Mat::zeros(binary_numbers.size(), CV_8UC3); contours_image = Scalar(255,255,255); // Do some processing on all contours (objects and holes!) vector<RotatedRect> min_bounding_rectangle(contours.size()); vector<vector<Point>> hulls(contours.size()); vector<vector<int>> hull_indices(contours.size()); vector<vector<Vec4i>> convexity_defects(contours.size()); vector<Moments> contour_moments(contours.size()); for (int contour_number=0; (contour_number<(int)contours.size()); contour_number++) { if (contours[contour_number].size() > 10) { min_bounding_rectangle[contour_number] = minAreaRect(contours[contour_number]); convexHull(contours[contour_number], hulls[contour_number]); convexHull(contours[contour_number], hull_indices[contour_number]); convexityDefects( contours[contour_number], hull_indices[contour_number], convexity_defects[contour_number]); contour_moments[contour_number] = moments( contours[contour_number] ); } } for (int contour_number=0; (contour_number>=0); contour_number=hierarchy[contour_number][0]) { if (contours[contour_number].size() > 10) { Scalar colour( rand()&0x7F, rand()&0x7F, rand()&0x7F ); drawContours( contours_image, contours, contour_number, colour, CV_FILLED, 8, hierarchy ); char output[500]; double area = contourArea(contours[contour_number])+contours[contour_number].size()/2+1; // Process any holes (removing the area from the are of the enclosing contour) for (int hole_number=hierarchy[contour_number][2]; (hole_number>=0); hole_number=hierarchy[hole_number][0]) { area -= (contourArea(contours[hole_number])-contours[hole_number].size()/2+1); Scalar colour( rand()&0x7F, rand()&0x7F, rand()&0x7F ); drawContours( contours_image, contours, hole_number, colour, CV_FILLED, 8, hierarchy ); sprintf(output,"Area=%.0f", contourArea(contours[hole_number])-contours[hole_number].size()/2+1); Point location( contours[hole_number][0].x +20, contours[hole_number][0].y +5 ); putText( contours_image, output, location, FONT_HERSHEY_SIMPLEX, 0.4, colour ); } // Draw the minimum bounding rectangle Point2f bounding_rect_points[4]; min_bounding_rectangle[contour_number].points(bounding_rect_points); line( contours_image, bounding_rect_points[0], bounding_rect_points[1], Scalar(0, 0, 127)); line( contours_image, bounding_rect_points[1], bounding_rect_points[2], Scalar(0, 0, 127)); line( contours_image, bounding_rect_points[2], bounding_rect_points[3], Scalar(0, 0, 127)); line( contours_image, bounding_rect_points[3], bounding_rect_points[0], Scalar(0, 0, 127)); float bounding_rectangle_area = min_bounding_rectangle[contour_number].size.area(); // Draw the convex hull drawContours( contours_image, hulls, contour_number, Scalar(127,0,127) ); // Highlight any convexities int largest_convexity_depth=0; for (int convexity_index=0; convexity_index < (int)convexity_defects[contour_number].size(); convexity_index++) { if (convexity_defects[contour_number][convexity_index][3] > largest_convexity_depth) largest_convexity_depth = convexity_defects[contour_number][convexity_index][3]; if (convexity_defects[contour_number][convexity_index][3] > 256*2) { line( contours_image, contours[contour_number][convexity_defects[contour_number][convexity_index][0]], contours[contour_number][convexity_defects[contour_number][convexity_index][2]], Scalar(0,0, 255)); line( contours_image, contours[contour_number][convexity_defects[contour_number][convexity_index][1]], contours[contour_number][convexity_defects[contour_number][convexity_index][2]], Scalar(0,0, 255)); } } double hu_moments[7]; HuMoments( contour_moments[contour_number], hu_moments ); sprintf(output,"Perimeter=%d, Area=%.0f, BArea=%.0f, CArea=%.0f", contours[contour_number].size(),area,min_bounding_rectangle[contour_number].size.area(),contourArea(hulls[contour_number])); Point location( contours[contour_number][0].x, contours[contour_number][0].y-3 ); putText( contours_image, output, location, FONT_HERSHEY_SIMPLEX, 0.4, colour ); sprintf(output,"HuMoments = %.2f, %.2f, %.2f", hu_moments[0],hu_moments[1],hu_moments[2]); Point location2( contours[contour_number][0].x+100, contours[contour_number][0].y-3+15 ); putText( contours_image, output, location2, FONT_HERSHEY_SIMPLEX, 0.4, colour ); } } imshow("Shape Statistics", contours_image ); char c = cvWaitKey(); cvDestroyAllWindows(); // Support Vector Machine imshow("Good - original",good_orings); imshow("Defective - original",bad_orings); imshow("Unknown - original",unknown_orings); SupportVectorMachineDemo(good_orings,"Good",bad_orings,"Defective",unknown_orings); c = cvWaitKey(); cvDestroyAllWindows(); // Template Matching Mat display_image, correlation_image; full_image.copyTo( display_image ); double min_correlation, max_correlation; Mat matched_template_map; int result_columns = full_image.cols - template1.cols + 1; int result_rows = full_image.rows - template1.rows + 1; correlation_image.create( result_columns, result_rows, CV_32FC1 ); timer->reset(); double before_tick_count = static_cast<double>(getTickCount()); matchTemplate( full_image, template1, correlation_image, CV_TM_CCORR_NORMED ); double after_tick_count = static_cast<double>(getTickCount()); double duration_in_ms = 1000.0*(after_tick_count-before_tick_count)/getTickFrequency(); minMaxLoc( correlation_image, &min_correlation, &max_correlation ); FindLocalMaxima( correlation_image, matched_template_map, max_correlation*0.99 ); timer->recordTime("Template Matching (1)"); Mat matched_template_display1; cvtColor(matched_template_map, matched_template_display1, CV_GRAY2BGR); Mat correlation_window1 = convert_32bit_image_for_display( correlation_image, 0.0 ); DrawMatchingTemplateRectangles( display_image, matched_template_map, template1, Scalar(0,0,255) ); double precision, recall, accuracy, specificity, f1; Mat template1locations_gray; cvtColor(template1locations, template1locations_gray, CV_BGR2GRAY); CompareRecognitionResults( matched_template_map, template1locations_gray, precision, recall, accuracy, specificity, f1 ); char results[400]; Scalar colour( 255, 255, 255); sprintf( results, "precision=%.2f", precision); Point location( 7, 213 ); putText( display_image, "Results (1)", location, FONT_HERSHEY_SIMPLEX, 0.4, colour ); location.y += 13; putText( display_image, results, location, FONT_HERSHEY_SIMPLEX, 0.4, colour ); sprintf( results, "recall=%.2f", recall); location.y += 13; putText( display_image, results, location, FONT_HERSHEY_SIMPLEX, 0.4, colour ); sprintf( results, "accuracy=%.2f", accuracy); location.y += 13; putText( display_image, results, location, FONT_HERSHEY_SIMPLEX, 0.4, colour ); sprintf( results, "specificity=%.2f", specificity); location.y += 13; putText( display_image, results, location, FONT_HERSHEY_SIMPLEX, 0.4, colour ); sprintf( results, "f1=%.2f", f1); location.y += 13; putText( display_image, results, location, FONT_HERSHEY_SIMPLEX, 0.4, colour ); result_columns = full_image.cols - template2.cols + 1; result_rows = full_image.rows - template2.rows + 1; correlation_image.create( result_columns, result_rows, CV_32FC1 ); timer->ignoreTimeSinceLastRecorded(); matchTemplate( full_image, template2, correlation_image, CV_TM_CCORR_NORMED ); minMaxLoc( correlation_image, &min_correlation, &max_correlation ); FindLocalMaxima( correlation_image, matched_template_map, max_correlation*0.99 ); timer->recordTime("Template Matching (2)"); Mat matched_template_display2; cvtColor(matched_template_map, matched_template_display2, CV_GRAY2BGR); Mat correlation_window2 = convert_32bit_image_for_display( correlation_image, 0.0 ); DrawMatchingTemplateRectangles( display_image, matched_template_map, template2, Scalar(0,0,255) ); timer->putTimes(display_image); Mat template2locations_gray; cvtColor(template2locations, template2locations_gray, CV_BGR2GRAY); CompareRecognitionResults( matched_template_map, template2locations_gray, precision, recall, accuracy, specificity, f1 ); sprintf( results, "precision=%.2f", precision); location.x = 123; location.y = 213; putText( display_image, "Results (2)", location, FONT_HERSHEY_SIMPLEX, 0.4, colour ); location.y += 13; putText( display_image, results, location, FONT_HERSHEY_SIMPLEX, 0.4, colour ); sprintf( results, "recall=%.2f", recall); location.y += 13; putText( display_image, results, location, FONT_HERSHEY_SIMPLEX, 0.4, colour ); sprintf( results, "accuracy=%.2f", accuracy); location.y += 13; putText( display_image, results, location, FONT_HERSHEY_SIMPLEX, 0.4, colour ); sprintf( results, "specificity=%.2f", specificity); location.y += 13; putText( display_image, results, location, FONT_HERSHEY_SIMPLEX, 0.4, colour ); sprintf( results, "f1=%.2f", f1); location.y += 13; putText( display_image, results, location, FONT_HERSHEY_SIMPLEX, 0.4, colour ); Mat correlation_display1, correlation_display2; cvtColor(correlation_window1, correlation_display1, CV_GRAY2BGR); cvtColor(correlation_window2, correlation_display2, CV_GRAY2BGR); Mat output1 = JoinImagesVertically(template1,"Template (1)",correlation_display1,"Correlation (1)",4); Mat output2 = JoinImagesVertically(output1,"",matched_template_display1,"Local maxima (1)",4); Mat output3 = JoinImagesVertically(template2,"Template (2)",correlation_display2,"Correlation (2)",4); Mat output4 = JoinImagesVertically(output3,"",matched_template_display2,"Local maxima (2)",4); Mat output5 = JoinImagesHorizontally( full_image, "Original Image", output2, "", 4 ); Mat output6 = JoinImagesHorizontally( output5, "", output4, "", 4 ); Mat output7 = JoinImagesHorizontally( output6, "", display_image, "", 4 ); imshow( "Template matching result", output7 ); c = cvWaitKey(); cvDestroyAllWindows(); // Chamfer Matching Mat model_gray,model_edges,model_edges2; cvtColor(bicycle_model, model_gray, CV_BGR2GRAY); threshold(model_gray,model_edges,127,255,THRESH_BINARY); Mat current_frame; bicycle_video.set(CV_CAP_PROP_POS_FRAMES,400); // Just in case the video has already been used. bicycle_video >> current_frame; bicycle_background = current_frame.clone(); bicycle_video.set(CV_CAP_PROP_POS_FRAMES,500); timer->reset(); int count = 0; while (!current_frame.empty() && (count < 8)) { Mat result_image = current_frame.clone(); count++; Mat difference_frame, difference_gray, current_edges; absdiff(current_frame,bicycle_background,difference_frame); cvtColor(difference_frame, difference_gray, CV_BGR2GRAY); Canny(difference_frame, current_edges, 100, 200, 3); vector<vector<Point> > results; vector<float> costs; threshold(model_gray,model_edges,127,255,THRESH_BINARY); Mat matching_image, chamfer_image, local_minima; timer->ignoreTimeSinceLastRecorded(); threshold(current_edges,current_edges,127,255,THRESH_BINARY_INV); distanceTransform( current_edges, chamfer_image, CV_DIST_L2 , 3); timer->recordTime("Chamfer Image"); ChamferMatching( chamfer_image, model_edges, matching_image ); timer->recordTime("Matching"); FindLocalMinima( matching_image, local_minima, 500.0 ); timer->recordTime("Find Minima"); DrawMatchingTemplateRectangles( result_image, local_minima, model_edges, Scalar( 255, 0, 0 ) ); Mat chamfer_display_image = convert_32bit_image_for_display( chamfer_image ); Mat matching_display_image = convert_32bit_image_for_display( matching_image ); //timer->putTimes(result_image); Mat current_edges_display, local_minima_display, model_edges_display, colour_matching_display_image, colour_chamfer_display_image; cvtColor(current_edges, current_edges_display, CV_GRAY2BGR); cvtColor(local_minima, local_minima_display, CV_GRAY2BGR); cvtColor(model_edges, model_edges_display, CV_GRAY2BGR); cvtColor(matching_display_image, colour_matching_display_image, CV_GRAY2BGR); cvtColor(chamfer_display_image, colour_chamfer_display_image, CV_GRAY2BGR); Mat output1 = JoinImagesVertically(current_frame,"Video Input",current_edges_display,"Edges from difference", 4); Mat output2 = JoinImagesVertically(output1,"",model_edges_display,"Model", 4); Mat output3 = JoinImagesVertically(bicycle_background,"Static Background",colour_chamfer_display_image,"Chamfer image", 4); Mat output4 = JoinImagesVertically(output3,"",colour_matching_display_image,"Degree of fit", 4); Mat output5 = JoinImagesVertically(difference_frame,"Difference",result_image,"Result", 4); Mat output6 = JoinImagesVertically(output5,"",local_minima_display,"Local minima", 4); Mat output7 = JoinImagesHorizontally( output2, "", output4, "", 4 ); Mat output8 = JoinImagesHorizontally( output7, "", output6, "", 4 ); imshow("Chamfer matching", output8); c = waitKey(1000); // This makes the image appear on screen bicycle_video >> current_frame; } c = cvWaitKey(); cvDestroyAllWindows(); // Cascade of Haar classifiers (most often shown for face detection). VideoCapture camera; camera.open(1); camera.set(CV_CAP_PROP_FRAME_WIDTH, 320); camera.set(CV_CAP_PROP_FRAME_HEIGHT, 240); if( camera.isOpened() ) { timer->reset(); Mat current_frame; do { camera >> current_frame; if( current_frame.empty() ) break; vector<Rect> faces; timer->ignoreTimeSinceLastRecorded(); Mat gray; cvtColor( current_frame, gray, CV_BGR2GRAY ); equalizeHist( gray, gray ); cascade.detectMultiScale( gray, faces, 1.1, 2, CV_HAAR_SCALE_IMAGE, Size(30, 30) ); timer->recordTime("Haar Classifier"); for( int count = 0; count < (int)faces.size(); count++ ) rectangle(current_frame, faces[count], cv::Scalar(255,0,0), 2); //timer->putTimes(current_frame); imshow( "Cascade of Haar Classifiers", current_frame ); c = waitKey(10); // This makes the image appear on screen } while (c == -1); }
void DelayMsec(unsigned short msec){ int tick_count= getTickCount(); while((getTickCount()-tick_count)<msec); }
void Tree::SplitNode( const spliteType& sType, const vector<Sample>& samples, const Mat_<double>& meanShape, const vector<int>& sample_idx, // output double& threshold, Point2d* feat, vector<int> &lcID, vector<int> &rcID ) { if (sample_idx.size() == 0) { threshold = 0; feat = new Point2d[2]; feat[0].x = 0; feat[0].y = 0; feat[1].x = 0; feat[1].y = 0; lcID.clear(); rcID.clear(); return; } RNG randomGenerator(getTickCount()); Mat_<double> candidatePixelLocations(numFeats, 4); for (int i = 0; i < candidatePixelLocations.rows; i++) { double x1 = randomGenerator.uniform(-1.0, 1.0); double x2 = randomGenerator.uniform(-1.0, 1.0); double y1 = randomGenerator.uniform(-1.0, 1.0); double y2 = randomGenerator.uniform(-1.0, 1.0); if ((x1 * x1 + y1 * y1 > 1.0) || (x2 * x2 + y2 * y2 > 1.0)) { i--; continue; } candidatePixelLocations(i, 0) = x1 * radioRadius; candidatePixelLocations(i, 1) = y1 * radioRadius; candidatePixelLocations(i, 2) = x2 * radioRadius; candidatePixelLocations(i, 3) = y2 * radioRadius; } // get pixel difference features Mat_<int> densities(numFeats, (int)sample_idx.size()); for (int i = 0; i < sample_idx.size(); i++) { Mat_<double> rotation; double scale; Mat_<double> temp = Joint::Project(samples[sample_idx[i]].current, samples[sample_idx[i]].bb); Joint::SimilarityTransform(temp, meanShape, rotation, scale); for (int j = 0; i < numFeats; i++) { double project_x1 = rotation(0, 0) * candidatePixelLocations(j, 0) + rotation(0, 1) * candidatePixelLocations(j, 1); double project_y1 = rotation(1, 0) * candidatePixelLocations(j, 0) + rotation(1, 1) * candidatePixelLocations(j, 1); project_x1 = scale * project_x1 * samples[sample_idx[i]].bb.width / 2.0; project_y1 = scale * project_y1 * samples[sample_idx[i]].bb.height / 2.0; int real_x1 = scale * project_x1 + samples[sample_idx[i]].current(landmarkID, 0); int real_y1 = scale * project_y1 + samples[sample_idx[i]].current(landmarkID, 1); real_x1 = max(0.0, min((double)real_x1, samples[sample_idx[i]].image.cols - 1.0)); real_y1 = max(0.0, min((double)real_y1, samples[sample_idx[i]].image.rows - 1.0)); double project_x2 = rotation(0, 0) * candidatePixelLocations(j, 2) + rotation(0, 1) * candidatePixelLocations(j, 3); double project_y2 = rotation(1, 0) * candidatePixelLocations(j, 2) + rotation(1, 1) * candidatePixelLocations(j, 3); project_x2 = scale * project_x2 * samples[sample_idx[i]].bb.width / 2.0; project_y2 = scale * project_y2 * samples[sample_idx[i]].bb.height / 2.0; int real_x2 = scale * project_x2 + samples[sample_idx[i]].current(landmarkID, 0); int real_y2 = scale * project_y2 + samples[sample_idx[i]].current(landmarkID, 1); real_x2 = max(0.0, min((double)real_x2, samples[sample_idx[i]].image.cols - 1.0)); real_y2 = max(0.0, min((double)real_y2, samples[sample_idx[i]].image.rows - 1.0)); densities(j, i) = ((int)(samples[sample_idx[i]].image(real_y1, real_x1)) - (int)(samples[sample_idx[i]].image(real_y2, real_x2))); } } // pick the feature Mat_<int> densities_sorted = densities.clone(); cv::sort(densities, densities_sorted, CV_SORT_ASCENDING); if (sType == CLASSIFICATION) { // classification node double min_entropy = INT_MAX; double tempThresh = 0; double pos_pos_count = 0; double neg_pos_count = 0; double pos_neg_count = 0; double neg_neg_count = 0; double entropy = 0; double min_id; for (int i = 0; i < numFeats; i++) { int ind = (int)(sample_idx.size() * randomGenerator.uniform(0.05, 0.95)); tempThresh = densities_sorted(i, ind); for (int j = 0; j < sample_idx.size(); j++) { if (densities(i, j) < tempThresh) { if (samples[sample_idx[j]].label == -1) { neg_neg_count++; } else { neg_pos_count++; } } else { if (samples[sample_idx[j]].label == 1) { pos_pos_count++; } else { pos_neg_count++; } } } double p1 = (double)pos_pos_count / (pos_pos_count + pos_neg_count); double p2 = (double)pos_neg_count / (pos_pos_count + pos_neg_count); double p3 = (double)neg_pos_count / (neg_pos_count + neg_neg_count); double p4 = (double)neg_neg_count / (neg_pos_count + neg_neg_count); entropy = p1 * log(p1) + p2 * log(p2) + p3 * log(p3) + p4 * log(p4); if (entropy < min_entropy) { threshold = tempThresh; min_id = i; } } feat[0].x = candidatePixelLocations(min_id, 0) / radioRadius; feat[0].y = candidatePixelLocations(min_id, 1) / radioRadius; feat[1].x = candidatePixelLocations(min_id, 2) / radioRadius; feat[1].y = candidatePixelLocations(min_id, 3) / radioRadius; lcID.clear(); rcID.clear(); for (int j = 0; j < sample_idx.size(); j++) { if (densities(min_id, j) < threshold) { lcID.push_back(sample_idx[j]); } else { rcID.push_back(sample_idx[j]); } } } else if (sType == REGRESSION) { Mat_<double> shape_residual((int)sample_idx.size(), 2); int posCount = 0; for (int i = 0; i < sample_idx.size(); i++) { if (samples[sample_idx[i]].label == 1) { Mat_<double> residual = Joint::GetShapeResidual(samples[sample_idx[i]], meanShape); shape_residual(i, 0) = residual(landmarkID, 0); shape_residual(i, 1) = residual(landmarkID, 1); posCount++; } } vector<double> lc1, lc2; vector<double> rc1, rc2; lc1.reserve(sample_idx.size()); lc2.reserve(sample_idx.size()); rc1.reserve(sample_idx.size()); rc2.reserve(sample_idx.size()); double varOverall = (Joint::CalculateVar(shape_residual.col(0)) + Joint::CalculateVar(shape_residual.col(1))) * posCount; double maxVarReduction = 0; double tempThresh; double varLeft = 0; double varRight = 0; double varReduction = 0; double max_id = 0; for (int i = 0; i < numFeats; i++) { lc1.clear(); lc2.clear(); rc1.clear(); rc2.clear(); int ind = (sample_idx.size() * randomGenerator.uniform(0.05, 0.95)); tempThresh = densities_sorted(i, ind); for (int j = 0; j < sample_idx.size(); j++) { if (samples[sample_idx[i]].label == 1) { if (densities(i, j) < tempThresh) { lc1.push_back(shape_residual(j, 0)); lc2.push_back(shape_residual(j, 1)); } else { rc1.push_back(shape_residual(j, 0)); rc2.push_back(shape_residual(j, 1)); } } } varLeft = (Joint::CalculateVar(lc1) + Joint::CalculateVar(lc2)) * lc1.size(); varRight = (Joint::CalculateVar(rc1) + Joint::CalculateVar(rc2)) * rc2.size(); varReduction = varOverall - varLeft - varRight; if (varReduction > maxVarReduction) { maxVarReduction = varReduction; threshold = tempThresh; max_id = i; } } feat[0].x = candidatePixelLocations(max_id, 0) / radioRadius; feat[0].y = candidatePixelLocations(max_id, 1) / radioRadius; feat[1].x = candidatePixelLocations(max_id, 2) / radioRadius; feat[1].y = candidatePixelLocations(max_id, 3) / radioRadius; lcID.clear(); rcID.clear(); for (int j = 0; j < sample_idx.size(); j++) { if (densities(max_id, j) < threshold) { lcID.push_back(sample_idx[j]); } else { rcID.push_back(sample_idx[j]); } } } }
Stitcher::Status Stitcher::matchImages() { if ((int)imgs_.size() < 2) { LOGLN("Need more images"); return ERR_NEED_MORE_IMGS; } work_scale_ = 1; seam_work_aspect_ = 1; seam_scale_ = 1; bool is_work_scale_set = false; bool is_seam_scale_set = false; Mat full_img, img; features_.resize(imgs_.size()); seam_est_imgs_.resize(imgs_.size()); full_img_sizes_.resize(imgs_.size()); LOGLN("Finding features..."); #if ENABLE_LOG int64 t = getTickCount(); #endif for (size_t i = 0; i < imgs_.size(); ++i) { full_img = imgs_[i]; full_img_sizes_[i] = full_img.size(); if (registr_resol_ < 0) { img = full_img; work_scale_ = 1; is_work_scale_set = true; } else { if (!is_work_scale_set) { work_scale_ = min(1.0, sqrt(registr_resol_ * 1e6 / full_img.size().area())); is_work_scale_set = true; } resize(full_img, img, Size(), work_scale_, work_scale_); } if (!is_seam_scale_set) { seam_scale_ = min(1.0, sqrt(seam_est_resol_ * 1e6 / full_img.size().area())); seam_work_aspect_ = seam_scale_ / work_scale_; is_seam_scale_set = true; } if (rois_.empty()) (*features_finder_)(img, features_[i]); else { vector<Rect> rois(rois_[i].size()); for (size_t j = 0; j < rois_[i].size(); ++j) { Point tl(cvRound(rois_[i][j].x * work_scale_), cvRound(rois_[i][j].y * work_scale_)); Point br(cvRound(rois_[i][j].br().x * work_scale_), cvRound(rois_[i][j].br().y * work_scale_)); rois[j] = Rect(tl, br); } (*features_finder_)(img, features_[i], rois); } features_[i].img_idx = (int)i; LOGLN("Features in image #" << i+1 << ": " << features_[i].keypoints.size()); resize(full_img, img, Size(), seam_scale_, seam_scale_); seam_est_imgs_[i] = img.clone(); } // Do it to save memory features_finder_->collectGarbage(); full_img.release(); img.release(); LOGLN("Finding features, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec"); LOG("Pairwise matching"); #if ENABLE_LOG t = getTickCount(); #endif (*features_matcher_)(features_, pairwise_matches_, matching_mask_); features_matcher_->collectGarbage(); LOGLN("Pairwise matching, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec"); // Leave only images we are sure are from the same panorama indices_ = detail::leaveBiggestComponent(features_, pairwise_matches_, (float)conf_thresh_); vector<Mat> seam_est_imgs_subset; vector<Mat> imgs_subset; vector<Size> full_img_sizes_subset; for (size_t i = 0; i < indices_.size(); ++i) { imgs_subset.push_back(imgs_[indices_[i]]); seam_est_imgs_subset.push_back(seam_est_imgs_[indices_[i]]); full_img_sizes_subset.push_back(full_img_sizes_[indices_[i]]); } seam_est_imgs_ = seam_est_imgs_subset; imgs_ = imgs_subset; full_img_sizes_ = full_img_sizes_subset; if ((int)imgs_.size() < 2) { LOGLN("Need more images"); return ERR_NEED_MORE_IMGS; } return OK; }
void Tree::Train(vector<Sample> &samples, const Mat_<double> &meanShape, int stages_, int landmarkID_ ) { // set parameters landmarkID = landmarkID_; numFeats = GlobalParams::numFeats[stages_]; radioRadius = GlobalParams::radius[stages_]; numNodes = 1; numLeafNodes = 1; // index: indicates the training samples id in training data set int num_nodes_iter; int num_split; for (int i = 0; i < samples.size(); i++) { // push the indies of training samples into root node nodes[0].sample_idx.push_back(i); } // initialize the root nodes[0].isSplit = false; nodes[0].pNodeID = 0; nodes[0].depth = 1; nodes[0].cNodesID[0] = 0; nodes[0].cNodesID[1] = 0; nodes[0].isLeaf = true; nodes[0].threshold = 0; nodes[0].feat[0].x = 1; nodes[0].feat[0].y = 1; nodes[0].feat[1].x = 1; nodes[0].feat[1].y = 1; bool stop = false; int num_nodes = 1; int num_leafnodes = 1; double thresh; Point2d feat[2]; vector<int> lcID, rcID; lcID.reserve(nodes[0].sample_idx.size()); rcID.reserve(nodes[0].sample_idx.size()); while (!stop) { num_nodes_iter = num_nodes; num_split = 0; for (int n = 0; n < num_nodes_iter; n++) { if (!nodes[n].isSplit) { if (nodes[n].depth == maxDepth) { nodes[n].isSplit = true; } } else { // separate the training samples into left and right path // splite the tree // In each internal node, we randomly choose to either minimize the // binary entropy for classification (with probablity p) // or the variance of ficial point increments for regression // (with probability 1-p) RNG randonGenerator(getTickCount()); double p = 1 - 0.1 * stages_; double val = randonGenerator.uniform(0.0, 1.0); if (val <= p) { SplitNode(CLASSIFICATION, samples, meanShape, nodes[n].sample_idx, thresh, feat, lcID, rcID); } else { SplitNode(REGRESSION, samples, meanShape, nodes[n].sample_idx, thresh, feat, lcID, rcID); } // set the threshold and feature for current node nodes[n].feat[0] = feat[0]; nodes[n].feat[1] = feat[1]; nodes[n].threshold = thresh; nodes[n].isSplit = true; nodes[n].isLeaf = false; nodes[n].cNodesID[0] = num_nodes; nodes[n].cNodesID[1] = num_nodes + 1; // add left and right child into the random tree nodes[num_nodes].sample_idx = lcID; nodes[num_nodes].isSplit = false; nodes[num_nodes].pNodeID = n; nodes[num_nodes].depth = nodes[n].depth + 1; nodes[num_nodes].cNodesID[0] = 0; nodes[num_nodes].cNodesID[1] = 0; nodes[num_nodes].isLeaf = true; nodes[num_nodes + 1].sample_idx = rcID; nodes[num_nodes + 1].isSplit = false; nodes[num_nodes + 1].pNodeID = n; nodes[num_nodes + 1].depth = nodes[n].depth + 1; nodes[num_nodes + 1].cNodesID[0] = 0; nodes[num_nodes + 1].cNodesID[1] = 0; nodes[num_nodes + 1].isLeaf = true; num_split++; num_leafnodes++; num_nodes += 2; } } if (num_split == 0) { stop = 1; } else { numNodes = num_nodes; numLeafNodes = num_leafnodes; } } // mark leaf nodes. // clear sample indices in each node leafID.clear(); for (int i = 0; i < numNodes; i++) { nodes[i].sample_idx.clear(); if (nodes[i].isLeaf) { leafID.push_back(i); } } }
void QElapsedTimer::start() { t1 = getTickCount(); t2 = 0; }
//Sends 1000 Feedback low-level commands to read digital IO and analog inputs. //On the first send, the following are set: DAC0 to 2.5 volts, DAC1 to 3.5 //volts, and digital IOs to inputs. int allIO(int socketFD, ue9CalibrationInfo *caliInfo) { uint8 sendBuff[34], recBuff[64]; int sendChars, recChars; int i, j; uint16 checksumTotal, bytesVoltage; int initialize; //boolean to init. DAC and digital IO settings int numIterations; long time; uint8 numChannels; //Number of AIN channels, 0-16. long ainResolution; double valueAIN[16]; int valueDIPort; uint8 settlingTime; uint16 ainMask; numIterations = 1000; initialize = 1; time = 0; numChannels = 8; ainResolution = 12; for(i = 0; i < 16; i++) valueAIN[i] = 9999.0; settlingTime = 0; ainMask = pow(2.0, numChannels) - 1; sendBuff[1] = (uint8)(0xF8); //command byte sendBuff[2] = (uint8)(0x0E); //number of data words sendBuff[3] = (uint8)(0x00); //extended command number sendBuff[6] = 255; //FIOMask : setting the mask of all FIOs sendBuff[7] = 0; //FIODir : setting all FIO directions to input sendBuff[8] = 0; //FIOState : all FIO directions are input, so // state writes do not apply sendBuff[9] = 255; //EIOMask : setting the mask of all EIOs sendBuff[10] = 0; //EIODir : setting all EIO directions to input sendBuff[11] = 0; //EIOState : all EIO directions are input, so // state writes do not apply sendBuff[12] = 15; //CIOMask : setting the mask of all CIOs sendBuff[13] = 0; //CIODirState : setting all CIO directions to input, // state writes do not apply sendBuff[14] = 7; //MIOMask : setting the mask of all MIOs sendBuff[15] = 0; //MIODirState : setting all MIO directions to input, // state writes do not apply //getting binary DAC0 value of 2.5 volts if(analogToCalibratedBinaryVoltage(caliInfo, 0, 2.500, &bytesVoltage) < 0) return -1; //setting the voltage of DAC0 to 2.5 sendBuff[16] = (uint8)(bytesVoltage & 255); //low bits of DAC0 sendBuff[17] = (uint8)(bytesVoltage/256) + 192; //high bits of DAC0 //(bit 7 : Enable, //bit 6: Update) //getting binary DAC1 value of 3.5 volts if(analogToCalibratedBinaryVoltage(caliInfo, 1, 3.500, &bytesVoltage) < 0) return -1; //setting the voltage of DAC1 to 3.5 volts sendBuff[18] = (uint8)(bytesVoltage & 255); //low bits of DAC1 sendBuff[19] = (uint8)(bytesVoltage/256) + 192; //high bits of DAC1 //(bit 7 : Enable, //bit 6: Update) //AINMask - reading the number of AINs specified by numChannels sendBuff[20] = ainMask & 255; //AINMask (low byte) sendBuff[21] = ainMask/256; //AINMask (high byte) sendBuff[22] = 14; //AIN14ChannelNumber : setting to channel 14 sendBuff[23] = 15; //AIN15ChannelNumber : setting to channel 15 sendBuff[24] = ainResolution; //Resolution : Resolution specified by // ainResolution sendBuff[25] = settlingTime; //SettlingTime //setting all BipGains (Gain = 1, Bipolar = 1) for(i = 26; i < 34; i++) sendBuff[i] = (uint8)(0x00); extendedChecksum(sendBuff, 34); time = getTickCount(); for(i = 0; i < numIterations; i++) { //Sending command to UE9 sendChars = send(socketFD, sendBuff, 34, 0); if(sendChars < 34) { if(sendChars == 0) printf("Feedback error (Iteration %d) : send failed\n", i); else printf("Feedback error (Iteration %d) : did not send all of the buffer\n", i); return -1; } //Reading response from UE9 recChars = recv(socketFD, recBuff, 64, 0); if(recChars < 64) { if(recChars == 0) printf("Feedback error (Iteration %d) : rcv failed\n", i); else printf("Feedback error (Iteration %d) : did not rcv all of the buffer\n", i); return -1; } checksumTotal = extendedChecksum16(recBuff, 64); if( (uint8)((checksumTotal / 256) & 0xff) != recBuff[5]) { printf("Feedback error (Iteration %d) : read buffer has bad checksum16(MSB)\n", i); return -1; } if( (uint8)(checksumTotal & 255) != recBuff[4]) { printf("Feedback error (Iteration %d) : read buffer has bad checksum16(LBS)\n", i); return -1; } if( extendedChecksum8(recBuff) != recBuff[0]) { printf("Feedback error (Iteration %d) : read buffer has bad checksum8\n", i); return -1; } if(recBuff[1] != (uint8)(0xF8) || recBuff[2] != (uint8)(0x1D) || recBuff[3] != (uint8)(0x00)) { printf("Feedback error (Iteration %d) : read buffer has wrong command bytes\n", i); return -1; } for(j = 0; j < numChannels && j < 16; j++) binaryToCalibratedAnalogVoltage(caliInfo, 0x00, ainResolution, recBuff[12 + j*2] + recBuff[13 + j*2]*256, &valueAIN[j]); valueDIPort = recBuff[7] + recBuff[9]*256 + (recBuff[10] & 15)*65536 + (recBuff[11] & 7)*1048576; if(initialize == 1) { //unsetting digital IO bit masks since we only want to read states now sendBuff[6] = 0; //FIOMask sendBuff[9] = 0; //EIOMask sendBuff[12] = 0; //CIOMask sendBuff[14] = 0; //MIOMask //turning off Update bit of DACs sendBuff[17] = sendBuff[17] - 64; //high bits of DAC0 sendBuff[19] = sendBuff[19] - 64; //high bits of DAC1 extendedChecksum(sendBuff, 34); initialize = 0; } } time = getTickCount() - time; printf("Milleseconds per iteration = %.3f\n", (double)time / (double)numIterations); printf("\nDigital Input (FIO0-7, EIO0-7, CIO0-3, MIO0-2) = %d\n",valueDIPort); printf("\nAIN readings from last iteration:\n"); for( j = 0; j < numChannels; j++) printf("%.6f\n", valueAIN[j]); return 0; }
int process(VideoCapture& capture) { long captureTime; cout << "Press q or escape to quit!" << endl; CvFont infoFont; cvInitFont(&infoFont, CV_FONT_HERSHEY_SIMPLEX, 1, 1); namedWindow(VIDEO_WINDOW_NAME, CV_WINDOW_AUTOSIZE); namedWindow(ERODE_PREVIEW_WIN_NAME, CV_WINDOW_NORMAL); resizeWindow(ERODE_PREVIEW_WIN_NAME, 320, 240); ControlsWindow* controlsWindow = new ControlsWindow(); if(fileExists(preferenceFileName)) { loadSettings(controlsWindow, (char*)preferenceFileName); } Mat frame; while (true) { capture >> frame; captureTime = (int)(getTickCount()/getTickFrequency())*1000; if (frame.empty()) break; int target_width = 320; int height = (target_width/capture.get(3 /*width*/)) * capture.get(4 /*height*/); resize(frame, frame, Size(target_width, height)); if (controlsWindow->getBlurDeviation() > 0) { GaussianBlur(frame, frame, Size(GAUSSIAN_KERNEL, GAUSSIAN_KERNEL), controlsWindow->getBlurDeviation()); } //Apply brightness and contrast frame.convertTo(frame, -1, controlsWindow->getContrast(), controlsWindow->getBrightness()); Mat maskedImage = thresholdImage(controlsWindow, frame); Mat erodedImage = erodeDilate(maskedImage, controlsWindow); Mat erodedImageBinary; cvtColor(erodedImage, erodedImageBinary, COLOR_BGR2GRAY); threshold(erodedImageBinary, erodedImageBinary, 0, 255, CV_THRESH_BINARY); if(controlsWindow->getInvert()) { erodedImageBinary = 255 - erodedImageBinary; } cv::SimpleBlobDetector::Params params; params.minDistBetweenBlobs = 50.0f; params.filterByInertia = false; params.filterByConvexity = false; params.filterByColor = true; params.filterByCircularity = false; params.filterByArea = true; params.minArea = 1000.0f; params.maxArea = 100000.0f; params.blobColor = 255; vector<KeyPoint> centers; vector<vector<Point>> contours; ModBlobDetector* blobDetector = new ModBlobDetector(params); vector<vector<Point>> contourHulls; vector<RotatedRect> contourRects; blobDetector->findBlobs(erodedImageBinary, erodedImageBinary, centers, contours); for(vector<Point> ctpts : contours) { vector<Point> hull; convexHull(ctpts, hull); contourHulls.push_back(hull); contourRects.push_back(minAreaRect(hull)); } #ifdef DEBUG_BLOBS drawContours(frame, contours, -1, Scalar(128,255,128), 2, CV_AA); drawContours(frame, contourHulls, -1, Scalar(255, 128,0), 2, CV_AA); int ptnum; for(KeyPoint pt : centers) { Scalar color(255, 0, 255); circle(frame, pt.pt, 5 , color, -1 /*filled*/, CV_AA); circle(frame, pt.pt, pt.size, color, 1, CV_AA); ptnum++; } #endif for(RotatedRect rr : contourRects) { Point2f points[4]; rr.points(points); float side1 = distance(points[0], points[1]); float side2 = distance(points[1], points[2]); float shortestSide = min(side1, side2); float longestSide = max(side1, side2); float aspectRatio = longestSide/shortestSide; int b = 0; bool isTape = objInfo.aspectRatio == 0 ? false : abs(objInfo.aspectRatio - aspectRatio) < 0.2*objInfo.aspectRatio; /* * TODO * Make a list of possible tape candidates * Use tape candidate with smallest difference in ratio to the real ratio as the tape */ if(isTape) { b = 255; string widthText = "Width (px): "; widthText.append(toString(longestSide)); string heightText = "Height (px): "; heightText.append(toString(shortestSide)); string rotText = "Rotation (deg): "; rotText.append(toString(abs((int)rr.angle))); string distText; if(camSettings.focalLength == -1) { distText = "Focal length not defined"; } else { float dist = objInfo.width * camSettings.focalLength / longestSide; distText = "Distance (cm): "; distText.append(toString(dist)); } putText(frame, widthText, Point(0, 20), CV_FONT_HERSHEY_SIMPLEX, 0.5f, Scalar(0, 255, 255)); putText(frame, heightText, Point(0, 40), CV_FONT_HERSHEY_SIMPLEX, 0.5f, Scalar(0, 255, 255)); putText(frame, rotText, Point(0, 60), CV_FONT_HERSHEY_SIMPLEX, 0.5f, Scalar(0, 255, 255)); putText(frame, distText, Point(0, 80), CV_FONT_HERSHEY_SIMPLEX, 0.5f, Scalar(0, 255, 255)); } rotated_rect(frame, rr, Scalar(b, 0, 255)); if(isTape)break; } if(objInfo.aspectRatio == 0) { putText(frame, "Invalid object info (object.xml)", Point(0, 20), CV_FONT_HERSHEY_SIMPLEX, 0.5f, Scalar(0, 255, 255)); } delete blobDetector; imshow(ERODE_PREVIEW_WIN_NAME, erodedImageBinary); imshow(VIDEO_WINDOW_NAME, frame); //int waitTime = max((int)(((1.0/framerate)*1000) // - ((int)(getTickCount()/getTickFrequency())*1000 - captureTime)) // , 1); char key = (char)waitKey(1); switch (key) { case 'q': case 'Q': case 27: //escape saveSettings(controlsWindow, (char*)preferenceFileName); return 0; default: break; } std::this_thread::yield(); } saveSettings(controlsWindow, (char*)preferenceFileName); delete(controlsWindow); destroyAllWindows(); return 0; }
//Reads the StreamData low-level function response in a loop. All voltages from //the stream are stored in the voltages 2D array. int StreamData_example(HANDLE hDevice, ue9CalibrationInfo *caliInfo) { uint16 voltageBytes, checksumTotal; int recChars, backLog, overflow; int i, j, k, m, packetCounter, currChannel, scanNumber; int totalPackets; //The total number of StreamData responses read int numDisplay; //Number of times to display streaming information int numReadsPerDisplay; //Number of packets to read before displaying streaming information int readSizeMultiplier; //Multiplier for the StreamData receive buffer size long startTime, endTime; packetCounter = 0; currChannel = 0; scanNumber = 0; totalPackets = 0; recChars = 0; numDisplay = 6; numReadsPerDisplay = 3; readSizeMultiplier = 10; /* Each StreamData response contains (16/NumChannels) * readSizeMultiplier * samples for each channel. * Total number of scans = (16 / NumChannels) * 4 * readSizeMultiplier * numReadsPerDisplay * numDisplay */ double voltages[(16/NumChannels)*4*readSizeMultiplier*numReadsPerDisplay*numDisplay][NumChannels]; uint8 recBuff[192*readSizeMultiplier]; printf("Reading Samples...\n"); startTime = getTickCount(); for( i = 0; i < numDisplay; i++ ) { for( j = 0; j < numReadsPerDisplay; j++ ) { /* For USB StreamData, use Endpoint 2 for reads and 192 byte packets * instead of the 46. The 192 byte response is 4 StreamData packet * responses of 48 bytes. * You can read the multiple StreamData responses of 192 bytes to help * improve streaming performance. In this example this multiple is * adjusted by the readSizeMultiplier variable. */ //Reading response from UE9 recChars = LJUSB_Stream(hDevice, recBuff, 192*readSizeMultiplier); if( recChars < 192*readSizeMultiplier ) { if( recChars == 0 ) printf("Error : read failed (StreamData).\n"); else printf("Error : did not read all of the buffer %d (StreamData).\n", recChars); return -1; } overflow = 0; //Checking for errors and getting data out of each StreamData response for( m = 0; m < 4*readSizeMultiplier; m++ ) { totalPackets++; checksumTotal = extendedChecksum16(recBuff + m*48, 46); if( (uint8)((checksumTotal >> 8) & 0xff) != recBuff[m*48 + 5] ) { printf("Error : read buffer has bad checksum16(MSB) (StreamData).\n"); return -1; } if( (uint8)(checksumTotal & 0xff) != recBuff[m*48 + 4] ) { printf("Error : read buffer has bad checksum16(LSB) (StreamData).\n"); return -1; } checksumTotal = extendedChecksum8(recBuff + m*48); if( checksumTotal != recBuff[m*48] ) { printf("Error : read buffer has bad checksum8 (StreamData).\n"); return -1; } if( recBuff[m*48 + 1] != (uint8)(0xF9) || recBuff[m*48 + 2] != (uint8)(0x14) || recBuff[m*48 + 3] != (uint8)(0xC0) ) { printf("Error : read buffer has wrong command bytes (StreamData).\n"); return -1; } if( recBuff[m*48 + 11] != 0 ) { printf("Errorcode # %d from StreamData read.\n", (unsigned int)recBuff[11]); return -1; } if( packetCounter != (int) recBuff[m*48 + 10] ) { printf("PacketCounter does not match with with current packet count (StreamData).\n"); return -1; } backLog = recBuff[m*48 + 45]&0x7F; //Checking MSB for Comm buffer overflow if( (recBuff[m*48 + 45] & 128) == 128 ) { printf("\nComm buffer overflow detected in packet %d\n", totalPackets); printf("Current Comm backlog: %d\n", recBuff[m*48 + 45]&0x7F); overflow = 1; } for( k = 12; k < 43; k += 2 ) { voltageBytes = (uint16)recBuff[m*48 + k] + (uint16)recBuff[m*48 + k+1]*256; getAinVoltCalibrated(caliInfo, (uint8)(0x00), ainResolution, voltageBytes, &(voltages[scanNumber][currChannel])); currChannel++; if( currChannel > 3 ) { currChannel = 0; scanNumber++; } } if( packetCounter >= 255 ) packetCounter = 0; else packetCounter++; //Handle Comm buffer overflow by stopping, flushing and restarting stream if( overflow == 1 ) { printf("\nRestarting stream...\n"); doFlush(hDevice); if( StreamConfig_example(hDevice) != 0 ) { printf("Error restarting StreamConfig.\n"); return -1; } if( StreamStart(hDevice) != 0 ) { printf("Error restarting StreamStart.\n"); return -1; } packetCounter = 0; break; } } } printf("\nNumber of scans: %d\n", scanNumber); printf("Total packets read: %d\n", totalPackets); printf("Current PacketCounter: %d\n", ((packetCounter == 0) ? 255 : packetCounter-1)); printf("Current Comm backlog: %d\n", backLog); for( k = 0; k < 4; k++ ) printf(" AI%d: %.4f V\n", k, voltages[scanNumber - 1][k]); } endTime = getTickCount(); printf("\nRate of samples: %.0lf samples per second\n", (scanNumber*NumChannels)/((endTime - startTime)/1000.0)); printf("Rate of scans: %.0lf scans per second\n\n", scanNumber/((endTime - startTime)/1000.0)); return 0; }
double IMCSEngine::getTimingResult(const double tStart) { return (((double)getTickCount() - tStart) / getTickFrequency()) * 1000; //Calculate execution time in milliseconds relative to tStart }
vector<Mat_<double> > FernCascade::Train(const vector<Mat_<uchar> >& images, const vector<Mat_<double> >& current_shapes, const vector<Mat_<double> >& ground_truth_shapes, const vector<BoundingBox> & bounding_box, const Mat_<double>& mean_shape, int second_level_num, int candidate_pixel_num, int fern_pixel_num){ Mat_<double> candidate_pixel_locations(candidate_pixel_num,2); Mat_<int> nearest_landmark_index(candidate_pixel_num,1); vector<Mat_<double> > regression_targets; RNG random_generator(getTickCount()); second_level_num_ = second_level_num; // calculate regression targets: the difference between ground truth shapes and current shapes // candidate_pixel_locations: the locations of candidate pixels, indexed relative to its nearest landmark on mean shape regression_targets.resize(current_shapes.size()); for(int i = 0;i < current_shapes.size();i++){ regression_targets[i] = ProjectShape(ground_truth_shapes[i],bounding_box[i]) - ProjectShape(current_shapes[i],bounding_box[i]); Mat_<double> rotation; double scale; SimilarityTransform(mean_shape,ProjectShape(current_shapes[i],bounding_box[i]),rotation,scale); transpose(rotation,rotation); regression_targets[i] = scale * regression_targets[i] * rotation; } // get candidate pixel locations, please refer to 'shape-indexed features' for(int i = 0;i < candidate_pixel_num;i++){ double x = random_generator.uniform(-1.0,1.0); double y = random_generator.uniform(-1.0,1.0); if(x*x + y*y > 1.0){ i--; continue; } // find nearest landmark index double min_dist = 1e10; int min_index = 0; for(int j = 0;j < mean_shape.rows;j++){ double temp = pow(mean_shape(j,0)-x,2.0) + pow(mean_shape(j,1)-y,2.0); if(temp < min_dist){ min_dist = temp; min_index = j; } } candidate_pixel_locations(i,0) = x - mean_shape(min_index,0); candidate_pixel_locations(i,1) = y - mean_shape(min_index,1); nearest_landmark_index(i) = min_index; } // get densities of candidate pixels for each image // for densities: each row is the pixel densities at each candidate pixels for an image // Mat_<double> densities(images.size(), candidate_pixel_num); vector<vector<double> > densities; densities.resize(candidate_pixel_num); for(int i = 0;i < images.size();i++){ Mat_<double> rotation; double scale; Mat_<double> temp = ProjectShape(current_shapes[i],bounding_box[i]); SimilarityTransform(temp,mean_shape,rotation,scale); for(int j = 0;j < candidate_pixel_num;j++){ double project_x = rotation(0,0) * candidate_pixel_locations(j,0) + rotation(0,1) * candidate_pixel_locations(j,1); double project_y = rotation(1,0) * candidate_pixel_locations(j,0) + rotation(1,1) * candidate_pixel_locations(j,1); project_x = scale * project_x * bounding_box[i].width / 2.0; project_y = scale * project_y * bounding_box[i].height / 2.0; int index = nearest_landmark_index(j); int real_x = project_x + current_shapes[i](index,0); int real_y = project_y + current_shapes[i](index,1); real_x = std::max(0.0,std::min((double)real_x,images[i].cols-1.0)); real_y = std::max(0.0,std::min((double)real_y,images[i].rows-1.0)); densities[j].push_back((int)images[i](real_y,real_x)); } } // calculate the covariance between densities at each candidate pixels Mat_<double> covariance(candidate_pixel_num,candidate_pixel_num); Mat_<double> mean; for(int i = 0;i < candidate_pixel_num;i++){ for(int j = i;j< candidate_pixel_num;j++){ double correlation_result = calculate_covariance(densities[i],densities[j]); covariance(i,j) = correlation_result; covariance(j,i) = correlation_result; } } // train ferns vector<Mat_<double> > prediction; prediction.resize(regression_targets.size()); for(int i = 0;i < regression_targets.size();i++){ prediction[i] = Mat::zeros(mean_shape.rows,2,CV_64FC1); } ferns_.resize(second_level_num); for(int i = 0;i < second_level_num;i++){ cout<<"Training ferns: "<<i+1<<" out of "<<second_level_num<<endl; vector<Mat_<double> > temp = ferns_[i].Train(densities,covariance,candidate_pixel_locations,nearest_landmark_index,regression_targets,fern_pixel_num); // update regression targets for(int j = 0;j < temp.size();j++){ prediction[j] = prediction[j] + temp[j]; regression_targets[j] = regression_targets[j] - temp[j]; } } for(int i = 0;i < prediction.size();i++){ Mat_<double> rotation; double scale; SimilarityTransform(ProjectShape(current_shapes[i],bounding_box[i]),mean_shape,rotation,scale); transpose(rotation,rotation); prediction[i] = scale * prediction[i] * rotation; } return prediction; }
//------------------------------------------------------------------------------ tOplkError timeru_setTimer(tTimerHdl* pTimerHdl_p, ULONG timeInMs_p, tTimerArg argument_p) { tTimerEntry* pNewEntry; tTimerEntry** ppEntry; // check pointer to handle if (pTimerHdl_p == NULL) return kErrorTimerInvalidHandle; // fetch entry from free timer list enterCriticalSection(TIMERU_FREE_LIST); pNewEntry = timeruInstance_l.pFreeListFirst; if (pNewEntry != NULL) { timeruInstance_l.pFreeListFirst = pNewEntry->pNext; timeruInstance_l.freeEntries--; if (timeruInstance_l.minFreeEntries > timeruInstance_l.freeEntries) { timeruInstance_l.minFreeEntries = timeruInstance_l.freeEntries; } } leaveCriticalSection(TIMERU_FREE_LIST); if (pNewEntry == NULL) { // sorry, no free entry return kErrorTimerNoTimerCreated; } *pTimerHdl_p = (tTimerHdl)pNewEntry; OPLK_MEMCPY(&pNewEntry->timerArg, &argument_p, sizeof(tTimerArg)); // insert timer entry in timer list enterCriticalSection(TIMERU_TIMER_LIST); // calculate timeout based on start time pNewEntry->timeoutInMs = (getTickCount() - timeruInstance_l.startTimeInMs) + timeInMs_p; ppEntry = &timeruInstance_l.pTimerListFirst; while (*ppEntry != NULL) { if ((*ppEntry)->timeoutInMs > pNewEntry->timeoutInMs) { (*ppEntry)->timeoutInMs -= pNewEntry->timeoutInMs; break; } pNewEntry->timeoutInMs -= (*ppEntry)->timeoutInMs; ppEntry = &(*ppEntry)->pNext; } // insert before **ppEntry pNewEntry->pNext = *ppEntry; *ppEntry = pNewEntry; leaveCriticalSection(TIMERU_TIMER_LIST); #if (TARGET_SYSTEM == _WIN32_ || TARGET_SYSTEM == _WINCE_ ) if (ppEntry == &timeruInstance_l.pTimerListFirst) { SetEvent(timeruInstance_l.ahEvents[TIMERU_EVENT_WAKEUP]); } #endif return kErrorOk; }
// iterative deepening int SimplePVSearch::idSearch(Board& board) { int bestScore = -maxScore; int iterationScore[maxSearchPly]; int64_t iterationTime[maxSearchPly]; board.setInCheck(board.getSideToMove()); MoveIterator::Move easyMove; rootSearchInfo.alpha = -maxScore; rootSearchInfo.beta = maxScore; rootSearchInfo.allowNullMove = false; int lastDepth=0; MoveIterator::Move bestMove; MoveIterator::Move ponderMove; board.generateAllMoves(rootMoves, board.getSideToMove()); filterLegalMoves(board,rootMoves,agent->getSearchMoves()); scoreRootMoves(board,rootMoves); rootMoves.sort(); setCompletedIteration(false); if (rootMoves.get(0).score > rootMoves.get(1).score + easyMargin ) { easyMove=rootMoves.get(0); } PvLine pv = PvLine(); pv.index=0; for (int depth = 1; depth <= depthToSearch; depth++) { int score = 0; int aspirationDelta=0; maxPlySearched = 0; lastDepth=depth; iterationPVChange[depth]=0; rootSearchInfo.depth=depth; rootSearchInfo.ply=0; score=-maxScore; if (depth >= aspirationDepth) { const int delta = iterationScore[depth-1]-iterationScore[depth-2]; aspirationDelta = std::max(delta, 10) + 5; rootSearchInfo.alpha = std::max(iterationScore[depth-1]-aspirationDelta,-maxScore); rootSearchInfo.beta = std::min(iterationScore[depth-1]+aspirationDelta,+maxScore); } bool finished=false; setCompletedIteration(false); rootMoves.clearScore(); while (!finished) { rootMoves.sort(); rootMoves.first(); score = rootSearch(board, rootSearchInfo, pv); rootMoves.sort(); rootMoves.first(); if (!stop(rootSearchInfo)) { setCompletedIteration(true); } iterationScore[depth]=score; updateHashWithPv(board,pv); if (!stop(rootSearchInfo)) { uciOutput(pv, score, getTickCount()-startTime, agent->hashFull(), rootSearchInfo.depth, maxPlySearched, rootSearchInfo.alpha, rootSearchInfo.beta); } const bool fail = score <= rootSearchInfo.alpha || score >= rootSearchInfo.beta; const bool fullWidth = rootSearchInfo.alpha == -maxScore && rootSearchInfo.beta == maxScore; finished = !fail || fullWidth || depth < aspirationDepth || (stop(rootSearchInfo) && depth>1); if (!finished) { rootSearchInfo.alpha = std::max(rootSearchInfo.alpha-aspirationDelta,-maxScore); rootSearchInfo.beta = std::min(rootSearchInfo.beta+aspirationDelta,+maxScore); aspirationDelta = std::max(aspirationDelta*130/100,15); } } if (isCompletedIteration()) { bestMove=pv.moves[0]; ponderMove=pv.moves[1]; if (score > bestScore) { bestScore = score; } } iterationTime[depth]=getTickCount()-startTime; if (stop(rootSearchInfo) && depth > 1) { break; } int repetition=0; for (int x=depth-1;x>=1;x--) { if (score==iterationScore[x]) { repetition++; } else { break; } } if (!(searchFixedDepth || infinite)) { if (depth>5 && (abs(iterationScore[depth]) >= maxScore-maxSearchPly && abs(iterationScore[depth-1]) >= maxScore-maxSearchPly && abs(iterationScore[depth-2]) >= maxScore-maxSearchPly)) { break; } if (repetition >= maxScoreRepetition && iterationScore[depth-0] == drawScore) { break; } if (depth>12) { if (bestMove!=easyMove) { easyMove = MoveIterator::Move(); } } if (depth>10 && rootMoves.size()==1) { break; } if (depth>7) { if (!easyMove.none() && easyMove==bestMove && nodesPerMove[0]>=nodes*85/100 && iterationTime[depth] > timeToSearch/2) { break; } } if (depth > 8 && depth < 40 && iterationPVChange[depth]>0) { agent->addExtraTime(depth,iterationPVChange); } if (iterationTime[depth] > getTimeToSearch()*70/100) { break; } } } if (bestMove.none()) { bestMove=rootMoves.get(0); ponderMove=emptyMove; } uciOutput(pv, bestMove.score, getTickCount()-startTime, agent->hashFull(), lastDepth, maxPlySearched, rootSearchInfo.alpha, rootSearchInfo.beta); uciOutput(bestMove,ponderMove); return bestMove.score; }
DWORD WINAPI netGameLoop(void *param) { int len, c, x, y; Psquare p; TnetGameSettings *b; char *m, *u, *a; hostent *h; char buf[256]; if(!param){ buf[0]=(BYTE)C_INIT1; buf[1]=NETGAME_VERSION; wr(buf, 2); c=rd1(); if(c!=C_INIT2) goto le; c=rd1(); if(c<1) goto le; netGameVersion=c; SetForegroundWindow(hWin); h= gethostbyaddr((char*)&netGameIP.sin_addr, 4, netGameIP.sin_family); a= inet_ntoa(netGameIP.sin_addr); if(msg1(MB_YESNO|MB_ICONQUESTION, lng(868, "Do you want to play with %s [%s] ?"), (h && h->h_name) ? h->h_name : "???", a ? a : "???")!=IDYES){ wr1(C_INIT_DENY); goto le; } buf[0]=(BYTE)C_INFO; buf[1]=sizeof(TnetGameSettings); b= (TnetGameSettings*)(buf+2); b->width=(char)width; b->height=(char)height; b->begin= (player==1); b->rule5=(char)ruleFive; b->cont=(char)continuous; if(netGameVersion<2) b->rule5=b->cont=0; wr(buf, 2+sizeof(TnetGameSettings)); if(rd1()!=C_INFO_OK) goto le; b->begin= !b->begin; } else{ buf[0]=(BYTE)C_INIT2; buf[1]=NETGAME_VERSION; wr(buf, 2); c=rd1(); if(c==C_BUSY) wrLog(lng(874, "The other player is already playing with someone else")); if(c!=C_INIT1) goto le; c=rd1(); if(c<1) goto le; netGameVersion=c; wrLog(lng(871, "Connection established. Waiting for response...")); c=rd1(); if(c==C_INIT_DENY) wrLog(lng(870, "The other player doesn't want to play with you !")); if(c!=C_INFO) goto le; len=rd1(); b= (TnetGameSettings*)buf; memset(buf, 0, sizeof(TnetGameSettings)); if(len<2 || rd(buf, len)<0) goto le; wr1(C_INFO_OK); } SendMessage(hWin, WM_COMMAND, 992, (LPARAM)b); undoRequest=0; for(;;){ x=rd1(); switch(x){ case C_MSG: //message len=rd2(); if(len<=0) goto le; u=new char[2*len]; if(rd(u, 2*len)>=0){ m=new char[len+1]; WideCharToMultiByte(CP_ACP, 0, (WCHAR*)u, len, m, len, 0, 0); m[len]='\0'; wrLog("---> %s", m); delete[] m; SetWindowPos(logDlg, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_ASYNCWINDOWPOS|SWP_NOACTIVATE|SWP_SHOWWINDOW); } delete[] u; break; case C_UNDO_REQUEST: y=rd2(); if(y<=0 || y>moves) goto le; if(undoRequest){ //both players pressed undo simultaneously if(undoRequest<0) undoRequest=y; amax(undoRequest, y); postUndo(); } else{ c=msg1(MB_YESNO|MB_ICONQUESTION, lng(876, "The other player wants to UNDO the last move.\r\nDo you agree ?")); if(c==IDYES){ undoRequest=y; wr1(C_UNDO_YES); postUndo(); } else{ wr1(C_UNDO_NO); } } break; case C_NEW_REQUEST: if(undoRequest){ if(undoRequest<0){ //both players pressed NewGame simultaneously postNewGame(); } else{ postUndo(); } } else{ c=msg1(MB_YESNO|MB_ICONQUESTION, lng(877, "The other player wants to start a NEW game.\r\nDo you agree ?")); if(c==IDYES){ undoRequest=-1; wr1(C_NEW_YES); postNewGame(); } else{ wr1(C_NEW_NO); } } break; case C_UNDO_YES: if(undoRequest<=0) goto le; postUndo(); break; case C_UNDO_NO: if(undoRequest>0){ msglng(878, "Sorry, the other player does not allow to UNDO your move."); undoRequest=0; } break; case C_NEW_YES: if(undoRequest>=0) goto le; postNewGame(); break; case C_NEW_NO: if(undoRequest<0){ msglng(879, "Sorry, the other player does not allow to start a NEW game."); undoRequest=0; } break; default: //move or error if(x<0 || x>=width){ show(logDlg); goto le; } while(finished && (getTickCount()-lastTick<5000 || saveLock)){ Sleep(200); } if(finished) SendMessage(hWin, WM_COMMAND, 101, 0); y=rd1(); if(y<0 || y>=height) goto le; p=Square(x, y); p->time=rd4(); PostMessage(hWin, WM_COMMAND, 991, (LPARAM)p); } } le: EnterCriticalSection(&netLock); netGameDone(); LeaveCriticalSection(&netLock); return 0; }
bool ObjPatchMatcher::Match(const Mat& cimg, const Mat& dmap_raw, Mat& mask_map) { /* * precompute feature maps */ // gradient Mat gray_img, gray_img_float, edge_map; cvtColor(cimg, gray_img, CV_BGR2GRAY); gray_img.convertTo(gray_img_float, CV_32F, 1.f/255); Canny(gray_img, edge_map, 10, 50); cv::imshow("edge", edge_map); cv::imshow("color", cimg); cv::waitKey(10); Mat grad_x, grad_y, grad_mag; Sobel(gray_img_float, grad_x, CV_32F, 1, 0); Sobel(gray_img_float, grad_y, CV_32F, 0, 1); magnitude(grad_x, grad_y, grad_mag); // depth Mat dmap_float, pts3d, normal_map; if( use_depth ) { Feature3D feat3d; dmap_raw.convertTo(dmap_float, CV_32F); Mat cmp_mask; compare(dmap_float, 800, cmp_mask, CMP_LT); dmap_float.setTo(800, cmp_mask); compare(dmap_float, 7000, cmp_mask, CMP_GT); dmap_float.setTo(7000, cmp_mask); dmap_float = (dmap_float-800)/(7000-800); feat3d.ComputeKinect3DMap(dmap_float, pts3d, false); feat3d.ComputeNormalMap(pts3d, normal_map); } /* * start searching */ // init searcher //searcher.Build(patch_data, BruteForce_L2); // opencv bfmatcher has size limit: maximum 2^31 LSHCoder lsh_coder; if(use_code) { lsh_coder.Load(); } Mat score_map = Mat::zeros(edge_map.rows, edge_map.cols, CV_32F); Mat mask_vote_map = Mat::zeros(cimg.rows, cimg.cols, CV_32F); mask_map = Mat::zeros(cimg.rows, cimg.cols, CV_32F); Mat mask_count = Mat::zeros(cimg.rows, cimg.cols, CV_32S); // number of mask overlapped on each pixel Mat feat; int topK = 40; int total_cnt = countNonZero(edge_map); vector<VisualObject> query_patches; query_patches.reserve(total_cnt); cout<<"Start match..."<<endl; float max_dist = 0; int cnt = 0; char str[30]; double start_t = getTickCount(); //#pragma omp parallel for for(int r=patch_size.height/2; r<gray_img.rows-patch_size.height/2; r+=3) { for(int c=patch_size.width/2; c<gray_img.cols-patch_size.width/2; c+=3) { /*int rand_r = rand()%gray_img.rows; int rand_c = rand()%gray_img.cols; if(rand_r < patch_size.height/2 || rand_r > gray_img.rows-patch_size.height/2 || rand_c < patch_size.width/2 || rand_c > gray_img.cols-patch_size.width/2) continue;*/ int rand_r = r, rand_c = c; if(edge_map.at<uchar>(rand_r, rand_c) > 0) { cnt++; destroyAllWindows(); Rect box(rand_c-patch_size.width/2, rand_r-patch_size.height/2, patch_size.width, patch_size.height); MatFeatureSet featset; gray_img_float(box).copyTo(featset["gray"]); //grad_mag(box).copyTo(featset["gradient"]); if(use_depth) { normal_map(box).copyTo(featset["normal"]); dmap_float(box).copyTo(featset["depth"]); } ComputePatchFeat(featset, feat); vector<DMatch> matches; if(use_code) { BinaryCodes codes; HashKey key_val; lsh_coder.ComputeCodes(feat, codes); HashingTools<HashKeyType>::CodesToKey(codes, key_val); MatchCode(key_val, topK, matches); } else { MatchPatch(feat, topK, matches); } if(matches[0].distance < 0 || matches[0].distance > 1000) { cout<<"match dist: "<<matches[0].distance<<endl; double minv, maxv; cout<<norm(feat, patch_data.row(matches[0].trainIdx), NORM_L2)<<endl; minMaxLoc(feat, &minv, &maxv); cout<<minv<<" "<<maxv<<endl; cout<<feat<<endl<<endl; minMaxLoc(patch_data.row(matches[0].trainIdx), &minv, &maxv); cout<<minv<<" "<<maxv<<endl; cout<<patch_data.row(matches[0].trainIdx)<<endl; imshow("cimg", cimg); waitKey(0); } vector<vector<Mat>> pixel_mask_vals(patch_size.height, vector<Mat>(patch_size.width, Mat::zeros(1, topK, CV_32F))); VisualObject cur_query; cur_query.visual_data.bbox = box; cur_query.visual_data.mask = Mat::zeros(patch_size.height, patch_size.width, CV_32F); for(size_t i=0; i<topK; i++) { score_map.at<float>(rand_r,rand_c) += matches[i].distance; cur_query.visual_data.mask += patch_meta.objects[matches[i].trainIdx].visual_data.mask; for(int mr=0; mr<patch_size.height; mr++) for(int mc=0; mc<patch_size.width; mc++) { pixel_mask_vals[mr][mc].at<float>(i) = patch_meta.objects[matches[i].trainIdx].visual_data.mask.at<float>(mr, mc); } } score_map.at<float>(rand_r,rand_c) /= topK; cur_query.visual_data.mask /= topK; // average returned mask // compute mask quality Scalar mean_, std_; /*ofstream out("pixel_mask_std_100.txt", ios::app); for(int mr=0; mr<patch_size.height; mr++) for(int mc=0; mc<patch_size.width; mc++) { meanStdDev(pixel_mask_vals[mr][mc], mean_, std_); out<<std_.val[0]<<" "; } out<<endl;*/ meanStdDev(cur_query.visual_data.mask, mean_, std_); cur_query.visual_data.scores.push_back(mean_.val[0]); cur_query.visual_data.scores.push_back(std_.val[0]); Mat align_mask = Mat::zeros(cimg.rows, cimg.cols, CV_8U); int gt_mask_id = patch_meta.objects[matches[0].trainIdx].meta_data.category_id; if(gt_mask_id != -1) { Mat nn_mask = gt_obj_masks[gt_mask_id]; //imshow("gt mask", nn_mask*255); //waitKey(10); Rect gt_box = patch_meta.objects[matches[0].trainIdx].visual_data.bbox; Rect align_box = AlignBox(box, gt_box, cimg.cols, cimg.rows); vector<ImgWin> boxes; boxes.push_back(align_box); //ImgVisualizer::DrawWinsOnImg("alignbox", cimg, boxes); //waitKey(10); Rect target_box = Rect(box.x-(gt_box.x-align_box.x), box.y-(gt_box.y-align_box.y), align_box.width, align_box.height); cout<<target_box<<endl; nn_mask(align_box).copyTo(align_mask(target_box)); } align_mask.convertTo(align_mask, CV_32F); mask_map += align_mask * matches[0].distance; //*score_map.at<float>(r,c); //mask_count(box) = mask_count(box) + 1; //cout<<score_map.at<float>(r,c)<<endl; max_dist = MAX(max_dist, matches[0].distance); query_patches.push_back(cur_query); // vote object regions /*Point3f line_ori; int obj_pt_sign; ComputeDominantLine(cur_query.visual_desc.mask, box.tl(), line_ori, obj_pt_sign); for(int rr=0; rr<cimg.rows; rr++) for(int cc=0; cc<cimg.cols; cc++) { float line_val = line_ori.x*cc+line_ori.y*rr+line_ori.z; if((line_val>0?1:-1)==obj_pt_sign) mask_vote_map.at<float>(rr, cc)++; }*/ #ifdef VERBOSE // current patch Mat disp, patch_gray, patch_grad, patch_normal, patch_depth; disp = cimg.clone(); rectangle(disp, box, CV_RGB(255,0,0), 2); resize(gray_img(box), patch_gray, Size(50,50)); resize(grad_mag(box), patch_grad, Size(50,50)); Mat cur_mask; resize(cur_query.visual_desc.mask, cur_mask, Size(50,50)); if(use_depth) { resize(normal_map(box), patch_normal, Size(50,50)); normalize(dmap_float(box), patch_depth, 1, 0, NORM_MINMAX); patch_depth.convertTo(patch_depth, CV_8U, 255); //dmap_float(box).convertTo(patch_depth, CV_8U, 255); resize(patch_depth, patch_depth, Size(50,50)); } Mat onormal; sprintf_s(str, "query_gray_%d.jpg", cnt); imshow(str, patch_gray); imwrite(str, patch_gray); /*sprintf_s(str, "query_grad_%d.jpg", cnt); ImgVisualizer::DrawFloatImg(str, patch_grad, onormal, true); imwrite(str, onormal);*/ sprintf_s(str, "query_depth_%d.jpg", cnt); imshow(str, patch_depth); imwrite(str, patch_depth); sprintf_s(str, "query_normal_%d.jpg", cnt); ImgVisualizer::DrawNormals(str, patch_normal, onormal, true); imwrite(str, onormal); sprintf_s(str, "query_box_%d.jpg", cnt); imshow(str, disp); imwrite(str, disp); //imshow("align mask", align_mask*255); cur_mask.convertTo(cur_mask, CV_8U, 255); sprintf_s(str, "query_tmask_%d.jpg", cnt); imshow(str, cur_mask); imwrite(str, cur_mask); // show match results vector<Mat> res_imgs(topK); vector<Mat> res_gradients(topK); vector<Mat> res_normals(topK); vector<Mat> res_depth(topK); vector<Mat> db_boxes(topK); vector<Mat> res_masks(topK); for(size_t i=0; i<topK; i++) { VisualObject& cur_obj = patch_meta.objects[matches[i].trainIdx]; // mask cur_obj.visual_desc.mask.convertTo(res_masks[i], CV_8U, 255); // gray cur_obj.visual_desc.extra_features["gray"].convertTo(res_imgs[i], CV_8U, 255); // gradient //ImgVisualizer::DrawFloatImg("", cur_obj.visual_desc.extra_features["gradient"], res_gradients[i], false); // 3D if(use_depth) { // normal tools::ImgVisualizer::DrawNormals("", cur_obj.visual_desc.extra_features["normal"], res_normals[i]); // depth normalize(cur_obj.visual_desc.extra_features["depth"], res_depth[i], 1, 0, NORM_MINMAX); res_depth[i].convertTo(res_depth[i], CV_8U, 255); //cur_obj.visual_desc.extra_features["depth"].convertTo(res_depth[i], CV_8U, 255); } // box on image db_boxes[i] = imread(patch_meta.objects[matches[i].trainIdx].imgpath); resize(db_boxes[i], db_boxes[i], Size(cimg.cols, cimg.rows)); rectangle(db_boxes[i], patch_meta.objects[matches[i].trainIdx].visual_desc.box, CV_RGB(255,0,0), 2); } Mat out_img; sprintf_s(str, "res_gray_%d.jpg", cnt); ImgVisualizer::DrawImgCollection(str, res_imgs, topK, Size(50,50), out_img); imwrite(str, out_img); sprintf_s(str, "res_normal_%d.jpg", cnt); ImgVisualizer::DrawImgCollection(str, res_normals, topK, Size(50,50), out_img); imwrite(str, out_img); sprintf_s(str, "res_depth_%d.jpg", cnt); ImgVisualizer::DrawImgCollection(str, res_depth, topK, Size(50,50), out_img); imwrite(str, out_img); /*sprintf_s(str, "res_gradient_%d.jpg", cnt); tools::ImgVisualizer::DrawImgCollection(str, res_gradients, topK, Size(50,50), out_img); imwrite(str, out_img);*/ sprintf_s(str, "res_mask_%d.jpg", cnt); tools::ImgVisualizer::DrawImgCollection(str, res_masks, topK, Size(50,50), out_img); imwrite(str, out_img); sprintf_s(str, "res_box_%d.jpg", cnt); tools::ImgVisualizer::DrawImgCollection(str, db_boxes, topK/2, Size(200, 200), out_img); imwrite(str, out_img); waitKey(0); #endif cout<<total_cnt--<<endl; } } } cout<<"match done. Time cost: "<<(getTickCount()-start_t)/getTickFrequency()<<"s."<<endl; //score_map(Rect(patch_size.width/2, patch_size.height/2, score_map.cols-patch_size.width/2, score_map.rows-patch_size.height/2)).copyTo(score_map); //score_map.setTo(max_dist, 255-edge_map); normalize(score_map, score_map, 1, 0, NORM_MINMAX); score_map = 1-score_map; //tools::ImgVisualizer::DrawFloatImg("bmap", score_map); mask_map /= max_dist; cout<<max_dist<<endl; normalize(mask_map, mask_map, 1, 0, NORM_MINMAX); //tools::ImgVisualizer::DrawFloatImg("maskmap", mask_map); //normalize(mask_vote_map, mask_vote_map, 1, 0, NORM_MINMAX); //ImgVisualizer::DrawFloatImg("vote map", mask_vote_map); //waitKey(0); return true; // pick top weighted points to see if they are inside objects // try graph-cut for region proposal // among all retrieved mask patch, select most discriminative one and do graph-cut sort(query_patches.begin(), query_patches.end(), [](const VisualObject& a, const VisualObject& b) { return a.visual_data.scores[1] > b.visual_data.scores[1]; }); for(size_t i=0; i<query_patches.size(); i++) { Mat disp_img = cimg.clone(); rectangle(disp_img, query_patches[i].visual_data.bbox, CV_RGB(255,0,0)); imshow("max std box", disp_img); Mat big_mask; resize(query_patches[i].visual_data.mask, big_mask, Size(50,50)); ImgVisualizer::DrawFloatImg("max std mask", big_mask); waitKey(0); // use mask to do graph-cut Mat fg_mask(cimg.rows, cimg.cols, CV_8U); fg_mask.setTo(cv::GC_PR_FGD); Mat th_mask; threshold(query_patches[i].visual_data.mask, th_mask, query_patches[i].visual_data.scores[0], 1, CV_THRESH_BINARY); th_mask.convertTo(th_mask, CV_8U); fg_mask(query_patches[i].visual_data.bbox).setTo(cv::GC_FGD, th_mask); th_mask = 1-th_mask; fg_mask(query_patches[i].visual_data.bbox).setTo(cv::GC_BGD, th_mask); cv::grabCut(cimg, fg_mask, Rect(0,0,1,1), Mat(), Mat(), 3, cv::GC_INIT_WITH_MASK); fg_mask = fg_mask & 1; disp_img.setTo(Vec3b(0,0,0)); cimg.copyTo(disp_img, fg_mask); cv::imshow("cut", disp_img); cv::waitKey(0); } float ths[] = {0.9f, 0.8f, 0.7f, 0.6f, 0.5f, 0.4f, 0.3f, 0.2f}; for(size_t i=0; i<8; i++) { Mat th_mask; threshold(mask_map, th_mask, ths[i], 1, CV_THRESH_BINARY); char str[30]; sprintf_s(str, "%f", ths[i]); ImgVisualizer::DrawFloatImg(str, th_mask); waitKey(0); } return true; }