void UpdateCameraView::operator()( osg::Node* node, osg::NodeVisitor* nv ) { Chronometer timer; timer.tic(); cv::Mat currentImg; videoStream >> currentImg; if (!currentImg.empty()) { if ((!augEnvironment->posFound)&&(augEnvironment->synchroniser.tac() > 200)) //wait 1s to match { cvImg = augEnvironment->match(currentImg); augEnvironment->posFound = true; } else if ((augEnvironment->hasProjected)&&(augEnvironment->synchroniser.tac() > 300)&&(!imageCaptured)) { static int count = 0; int lastindex = augEnvironment->parameterFileName.find_last_of("/"); string dir = augEnvironment->parameterFileName.substr(0, lastindex); string errFilename = dir + "/" + augEnvironment->errorFileNameBase + "err.png"; imwrite(errFilename, currentImg); if (augEnvironment->calculateErr) { double meanErr, maxErr; augEnvironment->calculateReprojectionErr(augEnvironment->groundTruthFileName, currentImg); cout<<meanErr<<" "<<maxErr<<endl; } cvImg = currentImg.clone(); imageCaptured = true; } else { cvImg = currentImg.clone(); } osg::ref_ptr<osg::Group> backgroundCameraNode = dynamic_cast<osg::Group*>(node); for (unsigned int i = 0 ; i < backgroundCameraNode->getNumChildren() ; i++) { if (backgroundCameraNode->getChild(i)->getName() == string("Background texture")) { updateBackgroundTexture(backgroundCameraNode->getChild(i)); } } if (augEnvironment->recordVideo) { if (! vWriter.isOpened()) { vWriter.open("recordedStream.mpg", CV_FOURCC('P','I','M','1'), 20.0f, currentImg.size(), true); } vWriter << currentImg; if (! vWriter2.isOpened()) { vWriter2.open("outStream.mpg", CV_FOURCC('P','I','M','1'), 20.0f, currentImg.size(), true); } vWriter2 << cvImg; } traverse( node, nv ); } }
int Detector::batchDetect(const string &src_image_folder, const string &dst_image_folder) { if (access(src_image_folder.c_str(), 0) == -1) { return 0; } if (access(dst_image_folder.c_str(), 0) == -1) { _mkdir(dst_image_folder.c_str()); } const int MAX_DETECTION = 500; CB_RectT rects[MAX_DETECTION]; _chdir(src_image_folder.c_str()); CFileFind file_finder; bool is_working = file_finder.FindFile(); int count = 0; while (is_working) { is_working = file_finder.FindNextFile(); string file_name = file_finder.GetFileName(); string file_path = file_finder.GetFilePath(); if (file_name == "." || file_name == ".."|| file_name == "Thumbs.db") { continue; } printf("%s\n", file_path.c_str()); Mat image = imread(file_path, CV_LOAD_IMAGE_GRAYSCALE); if (image.data == NULL) { continue; } double ratio = 1.0; if (param.resize_w > 0 && param.resize_h > 0) { int w = param.resize_w; int h = image.rows * w / image.cols; ratio = (double)image.cols / w; Size size(w, h); resize(image, image, size); } param.hot_rect.top *= (image.rows / 100); param.hot_rect.bottom *= (image.rows / 100); param.hot_rect.left *= (image.cols / 100); param.hot_rect.right = (image.cols / 100); int subwin_count; int num = detect(image, MAX_DETECTION, rects, subwin_count); Mat dst_image = imread(file_path, CV_LOAD_IMAGE_COLOR); file_path = dst_image_folder + "\\" + file_name; drawRects(dst_image, num, rects, Scalar(0, 255, 0), 3, ratio); num = merger.merge(num, rects); drawRects(dst_image, num, rects, Scalar(0, 0, 255), 7, ratio); imwrite(file_path, dst_image); count++; } return 1; }
void *sobel_loop(void *number) { /* if(set_single_core_affinity()!=EXIT_SUCCESS) { perror("Core Affinity"); } */ int i=0; Mat sobel_src, src_gray, s_src; Mat grad; struct thread_limits *ptr_obj = (struct thread_limits *) number; int start_loop = ptr_obj->start_no; int stop_loop = ptr_obj->stop_no; for (i=start_loop; i<=stop_loop; i++) { /*------------------------- Loading the Image --------------------------*/ // Select the right frame sprintf(frame_name,"Frame_no_%05u.ppm",i); // Load the Image sobel_src = imread( frame_name,1 ); if( !sobel_src.data ) { perror("Reading from file"); } // Apply Gaussian Blur function GaussianBlur( sobel_src, s_src, Size(3,3), 0, 0, BORDER_DEFAULT ); // Convert Color image to gray //printf("cvtcolor\n"); cvtColor( s_src, src_gray, CV_RGB2GRAY ); //printf("after cvtcolor\n"); // Generate x and y matrix Mat sobel_x, sobel_y; Mat sobel_abs_x, sobel_abs_y; // Gradient X Sobel( src_gray, sobel_x, SOBEL_DEPTH, 1, 0, 3, SOBEL_SCALE, SOBEL_DELTA, BORDER_DEFAULT ); convertScaleAbs( sobel_x, sobel_abs_x ); // Gradient Y Sobel( src_gray, sobel_y, SOBEL_DEPTH, 0, 1, 3, SOBEL_SCALE, SOBEL_DELTA, BORDER_DEFAULT ); convertScaleAbs( sobel_y, sobel_abs_y ); // Total Gradient addWeighted( sobel_abs_x, 0.5, sobel_abs_y, 0.5, 0, grad ); /*------------------------- Storing the Image ---------------------------*/ // Storing the Image sprintf(frame_name2,"Sobel_frame_no_%05u.ppm",i); imwrite(frame_name2, grad); } // End of 'for' loop return (NULL); }
int main(int argc, char **argv) { char c; char *filepath; int cut_horizontal = 0; int cut_vertical = 0; int timed = 0; int show = 0; while ((c = getopt(argc, argv, "f:h:v:ts")) != -1) { switch (c) { case 'f': filepath = optarg; break; case 'h': cut_horizontal = (int)strtol(optarg, NULL, 10); break; case 'v': cut_vertical = (int)strtol(optarg, NULL, 10); break; case 't': timed = 1; break; case 's': show = 1; break; default: exit(1); } } // OpenCL boilerplate std::string ..._kernel_str; std::string ..._name_str = std::string("..."); std::string ..._kernel_file = std::string("..."); cl_vars_t cv; cl_kernel ...; readFile(..._kernel_file, ..._kernel_str); initialize_ocl(cv); compile_ocl_program(..., cv, ..._kernel_str.c_str(), ..._name_str.c_str()); // Read image Mat_<Vec3b> image = imread(filepath); if (!image.data) { cout << "Invalid input"; image.release(); return -1; } if (show) { imshow("Original Image", image); } SeamCarver s(image); // imshow("Gradient", s.energy); // Mat tmp = s.energy/195075.0*255.0; // s.energy.convertTo(tmp,CV_8U,-1); // imwrite("bench_gradient.jpg", tmp); // vector<uint> sm = s.findVerticalSeam(); // s.showVerticalSeam(sm); // Carving happens here double start = get_time(); ...; double elapsed = get_time() - start; // -------------------- // double start = get_time(); // for (int i = 0; i < cut_horizontal; ++i) { // vector<uint> seam = s.findHorizontalSeam(); // // s.showHorizontalSeam(seam); // s.removeHorizontalSeam(seam); // } // for (int i = 0; i < cut_vertical; ++i) { // vector<uint> seam = s.findVerticalSeam(); // // s.showVerticalSeam(seam); // s.removeVerticalSeam(seam); // } // double elapsed = get_time() - start; if (timed) { printf("Elapsed time: %.3lf seconds\n", elapsed); } Mat_<Vec3b> output = s.getImage(); imwrite("scarved.jpg", output); if (show) { imshow("Carved Image", output); while (waitKey(20) != 27); } // cout << "Seam Length: " << seam.size() << endl; // s.showImage(); // s.showEnergy(); // imwrite("bench_carved.jpg", s.getImage()); // for (int i = 0; i < 5; ++i) { // for (int j = 0; j < 5; ++j) { // cout << s.energy.at<uint32_t>(i,j) << " "; // } // cout << endl; // } uninitialize_ocl(cv); ...; clReleaseMemObject(...); image.release(); return 0; }
void writeFrame(unsigned char *frame) { imwrite(fname, cv::Mat(height, width, CV_8U, frame, cv::Mat::AUTO_STEP)); }
void *affine_loop(void *number) { /* if(set_single_core_affinity()!=EXIT_SUCCESS) { perror("Core Affinity"); } */ int block_size; int max_files; int i=0; int section=0; float pos[8]={0,0,1,0,0,1,1,1}; Point2f srcTri[4]; Point2f dstTri[4]; max_files=3601; block_size=max_files/4; Mat rot_mat( 2, 3, CV_32FC1 ); Mat warp_mat( 2, 3, CV_32FC1 ); // Output variables Mat src, warp_dst, warp_rotate_dst; struct thread_limits *ptr_obj = (struct thread_limits *) number; int start_loop = ptr_obj->start_no; int stop_loop = ptr_obj->stop_no; /*------------------------- Starting the loop --------------------------*/ for (i=start_loop; i<=stop_loop; i++) { /*------------------------- Loading the Image --------------------------*/ if(option==1) { // Select the right frame sprintf(frame_name2,"Sobel_frame_no_%05u.ppm",i); // Load the Image src = imread( frame_name2, 1 ); } else { sprintf(frame_name,"Frame_no_%05u.ppm",i); src = imread( frame_name, 1 ); } /*---------------------- Affine Transform : Warp -----------------------*/ // Setting up the output image parameters warp_dst = Mat::zeros( src.rows, src.cols, src.type() ); /*---------------------- Change the parameter values ----------------------*/ switch(section) { case 0: { pos[1]=pos[1]+0.001; pos[2]=pos[2]-0.001; pos[4]=pos[4]+0.001; pos[7]=pos[7]-0.001; // Setting parameters for matrix computation srcTri[0] = Point2f( 0,0 ); srcTri[1] = Point2f( src.cols - 1, 0 ); srcTri[2] = Point2f( 0, src.rows - 1 ); srcTri[3] = Point2f( src.cols - 1, src.rows - 1 ); dstTri[0] = Point2f( src.cols*pos[0], src.rows*pos[1] ); dstTri[1] = Point2f( src.cols*pos[2], src.rows*pos[3] ); dstTri[2] = Point2f( src.cols*pos[4], src.rows*pos[5] ); dstTri[3] = Point2f( src.cols*pos[6], src.rows*pos[7] ); section=i/block_size; //printf("Case 0: %u\t %f %f %f %f %f %f %f %f\n",i,pos[0],pos[1],pos[2],pos[3],pos[4],pos[5],pos[6],pos[7]); break; } case 1: { pos[0]=pos[0]+0.001; pos[3]=pos[3]+0.001; pos[5]=pos[5]-0.001; pos[6]=pos[6]-0.001; // Setting parameters for matrix computation srcTri[0] = Point2f( 0,0 ); srcTri[1] = Point2f( src.cols - 1, 0 ); srcTri[2] = Point2f( 0, src.rows - 1 ); srcTri[3] = Point2f( src.cols - 1, src.rows - 1 ); dstTri[0] = Point2f( src.cols*pos[0], src.rows*pos[1] ); dstTri[1] = Point2f( src.cols*pos[2], src.rows*pos[3] ); dstTri[2] = Point2f( src.cols*pos[4], src.rows*pos[5] ); dstTri[3] = Point2f( src.cols*pos[6], src.rows*pos[7] ); section=i/block_size; //printf("Case 1: %u\t %f %f %f %f %f %f %f %f\n",i,pos[0],pos[1],pos[2],pos[3],pos[4],pos[5],pos[6],pos[7]); break; } case 2: { pos[1]=pos[1]-0.001; pos[2]=pos[2]+0.001; pos[4]=pos[4]-0.001; pos[7]=pos[7]+0.001; // Setting parameters for matrix computation srcTri[0] = Point2f( 0,0 ); srcTri[1] = Point2f( src.cols - 1, 0 ); srcTri[2] = Point2f( 0, src.rows - 1 ); srcTri[3] = Point2f( src.cols - 1, src.rows - 1 ); dstTri[0] = Point2f( src.cols*pos[0], src.rows*pos[1] ); dstTri[1] = Point2f( src.cols*pos[2], src.rows*pos[3] ); dstTri[2] = Point2f( src.cols*pos[4], src.rows*pos[5] ); dstTri[3] = Point2f( src.cols*pos[6], src.rows*pos[7] ); section=i/block_size; //printf("Case 2: %u\t %f %f %f %f %f %f %f %f\n",i,pos[0],pos[1],pos[2],pos[3],pos[4],pos[5],pos[6],pos[7]); break; } case 3: { pos[0]=pos[0]-0.001; pos[3]=pos[3]-0.001; pos[5]=pos[5]+0.001; pos[6]=pos[6]+0.001; // Setting parameters for matrix computation srcTri[0] = Point2f( 0,0 ); srcTri[1] = Point2f( src.cols - 1, 0 ); srcTri[2] = Point2f( 0, src.rows - 1 ); srcTri[3] = Point2f( src.cols - 1, src.rows - 1 ); dstTri[0] = Point2f( src.cols*pos[0], src.rows*pos[1] ); dstTri[1] = Point2f( src.cols*pos[2], src.rows*pos[3] ); dstTri[2] = Point2f( src.cols*pos[4], src.rows*pos[5] ); dstTri[3] = Point2f( src.cols*pos[6], src.rows*pos[7] ); section=i/block_size; //printf("Case 3: %u\t %f %f %f %f %f %f %f %f\n",i,pos[0],pos[1],pos[2],pos[3],pos[4],pos[5],pos[6],pos[7]); break; } default: { //printf("Value: %d\n",section); //perror("Default switch() case"); break; } } // Calculate the Affine Transform matrix warp_mat = getAffineTransform( srcTri, dstTri ); // Applying the Affine Transform to the src image warpAffine( src, warp_dst, warp_mat, warp_dst.size() ); /*-------------------- Affine Transform : Rotate -----------------------*/ // Compute the Rotation Matrix Parameters Point center = Point( warp_dst.cols/2, warp_dst.rows/2 ); double angle = ROTATION_ANGLE; double scale = ISOTROPIC_SCALE_FACTOR; // Generate the Rotation Matrix rot_mat = getRotationMatrix2D( center, angle, scale ); // Rotate the Image warpAffine( warp_dst, warp_rotate_dst, rot_mat, warp_dst.size() ); /*------------------------- Storing the Image ---------------------------*/ sprintf(frame_name3,"Affine_frame_no_%05u.ppm",i); // Storing the Image imwrite(frame_name3, warp_dst); } // End of 'for' loop return NULL; }
/** * Callback on the "/xyz_image", * "/depth", "/amplitude", "/raw_amplitdue", and "/confidence" topics */ void ImageCb(const sensor_msgs::Image::ConstPtr& im, const std::string& im_type) { std::string target_file = this->outdir_; int this_idx = 0; cv_bridge::CvImagePtr cv_ptr; if (im_type == "depth") { this->depth_idx_mutex_.lock(); this_idx = this->depth_idx_; this->depth_idx_++; this->depth_idx_mutex_.unlock(); target_file += "/depth/depth_"; cv_ptr = cv_bridge::toCvCopy(im, sensor_msgs::image_encodings::MONO16); } else if (im_type == "amplitude") { this->amplitude_idx_mutex_.lock(); this_idx = this->amplitude_idx_; this->amplitude_idx_++; this->amplitude_idx_mutex_.unlock(); target_file += "/amplitude/amplitude_"; cv_ptr = cv_bridge::toCvCopy(im, sensor_msgs::image_encodings::MONO16); } else if (im_type == "raw_amplitude") { this->raw_amplitude_idx_mutex_.lock(); this_idx = this->raw_amplitude_idx_; this->raw_amplitude_idx_++; this->raw_amplitude_idx_mutex_.unlock(); target_file += "/raw_amplitude/raw_amplitude_"; cv_ptr = cv_bridge::toCvCopy(im, sensor_msgs::image_encodings::MONO16); } else if (im_type == "confidence") { this->confidence_idx_mutex_.lock(); this_idx = this->confidence_idx_; this->confidence_idx_++; this->confidence_idx_mutex_.unlock(); target_file += "/confidence/confidence_"; cv_ptr = cv_bridge::toCvCopy(im, sensor_msgs::image_encodings::MONO8); } else if (im_type == "xyz_image") { this->xyz_idx_mutex_.lock(); this_idx = this->xyz_idx_; this->xyz_idx_++; this->xyz_idx_mutex_.unlock(); target_file += "/xyz_image/xyz_"; cv_ptr = cv_bridge::toCvCopy(im, sensor_msgs::image_encodings::TYPE_16SC3); } else { return; } std::stringstream ss; ss << std::setw(10) << std::setfill('0') << this_idx; target_file += ss.str(); if (this->dump_yaml_ || im_type == "xyz_image") { cv::FileStorage storage(target_file + ".yml", cv::FileStorage::WRITE); storage << "img" << cv_ptr->image; storage.release(); } if (im_type != "xyz_image") { imwrite(target_file + ".png", cv_ptr->image); } }
//found the human body in the frame and return the foundRect as vector //The parameter Mat frame will be drawen on several rectangles void MotionDetection::PeopleDetectByHOG( Mat frame , Mat mask, double ScaleFactor, HOGDescriptor hog) { Mat frameCopy = frame.clone(); resize(frameCopy, frameCopy, Size(), ScaleFactor, ScaleFactor, INTER_AREA); cout << frameCopy.size() << endl; // imshow("test1",frameCopy); vector<Rect> found, found_filtered; //do detection //if the size of src image is too small, the program will be error as assertion fault hog.detectMultiScale(frameCopy, found, 0, Size(8,8), Size(32,32), 1.05, 2); // hog.detectMultiScale(frameCopy, found); //remove nested rectangle size_t i, j; for( i = 0; i < found.size(); i++ ) { Rect r = found[i]; for( j = 0; j < found.size(); j++ ) if( j != i && (r & found[j]) == r) break; if( j == found.size() ) found_filtered.push_back(r); } Mat drawing = Mat::zeros(frameCopy.size(),CV_8UC1); for( i = 0; i < found_filtered.size(); i++ ) { Rect r = found_filtered[i]; // the HOG detector returns slightly larger rectangles than the real objects. // so we slightly shrink the rectangles to get a nicer output. // r.x += cvRound(r.width*0.1); // r.width = cvRound(r.width*0.8); // r.y += cvRound(r.height*0.07); // r.height = cvRound(r.height*0.8); r = Rect_AdjustSizeAroundCenter(r,0.55,0.8); //rectangle(frame, r.tl(), r.br(), cv::Scalar(0,255,0), 1); //rectangle(img, r.tl(), r.br(), cv::Scalar(0,255,0), 1); // int tlx = r.tl().x, tly = r.tl().y; // for(int j = tlx; j < (tlx + r.width) ; j++) // { // for(int ii = tly; ii < (tly + r.height); ii++) // { // frameCopy_mask.at<uchar>(Point(j,ii)) = 255; // } // } rectangle( drawing, r.tl(), r.br(), Scalar(255,255,255), -1, 8, 0); } // imshow("test2",drawing); //recover to the original size resize(drawing, drawing, Size(), 1/ScaleFactor, 1/ScaleFactor, INTER_NEAREST); if(option_str == "-h") { static int counter = 0; String output_str = outPut_Mask_Path + NumberToString(++counter) + ".png"; imwrite(output_str, drawing); } //add the new mask on the final mask //!!problem: the convert between float and int will affect the size of video resolution //After doing the convert from float to int, the new video resolution must be smaller than //the orginal one, which is beacuse we always do the downScale; therefore, we use the smaller //size as the counter in order not to encounter the array problem for(int i = 0; i < drawing.cols; i ++) { for(int j = 0; j < drawing.rows; j++) { Point p = Point(i,j); if(drawing.at<uchar>(p) == 255) mask.at<uchar>(p) += mask_add_step; } } // imshow("test3",mask); }
vector<vector<float> > Encoder::extractMultiDSIFT(Mat normMat, Mat landmarks, int level){ vector<vector<float> > ret; //hard code max LBP size int tunedCellSize = 10; int tunedCols, tunedRows; int dimension = patchSize / cellSize; vector<float> dsiftCode; for (int l = 0; l < level; l++){ int tmpcellSize = cellSize - l; int tmppatchSize = tmpcellSize*dimension; for (unsigned int i = 0; i < landmarks.cols; i++){ if (landmarks.at<float>(0, i) > tmppatchSize/2 && landmarks.at<float>(1, i) > tmppatchSize/2 && landmarks.at<float>(0, i) + tmppatchSize/2 < normMat.cols && landmarks.at<float>(1, i) + tmppatchSize/2 < normMat.rows){ Mat roi(normMat, Rect(landmarks.at<float>(0, i) - tmppatchSize/2 , landmarks.at<float>(1, i) - tmppatchSize/2, tmppatchSize, tmppatchSize)); vector<float> data; for (int j = 0; j < roi.cols; j++){ for (int k = 0; k < roi.rows; k++){ data.push_back((float)roi.at<unsigned char>(k, j)/255); } } //dsift int numFrames ; int descrSize ; VlDsiftKeypoint const *frames ; float const *descrs ; VlDsiftFilter *dsift ; VlDsiftDescriptorGeometry geom ; geom.numBinX = 2 ; geom.numBinY = 2 ; geom.numBinT = 4 ; geom.binSizeX = 4 ; geom.binSizeY = 4 ; dsift = vl_dsift_new (roi.rows, roi.cols) ; vl_dsift_set_geometry(dsift, &geom) ; vl_dsift_set_steps(dsift, 2, 2) ; vl_dsift_set_flat_window(dsift, 1) ; numFrames = vl_dsift_get_keypoint_num (dsift) ; descrSize = vl_dsift_get_descriptor_size (dsift) ; geom = *vl_dsift_get_geometry (dsift) ; vl_dsift_process (dsift, &data[0]) ; frames = vl_dsift_get_keypoints (dsift) ; descrs = vl_dsift_get_descriptors (dsift) ; //cout<<"frames: "<<numFrames<<" descrs: "<<descrSize<<" cols: "<<roi.cols<<" rows: "<<roi.rows<<endl; float tranDescr[128]; for (int f = 0; f < numFrames; f++){ vl_dsift_transpose_descriptor (tranDescr, descrs + descrSize * f, geom.numBinT, geom.numBinX, geom.numBinY) ; for (int d = 0 ; d < descrSize ; d++) { tranDescr[d] = VL_MIN(512.0F * tranDescr[d], 255.0F) ; dsiftCode.push_back(tranDescr[d]); //cout<<tmpDescr[i]<<" "; } } //if (i != 0 && i != 2){ ret.push_back(dsiftCode); dsiftCode.clear(); //} vl_dsift_delete (dsift) ; } else{ cout<<"Patch out of bound: "<<landmarks.at<float>(0, i)<<" "<<landmarks.at<float>(1, i)<<endl; cout<<"landmark: "<<i<<" Cols: "<<tunedCols<<" Rows: "<<tunedRows<<endl; imwrite("tmp/outOfBound.jpg", normMat); exit(1); } } } return ret; }
std::pair<vector<Point>, std::pair<Point,double>> Detect::operator() (Mat& frame, Mat& raw, int count) { vector<Point> tips; // first find the curves in the image vector<vector<Point>> polyCurves = getPolyCurves(frame); //std::cout << polyCurves.size() << std::endl; // Find max inscribed circle for the 0th polycurve and draw it. std::pair<Point, double> maxCircle = findMaxInscribedCircle(polyCurves, raw); circle(raw, maxCircle.first, maxCircle.second, cv::Scalar(220,75,20), 1, CV_AA); // Good PolyCurve is with 3.5 * max inscribed radius vector<vector<Point>> goodPolyCurves; getRegionOfInterest(goodPolyCurves, polyCurves, maxCircle); // draw good poly curve drawContours(raw,goodPolyCurves, -1 , cv::Scalar(0,255,0), 2); // Find min enclosing circle on goodPolyCurve and draw it std::pair<Point2f, double> minCircle = findMinEnclosingCircle(goodPolyCurves); circle(raw, minCircle.first, minCircle.second, cv::Scalar(220,75,20), 1, CV_AA); // now find the convex hull for each polyCurve if (goodPolyCurves.size() < 1) { return std::pair<vector<Point>, std::pair<Point, double>>(tips, maxCircle); } // Get convex hulls vector<vector<int>> hullIndices = getConvexHulls(goodPolyCurves); vector<vector<Point>> hullPoints; for(int i = 0; i < goodPolyCurves.size(); i++) { if (goodPolyCurves[i].size() > 0) { vector<Point> hullPoint; convexHull(goodPolyCurves[i], hullPoint); hullPoints.push_back(hullPoint); } } // Draw the convex hulls drawContours(raw, hullPoints, -1, cv::Scalar(255, 0, 0), 2); for (int i = 0; i < 1; ++i) { vector<Vec4i> defects; // find convexity defects for each poly curve and draw them convexityDefects(goodPolyCurves[i], hullIndices[i], defects); defects = filterDefects(defects); vector<Point> defectEnds; for (int j = 0; j < defects.size(); ++j) { Vec4i& defect = defects[j]; int startIdx = defect[0]; int endIdx = defect[1]; int farIdx = defect[2]; Point start = goodPolyCurves[i][startIdx]; Point end = goodPolyCurves[i][endIdx]; Point far = goodPolyCurves[i][farIdx]; if(euclideanDist(far, start) > maxCircle.second) { defectEnds.push_back(start); defectEnds.push_back(end); } } tips = findFingerTips(defectEnds, maxCircle, raw); } flip(raw, raw, 1); imshow("hand", raw); // movie code (we should return the frame to HandMade and put movie code there with the others.) if(makeMoviesD) { char buffer[30]; sprintf(buffer, "detected/detected_%03d.jpg", count++); imwrite(buffer, raw); } return std::pair<vector<Point>, std::pair<Point, double>>(tips, maxCircle); }
vector<vector<float> > Encoder::extractTunedDSIFT(Mat normMat, Mat landmarks){ //eye, eyebrow, eye-band, nose-ver, mouth, left face, right face, jaw, forehead, nose-hor //40x20x2, 40x20x2, 100x40, 40x60, 60x40, 40x60,40x60, 50x20, 100x40, 60x40 vector<vector<float> > ret; //hard code max LBP size int tunedCellSize = 10; int tunedCols, tunedRows; vector<float> dsiftCode; for (int l = 0; l <= 2; l++){ for (int i = 0; i < landmarks.cols; i++){ switch (i){ case 0: case 1: case 2: case 3: tunedCols = 30 + 10*l; tunedRows = 10 + 10*l; break; case 4: tunedCols = 70 + 20*l; tunedRows = 30 + 10*l; break; case 5: tunedCols = 30 + 10*l; tunedRows = 40 + 20*l; break; case 6: tunedCols = 40 + 20*l; tunedRows = 30 + 10*l; break; case 7: case 8: tunedCols = 30 + 10*l; tunedRows = 40 + 20*l; break; case 9: tunedCols = 40 + 10*l; tunedRows = 20; break; case 10: tunedCols = 60 + 20*l; tunedRows = 30; break; case 11: tunedCols = 40 + 10*l; tunedRows = 20 + 10*l; break; default: cout<<"Wrong landmark size: "<<i<<endl; exit(1); } if (landmarks.at<float>(0, i) > tunedCols/2 && landmarks.at<float>(1, i) > tunedRows/2 && landmarks.at<float>(0, i) + tunedCols/2 < normMat.cols && landmarks.at<float>(1, i) + tunedRows/2 < normMat.rows){ Mat roi(normMat, Rect(landmarks.at<float>(0, i) - tunedCols/2 , landmarks.at<float>(1, i) - tunedRows/2, tunedCols, tunedRows)); vector<float> data; for (int j = 0; j < roi.cols; j++){ for (int k = 0; k < roi.rows; k++){ data.push_back((float)roi.at<unsigned char>(k, j)/255); } } //dsift int numFrames ; int descrSize ; VlDsiftKeypoint const *frames ; float const *descrs ; VlDsiftFilter *dsift ; VlDsiftDescriptorGeometry geom ; geom.numBinX = 2 ; geom.numBinY = 2 ; geom.numBinT = 4 ; geom.binSizeX = 4 ; geom.binSizeY = 4 ; dsift = vl_dsift_new (roi.rows, roi.cols) ; vl_dsift_set_geometry(dsift, &geom) ; vl_dsift_set_steps(dsift, 2, 2) ; vl_dsift_set_flat_window(dsift, 1) ; numFrames = vl_dsift_get_keypoint_num (dsift) ; descrSize = vl_dsift_get_descriptor_size (dsift) ; geom = *vl_dsift_get_geometry (dsift) ; vl_dsift_process (dsift, &data[0]) ; frames = vl_dsift_get_keypoints (dsift) ; descrs = vl_dsift_get_descriptors (dsift) ; //cout<<"frames: "<<numFrames<<" descrs: "<<descrSize<<" cols: "<<roi.cols<<" rows: "<<roi.rows<<endl; float tranDescr[128]; for (int f = 0; f < numFrames; f++){ vl_dsift_transpose_descriptor (tranDescr, descrs + descrSize * f, geom.numBinT, geom.numBinX, geom.numBinY) ; for (int d = 0 ; d < descrSize ; d++) { tranDescr[d] = VL_MIN(512.0F * tranDescr[d], 255.0F) ; dsiftCode.push_back(tranDescr[d]); //cout<<tmpDescr[i]<<" "; } } //if (i != 0 && i != 2){ ret.push_back(dsiftCode); dsiftCode.clear(); //} vl_dsift_delete (dsift) ; } else{ cout<<"Patch out of bound: "<<landmarks.at<float>(0, i)<<" "<<landmarks.at<float>(1, i)<<endl; cout<<"landmark: "<<i<<" Cols: "<<tunedCols<<" Rows: "<<tunedRows<<endl; imwrite("tmp/outOfBound.jpg", normMat); exit(1); } } } return ret; }
cv::Point findEyeCenter(cv::Mat face, cv::Rect eye, std::string debugWindow) { cv::Mat eyeROIUnscaled = face(eye); cv::Mat eyeROI; scaleToFastSize(eyeROIUnscaled, eyeROI); // draw eye region rectangle(face,eye,1234); //-- Find the gradient cv::Mat gradientX = computeMatXGradient(eyeROI); cv::Mat gradientY = computeMatXGradient(eyeROI.t()).t(); //-- Normalize and threshold the gradient // compute all the magnitudes cv::Mat mags = matrixMagnitude(gradientX, gradientY); //compute the threshold double gradientThresh = computeDynamicThreshold(mags, kGradientThreshold); //double gradientThresh = kGradientThreshold; //double gradientThresh = 0; //normalize for (int y = 0; y < eyeROI.rows; ++y) { double *Xr = gradientX.ptr<double>(y), *Yr = gradientY.ptr<double>(y); const double *Mr = mags.ptr<double>(y); for (int x = 0; x < eyeROI.cols; ++x) { double gX = Xr[x], gY = Yr[x]; double magnitude = Mr[x]; if (magnitude > gradientThresh) { Xr[x] = gX/magnitude; Yr[x] = gY/magnitude; } else { Xr[x] = 0.0; Yr[x] = 0.0; } } } imshow(debugWindow,gradientX); //-- Create a blurred and inverted image for weighting cv::Mat weight; GaussianBlur( eyeROI, weight, cv::Size( kWeightBlurSize, kWeightBlurSize ), 0, 0 ); for (int y = 0; y < weight.rows; ++y) { unsigned char *row = weight.ptr<unsigned char>(y); for (int x = 0; x < weight.cols; ++x) { row[x] = (255 - row[x]); } } //imshow(debugWindow,weight); //-- Run the algorithm! cv::Mat outSum = cv::Mat::zeros(eyeROI.rows,eyeROI.cols,CV_64F); // for each possible gradient location // Note: these loops are reversed from the way the paper does them // it evaluates every possible center for each gradient location instead of // every possible gradient location for every center. printf("Eye Size: %ix%i\n",outSum.cols,outSum.rows); for (int y = 0; y < weight.rows; ++y) { const unsigned char *Wr = weight.ptr<unsigned char>(y); const double *Xr = gradientX.ptr<double>(y), *Yr = gradientY.ptr<double>(y); for (int x = 0; x < weight.cols; ++x) { double gX = Xr[x], gY = Yr[x]; if (gX == 0.0 && gY == 0.0) { continue; } testPossibleCentersFormula(x, y, Wr[x], gX, gY, outSum); } } // scale all the values down, basically averaging them double numGradients = (weight.rows*weight.cols); cv::Mat out; outSum.convertTo(out, CV_32F,1.0/numGradients); //imshow(debugWindow,out); //-- Find the maximum point cv::Point maxP; double maxVal; cv::minMaxLoc(out, NULL,&maxVal,NULL,&maxP); //-- Flood fill the edges if(kEnablePostProcess) { cv::Mat floodClone; //double floodThresh = computeDynamicThreshold(out, 1.5); double floodThresh = maxVal * kPostProcessThreshold; cv::threshold(out, floodClone, floodThresh, 0.0f, cv::THRESH_TOZERO); if(kPlotVectorField) { //plotVecField(gradientX, gradientY, floodClone); imwrite("eyeFrame.png",eyeROIUnscaled); } cv::Mat mask = floodKillEdges(floodClone); //imshow(debugWindow + " Mask",mask); //imshow(debugWindow,out); // redo max cv::minMaxLoc(out, NULL,&maxVal,NULL,&maxP,mask); } return unscalePoint(maxP,eye); }
void GrabCutMF::Demo(CStr &wkDir, float w1, float w2, float w3, float alpha, float beta, float gama, float mu) { CStr imgDir = wkDir + "Imgs/", salDir = wkDir + "Sal4N/", iluDir = wkDir + "Ilu4N/"; vecS namesNE; int imgNum = CmFile::GetNamesNE(imgDir + "*.jpg", namesNE); CmFile::MkDir(salDir); CmFile::MkDir(iluDir); printf("w1 = %g, w2 = %g, w3 = %g, alpha = %g, beta = %g, gama = %g, mu = %g\n", w1, w2, w3, alpha, beta, gama, mu); // Number of labels //const int M = 2; CmTimer tm("Time"), tmIni("TimeIni"), tmRef("TimeRef"); double maxWeight = 2; // 2: 0.958119, 1: 0.953818, tm.Start(); #pragma omp parallel for for (int i = 0; i < imgNum; i++){ printf("Processing %d/%d: %s%s.jpg%20s\r\n", i, imgNum, _S(imgDir), _S(namesNE[i]), ""); CmFile::Copy(imgDir + namesNE[i] + ".jpg", salDir + namesNE[i] + ".jpg"); //CmFile::Copy(imgDir + namesNE[i] + ".png", salDir + namesNE[i] + "_GT.png"); Mat _imMat3u = imread(imgDir + namesNE[i] + ".jpg"), imMat3f, imMat3u, gt1u; Mat _gt1u = imread(imgDir + namesNE[i] + ".png", CV_LOAD_IMAGE_GRAYSCALE); if(_gt1u.rows == 0 && _gt1u.cols == 0) { cout<<"Error: unable to open "<<(imgDir + namesNE[i] + ".png")<<endl; continue; } blur(_gt1u, _gt1u, Size(3,3)); Mat _res1u = Mat::zeros(_imMat3u.size(), CV_8U); Rect wkRect = CmCv::GetMaskRange(_gt1u, 30, 200); _imMat3u(wkRect).copyTo(imMat3u); _gt1u(wkRect).copyTo(gt1u); imMat3u.convertTo(imMat3f, CV_32FC3, 1/255.0); Rect rect = CmCv::GetMaskRange(gt1u, 5, 128); Mat edge1u; // Use an edge map to expand the background mask in flat (no edge) region CmCv::CannySimpleRGB(imMat3u, edge1u, 120, 1200, 5); dilate(edge1u, edge1u, Mat(), Point(-1, -1), 3); Mat borderMask1u(imMat3u.size(), CV_8U), tmpMask; memset(borderMask1u.data, 255, borderMask1u.step.p[0] * borderMask1u.rows); borderMask1u(rect) = Scalar(0); getGrabMask(edge1u, borderMask1u); //* The Mean field based GrabCut //tmIni.Start(); GrabCutMF cutMF(imMat3f, imMat3u, salDir + namesNE[i], w1, w2, w3, alpha, beta, gama, mu); //Mat borderMask1u = CmCv::getGrabMask(imMat3u, rect), tmpMask; imwrite(salDir + namesNE[i] + "_BM.png", borderMask1u); imwrite(salDir + namesNE[i] + ".jpg", imMat3u); //imwrite(salDir + namesNE[i] + "_GT.png", gt1u); cutMF.initialize(rect, borderMask1u, (float)maxWeight, true); //cutMF.setGrabReg(rect, CmCv::getGrabMask(imMat3u, rect)); //tmIni.Stop(); //tmRef.Start(); cutMF.refine(); //tmRef.Stop(); Mat res1u = cutMF.drawResult(), invRes1u; res1u.copyTo(_res1u(wkRect)); imwrite(salDir + namesNE[i] + "_GCMF1.png", _res1u); //if (sum(res1u).val[0] < EPS){ // printf("%s.jpg don't contains a salient object\n", _S(namesNE[i])); // continue; //} dilate(res1u(rect), tmpMask, Mat(), Point(-1, -1), 10); bitwise_not(tmpMask, borderMask1u(rect)); getGrabMask(edge1u, borderMask1u); //blur(res1u, invRes1u, Size(3, 3)); // //PointSeti hullPnts; //convexHullOfMask(invRes1u, hullPnts); //fillConvexPoly(invRes1u, hullPnts, 255); //bitwise_not(invRes1u, invRes1u); //bitwise_or(invRes1u, borderMask1u, borderMask1u); imwrite(salDir + namesNE[i] + "_MB2.png", borderMask1u); //double w = maxWeight - (maxWeight-1)*sum(res1u).val[0]/(borderMask1u.rows*borderMask1u.cols*255 - sum(borderMask1u).val[0]); cutMF.initialize(rect, borderMask1u, 2); cutMF.refine(); //printf("weight = %g\n", w); //imshow("Result", res1u); //imshow("Possible", borderMask1u); //imshow("Image", imMat3f); //waitKey(0); res1u = cutMF.drawResult(); Rect rectRes = CmCv::GetMaskRange(res1u, 5, 128); if (rectRes.width * 1.1 < rect.width || rectRes.height * 1.1 < rect.height){ // Too short result printf("%s.jpg contains a small object\n", _S(namesNE[i])); memset(borderMask1u.data, 255, borderMask1u.step.p[0] * borderMask1u.rows); borderMask1u(rect) = Scalar(0); cutMF.initialize(rect, borderMask1u, 2); cutMF.refine(); res1u = cutMF.drawResult(); imwrite(salDir + namesNE[i] + "_MB2.png", borderMask1u); CmFile::Copy2Dir(salDir + namesNE[i] + "*.*", iluDir); imwrite(iluDir + namesNE[i] + "_GCMF.png", _res1u); } res1u.copyTo(_res1u(wkRect)); imwrite(salDir + namesNE[i] + "_GCMF.png", _res1u); } tm.Stop(); double avgTime = tm.TimeInSeconds()/imgNum; printf("Speed: %gs, %gfps\t\t\n", avgTime, 1/avgTime); //tmIni.Report(); //tmRef.Report(); //CmEvaluation::EvalueMask(imgDir + "*.png", salDir, ".png", "_GC.png"); char* pDes[] = { "GCMF1", "GCMF"}; //, "CudaG4", "Onecut", "GC", "CudaH", vecS des = charPointers2StrVec (pDes); CStr rootDir = CmFile::GetFatherFolder(wkDir), dbName = CmFile::GetNameNE(wkDir.substr(0, wkDir.size() - 1)); CmEvaluation::EvalueMask(imgDir + "*.png", salDir, des, wkDir.substr(0, wkDir.size() - 1) + "Res.m", 0.3, false, "", dbName); }
//./Sp_demo -i <img_filename> -n <nPixels_on_side> --i_std <i_std> int main( int argc, char** argv ) { // get the image filename, nPixels_in_square_side and i_std // the defaults String filename = "image/2.jpg"; int nPixels_in_square_side = 15; int i_std = 20; for (int i = 1; i < argc; ++i) { std::string arg = argv[i]; if ((arg == "-h") || (arg == "--help")) { show_usage(argv[0]); return 0; } else if ((arg == "-i") || (arg == "--img_filename")) { if (i + 1 < argc) { i++; filename = argv[i]; } else { std::cerr << "--img_filename option requires one argument." << std::endl; return 1; } } else if ((arg == "-n") || (arg == "--nPixels_on_side")) { if (i + 1 < argc) { i++; nPixels_in_square_side = atoi (argv[i]); if (nPixels_in_square_side<3) { std::cerr << "--nPixels_in_square_side option requires nPixels_in_square_side >= 3." << std::endl; return 1; } } else { std::cerr << "--nPixels_on_side option requires one argument." << std::endl; return 1; } } else if (arg == "--i_std") { if (i + 1 < argc) { i++; i_std = atoi (argv[i]); if (i_std<5 || i_std>40) { std::cerr << "--i_std option requires 5<= value <=40." << std::endl; return 1; } } else { std::cerr << "--i_std option requires a number." << std::endl; return 1; } }else{ } } cout << "finish reading the arguments" << endl; Mat image = imread(filename, CV_LOAD_IMAGE_COLOR); // Check for invalid input if(! image.data ){ cout << "Could not open or find the image" << std::endl ; return -1; } cout << "finish reading the image" << endl; //Part 1: Specify the parameters: superpixel_options spoptions = get_sp_options(nPixels_in_square_side, i_std); // Part 2 : prepare for segmentation int dimy = image.rows; int dimx = image.cols; Superpixels sp = Superpixels(dimx, dimy, spoptions); //cout << "finish init sp" << endl; cudaDeviceSynchronize(); sp.load_img((unsigned char*)(image.data)); //cout << "finish loading the image" << endl; // Part 3: Do the superpixel segmentation clock_t start,finish; start = clock(); sp.calc_seg(); cudaDeviceSynchronize(); sp.gpu2cpu(); cudaError_t err_t = cudaDeviceSynchronize(); if (err_t){ std::cerr << "CUDA error after cudaDeviceSynchronize. " << std::endl; return 0; } finish = clock(); cout<< "Segmentation takes " << ((double)(finish-start)/CLOCKS_PER_SEC) << " sec" << endl; // Part 4: Save the mean/boundary image Mat border_img = sp.get_img_overlaid(); String fname_res_border = "image/result/img_border.png"; imwrite(fname_res_border, border_img); cout << "saved " << fname_res_border << endl; Mat mean_img = sp.get_img_cartoon(); String fname_res_mean = "image/result/img_mean.png"; imwrite(fname_res_mean, mean_img); cout << "saving " << fname_res_mean << endl; return 0; }
vector<Plate> DetectRegions::segment(Mat input){ vector<Plate> output; //convert image to gray Mat img_gray; cvtColor(input, img_gray, CV_BGR2GRAY); blur(img_gray, img_gray, Size(5,5)); //Finde vertical lines. Car plates have high density of vertical lines Mat img_sobel; Sobel(img_gray, img_sobel, CV_8U, 1, 0, 3, 1, 0, BORDER_DEFAULT); if(showSteps) imshow("Sobel", img_sobel); //threshold image Mat img_threshold; threshold(img_sobel, img_threshold, 0, 255, CV_THRESH_OTSU+CV_THRESH_BINARY); if(showSteps) imshow("Threshold", img_threshold); //Morphplogic operation close Mat element = getStructuringElement(MORPH_RECT, Size(17, 3) ); morphologyEx(img_threshold, img_threshold, CV_MOP_CLOSE, element); if(showSteps) imshow("Close", img_threshold); //Find contours of possibles plates vector< vector< Point> > contours; findContours(img_threshold, contours, // a vector of contours CV_RETR_EXTERNAL, // retrieve the external contours CV_CHAIN_APPROX_NONE); // all pixels of each contours //Start to iterate to each contour founded vector<vector<Point> >::iterator itc= contours.begin(); vector<RotatedRect> rects; //Remove patch that are no inside limits of aspect ratio and area. while (itc!=contours.end()) { //Create bounding rect of object RotatedRect mr= minAreaRect(Mat(*itc)); if( !verifySizes(mr)){ itc= contours.erase(itc); }else{ ++itc; rects.push_back(mr); } } // Draw blue contours on a white image cv::Mat result; input.copyTo(result); cv::drawContours(result,contours, -1, // draw all contours cv::Scalar(255,0,0), // in blue 1); // with a thickness of 1 for(int i=0; i< (int)rects.size(); i++){ //For better rect cropping for each posible box //Make floodfill algorithm because the plate has white background //And then we can retrieve more clearly the contour box circle(result, rects[i].center, 3, Scalar(0,255,0), -1); //get the min size between width and height float minSize=(rects[i].size.width < rects[i].size.height)?rects[i].size.width:rects[i].size.height; minSize=minSize-minSize*0.5; //initialize rand and get 5 points around center for floodfill algorithm //srand ( time(NULL) ); //Initialize floodfill parameters and variables Mat mask; mask.create(input.rows + 2, input.cols + 2, CV_8UC1); mask= Scalar::all(0); int loDiff = 30; int upDiff = 30; int connectivity = 4; int newMaskVal = 255; int NumSeeds = 10; Rect ccomp; int flags = connectivity + (newMaskVal << 8 ) + CV_FLOODFILL_FIXED_RANGE + CV_FLOODFILL_MASK_ONLY; for(int j=0; j<NumSeeds; j++){ Point seed; seed.x=rects[i].center.x+rand()%(int)minSize-(minSize/2); seed.y=rects[i].center.y+rand()%(int)minSize-(minSize/2); circle(result, seed, 1, Scalar(0,255,255), -1); floodFill(input, mask, seed, Scalar(255,0,0), &ccomp, Scalar(loDiff, loDiff, loDiff), Scalar(upDiff, upDiff, upDiff), flags); } if(showSteps) imshow("MASK", mask); //cvWaitKey(0); //Check new floodfill mask match for a correct patch. //Get all points detected for get Minimal rotated Rect vector<Point> pointsInterest; Mat_<uchar>::iterator itMask= mask.begin<uchar>(); Mat_<uchar>::iterator end= mask.end<uchar>(); for( ; itMask!=end; ++itMask) if(*itMask==255) pointsInterest.push_back(itMask.pos()); RotatedRect minRect = minAreaRect(pointsInterest); if(verifySizes(minRect)){ // rotated rectangle drawing Point2f rect_points[4]; minRect.points( rect_points ); for( int j = 0; j < 4; j++ ) line( result, rect_points[j], rect_points[(j+1)%4], Scalar(0,0,255), 1, 8 ); //Get rotation matrix float r= (float)minRect.size.width / (float)minRect.size.height; float angle=minRect.angle; if(r<1) angle=90+angle; Mat rotmat= getRotationMatrix2D(minRect.center, angle,1); //Create and rotate image Mat img_rotated; warpAffine(input, img_rotated, rotmat, input.size(), CV_INTER_CUBIC); //Crop image Size rect_size=minRect.size; if(r < 1) swap(rect_size.width, rect_size.height); Mat img_crop; getRectSubPix(img_rotated, rect_size, minRect.center, img_crop); Mat resultResized; resultResized.create(33,144, CV_8UC3); resize(img_crop, resultResized, resultResized.size(), 0, 0, INTER_CUBIC); //Equalize croped image Mat grayResult; cvtColor(resultResized, grayResult, CV_BGR2GRAY); blur(grayResult, grayResult, Size(3,3)); grayResult=histeq(grayResult); if(saveRegions){ stringstream ss(stringstream::in | stringstream::out); ss << ".\\output\\possible_plates\\" << filename << "_" << i << ".JPG"; imwrite(ss.str(), grayResult); } output.push_back(Plate(grayResult,minRect.boundingRect())); } } if(showSteps) imshow("Contours", result); return output; }