void histEqualization(const Mat& input, Mat& output, Mat& T){ Mat input_tmp; Mat histVector; unsigned char* input_data; unsigned char* output_data; unsigned char* T_data; float* hist_data; Size inputSize; double time_count; // To check input data is float or uchar if(input.type() == CV_32FC(1)){ //printf("CV_32FC1 Dectect in histEqualization!!\n"); input.convertTo(input_tmp, CV_8UC(1), 255.0); } else if(input.type() == CV_8UC(1)){ //printf("CV_8UC1 Dectect in histEqualization!!\n"); input_tmp = input; } else{ printf("Error Type in histEqualization!!!\n"); exit(0); } time_count = (double)getTickCount(); T.create(1,256,CV_8UC(1)); T_data = (unsigned char*)T.data; output.create(input.size(), CV_8UC(1)); input_data = (unsigned char*)input_tmp.data; inputSize = input_tmp.size(); output_data = (unsigned char*)output.data; imageHist(input, histVector); // get the image hist hist_data = (float*)histVector.data; float accum = 0; // get the accumadata amount int height = inputSize.height; int width = inputSize.width; // To get the transform function for(int i=0; i<256; i++){ T_data[i] = (unsigned char)((255.0/(height*width))* (accum + hist_data[i]) + 0.5); accum += hist_data[i]; } // To write the output by T for(int i=0; i<inputSize.width*inputSize.height; i++){ output_data[i] = T_data[(int)input_data[i]]; } time_count = (double)getTickCount() - time_count; printf("histEqualization Total consume %gms\n", time_count*1000/getTickFrequency());// get processing time return; }
void StressLog::Initialize(unsigned facilities, unsigned level, unsigned maxBytesPerThread, unsigned maxBytesTotal, HANDLE hMod) { #if !defined(USE_PORTABLE_HELPERS) // @TODO: disabled because of assumption that hMod is a module base address in stress log code if (theLog.MaxSizePerThread != 0) { // guard ourself against multiple initialization. First init wins. return; } g_pStressLog = &theLog; theLog.pLock = new (nothrow) CrstStatic(); theLog.pLock->Init(CrstStressLog); if (maxBytesPerThread < STRESSLOG_CHUNK_SIZE) { maxBytesPerThread = STRESSLOG_CHUNK_SIZE; } theLog.MaxSizePerThread = maxBytesPerThread; if (maxBytesTotal < STRESSLOG_CHUNK_SIZE * 256) { maxBytesTotal = STRESSLOG_CHUNK_SIZE * 256; } theLog.MaxSizeTotal = maxBytesTotal; theLog.totalChunk = 0; theLog.facilitiesToLog = facilities | LF_ALWAYS; theLog.levelToLog = level; theLog.deadCount = 0; theLog.tickFrequency = getTickFrequency(); PalGetSystemTimeAsFileTime (&theLog.startTime); theLog.startTimeStamp = getTimeStamp(); theLog.moduleOffset = (size_t)hMod; // HMODULES are base addresses. #ifndef APP_LOCAL_RUNTIME StressLogChunk::s_LogChunkHeap = PalHeapCreate (0, STRESSLOG_CHUNK_SIZE * 128, 0); if (StressLogChunk::s_LogChunkHeap == NULL) #endif { StressLogChunk::s_LogChunkHeap = PalGetProcessHeap (); } _ASSERTE (StressLogChunk::s_LogChunkHeap); #endif // !defined(USE_PORTABLE_HELPERS) }
void imgWidgetDisplayBigView::receiveFormThread(QImage img2,QString recifilename){ qDebug()<<"currentfile == fileNam"<<currentfile<<recifilename; if(currentfile==recifilename){ img = img2; scaleFlag = 0;//重新加载图片,清零 double tim1 = static_cast<double>(getTickCount()); item->setPixmap(QPixmap::fromImage(img)); item->setPos((this->width()-img.width())/2.0,(this->height()-img.height())/2.0); tim1 = ((double)getTickCount()- tim1)/getTickFrequency(); //qDebug()<<"QPixmap::receiveFormThread()"<<tim1; //qDebug()<<""; qDebug()<<"222222currentfile == fileNam"<<currentfile<<recifilename; }else{ qDebug()<<"different img"; thread->exit(0); keepSameTimer->start(1000); } }
void unsharpFiltering(const Mat& input, const Mat& boxMask, float scale, Mat& output, Mat& scaledUnsharp, Mat& blurredInput){ Mat input_tmp; float* input_data; float* blur_data; float* scaled_data; float* output_data; Size inputSize; double t = (double)getTickCount(); // To check the input data type is float or uchar if(input.type() == CV_8UC(1)){ input.convertTo(input_tmp, CV_32FC(1), 1/255.0); } else if(input.type() == CV_32FC(1)){ input_tmp = input; } else{ printf("Error Type in unsharpFiltering!!!\n"); exit(0); } // blur the input spatialFiltering(input_tmp, boxMask, blurredInput); inputSize = input.size(); output.create(inputSize, CV_32FC(1)); scaledUnsharp.create(inputSize, CV_32FC(1)); input_data = (float*)input_tmp.data; blur_data = (float*)blurredInput.data; output_data = (float*)output.data; scaled_data = (float*)scaledUnsharp.data; // start doing unsharp for(int i=0; i<inputSize.width*inputSize.height; i++){ float buf; scaled_data[i] = scale * (input_data[i] - blur_data[i]);// get scaledUnsharp buf = input_data[i] + scaled_data[i]; if(buf > 255) // pix > 255 buf = 255; else if(buf < 0)// pix < 0 buf = 0; output_data[i] = buf; //write output data } t = ((double)getTickCount() - t)*1000/getTickFrequency(); printf("unsharpFiltering total consume %gms\n", t);// get the processing time return; }
bool IterativeSegmentor::Run(const Mat& cimg, const Mat& dmap, vector<SuperPixel>& sps) { double start_t = cv::getTickCount(); // oversegmentation int num = img_segmentor.DoSegmentation(cimg); imshow("input", cimg); //imshow("depth", dmap*255); imshow("seg", img_segmentor.m_segImg); waitKey(10); sps.clear(); sps = img_segmentor.superPixels; cur_seg_id = sps.size(); Mat adjMat; img_segmentor.ComputeAdjacencyMat(sps, adjMat); // add to collection for (size_t i=0; i<sps.size(); i++) { spProcessor.ExtractSegmentFeatures(sps[i], cimg, dmap, SP_COLOR); } cout<<"Initial superpixel num: "<<cur_seg_id<<endl; // create initial sp pairs sp_pairs.clear(); for (int r=0; r<adjMat.rows; r++) for(int c=r+1; c<adjMat.cols; c++) if(adjMat.at<uchar>(r,c) > 0) { float spdist = SegmentProcessor::ComputeSegmentDist(sps[r], sps[c]); sp_pairs[spdist] = Point(r, c); } // start merging process while(!sp_pairs.empty()) { DoMergeIteration(cimg, dmap, sps); } cout<<"Segmentation time: "<<(double)(getTickCount()-start_t) / getTickFrequency()<<"s."<<endl; return true; }
void powerlawTransform(const Mat& input, float r, Mat& output){ double C; double t; Mat tmp_input; Mat tmp_output; unsigned char max_val; unsigned char* input_data; unsigned char* output_data; // Check the Mat data type if(input.type() == CV_32FC1){ // if it is float //printf("CV_32FC1 Mat Dectect in powerlawTransform!!\n"); input.convertTo(tmp_input, CV_8UC1, 255.0); } else if(input.type() == CV_8UC1){ // if it is uchar //printf("CV_8UC1 Mat Dectect in powerlawTransform!!\n"); tmp_input = input; } else{ // if it is unknown type printf("Type Error in powerlawTransform!!!!\n"); exit(0); } Size inputSize = tmp_input.size(); // get input size tmp_output.create(inputSize, CV_8UC(1)); input_data = tmp_input.data; output_data = tmp_output.data; t = (double)getTickCount(); max_val = 0; // fine the max value for(int index=0; index<inputSize.height*inputSize.width; index++) if(input_data[index] > max_val) max_val = input_data[index]; C = 255.0/pow((double)max_val, (double)r); // calculate the constant C // do power operation for(int index=0; index<inputSize.height*inputSize.width; index++){ output_data[index] = (unsigned char)(C * pow((double)input_data[index], (double)r)+0.5); } t = (double)getTickCount() - t; printf("powerlawTransform Total consume %gms\n", t*1000/getTickFrequency());// get processing time tmp_output.convertTo(output, CV_32FC1, 1/255.0); //write output data return; }
////////////////////////////////////////////////////////////////////////////////// // ID: 102061149 Wang Fu-En // // For successfully compile this code you should include the following header // // <cstdlib> // // <stdlib.h> // // <stdio.h> // // <iostream> // // <math.h> // // <opencv2/highgui/highgui.hpp> // // <opencv2/core/core.hpp> // // All the function in the file will compute the processing time // // automatically to get the efficiency of the code, and functions will check // // whether the input data is valid or not; normally, input should be grayscale // // image. // ////////////////////////////////////////////////////////////////////////////////// void logTransform(const Mat& input, Mat &output){ double C; double t; unsigned char max_val; Mat tmp_input; Mat tmp_output; //The folowing is to check Mat type if(input.type() == CV_32FC1){ // if it is float type input.convertTo(tmp_input, CV_8UC1, 255.0); //printf("CV_32FC1 Mat Detect in logTransform\n"); } else if(input.type() == CV_8UC1){ // if it is uchar type //printf("CV_8UC1 Mat Detect in logTransform!!\n"); tmp_input = input; } else{ // if the type is unknown //printf("Type Error in logTransform!!!\n"); exit(0); } Size size_of_input = tmp_input.size(); //get input size unsigned char* data = tmp_input.data; t = (double)getTickCount(); max_val = 0; // find the max value to determin C for(int index=0; index < size_of_input.height * size_of_input.width; index++) if(data[index] > max_val) max_val = data[index]; C = 255.0/(log(1.0+max_val)); // calculate the C value tmp_output.create(size_of_input.height, size_of_input.width, CV_8UC(1)); unsigned char* out_data = tmp_output.data; // Do log operation to every point for(int index=0; index < size_of_input.height * size_of_input.width; index++){ out_data[index] = (unsigned char)(C * log(1.0 + data[index]) + 0.5); } t = (double)getTickCount() - t; printf("logTransform Total consume %gms\n", t*1000/getTickFrequency());// get processing time tmp_output.convertTo(output, CV_32FC1, 1/255.0); // write output data return; }
bool SparseRec2View::fmatrix() { //fmatrix double tt = (double)getTickCount(); Mat fmat = cv::findFundamentalMat(Mat(p1), Mat(p2), inliers); tt = (double)getTickCount() - tt; TagI("ransac time = %lf ms\n", tt/getTickFrequency()*1000.0); std::copy(fmat.begin<double>(), fmat.end<double>(), F); //draw int cnt=0; for(int i=0; i<(int)p1.size(); ++i) { if(!inliers[i]) continue; ++cnt; line(combined, p1[i], Point(p2[i].x+img1.cols,p2[i].y), CV_WHITE); } TagI("number of inliers = %d\n",cnt); inliersNum = cnt; return true; }
void laplacianFiltering(const Mat& input, const Mat& laplacianMask, float scale, Mat& output, Mat& scaledLaplacian){ Mat input_tmp,tmp; float* input_data; float* output_data; float* scaleLap_data; double t = (double)getTickCount(); Size inputSize; spatialFiltering(input, laplacianMask, scaledLaplacian); //get scaledLaplacian(haven't multiple scale) // To check input data is float or uchar if(input.type() == CV_8UC(1)){ input.convertTo(input_tmp, CV_32FC(1), 1/255.0); } else if(input.type() == CV_32FC(1)){ input_tmp = input; } else{ printf("Error Type in laplacianFiltering!!!\n"); exit(0); } inputSize = input.size(); output.create(inputSize, CV_32FC(1)); scaleLap_data = (float*)scaledLaplacian.data; input_data = (float*)input_tmp.data; output_data = (float*)output.data; // start doing laplace transform for(int p=0; p<inputSize.width*inputSize.height; p++){ float buf; scaleLap_data[p] = scaleLap_data[p]*scale;//multiple scale buf = input_data[p] + scaleLap_data[p]; // add to origin input if(buf>1) // if value > 1 buf = 1; else if(buf<0)// if value <0 buf = 0; output_data[p] = buf; } t = (double)getTickCount()-t; printf("Laplacian total consume %gms\n", t*1000/getTickFrequency());// get processing time return; }
void Balloon::updateBalloons(Size imageSize) { vector<ImageToMove> tempBalloonList; int c = _balloonList.size(); vector<ImageToMove>::iterator balloon; for (balloon = _balloonList.begin(); balloon < _balloonList.end(); balloon++) { balloon->phase += 4; if (balloon->phase == 360) { balloon->phase = 0; } balloon->velocityAngle = 60 * sin(PI * balloon->t * balloon->phase / 180) + balloon->velocityAngle0; float x = balloon->position.x; float y = balloon->position.y; //cout << x << "x" << y << endl; double k = ((double)getTickCount() - balloon->updateTick) / getTickFrequency(); k = k / 0.08; //cout << "k: " << k << endl; balloon->updateTick = (double)getTickCount(); balloon->position = Point(x + balloon->velocity * k * cos(PI * (balloon->velocityAngle) / 180), y - balloon->velocity * k * sin(PI * (balloon->velocityAngle) / 180)); //cout << "Balloon image size: " << _image.cols << "x" << _image.rows << endl; //cout << /*"Balloon position:*/" (" << balloon->position.x << "; " << balloon->position.y << "; " << balloon->velocity << ")"; //cout << "Image size: " << imageSize.width << "x" << imageSize.height << endl; if ((balloon->position.x < 0.) or (balloon->position.x + (float)_imageList[balloon->imageIndex].cols > (float)imageSize.width) or (balloon->position.y < 0.) or (balloon->position.y + (float)_imageList[balloon->imageIndex].rows > (float)imageSize.height)) { //_balloonList.erase(balloon); //c--; //cout << "dropped "; } else { tempBalloonList.push_back(*balloon); } //_balloonList.clear(); } //cout << c << "<=>" << tempBalloonList.size() << endl; _balloonList = tempBalloonList; }
bool SparseRec2View::detect() { double tt = (double)getTickCount(); detector->detect(igrey1, key1); detector->detect(igrey2, key2); TagI("key number for image 1 = %d\n",(int)key1.size()); TagI("key number for image 2 = %d\n",(int)key2.size()); tt = (double)getTickCount() - tt; TagI("detect time = %lf ms\n", tt/getTickFrequency()*1000.0); //draw for(int i=0; i<(int)key1.size(); ++i) { int radius = cvRound(key1[i].size*1.2/9.*2); circle(img1, key1[i].pt, 2, CV_BLUE, 1); circle(img1, key1[i].pt, radius, CV_RED, 1); } for(int i=0; i<(int)key2.size(); ++i) { int radius = cvRound(key2[i].size*1.2/9.*2); circle(img2, key2[i].pt, 2, CV_BLUE, 1); circle(img2, key2[i].pt, radius, CV_RED, 1); } //combined int cw = img1.cols + img2.cols; int ch = std::max(img1.rows, img2.rows); combined.create(ch, cw, img1.type()); IplImage imgl = IplImage(combined); cvSetImageROI(&imgl, cvRect(0,0,img1.cols,img1.rows)); cvCopy(&IplImage(img1), &imgl); IplImage imgr = IplImage(combined); cvSetImageROI(&imgr, cvRect(img1.cols-1,0,img2.cols,img2.rows)); cvCopy(&IplImage(img2), &imgr); return true; }
/** * Loop function of TLDC. * * @param opt tld structure with initial values and thresholds * @param cfg stream settings and initial bounding box */ void tldExample(TldStruct* opt, Config& cfg) { srand(0); double t = (double)getTickCount(); tld = *opt; tld.cfg = &cfg; /* INITIALIZATION -------------------------------------- */ tldInit(tld); if (!cfg.nodisplay) tldDisplay(0, 0, tld, t); /* RUN-TIME -------------------------------------------- */ unsigned long i = 1; while (i < 2500) { cvReleaseImage(&(tld.handle)); t = (double)getTickCount(); tldProcessFrame(tld, i); t = ((double)getTickCount() - t)/getTickFrequency(); if (!cfg.nodisplay) tldDisplay(1, i, tld, t); i++; } cvDestroyWindow("Result"); }
//event handler for the next button to go from one image to another void callbackBtnNext(int state, void* dataPassed) { //increase the file counter by 1 countFile++; //if (countFile == 203) // cout << "asd" << endl; //construct the next file names string countFileStr = static_cast<ostringstream*>( &(ostringstream() << countFile) )->str(); nameImg = folderRoot + folderImg + "(" + countFileStr + ")" + fileExtImg; namePtsFile = folderRoot + folderPts + "(" + countFileStr + ")" + fileExtPts; nameResultsFile = folderWrite + prefixResultsFile + "(" + countFileStr + ")" + fileExtResult; nameResultsImg = folderWrite + prefixResultsFile + "(" + countFileStr + ")" + fileExtImg; //Read the image and store in a matrix //cout << nameImg << " is loading..." << endl; //bgrMat = imread(nameImg, cv::IMREAD_COLOR); //cvtColor(bgrMat, hsvMat, CV_BGR2HSV); //Read the pts file and store x,y,z in a matrix //cout << namePtsFile << " is loading..." << endl; //Mat xyzMat = Mat::zeros(height, width, CV_64FC3); //readPtsDataFromFile(namePtsFile, xyzMat); //Calculate the variance of Y and the max diff of Y in patches //cout << "Patches are constructing..." << endl; //cout << "Variance and Difference in Y calulcation..." << endl; //varPatches = calculateVarianceYforPatches(xyzMat, size_patch); //diffPatches = calculateDifferenceMaxYforPatches(xyzMat, size_patch); //Calculate image patch histograms //cout << "Image patch histograms are building..." << endl; //histsBGR = calculateHistograms(bgrMat, SIZE_PATCH, CHANNELS, 3, SIZE_HIST, RANGES); //histsHS = calculateHistograms(hsvMat, size_patch, channels_hs, 2, size_hist_hs, ranges_hs, false); //show the original image //imshow(nameWindowMain, bgrMat); //Tracbar stuff //create slider for training sample threshold //createTrackbar( nameTrackbarVarYThreshold, nameWindowMain, &thresholdVarY, threshold_var_y_max, callbackTrackbarVarYThreshold ); //createTrackbar( nameTrackbarDiffYThreshold, nameWindowMain, &thresholdDiffY, THRESHOLD_DIFF_Y_MAX, callbackTrackbarDiffYThreshold ); //create next button to jump to the next image //createButton(nameBtnNext, callbackBtnNext, NULL); // Show some stuff //callbackTrackbarVarYThreshold( thresholdVarY, 0 ); //callbackTrackbarDiffYThreshold( thresholdDiffY, 0 ); //Read the image and store in a matrix cout << nameImg << " is loading..." << endl; bgrMat = imread(nameImg, cv::IMREAD_COLOR); //Read the pts file and store x,y,z in a matrix cout << namePtsFile << " is loading..." << endl; Mat xyzMat = Mat::zeros(height, width, CV_64FC3); readPtsDataFromFile(namePtsFile, xyzMat); //convert the variance threshold value obtained from the trackbar to double thresholdVarYd = ((double)thresholdVarY)/threshold_var_y_divider; //Get ticks before algorithm double t = (double)getTickCount(); //Mat classifiedMat = detectRoadSingleSVM(bgrMat, xyzMat, trainingBuffer, trainingBufferLib, model, weights, thresholdVarYd, false, distribution); Mat classifiedMat = detectRoad(bgrMat, xyzMat, trainingBuffer, means, covs, weights, thresholdVarYd, false, distribution); //Mat classifiedMat = detectRoadDebug(bgrMat, xyzMat, trainingBuffer, means, covs, weights, thresholdVarYd, false, distribution); //Mat classifiedMat = detectRoadBanded(bgrMat, xyzMat, trainingBuffer, means, covs, weights, thresholdVarYd, false, distribution); //cout << "Number of Training Sets : " << trainingBufferLib.size() << endl; //for (int i = 0; i < trainingBufferLib.size(); i++) // cout << "Set " << i << " : " << trainingBufferLib[i].rows << endl; //for (int i = 0; i < trainingBufferLib.size(); i++) // cout << "Set " << i << " : " << trainingBufferLib[i] << endl; //Get tocks after algorithm, find the difference and write in sec form t = ((double)getTickCount() - t)/getTickFrequency(); cout << "Times passed in seconds: " << t << endl; //Show the classification result for the Y variance namedWindow(nameWindowClassifiedVar, CV_WINDOW_AUTOSIZE); imshow(nameWindowClassifiedVar, classifiedMat); ////////////////////////////////////////////////////////////////// ///////////////////// WRITING THE RESULTS //////////////////////// ////////////////////////////////////////////////////////////////// //writing the classification results as image and/or text cout << "Writing to Image : " << nameResultsImg << endl; vector<int> output_params; output_params.push_back(CV_IMWRITE_PXM_BINARY); output_params.push_back(1); imwrite(nameResultsImg, classifiedMat, output_params); cout << "Writing to File: " << nameResultsFile << endl; writeResultDataToFile(nameResultsFile, classifiedMat); cout << "Done..." << endl; }
void DepthMap::DisparityFilter(Mat l,Mat r) { Mat left_for_matcher, right_for_matcher; Mat left_disp,right_disp; Mat filtered_disp; Mat conf_map = Mat(left.rows,left.cols,CV_8U); conf_map = Scalar(255); Rect ROI; Ptr<DisparityWLSFilter> wls_filter; double matching_time, filtering_time; left_for_matcher = l.clone(); right_for_matcher = r.clone(); int wsize = 3; Ptr<StereoSGBM> left_matcher = StereoSGBM::create(0,48,wsize); left_matcher->setP1(24*wsize*wsize); left_matcher->setP2(96*wsize*wsize); left_matcher->setPreFilterCap(5); left_matcher->setMinDisparity(-32); left_matcher->setBlockSize(3); left_matcher->setDisp12MaxDiff(12); left_matcher->setMode(StereoSGBM::MODE_SGBM_3WAY); wls_filter = createDisparityWLSFilter(left_matcher); Ptr<StereoMatcher> right_matcher = createRightMatcher(left_matcher); matching_time = (double)getTickCount(); left_matcher-> compute(left_for_matcher, right_for_matcher,left_disp); right_matcher->compute(right_for_matcher,left_for_matcher, right_disp); matching_time = ((double)getTickCount() - matching_time)/getTickFrequency(); //! [filtering] wls_filter->setLambda(8000); wls_filter->setSigmaColor(1.5); filtering_time = (double)getTickCount(); wls_filter->filter(left_disp,left,filtered_disp,right_disp); filtering_time = ((double)getTickCount() - filtering_time)/getTickFrequency(); //! [filtering] conf_map = wls_filter->getConfidenceMap(); // //// Get the ROI that was used in the last filter call: ROI = wls_filter->getROI(); left_matcher->save("g.txt"); cout.precision(2); cout<<"Matching time: "<<matching_time<<"s"<<endl; cout<<"Filtering time: "<<filtering_time<<"s"<<endl; cout<<endl; Mat filtered_disp_vis; getDisparityVis(filtered_disp,filtered_disp_vis,1.0); imwrite("dst_path.jpg",filtered_disp_vis); Mat raw_disp_vis; getDisparityVis(left_disp,raw_disp_vis,1.0); imwrite("dst_raw_path.jpg",raw_disp_vis); imwrite("dst_conf_path.png",conf_map); }
/** returns passed time in seconds. */ CV_WRAP double getTimeSec() const { return (double)getTimeTicks() / getTickFrequency(); }
void AerialCameraStitch::stitchCameraImages(){ cv::Stitcher imgstitcher = Stitcher::createDefault(true); vector< Mat > tmp_uav_cam_img; Mat out_stitch_img; Mat tmp_img1; Mat tmp_img2; Mat tmp_img3; Mat tmp_img1_; Mat tmp_img2_; Mat tmp_img3_; opt_pose = false; ROS_INFO_STREAM("STITCHING STARTED"); try{ destroyAllWindows(); tmp_img1 = uav1_cam_img; tmp_img2 = uav2_cam_img; tmp_img3 = uav3_cam_img; cv::resize(tmp_img1, tmp_img1_, cv::Size2i(tmp_img1.cols/2, tmp_img1.rows/2)); cv::resize(tmp_img2, tmp_img2_, cv::Size2i(tmp_img2.cols/2, tmp_img2.rows/2)); cv::resize(tmp_img3, tmp_img3_, cv::Size2i(tmp_img3.cols/2, tmp_img3.rows/2)); tmp_img1 = tmp_img1_; tmp_img2 = tmp_img2_; tmp_img3 = tmp_img3_; tmp_uav_cam_img.push_back(tmp_img1); tmp_uav_cam_img.push_back(tmp_img2); tmp_uav_cam_img.push_back(tmp_img3); ROS_INFO_STREAM("UAV 1 : "<<tmp_uav_cam_img[0].size()); ROS_INFO_STREAM("UAV 2 : "<<tmp_uav_cam_img[1].size()); ROS_INFO_STREAM("UAV 3 : "<<tmp_uav_cam_img[2].size()); unsigned long AAtime=0, BBtime=0; //check processing time AAtime = getTickCount(); //check processing time Stitcher::Status status = imgstitcher.stitch(tmp_uav_cam_img, out_stitch_img); BBtime = getTickCount(); //check processing time printf("Time Taken: %.2lf sec \n", (BBtime - AAtime)/getTickFrequency() ); //check processing time cv::Size out_size = out_stitch_img.size(); int out_row = out_size.height; int out_cols = out_size.width; if (status != Stitcher::OK || (out_row == 1 && out_cols == 1)) { ROS_INFO_STREAM("IMAGES DIDNT STITCH - Not Enough Common Features Detected : "<< out_stitch_img.size()); ROS_INFO_STREAM("Increasing height to optimum level, if possible"); opt_pose = true; uav_int_pose = uavpose_z + 5; ROS_INFO_STREAM("Optimal Height : " << uav_int_pose); ROS_INFO_STREAM("Translating ... "); }else{ ROS_INFO_STREAM("STITCHING RESULT: "<< out_size); ROS_INFO_STREAM("SUCCESSFUL "); namedWindow("Stitching Result", WINDOW_AUTOSIZE); imshow("Stitching Result", out_stitch_img); } }catch(cv::Exception& e){ const char* err_msg = e.what(); ROS_INFO_STREAM("SOMETHING WENT WRONG!!!"); ROS_INFO_STREAM("exception caught : " << err_msg); } waitKey(0); out_stitch_img.release(); tmp_img1.release(); tmp_img2.release(); tmp_img3.release(); tmp_img1_.release(); tmp_img2_.release(); tmp_img3_.release(); tmp_uav_cam_img[0].release(); tmp_uav_cam_img[1].release(); tmp_uav_cam_img[2].release(); }
int main(int argc, char* argv[]) { uint64_t pop_size = 80; double inertia_r = 0.1; double cog_r = 0.3; double soc_r = 0.3; // uint64_t rng_seed = 0xF0F0F0F0; // uint64_t rng_seed = getTickCount(); uint64_t num_trials = NUMBER_TRIALS; uint64_t num_gens = NUMBER_GENERATIONS; uint64_t gen_limit = NUMBER_GENERATIONS/10; double last_tick_count = 0.0; string outname = "./outfile_shafer-pso_best.csv"; try { po::options_description desc("Allowed options"); desc.add_options() ("help", "Produce help message") ("popsize", po::value<uint64_t>(), "Set Population Size") ("inertia", po::value<double>(), "Set Particle Inertia") ("cog", po::value<double>(), "Set Cognitive effect of particle") ("soc", po::value<double>(), "Set Social effect of swarm") ("trials", po::value<uint64_t>(), "Set Number of Trials") ("gens", po::value<uint64_t>(), "Set Number of Generations") ; po::variables_map vm; po::store(po::parse_command_line(argc, argv, desc), vm); po::notify(vm); if (vm.count("help")) { cout << desc << "\n"; return 1; } if (vm.count("popsize")) { pop_size = vm["popsize"].as<uint64_t>(); cout << "Population size was set to " << pop_size << ".\n"; } else { cout << "Population size was set to default of " << pop_size << ".\n"; } if (vm.count("inertia")) { inertia_r = vm["inertia"].as<double>(); cout << "Inertia was set to " << inertia_r << ".\n"; } else { cout << "Inertia was set to default of " << inertia_r << ".\n"; } if (vm.count("cog")) { cog_r = vm["cog"].as<double>(); cout << "COG was set to " << cog_r << ".\n"; } else { cout << "COG was set to default of " << cog_r << ".\n"; } if (vm.count("soc")) { soc_r = vm["soc"].as<double>(); cout << "SOC was set to " << soc_r << ".\n"; } else { cout << "SOC was set to default of " << soc_r << ".\n"; } if (vm.count("trials")) { num_trials = vm["trials"].as<uint64_t>(); cout << "Number of Trials was set to " << num_trials << ".\n"; } else { cout << "Number of Trials was set to default of " << num_trials << ".\n"; } if (vm.count("gens")) { num_gens = vm["gens"].as<uint64_t>(); gen_limit = num_gens/10; if (gen_limit < 50) gen_limit = 50; cout << "Number of Generations was set to " << num_gens << ".\n"; } else { cout << "Number of Trials was set to default of " << num_gens << ".\n"; } } catch(std::exception& e) { cout << "error: " << e.what() << "\n"; return 1; } catch(...) { cout << "You threw an exception, idiot.\n"; return 1; } // RNG randi (rng_seed); uint64_t iter;//, jter; particle_t mini, maxi; for (iter=0; iter<NUMBER_ATTRIBUTES; iter++) { mini.pos[iter] = 0.0; maxi.pos[iter] = 1.0; mini.vel[iter] = -4.0; maxi.vel[iter] = 4.0; } // Print data to file // stringstream strstr (stringstream::in | stringstream::out); ofstream outfileTheBest; outfileTheBest.open( outname.c_str(), std::ofstream::out | std::ofstream::app ); if ( !outfileTheBest.is_open() ) { cerr << "Unable to open file: " << outname << "\n"; return 1; } /* ofstream outfileBestGenLog; outfileBestGenLog.open( "./outfile_shafer-pso_gen_log.csv", std::ofstream::out | std::ofstream::app ); if ( !outfileBestGenLog.is_open() ) { cerr << "Unable to open file: " << "./outfile_shafer-pso_gen_log.csv" << "\n"; return 1; } ofstream outfileBestFitLog; outfileBestFitLog.open( "./outfile_shafer-pso_fit_log.csv", std::ofstream::out | std::ofstream::app ); if ( !outfileBestFitLog.is_open() ) { cerr << "Unable to open file: " << "./outfile_shafer-pso_fit_log.csv" << "\n"; return 1; } */ uint64_t best_default_precision = outfileTheBest.precision(); // outfileTheBest.precision(35); outfileTheBest << "NumGens,PopSize,RNG_Seed,Trial,FitEvals,EndGen,Genotype,Fitness"; population *hoponpop; last_tick_count = (double) getTickCount(); uint64_t trailer_trash; for (trailer_trash=0; trailer_trash<num_trials; trailer_trash++) { uint64_t rng_seed = getTickCount(); RNG randi (rng_seed); // population(RNG& rudi, uint64_t pop_size, particle_t min_lim, particle_t max_lim, double cog, double soc, double inertia, uint64_t gen_limit); hoponpop = new population(randi, pop_size, mini, maxi, cog_r, soc_r, inertia_r, gen_limit); // cout << "Trial: " << trailer_trash << endl; if (trailer_trash==0) cout << "Trial: "; cout << trailer_trash << " "; cout.flush(); hoponpop->populator(); // outfileBestFitLog << "\n" << rng_seed; // outfileBestGenLog << "\n" << rng_seed; uint64_t generational_recursion; for (generational_recursion=0; generational_recursion<num_gens; generational_recursion++) { hoponpop->iterate(); // specimen_t toad = hoponpop->First(); // outfileBestGenLog << "," << toad.gen; // outfileBestFitLog << "," << toad.fit; } specimen_t bestinswarm = hoponpop->BestInSwarm(); uint64_t aged = hoponpop->Age(); // outfileTheBest << "NumGens,PopSize,RNG_Seed,Trial,FitEvals,EndGen,Genotype,Fitness"; outfileTheBest.precision(best_default_precision); outfileTheBest << "\n" << num_gens << "," << pop_size << "," << rng_seed << "," << trailer_trash << "," << bestinswarm.calced << "," << aged << ","; outfileTheBest.precision(35); outfileTheBest << bestinswarm.gen << "," << bestinswarm.fit; // outfileBestGenLog << "\n"; // outfileBestGenLog.flush(); // outfileBestFitLog << "\n"; // outfileBestFitLog.flush(); outfileTheBest.flush(); delete hoponpop; }//End of Trial cout << endl; // outfileBestGenLog << "\n"; // outfileBestGenLog.flush(); // outfileBestFitLog << "\n"; // outfileBestFitLog.flush(); outfileTheBest << "\n"; outfileTheBest.flush(); cout << "\tProcessing time: " << ((double) getTickCount() - last_tick_count)/getTickFrequency() << "[s]\n"; // outfileBestGenLog.close(); // outfileBestFitLog.close(); outfileTheBest.close(); return 0; }
void RhoanaGainCompensator::feed(const vector<Point> &corners, const vector<UMat> &images, const vector<pair<UMat,uchar> > &masks) { LOGLN("Exposure compensation..."); #if ENABLE_LOG int64 t = getTickCount(); #endif CV_Assert(corners.size() == images.size() && images.size() == masks.size()); const int num_images = static_cast<int>(images.size()); Mat_<int> N(num_images, num_images); N.setTo(0); Mat_<double> I(num_images, num_images); I.setTo(0); //Rect dst_roi = resultRoi(corners, images); Mat subimg1, subimg2; Mat_<uchar> submask1, submask2, intersect; for (int i = 0; i < num_images; ++i) { //std::cout << "Here1.1" << std::endl; for (int j = i; j < num_images; ++j) { Rect roi; if (overlapRoi(corners[i], corners[j], images[i].size(), images[j].size(), roi)) { subimg1 = images[i](Rect(roi.tl() - corners[i], roi.br() - corners[i])).getMat(ACCESS_READ); subimg2 = images[j](Rect(roi.tl() - corners[j], roi.br() - corners[j])).getMat(ACCESS_READ); submask1 = masks[i].first(Rect(roi.tl() - corners[i], roi.br() - corners[i])).getMat(ACCESS_READ); submask2 = masks[j].first(Rect(roi.tl() - corners[j], roi.br() - corners[j])).getMat(ACCESS_READ); intersect = (submask1 == masks[i].second) & (submask2 == masks[j].second); N(i, j) = N(j, i) = std::max(1, countNonZero(intersect)); double Isum1 = 0, Isum2 = 0; for (int y = 0; y < roi.height; ++y) { ///const Point3_<uchar>* r1 = subimg1.ptr<Point3_<uchar> >(y); ///const Point3_<uchar>* r2 = subimg2.ptr<Point3_<uchar> >(y); const uchar* r1 = subimg1.ptr<uchar>(y); const uchar* r2 = subimg2.ptr<uchar>(y); for (int x = 0; x < roi.width; ++x) { if (intersect(y, x)) { ///Isum1 += sqrt(static_cast<double>(sqr(r1[x].x) + sqr(r1[x].y) + sqr(r1[x].z))); ///Isum2 += sqrt(static_cast<double>(sqr(r2[x].x) + sqr(r2[x].y) + sqr(r2[x].z))); Isum1 += sqrt(static_cast<double>(sqr(r1[x]))); Isum2 += sqrt(static_cast<double>(sqr(r2[x]))); } } } I(i, j) = Isum1 / N(i, j); I(j, i) = Isum2 / N(i, j); } } //std::cout << "Here1.2" << std::endl; } double alpha = 0.01; double beta = 100; Mat_<double> A(num_images, num_images); A.setTo(0); Mat_<double> b(num_images, 1); b.setTo(0); for (int i = 0; i < num_images; ++i) { for (int j = 0; j < num_images; ++j) { b(i, 0) += beta * N(i, j); A(i, i) += beta * N(i, j); if (j == i) continue; A(i, i) += 2 * alpha * I(i, j) * I(i, j) * N(i, j); A(i, j) -= 2 * alpha * I(i, j) * I(j, i) * N(i, j); } } solve(A, b, gains_); LOGLN("Exposure compensation, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec"); }
int main(int argc, char* argv[]) { CNN net; double time_cost; //-------- CNN Initializing -------- //---------------------------------- //Read parameters file net.readPara(parameter_file); //-------- Load Dataset ------------ //---------------------------------- #ifdef _HANY_NET_WITH_LABEL_NAMES ifstream read_label(label_file); for(int c = 0; c < net.class_count; c++) { string new_label_name; read_label >> new_label_name; label_list.push_back(make_pair(c, new_label_name)); } #endif #ifdef _HANY_NET_LOAD_MNIST #ifdef _HANY_NET_PRINT_MSG cout << "Loading MNIST dataset..." << endl; time_cost = (double)getTickCount(); #endif loadMNIST("train-images.idx3-ubyte", "train-labels.idx1-ubyte", net.train_set); loadMNIST("t10k-images.idx3-ubyte", "t10k-labels.idx1-ubyte", net.test_set); #ifdef _HANY_NET_PRINT_MSG time_cost = ((double)getTickCount() - time_cost) / getTickFrequency(); cout << "Load samples done." << endl << "Time cost: " << time_cost << "s." << endl << endl; #endif #endif #ifdef _HANY_NET_TRAIN_FROM_SCRATCH #ifdef _HANY_NET_LOAD_SAMPLE_FROM_PIC #ifdef _HANY_NET_PRINT_MSG cout << "Loading samples..." << endl; time_cost = (double)getTickCount(); #endif for(int c = 0; c < net.class_count; c++) { for(int i = 0; i < sample_num; i++) { string file_name = sample_file_pre + to_string(c) + "_" + to_string(i) + ".jpg"; Mat img_read = imread(file_name, CV_LOAD_IMAGE_GRAYSCALE); if(img_read.data == NULL) { break; } Mat img_nor; resize(img_read, img_nor, Size(net.sample_width, net.sample_height)); net.train_set.push_back(make_pair(img_nor, (uchar)(c))); } } #ifdef _HANY_NET_PRINT_MSG time_cost = ((double)getTickCount() - time_cost) / getTickFrequency(); cout << "Load samples done." << endl << "Time cost: " << time_cost << "s." << endl << endl; #endif #endif #ifdef _HANY_NET_CAPTURE_FACE_FROM_CAMERA #ifdef _HANY_NET_PRINT_MSG cout << "Capturing samples..." << endl; time_cost = (double)getTickCount(); #endif VideoCapture cap_in(0); if(!cap_in.isOpened()) { cout << "Cannot access camera. Press ANY key to exit." << endl; cin.get(); exit(-1); } CascadeClassifier cascade_in; cascade_in.load(haar_file); Mat frame; int frame_count = 0; int capture_count = 0; int class_idx = 0; int class_count = 0; bool sample_suff = false; bool cap_sample = true; while(cap_in.read(frame)) { capture_count++; vector<Rect> faces; Mat frame_gray, img_gray; cvtColor(frame, frame_gray, CV_BGR2GRAY); equalizeHist(frame_gray, img_gray); cascade_in.detectMultiScale(img_gray, faces, 1.1, 2, 0, Size(120, 120)); int face_area = 0; int face_idx = 0; if(faces.size() > 0) { for(int f = 0; f < faces.size(); f++) { if(faces[f].area() > face_area) { face_area = faces[f].area(); face_idx = f; } } rectangle(frame, faces[face_idx], Scalar(255, 0, 0), 3); if(frame_count % 5 == 0 && cap_sample && !sample_suff) { Mat face, face_nor; img_gray(faces[face_idx]).copyTo(face); resize(face, face_nor, Size(net.sample_width, net.sample_height)); net.train_set.push_back(make_pair(face_nor, (uchar)class_idx)); class_count++; } } putText(frame, "Class: " + to_string(class_idx), Point(50, 100), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 255, 255), 2); putText(frame, "Sample: " + to_string(class_count), Point(50, 150), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 255, 255), 2); if(sample_suff) { putText(frame, "Enough samples. Press SPACE.", Point(50, 50), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 255, 255), 2); }else { putText(frame, "Capturing...", Point(50, 50), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 255, 255), 2); } if(!cap_sample) { putText(frame, "Wait for another person. Press SPACE.", Point(50, 200), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 255, 255), 2); } imshow(camera_window_name, frame); if(class_count >= sample_num) { sample_suff = true; } frame_count++; int key = waitKey(20); if(key == 27){ cap_in.release(); break; } else if(key == ' ') { if(cap_sample && sample_suff) { cap_sample = false; continue; } if(!cap_sample && sample_suff) { cap_sample = true; sample_suff = false; class_idx++; class_count = 0; continue; } } } #ifdef _HANY_NET_PRINT_MSG time_cost = ((double)getTickCount() - time_cost) / getTickFrequency(); cout << "Load samples done." << endl << "Time cost: " << time_cost << "s." << endl << endl; #endif #endif #endif //-------- CNN Initializing -------- //---------------------------------- #ifdef _HANY_NET_PRINT_MSG cout << "Initializing neural networks..." << endl; time_cost = (double)getTickCount(); #endif //Initialize CNN with knowledge of samples net.initCNN(); #ifdef _HANY_NET_PRINT_MSG time_cost = ((double)getTickCount() - time_cost) / getTickFrequency(); cout << "Total number of samples: " << (int)(net.train_set.size() + net.test_set.size()) << endl; cout << "Initializing neural networks done." << endl << "Time cost: " << time_cost << "s." << endl << endl; #endif //Load pre-trained CNN parameters from file and continue to train // net.uploadCNN(pretrained_cnn_file); //-------- CNN Training ---------- //-------------------------------- #ifdef _HANY_NET_TRAIN_FROM_SCRATCH #ifdef _HANY_NET_PRINT_MSG cout << "Start training CNN..." << endl; time_cost = (double)getTickCount(); #endif //Train CNN with train sample set net.trainCNN(); #ifdef _HANY_NET_PRINT_MSG time_cost = ((double)getTickCount() - time_cost) / getTickFrequency(); cout << "CNN training done." << endl << "Time cost: " << time_cost << "s." << endl << endl; #endif for(int i = 0; i < net.time_ff.size(); i++) { cout << "FeedForward stage " << i << ": " << net.time_ff[i] << "s" << endl; } for(int i = 0; i < net.time_bp.size(); i++) { cout << "BackPropagation stage " << i << ": " << net.time_bp[i] << "s" << endl; } //Draw stage loss graph Mat stage_loss_graph = Mat::zeros(600, 1100, CV_8UC3); Point2d pt1, pt2; pt1 = Point2d(50.0, 50.0); for(int stage = 0; stage < net.stage_loss.size(); stage++) { pt2 = Point2d(50.0 + 1200.0 / net.stage_loss.size() * stage, 550.0 - 500.0 * net.stage_loss[stage] / net.stage_loss[0]); line(stage_loss_graph, pt1, pt2, Scalar(255, 255, 255)); pt1 = pt2; } imshow("Stage Loss Graph", stage_loss_graph); imwrite("stage_loss_graph.jpg", stage_loss_graph); waitKey(10); #endif //-------- Save Trained Network ----- //----------------------------------- #ifdef _HANY_NET_TRAIN_FROM_SCRATCH #ifdef _HANY_NET_PRINT_MSG cout << "Dumping trained CNN parameters to file " << pretrained_cnn_file << "..." << endl; #endif //Dump trained CNN parameters to file net.downloadCNN(trained_cnn_file); #ifdef _HANY_NET_PRINT_MSG cout << "Dumping trained CNN parameters to file done." << endl << endl; #endif #endif //-------- Load Pre-trained Network ----- //--------------------------------------- #ifndef _HANY_NET_TRAIN_FROM_SCRATCH #ifdef _HANY_NET_PRINT_MSG cout << "Loading pre-trained CNN parameters from file " << pretrained_cnn_file << "..." << endl; #endif //Load pre-trained CNN parameters from file net.uploadCNN(pretrained_cnn_file); #ifdef _HANY_NET_PRINT_MSG cout << "Loading pre-trained CNN parameters from file done." << endl << endl; #endif #endif //-------- Predict New Samples------- //-------------------------------------- #ifdef _HANY_NET_PREDICT_MNIST #ifdef _HANY_NET_PRINT_MSG cout << "Predicting MNIST test dataset..." << endl; time_cost = (double)getTickCount(); #endif //Calculate correctness ratio with test samples int total_correct_count = 0; for(int sample_idx = 0; sample_idx < net.test_set.size(); sample_idx++) { vector<Mat> input_sample; input_sample.push_back(net.test_set[sample_idx].first); vector<Mat> predict_result = net.predictCNN(input_sample); if((int)predict_result[0].ptr<uchar>(0)[0] == net.test_set[sample_idx].second) { total_correct_count++; } } double total_correct_ratio = (double)total_correct_count / net.test_set.size(); #ifdef _HANY_NET_PRINT_MSG time_cost = ((double)getTickCount() - time_cost) / getTickFrequency(); cout << "MNIST testing done." << endl << "Time cost: " << time_cost << "s." << endl; cout << "Total correctness ratio: " << total_correct_ratio << endl << endl; #endif #endif #ifdef _HANY_NET_PREDICT_IMAGE_SERIES #ifdef _HANY_NET_PRINT_MSG cout << "Predicting from image series..." << endl; #endif // VideoWriter wri(output_video_file, CV_FOURCC('M', 'J', 'P', 'G'), 25.0, Size(640, 480)); for(int c = 0; c < net.class_count; c++) { for(int i = 0; i < sample_num; i++) { string file_name = sample_file_pre + to_string(c) + "_" + to_string(i) + ".jpg"; Mat img_read = imread(file_name, CV_LOAD_IMAGE_GRAYSCALE); if(img_read.data == NULL) { break; } Mat img_nor, img_show; resize(img_read, img_show, Size(400, 400)); resize(img_read, img_nor, Size(net.sample_width, net.sample_height)); vector<Mat> input_sample; input_sample.push_back(img_nor); vector<Mat> predict_result = net.predictCNN(input_sample); int pred_rst = (int)predict_result[0].ptr<uchar>(0)[0]; if(pred_rst <= net.class_count) putText(img_show, label_list[pred_rst].second, Point(10, 40), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 255, 255), 2); putText(img_show, to_string(c)+"-"+to_string(i), Point(img_show.cols-80, 40), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 255, 255), 2); int frame_count = 25; while(--frame_count) { // wri.write(img_show); } imshow(camera_window_name, img_show); int key_get = waitKey(20); switch(key_get) { case 27: // wri.release(); return 0; default: break; } } } #endif #ifdef _HANY_NET_PREDICT_VEDIO_SERIES #ifdef _HANY_NET_PRINT_MSG cout << "Predicting from video series..." << endl; #endif VideoWriter wri(output_video_file, CV_FOURCC('M', 'J', 'P', 'G'), 25.0, Size(640, 480)); namedWindow(camera_window_name); CascadeClassifier cascade_out; cascade_out.load(haar_file); for(int c = 1; c <= net.class_count; c++) { string file_name = "path_to_face_videos\\" + to_string(c) + ".wmv"; VideoCapture cap(file_name); if(!cap.isOpened()) continue; Mat img_read; while(cap.read(img_read)) { Mat img_gray, nor_gray, img_show; img_read.copyTo(img_show); cvtColor(img_read, img_gray, CV_BGR2GRAY); vector<Rect> faces; equalizeHist(img_gray, img_gray); cascade_out.detectMultiScale(img_gray, faces, 1.1, 2, 0, Size(120, 120)); for(int f = 0; f < faces.size(); f++) { rectangle(img_show, faces[f], Scalar(0, 255, 255), 3); resize(img_gray(faces[f]), nor_gray, Size(net.sample_width, net.sample_height)); vector<Mat> input_sample; input_sample.push_back(nor_gray); vector<Mat> predict_result = net.predictCNN(input_sample); int pred_rst = (int)predict_result[0].ptr<uchar>(0)[0]; if(pred_rst <= net.class_count) putText(img_show, to_string(pred_rst), Point(faces[f].x+faces[f].width, faces[f].y+faces[f].height), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 255, 255), 2); } int frame_count = 2; while(--frame_count) { wri.write(img_show); } imshow(camera_window_name, img_show); int key_get = waitKey(20); switch(key_get) { case 27: wri.release(); return 0; default: break; } } } wri.release(); #endif #ifdef _HANY_NET_PREDICT_CAMERA #ifdef _HANY_NET_PRINT_MSG cout << "Predicting from camera..." << endl; #endif VideoCapture cap_out(0); if(!cap_out.isOpened()) { cout << "Cannot access camera." << endl; cin.get(); exit(-1); } CascadeClassifier cascade_out; cascade_out.load(haar_file); // VideoWriter wri(output_video_file, CV_FOURCC('M', 'J', 'P', 'G'), 25.0, Size(640, 480)); Mat src_frame; namedWindow(camera_window_name); Mat img_read; while(cap_out.read(img_read)) { Mat img_gray, nor_gray, img_show; img_read.copyTo(img_show); cvtColor(img_read, img_gray, CV_BGR2GRAY); vector<Rect> faces; equalizeHist(img_gray, img_gray); cascade_out.detectMultiScale(img_gray, faces, 1.1, 2, 0, Size(120, 120)); for(int f = 0; f < faces.size(); f++) { rectangle(img_show, faces[f], Scalar(0, 255, 255), 3); resize(img_gray(faces[f]), nor_gray, Size(net.sample_width, net.sample_height)); vector<Mat> input_sample; input_sample.push_back(nor_gray); vector<Mat> predict_result = net.predictCNN(input_sample); int pred_rst = (int)predict_result[0].ptr<uchar>(0)[0]; if(pred_rst <= net.class_count) putText(img_show, label_list[pred_rst].second, Point(faces[f].x+faces[f].width, faces[f].y+faces[f].height), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 255, 255), 2); } int frame_count = 2; while(--frame_count) { // wri.write(img_show); } imshow(camera_window_name, img_show); int key_get = waitKey(20); if(key_get == 27) { // wri.release(); cap_out.release(); return 0; } } #endif cout << "Press any key to quit..." << endl; // waitKey(0); cin.get(); return 0; }
int _tmain(int argc, _TCHAR* argv[]) { if (InitEngine() < 0) { cout << "init failed" << endl; return -1; } string image_path = "..\\image\\download_image\\test"; vector<string> image_list; get_image_list(image_path, image_list); int lv_cnt[5] = {0}, fail_cnt = 0; int lv_correct_cnt[5] = {0}; double totalTime = 0; for (vector<string>::iterator it = image_list.begin(); it != image_list.end(); ++it) { string img_file = image_path + "\\" + *it; Mat img = imread(img_file, 4); if (img.data == NULL) { DeleteFile(img_file.c_str()); continue; } double t = (double)getTickCount(); char code[4] = {0}; float conf[4] = {0}; int ret = RecognizeCode((char*)(img_file.c_str()), code, conf); t = (double)getTickCount() - t; totalTime += t; if (ret >= 0) { cout << "[" << ret << "]" << "\t" << *it << endl; cout << "\t" << code[0] << code[1] << code[2] << code[3]; cout << "\t" << "[" << conf[0] << " " << conf[1] << " " << conf[2] << " " << conf[3] << "]" << endl; char str[5]; str[0] = code[0];str[1] = code[1];str[2] = code[2];str[3] = code[3];str[4]='\0'; if (it->substr(6, 4) == string(str)) lv_correct_cnt[ret]++; else { //imshow("code", img); //waitKey(0); //destroyAllWindows(); } lv_cnt[ret]++; } else { cout << *it << " "; cout << "[" << ret << "]" << "\t" << "pass." << endl; //MoveFile(img_file.c_str(), (image_path + "\\@\\" + *it).c_str()); fail_cnt++; } imshow("code", img); waitKey(0); destroyAllWindows(); } cout << "FAIL: " << fail_cnt << endl; int total_cnt = 0, correct_cnt = 0; for (int i = 0; i < 5; i++) { total_cnt += lv_cnt[i]; correct_cnt += lv_correct_cnt[i]; cout << "Lv[" << i << "]: " << lv_correct_cnt[i] << "/" << lv_cnt[i] << " = " << lv_correct_cnt[i]/(float)lv_cnt[i] << endl; } total_cnt += fail_cnt; cout << "TOTAL: " << correct_cnt << "/" << total_cnt << " = " << correct_cnt/(float)total_cnt << endl; cout << "Time : " << totalTime/(double)getTickFrequency()*1000. << "/" << total_cnt << " " << totalTime/(double)getTickFrequency()*1000./total_cnt << endl; if (ReleaseEngine() < 0) cout << "release failed" << endl; return 0; }
int main(int argc, char **argv) { int res; try { socket.bind ("tcp://*:14444"); s_sendmore (socket, "event"); s_send (socket, "{type:\"up\"}"); } catch (zmq::error_t e) { cerr << "Cannot bind to socket: " <<e.what() << endl; return -1; } // printf("Kinect camera test\n"); // // int i; // for (i=0; i<2048; i++) { // float v = i/2048.0; // v = powf(v, 3)* 6; // t_gamma[i] = v*6*256; // } // // g_argc = argc; // g_argv = argv; // // //setup Freenect... // if (freenect_init(&f_ctx, NULL) < 0) { // printf("freenect_init() failed\n"); // return 1; // } // // freenect_set_log_level(f_ctx, FREENECT_LOG_ERROR); // // int nr_devices = freenect_num_devices (f_ctx); // printf ("Number of devices found: %d\n", nr_devices); // // int user_device_number = 0; // if (argc > 1) // user_device_number = atoi(argv[1]); // // if (nr_devices < 1) // return 1; // // if (freenect_open_device(f_ctx, &f_dev, user_device_number) < 0) { // printf("Could not open device\n"); // return 1; // } // // freenect_set_tilt_degs(f_dev,freenect_angle); // freenect_set_led(f_dev,LED_RED); // freenect_set_depth_callback(f_dev, depth_cb); // freenect_set_video_callback(f_dev, rgb_cb); // freenect_set_video_format(f_dev, FREENECT_VIDEO_RGB); // freenect_set_depth_format(f_dev, FREENECT_DEPTH_11BIT); // // freenect_start_depth(f_dev); // freenect_start_video(f_dev); initFreenect(); //start the freenect thread to poll for events res = pthread_create(&ocv_thread, NULL, freenect_threadfunc, NULL); if (res) { printf("pthread_create failed\n"); return 1; } Mat depthf; Mat frameMat(rgbMat); Mat blobMaskOutput(frameMat.size(),CV_8UC1), outC(frameMat.size(),CV_8UC3); Mat prevImg(frameMat.size(),CV_8UC1), nextImg(frameMat.size(),CV_8UC1), prevDepth(depthMat.size(),CV_8UC1); vector<Point2f> prevPts,nextPts; vector<uchar> statusv; vector<float> errv; Rect cursor(frameMat.cols/2,frameMat.rows/2,10,10); bool update_bg_model = true; int fr = 1; int register_ctr = 0,register_secondbloc_ctr = 0; bool registered = false; Point2i appear(-1,-1); double appearTS = -1; Point2i midBlob(-1,-1); Point2i lastMove(-1,-1); int hcr_ctr = -1; vector<int> hc_stack(20); int hc_stack_ptr = 0; while (!die) { fr++; // imshow("rgb", rgbMat); pthread_mutex_lock(&buf_mutex); //Linear interpolation { Mat _tmp = (depthMat - 400.0); //minimum observed value is ~440. so shift a bit _tmp.setTo(Scalar(2048), depthMat > ((!registered) ? 700.0 : 750.0)); //cut off at 600 to create a "box" where the user interacts _tmp.convertTo(depthf, CV_8UC1, 255.0/1648.0); //values are 0-2048 (11bit), account for -400 = 1648 } { Mat _tmp; depthMat.convertTo(_tmp, CV_8UC1, 255.0/2048.0); cvtColor(_tmp, outC, CV_GRAY2BGR); } pthread_mutex_unlock(&buf_mutex); // { //saving the frames to files for debug // stringstream ss; ss << "depth_"<<fr<<".png"; // imwrite(ss.str(), depthf); // } //Logarithm interpolation - try it!, It should be more "sensitive" for closer depths // { // Mat tmp,tmp1; // depthMat.convertTo(tmp, CV_32FC1); // log(tmp,tmp1); // tmp1.convertTo(depthf, CV_8UC1, 255.0/7.6246189861593985); // } // imshow("depth",depthf); Mat blobMaskInput = depthf < 255; //anything not white is "real" depth vector<Point> ctr,ctr2; Scalar blb = refineSegments(Mat(),blobMaskInput,blobMaskOutput,ctr,ctr2,midBlob); //find contours in the foreground, choose biggest imshow("first", blobMaskOutput); /////// blb : //blb[0] = x, blb[1] = y, blb[2] = 1st blob size, blb[3] = 2nd blob size. // uint mode_counters[3] = {0}; if(blb[0]>=0 && blb[2] > 500) { //1st blob detected, and is big enough //cvtColor(depthf, outC, CV_GRAY2BGR); //closest point to the camera Point minLoc; double minval,maxval; minMaxLoc(depthf, &minval, &maxval, &minLoc, NULL, blobMaskInput); circle(outC, minLoc, 5, Scalar(0,255,0), 3); Scalar mn,stdv; meanStdDev(depthf,mn,stdv,blobMaskInput); //cout << "min: " << minval << ", max: " << maxval << ", mean: " << mn[0] << endl; blobMaskInput = depthf < (mn[0] + stdv[0]*.5); blb = refineSegments(Mat(),blobMaskInput,blobMaskOutput,ctr,ctr2,midBlob); imshow("second", blobMaskOutput); if(blb[0] >= 0 && blb[2] > 300) { //draw contour Scalar color(0,0,255); for (int idx=0; idx<ctr.size()-1; idx++) line(outC, ctr[idx], ctr[idx+1], color, 1); line(outC, ctr[ctr.size()-1], ctr[0], color, 1); if(ctr2.size() > 0) { Scalar color2(255,0,255); for (int idx=0; idx<ctr2.size()-1; idx++) line(outC, ctr2[idx], ctr2[idx+1], color2, 2); line(outC, ctr2[ctr2.size()-1], ctr2[0], color2, 2); } //draw "major axis" // Vec4f _line; Mat curve(ctr); // fitLine(curve, _line, CV_DIST_L2, 0, 0.01, 0.01); // line(outC, Point(blb[0]-_line[0]*70,blb[1]-_line[1]*70), // Point(blb[0]+_line[0]*70,blb[1]+_line[1]*70), // Scalar(255,255,0), 1); //blob center circle(outC, Point(blb[0],blb[1]), 50, Scalar(255,0,0), 3); // cout << "min depth " << minval << endl; register_ctr = MIN((register_ctr + 1),60); if(blb[3] > 5000) register_secondbloc_ctr = MIN((register_secondbloc_ctr + 1),60); if (register_ctr > 30 && !registered) { registered = true; appear.x = -1; update_bg_model = false; lastMove.x = blb[0]; lastMove.y = blb[1]; cout << "blob size " << blb[2] << endl; if(register_secondbloc_ctr < 30) { if(blb[2] > 10000) { cout << "register panner" << endl; send_event("Register", "\"mode\":\"openhand\""); } else { cout << "register pointer" << endl; send_event("Register", "\"mode\":\"theforce\""); } } else { cout << "register tab swithcer" << endl; send_event("Register", "\"mode\":\"twohands\""); } } if(registered) { stringstream ss; ss << "\"x\":" << (int)floor(blb[0]*100.0/640.0) << ",\"y\":" << (int)floor(blb[1]*100.0/480.0) << ",\"z\":" << (int)(mn[0] * 2.0); //cout << "move: " << ss.str() << endl; send_event("Move", ss.str()); //---------------------- fist detection --------------------- //calc laplacian of curve vector<Point> approxCurve; //approximate curve approxPolyDP(curve, approxCurve, 10.0, true); Mat approxCurveM(approxCurve); Mat curve_lap; calc_laplacian(approxCurveM, curve_lap); //calc laplacian hcr_ctr = 0; for (int i=0; i<approxCurve.size(); i++) { double n = norm(((Point2d*)(curve_lap.data))[i]); if (n > 10.0) { //high curvature point circle(outC, approxCurve[i], 3, Scalar(50,155,255), 2); hcr_ctr++; } } hc_stack.at(hc_stack_ptr) = hcr_ctr; hc_stack_ptr = (hc_stack_ptr + 1) % hc_stack.size(); Scalar _avg = mean(Mat(hc_stack)); if (abs(_avg[0] - (double)hcr_ctr) > 5.0) { //a big change in curvature = hand fisted/opened? cout << "Hand click!" << endl; send_event("HandClick", ""); } if (mode_state == MODE_NONE) { } // imshow("out",out); //doHist(depthf,out); { //some debug on screen.. stringstream ss; ss << "high curve pts " << hcr_ctr << ", avg " << _avg[0]; putText(outC, ss.str(), Point(50,50), CV_FONT_HERSHEY_PLAIN, 2.0,Scalar(0,0,255), 2); } } else { //not registered, look for gestures if(appear.x<0) { //first appearence of blob appear = midBlob; // update_bg_model = false; appearTS = getTickCount(); cout << "appear ("<<appearTS<<") " << appear.x << "," << appear.y << endl; } else { //blob was seen before, how much time passed double timediff = ((double)getTickCount()-appearTS)/getTickFrequency(); if (timediff > .2 && timediff < 1.0) { //enough time passed from appearence line(outC, appear, Point(blb[0],blb[1]), Scalar(0,0,255), 3); if (appear.x - blb[0] > 100) { cout << "right"<<endl; appear.x = -1; send_event("SwipeRight", ""); update_bg_model = true; register_ctr = 0; } else if (appear.x - blb[0] < -100) { cout << "left" <<endl; appear.x = -1; send_event("SwipeLeft", ""); update_bg_model = true; register_ctr = 0; } else if (appear.y - blb[1] > 100) { cout << "up" << endl; appear.x = -1; send_event("SwipeUp", ""); update_bg_model = true; register_ctr = 0; } else if (appear.y - blb[1] < -100) { cout << "down" << endl; appear.x = -1; send_event("SwipeDown", ""); update_bg_model = true; register_ctr = 0; } } if(timediff >= 1.0) { cout << "a ghost..."<<endl; update_bg_model = true; //a second passed from appearence - reset 1st appear appear.x = -1; appearTS = -1; midBlob.x = midBlob.y = -1; } } } send_image(outC); } } else { send_image(depthf); register_ctr = MAX((register_ctr - 1),0); register_secondbloc_ctr = MAX((register_secondbloc_ctr - 1),0); } imshow("blob",outC); if (register_ctr <= 15 && registered) { midBlob.x = midBlob.y = -1; registered = false; mode_state = MODE_NONE; update_bg_model = true; cout << "unregister" << endl; send_event("Unregister", ""); } char k = cvWaitKey(5); if( k == 27 ) break; if( k == ' ' ) update_bg_model = !update_bg_model; if (k=='s') { cout << "send test event" << endl; send_event("TestEvent", ""); } } printf("-- done!\n"); pthread_join(ocv_thread, NULL); pthread_exit(NULL); return 0; }
int main(int argc, char* argv[]) { uint64_t pop_size = 80; double inertia_r = 0.7; double cog_r = 2.0; double soc_r = 2.0; uint64_t num_trials = NUMBER_TRIALS; bool enable_history = false; bool fixPos = true; string out_filename = "./pso_fp_best.csv"; double last_tick_count = 0.0; try { po::options_description desc("Allowed options"); desc.add_options() ("help", "Produce help message") ("popsize", po::value<uint64_t>(), "Set Population Size") ("inertia", po::value<double>(), "Set Particle Inertia") ("cog", po::value<double>(), "Set Cognitive effect of particle") ("soc", po::value<double>(), "Set Social effect of swarm") ("trials", po::value<uint64_t>(), "Set Number of Trials") ("history", po::value<bool>(), "Enable full history") ("fix_particle", po::value<bool>(), "Disable Particle Position fixing at adjacency conflict") ("out", po::value<string>(), "Set output filename") ; po::variables_map vm; po::store(po::parse_command_line(argc, argv, desc), vm); po::notify(vm); if (vm.count("help")) { cout << desc << "\n"; return 1; } if (vm.count("popsize")) { pop_size = vm["popsize"].as<uint64_t>(); cout << "Population size was set to " << pop_size << ".\n"; } else { cout << "Population size was set to default of " << pop_size << ".\n"; } if (vm.count("inertia")) { inertia_r = vm["inertia"].as<double>(); cout << "Inertia was set to " << inertia_r << ".\n"; } else { cout << "Inertia was set to default of " << inertia_r << ".\n"; } if (vm.count("cog")) { cog_r = vm["cog"].as<double>(); cout << "COG was set to " << cog_r << ".\n"; } else { cout << "COG was set to default of " << cog_r << ".\n"; } if (vm.count("soc")) { soc_r = vm["soc"].as<double>(); cout << "SOC was set to " << soc_r << ".\n"; } else { cout << "SOC was set to default of " << soc_r << ".\n"; } if (vm.count("trials")) { num_trials = vm["trials"].as<uint64_t>(); cout << "Number of Trials was set to " << num_trials << ".\n"; } else { cout << "Number of Trials was set to default of " << num_trials << ".\n"; } if (vm.count("history")) { enable_history = vm["history"].as<bool>(); cout << "History enabled: " << enable_history << ".\n"; } else { cout << "History was set to default of " << enable_history << ".\n"; } if (vm.count("fix_particle")) { fixPos = vm["fix_particle"].as<bool>(); cout << "Fix Particle Position at adjacency conflict: " << fixPos << ".\n"; } else { cout << "Fix Particle Position at adjacency conflict was set to default of " << fixPos << ".\n"; } if (vm.count("out")) { out_filename = vm["out"].as<string>(); cout << "Output filename was set to: " << out_filename << ".\n"; } else { cout << "Output filename was set to default of: " << out_filename << ".\n"; } /* if (vm.count("rng")) { rng_seed = vm["rng"].as<uint64_t>(); cout << "OpenCV RNG was seeded with " << rng_seed << ".\n"; } else { cout << "OpenCV RNG was seeded with default of " << rng_seed << ".\n"; } */ } catch(std::exception& e) { cout << "error: " << e.what() << "\n"; return 1; } catch(...) { cout << "You threw an exception, idiot.\n"; return 1; } // Set up the bitset array that you could not initialize. uint64_t iter; particle_t mini, maxi; for (iter=0; iter<NUMBER_DIMENSIONS; iter++) { West73_Adjacency[iter].reset(); West73_NotAdjacency[iter].set(); West73_Yields[iter].Y1 = West73_Yields[iter].y1 * West73_Yields[iter].area; West73_Yields[iter].Y2 = West73_Yields[iter].y2 * West73_Yields[iter].area; West73_Yields[iter].Y3 = West73_Yields[iter].y3 * West73_Yields[iter].area; } for (iter=0; iter<196; iter++) { West73_Adjacency[West73_Adjacency_builder[iter].id-1].set(West73_Adjacency_builder[iter].ad-1); West73_NotAdjacency[West73_Adjacency_builder[iter].id-1].reset(West73_Adjacency_builder[iter].ad-1); } for (iter=0; iter<NUMBER_DIMENSIONS; iter++) { mini.pos[iter] = 0.0; maxi.pos[iter] = 4.0; mini.vel[iter] = -4.0; maxi.vel[iter] = 4.0; } // Print data to file stringstream strstr (stringstream::in | stringstream::out); ofstream outfileTheBest; outfileTheBest.open( out_filename.c_str(), std::ofstream::out | std::ofstream::app ); if ( !outfileTheBest.is_open() ) { cerr << "Unable to open file: " << out_filename << "\n"; return 1; } // outfileTheBest.precision(35); uint64_t best_default_precision = outfileTheBest.precision(); outfileTheBest << "\nNumGens,PopSize,Inertia,SOC,COG,FixPos,RNG_Seed,Trial,FitEvals,EndGen,Geno1,Geno2,Geno3,Fitness,Yield"; population *hoponpop; last_tick_count = (double) getTickCount(); uint64_t trailer_trash; for (trailer_trash=0; trailer_trash<num_trials; trailer_trash++) { uint64_t rng_seed = getTickCount(); RNG randi (rng_seed); // population(RNG& rudi, uint64_t pop_size, particle_t min_lim, particle_t max_lim, double cog, double soc, double inertia); hoponpop = new population(randi, pop_size, mini, maxi, cog_r, soc_r, inertia_r, fixPos); // cout << "Trial: " << trailer_trash << endl; if (trailer_trash==0) cout << "Trial: "; cout << trailer_trash << " "; cout.flush(); hoponpop->populator(); uint64_t generational_recursion; for (generational_recursion=0; generational_recursion<NUMBER_GENERATIONS; generational_recursion++) { // cout << "Generation: " << generational_recursion << endl; // hoponpop.iterate(); hoponpop->iterate(); // cout << "Generation over\n"; } specimen_t bestinswarm = hoponpop->BestInSwarm(); uint64_t aged = hoponpop->Age(); // outfileTheBest << "NumGens,PopSize,XO_P,XO_R,MU_R,Elitism,RNG_Seed,Trial,FitEvals,EndGen,Geno1,Geno2,Geno3,Fitness"; outfileTheBest.precision(best_default_precision); outfileTheBest << "\n" << NUMBER_GENERATIONS << "," << pop_size << "," << inertia_r << "," << soc_r << "," << cog_r << "," << fixPos << ","; outfileTheBest << rng_seed << "," << trailer_trash << "," << bestinswarm.calced << "," << aged << ","; outfileTheBest.precision(35); outfileTheBest << bestinswarm.gen.one << "," << bestinswarm.gen.two << "," << bestinswarm.gen.thr << "," << bestinswarm.fit; FITNESS_TYPE y1 = 0, y2 = 0, y3 = 0; for (iter=0; iter<NUMBER_DIMENSIONS; iter++) { if (bestinswarm.gen.one[iter]==1) y1 += West73_Yields[iter].Y1; if (bestinswarm.gen.two[iter]==1) y2 += West73_Yields[iter].Y2; if (bestinswarm.gen.thr[iter]==1) y3 += West73_Yields[iter].Y3; } outfileTheBest << "," << (y1+y2+y3); outfileTheBest.flush(); delete hoponpop; }//End of Trial cout << endl; outfileTheBest << "\n"; outfileTheBest.flush(); cout << "\tProcessing time: " << ((double) getTickCount() - last_tick_count)/getTickFrequency() << "[s]\n"; outfileTheBest.close(); return 0; }
inline float RateEstimator::instantRate () const { return 1.f / float((getTickCount() - last) / getTickFrequency()); }
void SecondOrderOptimizeFusionMove::optimize(Depth &result, const int max_iter) const { vector<Depth> proposals; genProposal(proposals); //proposals.push_back(noisyDisp); char buffer[1024] = {}; //initialize by random result.initialize(width, height, -1); const int nLabel = model->nLabel; std::default_random_engine generator; std::uniform_int_distribution<int> distribution(0, nLabel - 1); for (auto i = 0; i < width * height; ++i) { //result.setDepthAtInd(i, (double) distribution(generator)); //result.setDepthAtInd(i, noisyDisp[i]); result[i] = 0; } sprintf(buffer, "%s/temp/init_result.jpg", file_io.getDirectory().c_str()); result.saveImage(buffer, 256.0 / (double)nLabel); list<double> diffE; double lastEnergy = evaluateEnergy(result); double initialEnergy = lastEnergy; int iter = 0; const double termination = 0.1; float timming = (float) getTickCount(); const int smoothInterval = 5; while (true) { if (iter == max_iter) break; cout << "======================" << endl; Depth newProposal; // if (iter > 0 && iter % smoothInterval == 0) { // Depth orip1; // newProposal.initialize(width, height, -1); // orip1.initialize(width, height, -1); // for (auto i = 0; i < width * height; ++i) { // orip1.setDepthAtInd(i, result[i]); // newProposal.setDepthAtInd(i, result[i]); // } // int direction = iter / smoothInterval; // if (direction % 2 == 0) { // //horizontally // for (auto y = 0; y < height; ++y) { // for (auto x = 1; x < width - 1; ++x) // newProposal.setDepthAtInt(x, y, (orip1(x + 1, y) + orip1(x - 1, y)) / 2); // newProposal.setDepthAtInt(width - 1, y, orip1(width - 1, y)); // } // } else { // //vertically // for (auto x = 0; x < width; ++x) { // for (auto y = 1; y < height - 1; ++y) // newProposal.setDepthAtInt(x, y, (orip1(x, y + 1) + orip1(x, y - 1)) / 2); // newProposal.setDepthAtInt(x, height - 1, orip1(x, height - 1)); // } // } // cout << "Iteration " << iter << " using smoothing proposal " << endl; // } else { // } newProposal = proposals[iter % (proposals.size())]; printf("Fusing with proposal %d\n", (int)(iter % proposals.size())); //after several iteration, smooth the dispartiy fusionMove(result, newProposal); double e = evaluateEnergy(result); double energyDiff = lastEnergy - e; if (diffE.size() >= average_over) diffE.pop_front(); diffE.push_back(energyDiff); double average_diffe = std::accumulate(diffE.begin(), diffE.end(), 0.0) / (double) diffE.size(); printf("Done. Final energy: %.5f, energy decrease: %.5f average decrease: %.5f\n", e, energyDiff, average_diffe); lastEnergy = e; sprintf(buffer, "%s/temp/fusionmove_iter%05d.jpg", file_io.getDirectory().c_str(), iter); result.saveImage(buffer, 256.0 / (double) nLabel); if (iter > proposals.size() * 2 && average_diffe < termination) { cout << "Converge!" << endl; break; } iter++; } timming = ((float) getTickCount() - timming) / (float) getTickFrequency(); printf("All done. Initial energy: %.5f, final energy: %.5f, time usage: %.2fs\n", initialEnergy, lastEnergy, timming); }
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; }
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(); int64 t = getTickCount(); 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..."); t = getTickCount(); 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; }
QString SquareOcl::start_preview(int operate_type, QLabel* lb_image) { QString str_result = ""; Mat mat_image = imread(SQUARE_OCL_FILE_SRC, 1); vector<vector<Point> > squares; long start_time, end_time; if(mat_image.empty()) { str_result = "Couldn't load "; str_result.append(SQUARE_OCL_FILE_SRC); return str_result; } if(operate_type == OPENCL_CL_OFF_H) { start_time = getTickCount(); find_squares_cpu(mat_image, squares); end_time = getTickCount(); } else if(operate_type == OPENCL_CL_ON_H) { // OCL Init vector<ocl::Info> info; CV_Assert(cv::ocl::getDevice(info)); start_time = getTickCount(); find_squares_gpu(mat_image, squares); end_time = getTickCount(); } else { str_result = "Not Operate Mode!!"; return str_result; } draw_squares(mat_image, squares); QImage tmp_image = Mat2QImage(mat_image); lb_image->setPixmap(QPixmap::fromImage(tmp_image)); str_result = QString("Find Search Result : \n - Square Count : ") + QString::number(squares.size()); str_result = str_result + QString("\n - Search Time : "); str_result = str_result + QString::number(1000.0f * (double)(end_time - start_time) / getTickFrequency() / 10 ).append("ms"); return str_result; }
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..."); int64 t = getTickCount(); 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 (*features_finder_)(img, features_[i], rois_[i]); 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"); t = getTickCount(); (*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 MultiBandBlender::feed(InputArray _img, InputArray mask, Point tl) { #if ENABLE_LOG int64 t = getTickCount(); #endif UMat img = _img.getUMat(); CV_Assert(img.type() == CV_16SC3 || img.type() == CV_8UC3); CV_Assert(mask.type() == CV_8U); // Keep source image in memory with small border int gap = 3 * (1 << num_bands_); Point tl_new(std::max(dst_roi_.x, tl.x - gap), std::max(dst_roi_.y, tl.y - gap)); Point br_new(std::min(dst_roi_.br().x, tl.x + img.cols + gap), std::min(dst_roi_.br().y, tl.y + img.rows + gap)); // Ensure coordinates of top-left, bottom-right corners are divided by (1 << num_bands_). // After that scale between layers is exactly 2. // // We do it to avoid interpolation problems when keeping sub-images only. There is no such problem when // image is bordered to have size equal to the final image size, but this is too memory hungry approach. tl_new.x = dst_roi_.x + (((tl_new.x - dst_roi_.x) >> num_bands_) << num_bands_); tl_new.y = dst_roi_.y + (((tl_new.y - dst_roi_.y) >> num_bands_) << num_bands_); int width = br_new.x - tl_new.x; int height = br_new.y - tl_new.y; width += ((1 << num_bands_) - width % (1 << num_bands_)) % (1 << num_bands_); height += ((1 << num_bands_) - height % (1 << num_bands_)) % (1 << num_bands_); br_new.x = tl_new.x + width; br_new.y = tl_new.y + height; int dy = std::max(br_new.y - dst_roi_.br().y, 0); int dx = std::max(br_new.x - dst_roi_.br().x, 0); tl_new.x -= dx; br_new.x -= dx; tl_new.y -= dy; br_new.y -= dy; int top = tl.y - tl_new.y; int left = tl.x - tl_new.x; int bottom = br_new.y - tl.y - img.rows; int right = br_new.x - tl.x - img.cols; // Create the source image Laplacian pyramid UMat img_with_border; copyMakeBorder(_img, img_with_border, top, bottom, left, right, BORDER_REFLECT); LOGLN(" Add border to the source image, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec"); #if ENABLE_LOG t = getTickCount(); #endif std::vector<UMat> src_pyr_laplace; if (can_use_gpu_ && img_with_border.depth() == CV_16S) createLaplacePyrGpu(img_with_border, num_bands_, src_pyr_laplace); else createLaplacePyr(img_with_border, num_bands_, src_pyr_laplace); LOGLN(" Create the source image Laplacian pyramid, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec"); #if ENABLE_LOG t = getTickCount(); #endif // Create the weight map Gaussian pyramid UMat weight_map; std::vector<UMat> weight_pyr_gauss(num_bands_ + 1); if(weight_type_ == CV_32F) { mask.getUMat().convertTo(weight_map, CV_32F, 1./255.); } else // weight_type_ == CV_16S { mask.getUMat().convertTo(weight_map, CV_16S); UMat add_mask; compare(mask, 0, add_mask, CMP_NE); add(weight_map, Scalar::all(1), weight_map, add_mask); } copyMakeBorder(weight_map, weight_pyr_gauss[0], top, bottom, left, right, BORDER_CONSTANT); for (int i = 0; i < num_bands_; ++i) pyrDown(weight_pyr_gauss[i], weight_pyr_gauss[i + 1]); LOGLN(" Create the weight map Gaussian pyramid, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec"); #if ENABLE_LOG t = getTickCount(); #endif int y_tl = tl_new.y - dst_roi_.y; int y_br = br_new.y - dst_roi_.y; int x_tl = tl_new.x - dst_roi_.x; int x_br = br_new.x - dst_roi_.x; // Add weighted layer of the source image to the final Laplacian pyramid layer for (int i = 0; i <= num_bands_; ++i) { Rect rc(x_tl, y_tl, x_br - x_tl, y_br - y_tl); #ifdef HAVE_OPENCL if ( !cv::ocl::useOpenCL() || !ocl_MultiBandBlender_feed(src_pyr_laplace[i], weight_pyr_gauss[i], dst_pyr_laplace_[i](rc), dst_band_weights_[i](rc)) ) #endif { Mat _src_pyr_laplace = src_pyr_laplace[i].getMat(ACCESS_READ); Mat _dst_pyr_laplace = dst_pyr_laplace_[i](rc).getMat(ACCESS_RW); Mat _weight_pyr_gauss = weight_pyr_gauss[i].getMat(ACCESS_READ); Mat _dst_band_weights = dst_band_weights_[i](rc).getMat(ACCESS_RW); if(weight_type_ == CV_32F) { for (int y = 0; y < rc.height; ++y) { const Point3_<short>* src_row = _src_pyr_laplace.ptr<Point3_<short> >(y); Point3_<short>* dst_row = _dst_pyr_laplace.ptr<Point3_<short> >(y); const float* weight_row = _weight_pyr_gauss.ptr<float>(y); float* dst_weight_row = _dst_band_weights.ptr<float>(y); for (int x = 0; x < rc.width; ++x) { dst_row[x].x += static_cast<short>(src_row[x].x * weight_row[x]); dst_row[x].y += static_cast<short>(src_row[x].y * weight_row[x]); dst_row[x].z += static_cast<short>(src_row[x].z * weight_row[x]); dst_weight_row[x] += weight_row[x]; } } } else // weight_type_ == CV_16S { for (int y = 0; y < y_br - y_tl; ++y) { const Point3_<short>* src_row = _src_pyr_laplace.ptr<Point3_<short> >(y); Point3_<short>* dst_row = _dst_pyr_laplace.ptr<Point3_<short> >(y); const short* weight_row = _weight_pyr_gauss.ptr<short>(y); short* dst_weight_row = _dst_band_weights.ptr<short>(y); for (int x = 0; x < x_br - x_tl; ++x) { dst_row[x].x += short((src_row[x].x * weight_row[x]) >> 8); dst_row[x].y += short((src_row[x].y * weight_row[x]) >> 8); dst_row[x].z += short((src_row[x].z * weight_row[x]) >> 8); dst_weight_row[x] += weight_row[x]; } } } } #ifdef HAVE_OPENCL else {
void DeNoiser::DeNoiseSemiStatic(Mat &image, AlgParameters* params) { if(params==NULL) { params = new AlgParameters(); params->P0 = 1; params->P1 = 3; params->P2 = 1; params->P3 = 0; params->P4 = 10; params->P5 = 0; params->P6 = 0; params->P7 = 0; params->P8 = 0; params->P9 = 0; } long t = getTickCount(); if(params->P0>0) { for(int i = 0;i<params->P1;i++) medianBlur(image,image,params->P2*2+1); } if(params->P3 >0) { if(image_count<hist_size-1) { image.copyTo(*images[(hist_size-1)-image_count]); image_count++; } else { //guardar el actual en el inicio del historial image.copyTo(*images[0]); //poner el acumulador igual que la imagen image.convertTo(Accum,Accum.type()); int i,j,img,count; Mat* actual; uchar value; uchar base; float accum; for( i = 0; i < h; ++i) { for ( j = 0; j < w; ++j) { base = image.at<uchar>(i,j); accum = base; count = 1; for(img = 1; img<hist_size; ++img) { actual = images[img]; value = actual->at<uchar>(i,j); if(abs(value-base)>params->P4) break; count++; accum+=value; } Accum.at<float>(i,j) = accum/count; } } //poner el resultado en la imagen Accum.convertTo(image,image.type()); //correr las imagenes Mat* last = images[hist_size-1]; for(int i =hist_size-1;i>0;i--) images[i] = images[i-1]; images[0] = last; } } t = getTickCount() - t; float time = t*1000/getTickFrequency(); if(first_time) { printf("Noise time: %fms, Fps: %f\n",time,1000/time); first_time = false; } }