/** * @brief getPixelValue returns the pixel value at location of point in matrix M * @param M input matrix * @param p indicates the location * @return */ T getPixelValue(Mat &M,Point p) { T val=M.data + M.step*p.y + p.x*M.elemSize(); return val; }
static void convertFromCCS( const Mat& _src0, const Mat& _src1, Mat& _dst, int flags ) { if( _dst.rows > 1 && (_dst.cols > 1 || (flags & DFT_ROWS)) ) { int i, count = _dst.rows, len = _dst.cols; bool is2d = (flags & DFT_ROWS) == 0; Mat src0row, src1row, dstrow; for( i = 0; i < count; i++ ) { int j = !is2d || i == 0 ? i : count - i; src0row = _src0.row(i); src1row = _src1.row(j); dstrow = _dst.row(i); convertFromCCS( src0row, src1row, dstrow, 0 ); } if( is2d ) { src0row = _src0.col(0); dstrow = _dst.col(0); convertFromCCS( src0row, src0row, dstrow, 0 ); if( (len & 1) == 0 ) { src0row = _src0.col(_src0.cols - 1); dstrow = _dst.col(len/2); convertFromCCS( src0row, src0row, dstrow, 0 ); } } } else { int i, n = _dst.cols + _dst.rows - 1, n2 = (n+1) >> 1; int cn = _src0.channels(); int srcstep = cn, dststep = 1; if( !_dst.isContinuous() ) dststep = (int)(_dst.step/_dst.elemSize()); if( !_src0.isContinuous() ) srcstep = (int)(_src0.step/_src0.elemSize1()); if( _dst.depth() == CV_32F ) { Complexf* dst = _dst.ptr<Complexf>(); const float* src0 = _src0.ptr<float>(); const float* src1 = _src1.ptr<float>(); int delta0, delta1; dst->re = src0[0]; dst->im = 0; if( (n & 1) == 0 ) { dst[n2*dststep].re = src0[(cn == 1 ? n-1 : n2)*srcstep]; dst[n2*dststep].im = 0; } delta0 = srcstep; delta1 = delta0 + (cn == 1 ? srcstep : 1); if( cn == 1 ) srcstep *= 2; for( i = 1; i < n2; i++, delta0 += srcstep, delta1 += srcstep ) { float t0 = src0[delta0]; float t1 = src0[delta1]; dst[i*dststep].re = t0; dst[i*dststep].im = t1; t0 = src1[delta0]; t1 = -src1[delta1]; dst[(n-i)*dststep].re = t0; dst[(n-i)*dststep].im = t1; } } else { Complexd* dst = _dst.ptr<Complexd>(); const double* src0 = _src0.ptr<double>(); const double* src1 = _src1.ptr<double>(); int delta0, delta1; dst->re = src0[0]; dst->im = 0; if( (n & 1) == 0 ) { dst[n2*dststep].re = src0[(cn == 1 ? n-1 : n2)*srcstep]; dst[n2*dststep].im = 0; } delta0 = srcstep; delta1 = delta0 + (cn == 1 ? srcstep : 1); if( cn == 1 ) srcstep *= 2; for( i = 1; i < n2; i++, delta0 += srcstep, delta1 += srcstep ) { double t0 = src0[delta0]; double t1 = src0[delta1]; dst[i*dststep].re = t0; dst[i*dststep].im = t1; t0 = src1[delta0]; t1 = -src1[delta1]; dst[(n-i)*dststep].re = t0; dst[(n-i)*dststep].im = t1; } } } }
static void mulComplex( const Mat& src1, const Mat& src2, Mat& dst, int flags ) { dst.create(src1.rows, src1.cols, src1.type()); int i, j, depth = src1.depth(), cols = src1.cols*2; CV_Assert( src1.size == src2.size && src1.type() == src2.type() && (src1.type() == CV_32FC2 || src1.type() == CV_64FC2) ); for( i = 0; i < dst.rows; i++ ) { if( depth == CV_32F ) { const float* a = src1.ptr<float>(i); const float* b = src2.ptr<float>(i); float* c = dst.ptr<float>(i); if( !(flags & CV_DXT_MUL_CONJ) ) for( j = 0; j < cols; j += 2 ) { double re = (double)a[j]*b[j] - (double)a[j+1]*b[j+1]; double im = (double)a[j+1]*b[j] + (double)a[j]*b[j+1]; c[j] = (float)re; c[j+1] = (float)im; } else for( j = 0; j < cols; j += 2 ) { double re = (double)a[j]*b[j] + (double)a[j+1]*b[j+1]; double im = (double)a[j+1]*b[j] - (double)a[j]*b[j+1]; c[j] = (float)re; c[j+1] = (float)im; } } else { const double* a = src1.ptr<double>(i); const double* b = src2.ptr<double>(i); double* c = dst.ptr<double>(i); if( !(flags & CV_DXT_MUL_CONJ) ) for( j = 0; j < cols; j += 2 ) { double re = a[j]*b[j] - a[j+1]*b[j+1]; double im = a[j+1]*b[j] + a[j]*b[j+1]; c[j] = re; c[j+1] = im; } else for( j = 0; j < cols; j += 2 ) { double re = a[j]*b[j] + a[j+1]*b[j+1]; double im = a[j+1]*b[j] - a[j]*b[j+1]; c[j] = re; c[j+1] = im; } } } }
// returns sequence of squares detected on the image. // the sequence is stored in the specified memory storage static void findSquares( const Mat& image, vector<vector<Point> >& squares ) { squares.clear(); Mat pyr, timg, gray0(image.size(), CV_8U), gray; // down-scale and upscale the image to filter out the noise pyrDown(image, pyr, Size(image.cols/2, image.rows/2)); pyrUp(pyr, timg, image.size()); vector<vector<Point> > contours; // find squares in every color plane of the image for( int c = 0; c < 3; c++ ) { int ch[] = {c, 0}; mixChannels(&timg, 1, &gray0, 1, ch, 1); // try several threshold levels for( int l = 0; l < N; l++ ) { // hack: use Canny instead of zero threshold level. // Canny helps to catch squares with gradient shading if( l == 0 ) { // apply Canny. Take the upper threshold from slider // and set the lower to 0 (which forces edges merging) Canny(gray0, gray, 0, thresh, 5); // dilate canny output to remove potential // holes between edge segments dilate(gray, gray, Mat(), Point(-1,-1)); } else { // apply threshold if l!=0: // tgray(x,y) = gray(x,y) < (l+1)*255/N ? 255 : 0 cv::threshold(gray0, gray, (l+1)*255/N, 255, THRESH_BINARY); } // find contours and store them all as a list findContours(gray, contours, RETR_LIST, CHAIN_APPROX_SIMPLE); vector<Point> approx; // test each contour for( size_t i = 0; i < contours.size(); i++ ) { // approximate contour with accuracy proportional // to the contour perimeter approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true)*0.02, true); // square contours should have 4 vertices after approximation // relatively large area (to filter out noisy contours) // and be convex. // Note: absolute value of an area is used because // area may be positive or negative - in accordance with the // contour orientation if( approx.size() == 4 && fabs(contourArea(Mat(approx))) > 1000 && isContourConvex(Mat(approx)) ) { double maxCosine = 0; for( int j = 2; j < 5; j++ ) { // find the maximum cosine of the angle between joint edges double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1])); maxCosine = MAX(maxCosine, cosine); } // if cosines of all angles are small // (all angles are ~90 degree) then write quandrange // vertices to resultant sequence if( maxCosine < 0.3 ) squares.push_back(approx); } } } } }
int _tmain(int argc, _TCHAR* argv[]) { //Initial detecting unity SurfFeatureDetector detector(400); vector<KeyPoint> lastKeyPoint; vector<KeyPoint> currentKeyPoint; //Read Video VideoCapture capture("..\\testVideo.mp4"); //Create the Mat for storing current frame and last frame Mat frame; Mat lastFrame; if (!capture.isOpened()) { cout<<"Video open failed!"<<endl; return 0; } namedWindow("Window"); while(1) { //Compute the key points in each frame-------- //Get Start Time to evaluate time period of the algorithm double t = (double)getTickCount(); //Read a frame from video capture //And move it to previous frame lastFrame = frame.clone(); capture>>frame; if(lastFrame.empty()) { //Skip first frame; continue; } //进行特征点提取 detector.detect(lastFrame,lastKeyPoint); detector.detect(frame,currentKeyPoint); //生成描述符 SurfDescriptorExtractor extractor; Mat descriptor1; Mat descriptor2; extractor.compute(frame,lastKeyPoint,descriptor1); extractor.compute(frame,currentKeyPoint,descriptor2); //特征匹配 FlannBasedMatcher matcher; vector<DMatch> matches; matcher.match( descriptor1, descriptor2, matches ); //-- Quick calculation of max and min distances between keypoints double min_dist = 100; double max_dist = 0; for( int i = 0; i < descriptor1.rows; i++ ) { double dist = matches[i].distance; if( dist < min_dist ) min_dist = dist; if( dist > max_dist ) max_dist = dist; } //-- Draw only "good" matches (i.e. whose distance is less than 2*min_dist ) //-- PS.- radiusMatch can also be used here. vector< DMatch > good_matches; for( int i = 0; i < descriptor1.rows; i++ ) { if( matches[i].distance < 2*min_dist ) { good_matches.push_back( matches[i]); } } //Compute the Total Time t = ((double)getTickCount()-t)/getTickFrequency(); cout<<"time is:"<<t<<endl; //-- Draw only "good" matches Mat img_matches; drawMatches( frame, keyPoint, frame, keyPoint2, good_matches, img_matches, Scalar::all(-1), Scalar::all(-1), vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS ); if (frame.empty()) { cout << " < < < Video End! > > > "<<endl; break; } imshow("Window",frame); waitKey(10); } return 0; ////画出KeyPoint //drawKeypoints(img,keyPoint,img); //drawKeypoints(smallimg,keyPoint2,smallimg); waitKey(); return 0; }
int main( int argc, char** argv ) { VideoCapture cap; TermCriteria termcrit(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,20,0.03); Size subPixWinSize(10,10), winSize(31,31); const int MAX_COUNT = 500; bool needToInit = false; bool nightMode = false; if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0]))) cap.open(argc == 2 ? argv[1][0] - '0' : 0); else if( argc == 2 ) cap.open(argv[1]); if( !cap.isOpened() ) { cout << "Could not initialize capturing...\n"; return 0; } help(); namedWindow( "LK Demo", 1 ); setMouseCallback( "LK Demo", onMouse, 0 ); Mat gray, prevGray, image; vector<Point2f> points[2]; for(;;) { Mat frame; cap >> frame; if( frame.empty() ) break; frame.copyTo(image); cvtColor(image, gray, CV_BGR2GRAY); if( nightMode ) image = Scalar::all(0); if( needToInit ) { // automatic initialization goodFeaturesToTrack(gray, points[1], MAX_COUNT, 0.01, 10, Mat(), 3, 0, 0.04); cornerSubPix(gray, points[1], subPixWinSize, Size(-1,-1), termcrit); addRemovePt = false; } else if( !points[0].empty() ) { vector<uchar> status; vector<float> err; if(prevGray.empty()) gray.copyTo(prevGray); calcOpticalFlowPyrLK(prevGray, gray, points[0], points[1], status, err, winSize, 3, termcrit, 0, 0.001); size_t i, k; for( i = k = 0; i < points[1].size(); i++ ) { if( addRemovePt ) { if( norm(point - points[1][i]) <= 5 ) { addRemovePt = false; continue; } } if( !status[i] ) continue; points[1][k++] = points[1][i]; circle( image, points[1][i], 3, Scalar(0,255,0), -1, 8); } points[1].resize(k); } if( addRemovePt && points[1].size() < (size_t)MAX_COUNT ) { vector<Point2f> tmp; tmp.push_back(point); cornerSubPix( gray, tmp, winSize, cvSize(-1,-1), termcrit); points[1].push_back(tmp[0]); addRemovePt = false; } needToInit = false; imshow("LK Demo", image); char c = (char)waitKey(10); if( c == 27 ) break; switch( c ) { case 'r': needToInit = true; break; case 'c': points[1].clear(); break; case 'n': nightMode = !nightMode; break; default: ; } std::swap(points[1], points[0]); swap(prevGray, gray); } return 0; }
int main(int argc, const char* argv[]) { GenerateCalibLabelSet(); string oFolder = "/media/ssd/data/aflw/data/neg24x24"; string listFolder = "/media/ssd/data/aflw/data"; int min_FaceSize; float scaleStep; int spacing; min_FaceSize = 40; scaleStep = 1.18; spacing = 4; FaceDetector facedetector(min_FaceSize, scaleStep, spacing); facedetector.LoadConfigs("/home/fanglin/caffe/FaceDetection/faceConfig_2nd.txt"); vector<string> folderNames; folderNames.push_back("/media/ssd/data/VOC2007/nonPerson"); //folderNames.push_back("/media/ssd/data/VOC2007/personHeadMasked"); vector<Mat> imgs; for (int k = 0; k < folderNames.size(); k++) { string folderName = folderNames[k]; vector<string> filePaths; GetFilePaths(folderName, ".jpg|.JPG", filePaths); //imgs.resize(oldSize+filePaths.size()); for (size_t i = 0; i < filePaths.size(); i++) { Mat img = imread(filePaths[i]); if (!img.empty()) imgs.push_back(img); } } ofstream negTrListPath(string(listFolder+"/neg24_train.txt").c_str()); ofstream negValListPath(string(listFolder+"/neg24_val.txt").c_str()); long long nTotalWindows = 0; long long nDetected = 0; for (int i = 0; i < imgs.size(); i++) { Mat img = imgs[i]; vector<Rect> rects; vector<float> scores; tic(); int nWs = facedetector.Detect(img, rects, scores); cout << "Total sliding windows " << nWs << endl; cout << "Detected faces " << rects.size() << endl; Mat img_ext; Size extSize(img.cols/2, img.rows/2); cv::copyMakeBorder(img, img_ext, extSize.height, extSize.height, extSize.width, extSize.width, BORDER_REPLICATE, CV_RGB(0,0,0)); for (int j = 0; j < rects.size(); j++) { Mat rsz, patch; Rect rect_ext = rects[j]; rect_ext.x += extSize.width; rect_ext.y += extSize.height; /* if (rects[j].x < 0 || rects[j].y < 0 || rects[j].br().x > img.cols -1 || rects[j].br().y > img.rows -1) continue; */ patch = img_ext(rect_ext); cv::resize(patch, rsz, cv::Size(24, 24)); stringstream ss; ss << oFolder << "/neg_24x24_" << i << "_" << j << ".bmp"; imwrite(ss.str(), rsz); if (imgs.size() -i > 100) negTrListPath << ss.str() << " " << 0 << endl; else negValListPath << ss.str() << " " << 0 << endl; } for (int j = 0; j < rects.size(); j++) { // Expand by 20% vertically cv::rectangle(img, rects[j], CV_RGB(255, 0, 0), 2); } nTotalWindows += nWs; nDetected += rects.size(); imshow("img", img); char c = cv::waitKey(1); if (c == 'q') break; } negTrListPath.close(); negValListPath.close(); cout << "Total negatives: " << nDetected << endl; cout << "Average faces per image " << 1.0*nDetected/imgs.size() << endl; return 0; }
int mainch( int argc, char** argv ) { srand(time(NULL)); DIR *dir; struct dirent *ent; std::string ext = "png"; std::string s = ""; string pwd = ""; // ofstream file("/Users/sanjeevsingh/Desktop/cv2.txt"); // approxPolyDP(<#InputArray curve#>, <#OutputArray approxCurve#>, <#double epsilon#>, <#bool closed#>) std::vector<cv::Point> polygon; // polygon.push_back(Point(400,236)); // polygon.push_back(Point(640,390)); // polygon.push_back(Point(640,240)); // polygon.push_back(Point(365,145)); polygon.push_back(Point(411,348)); polygon.push_back(Point(552,480)); polygon.push_back(Point(640,352)); polygon.push_back(Point(400,288)); // polygon.push_back(Point(861,463)); // polygon.push_back(Point(1092,706)); // polygon.push_back(Point(1280,394)); // polygon.push_back(Point(790,267)); Rect minRect = boundingRect(polygon); Mat polyMat = Mat::zeros(480, 640, CV_8UC1); // Mat polyMat = Mat::zeros(720, 1280, CV_8UC1); //fills the roi of the visible road fillConvexPoly(polyMat, polygon, Scalar(125), 8, 0); //rows = 480 = height, cols = 640 = width; // Mat mask = polyMat(Rect(minRect.x, minRect.y, minRect.width-1, minRect.height-1)); Mat mask = polyMat(Rect(minRect.x, minRect.y, 100, 100)); Mat image = Mat::zeros( 480, 640, CV_8UC3 ); // Mat image = Mat::zeros( 720, 1280, CV_8UC3 ); Mat tpl = imread("/Users/sanjeevsingh/Desktop/drawn.png",0); Canny(tpl, tpl, 5, 50, 3); namedWindow("result", CV_WINDOW_AUTOSIZE ); String filename; char* path = "/Volumes/MacintoshHD2/SideEye/videos/Vehicle/2/"; // char* path = "/Volumes/MacintoshHD2/SideEye/images/meng4/Vehicle/3/"; if ((dir = opendir (path)) != NULL) { /* print all the files and directories within directory */ while ((ent = readdir (dir)) != NULL) { std::stringstream sstm; std::string fname = ent->d_name; // filename // if filename's last characters are extension if (fname.find(ext, (fname.length() - ext.length())) != std::string::npos){ sstm << path<< fname; filename = sstm.str(); image = imread(filename,CV_LOAD_IMAGE_COLOR); // image = imread("/Volumes/MacintoshHD2/SideEye/videos/Vehicle/2/100.png",CV_LOAD_IMAGE_COLOR); Mat currGray; Mat output; Mat roi; Mat result; // currGray = image; cvtColor(image, currGray, CV_BGR2GRAY); roi = currGray(Rect(minRect.x, minRect.y - 60 , minRect.width-60 -1, minRect.height-1)); // roi = currGray(Rect(minRect.x, minRect.y - 30 // , 100 , 100)); roi.copyTo(output);//, mask); // Mat img = imread("/Users/sanjeevsingh/Desktop/caredge.png",0); //Mat cimg; cvtColor(roi, result, CV_GRAY2BGR); // if the image and the template are not edge maps but normal grayscale images, // you might want to uncomment the lines below to produce the maps. You can also // run Sobel instead of Canny. Canny(output, output, 68, 100, 3); tpl = imread("/Users/sanjeevsingh/Desktop/drawn.png",0); // imshow("roi", output); // imshow("tpl", tpl); // waitKey(0); vector<vector<Point> > results; vector<float> costs; double templScale = 1; int maxMatches = 1; //20 double minMatchDistance = 1.0; int padX = 3; int padY = 3; int scales = 5; double minScale = 0.6; double maxScale = 1.6; double orientationWeight = 0.5; double truncate = 20; // int best = chamerMatching( roi, tpl, results, costs ); int best = chamerMatching( output, tpl, results, costs, templScale, maxMatches, minMatchDistance, padX, padY, scales, minScale, maxScale, orientationWeight, truncate); if( best < 0 ) { cout << "not found;\n"; // file <<filename<<"\t"<<0<<endl; // imshow("result", roi); //waitKey(0); // if(waitKey(30) >= 0) break; // else // continue; // return 0; } else {// cout<<"found\n"; float cost = costs[best]; cout<<cost<<"\n"; // file <<filename<<"\t"<<cost*100<<endl; // if(cost*100>=23.00)// cost= 0;//25.36*(100+rand() % 100); // file <<filename<<"\t"<<cost*100<<endl; size_t i, n = results[best].size(); // if(cost<0.23) for( i = 0; i < n; i++ ) { Point pt = results[best][i]; if( pt.inside(Rect(0, 0, result.cols, result.rows)) ) result.at<Vec3b>(pt) = Vec3b(0, 255, 0); } // continue; namedWindow( "result", CV_WINDOW_AUTOSIZE ); imshow("result", result); char q = waitKey(0); // if(q=='1') // file <<filename<<"\t"<<cost*100<<endl; // if(waitKey(30) >= 0) break; }//else ends } } closedir (dir); } else { /* could not open directory */ perror (""); return EXIT_FAILURE; } // file.close(); return 0; }
void okaoClientCallback( const sensor_msgs::ImageConstPtr& imgmsg, const humans_msgs::HumansConstPtr& kinect ) { int okao_i = 0; int no_okao_i = 0; humans_msgs::Humans okao_human, no_okao_human; cv_bridge::CvImagePtr cv_ptr; try { cv_ptr = cv_bridge::toCvCopy(imgmsg, sensor_msgs::image_encodings::BGR8); for(int i = 0; i < kinect->num; i++) { //POS head2d, head3d, neck2d; Point top, bottom; geometry_msgs::Point head2d, neck2d;//, top, bottom; head2d.x = kinect->human[i].body.joints[HEAD].position_color_space.x; head2d.y = kinect->human[i].body.joints[HEAD].position_color_space.y; neck2d.x = kinect->human[i].body.joints[SPINE_S].position_color_space.x; neck2d.y = kinect->human[i].body.joints[SPINE_S].position_color_space.y; double diff_w = fabs(head2d.y-neck2d.y); double diff_h = fabs(head2d.y-neck2d.y); top.x = head2d.x - diff_w; top.y = head2d.y - diff_h; bottom.x = head2d.x + diff_w; bottom.y = head2d.y + diff_h; /* cout << "cut (" << cut.x << "," << cut.y << ")"<<endl; if( cut.x < 0 ) cut.x = 0; else if( cut.x >= (cv_ptr->image.cols - diff_w*2) ) cut.x = cv_ptr->image.cols-diff_w*2-1; if( cut.y < 0 ) cut.y = 0; else if( cut.y >= (cv_ptr->image.rows - diff_h*2) ) cut.y = cv_ptr->image.rows-diff_h*2-1; */ top.x = max(0, top.x); top.y = max(0, top.y); bottom.x = min(cv_ptr->image.cols-1, bottom.x); bottom.y = min(cv_ptr->image.rows-1, bottom.y); if (( top.x > bottom.x || top.y > bottom.y)||( top.x == bottom.x || top.y == bottom.y)) continue; /* cout << "(" << top.x << "," << top.y << ")" << "-" << "(" << bottom.x << "," << bottom.y << ")"<<endl; cout << "diff:" << "(" << diff_w << "," << diff_h<< ")" << endl; cout << "image:" << "(" << cv_ptr->image.cols << "," << cv_ptr->image.rows << ")" << endl; */ Mat cutRgbImage; try { cutRgbImage = Mat(cv_ptr->image, cv::Rect(top, bottom)); } catch(cv_bridge::Exception& e) { ROS_ERROR("cv_bridge exception: %s",e.what()); } Mat rgbImage = cutRgbImage.clone(); if( rgbImage.cols > 1280 ) { cv::resize( rgbImage, rgbImage, cv::Size(1280, cutRgbImage.rows*1280/cutRgbImage.cols) ); } if( rgbImage.rows > 1024 ) { cv::resize( rgbImage, rgbImage, cv::Size(cutRgbImage.cols*1024/cutRgbImage.rows , 1024) ); } //rgbImage = cutRgbImage; Mat grayImage; cv::cvtColor(rgbImage,grayImage,CV_BGR2GRAY); //test cv::rectangle( cv_ptr->image, top, bottom, cv::Scalar(0,200,0), 5, 8); try { cv::Mat img = grayImage; std::vector<unsigned char> buf(img.data, img.data + img.cols * img.rows * img.channels()); std::vector<int> encodeParam(2); encodeParam[0] = CV_IMWRITE_PNG_COMPRESSION; encodeParam[1] = 3; cv::imencode(".png", img, buf, encodeParam); picojson::object p; p.insert(std::make_pair("mode", picojson::value(std::string("FaceRecognition")))); p.insert(std::make_pair("format",picojson::value(std::string("PNG")))); p.insert(std::make_pair("width",picojson::value((double)img.cols))); p.insert(std::make_pair("height",picojson::value((double)img.rows))); p.insert(std::make_pair("depth",picojson::value((double)1))); picojson::value para = picojson::value(p); std::string param = para.serialize().c_str(); // リクエストメッセージの作成 OkaoServer::RequestMessage reqMsg; reqMsg.img = buf; reqMsg.param = param; // 送信 OkaoServer::sendRequestMessage(*responder, reqMsg); // 受信 OkaoServer::ReplyMessage repMsg; OkaoServer::recvReplyMessage(*responder, &repMsg); //std::cout << "repMsg.okao: " << repMsg.okao << std::endl; const char* json = repMsg.okao.c_str(); picojson::value v; std::string err; picojson::parse(v,json,json + strlen(json),&err); if(err.empty()) { humans_msgs::Face face_msg; humans_msgs::Body body_msg; bool p_ok = false; JsonToMsg::face(v, &face_msg, top.x, top.y, &p_ok, 0); MsgToMsg::bodyToBody(kinect->human[i].body, &body_msg); if( p_ok ) { ++okao_i; humans_msgs::Human h; h.body = body_msg; h.face.persons.resize( OKAO ); h.face = face_msg; okao_human.human.push_back( h ); cv::Point lt(face_msg.position.lt.x, face_msg.position.lt.y); cv::Point rb(face_msg.position.rb.x, face_msg.position.rb.y); cv::Point rb_out = bottom; cv::Scalar red(0,0,200); cv::Scalar green(0,200,0); cv::rectangle( cv_ptr->image, lt, rb, red, 5, 8); cv::putText( cv_ptr->image, face_msg.persons[0].name, rb_out, FONT_HERSHEY_SIMPLEX, 2.5, green, 2, CV_AA); /* ros::ServiceClient client = nh_.serviceClient< okao_client::OkaoStack>("stack_add"); okao_client::OkaoStack stack; //stack.request.rule = "add"; stack.request.person = face_msg.persons[0]; sensor_msgs::Image output; cv::Mat outcutImage; cv::resize(rgbImage, outcutImage, cv::Size(128,128)); output.height = outcutImage.rows; output.width = outcutImage.cols; output.encoding = idToEncoding( outcutImage.type() ); output.step = outcutImage.cols * outcutImage.elemSize(); output.data.assign(outcutImage.data, outcutImage.data + size_t(outcutImage.rows*output.step)); stack.request.image = output; if ( !client.call(stack) ) cout << "service missing!" << endl; */ } else { ++no_okao_i; humans_msgs::Human h; h.body = body_msg; no_okao_human.human.push_back( h ); } } } catch(const zmq::error_t& e) { std::cout << e.what() << std::endl; return ; } } //パブリッシュ okao_human.num = okao_i; no_okao_human.num = no_okao_i; okao_human.header.stamp = no_okao_human.header.stamp = ros::Time::now(); okao_human.header.frame_id = kinect->header.frame_id; no_okao_human.header.frame_id = kinect->header.frame_id; discovery_pub_.publish(okao_human); undiscovered_pub_.publish(no_okao_human); image_pub_.publish(cv_ptr->toImageMsg()); //cv::imshow(OPENCV_WINDOW, cv_ptr->image); //cv::waitKey(1); } catch(cv_bridge::Exception& e) { ROS_ERROR("cv_bridge exception: %s",e.what()); } }
int main(int argc, const char** argv) { string haystack_path, needle_path; if (argc > 2) { haystack_path = argv[1]; needle_path = argv[2]; } if (haystack_path.empty()) { showHelp(argv[0]); return 1; } Mat haystack = imread(haystack_path, CV_LOAD_IMAGE_COLOR); Mat needle = imread(needle_path, CV_LOAD_IMAGE_COLOR); if (haystack.empty() || needle.empty()) { cerr << "Couldn't load images!" << endl; return 1; } Mat haystack_sum; tpl::integral(haystack, haystack_sum); Mat needle_sum; tpl::integral(needle, needle_sum); int ns = needle_sum.at<int>(needle_sum.rows - 1, needle_sum.cols - 1); // Contains candidate results. deque<item> deq; for (int y = 0; y < haystack_sum.rows - needle_sum.rows; ++y) { for (int x = 0; x < haystack_sum.cols - needle_sum.cols; ++x) { int s = haystack_sum.at<int>(y, x) + haystack_sum.at<int>(y + needle_sum.rows, x + needle_sum.cols) - haystack_sum.at<int>(y, x + needle_sum.cols) - haystack_sum.at<int>(y + needle_sum.rows, x); int d = abs(s - ns); item itm = {d, x, y, s}; if (deq.size() > 0) { for (auto it = deq.begin(); it != deq.end(); ++it) { // Need to store candidates to check further. if (d < it->diff) { deq.insert(it, itm); break; } } } else { deq.push_front(itm); } if (deq.size() > 50) { deq.pop_back(); } } } const int nc = 3; // Number of channels. const int byte = 255; // One byte. const int max = needle.rows * needle.cols * byte * nc; // Maximum value that can be in comparing by brute force. float result = !deq.empty() && deq[0].diff == 0 ? 1 : 0; int min = INT_MAX; // Result point. int rx = !deq.empty() && deq[0].diff == 0 ? deq[0].x : -1; int ry = !deq.empty() && deq[0].diff == 0 ? deq[0].y : -1; // If perfect result has not been found -> need to find it by brute force. if (result != 1) { for (int d = 0; d < deq.size(); ++d) { unsigned long long s = 0; for (int j = 0; j < needle.rows; ++j) { for (int i = 0; i < needle.cols; ++i) { Vec3b c1 = haystack.at<Vec3b>(Point(deq[d].x + i, deq[d].y + j)); Vec3b c2 = needle.at<Vec3b>(Point(i, j)); s += abs(c1.val[0] - c2.val[0]); s += abs(c1.val[1] - c2.val[1]); s += abs(c1.val[2] - c2.val[2]); } } if (s < min) { min = s; rx = deq[d].x; ry = deq[d].y; result = 1 - (float(min) / max); } if (s == 0) { break; } } } cout << "Result: " << result << endl; if (result) { cout << "Found at [" << rx << "," << ry << "]" << endl; rectangle(haystack, Point(rx, ry), Point(rx + needle.cols, ry + needle.rows), Scalar::all(0), 2, 8, 0); imshow("Result", haystack); waitKey(0); } return 0; }
int main(int argc, char const *argv[]) { int board_width, board_height, num_imgs; float square_size; char* img_dir; char* leftimg_filename; char* rightimg_filename; char* out_file; static struct poptOption options[] = { { "board_width",'w',POPT_ARG_INT,&board_width,0,"Checkerboard width","NUM" }, { "board_height",'h',POPT_ARG_INT,&board_height,0,"Checkerboard height","NUM" }, { "square_size",'s',POPT_ARG_FLOAT,&square_size,0,"Checkerboard square size","NUM" }, { "num_imgs",'n',POPT_ARG_INT,&num_imgs,0,"Number of checkerboard images","NUM" }, { "img_dir",'d',POPT_ARG_STRING,&img_dir,0,"Directory containing images","STR" }, { "leftimg_filename",'l',POPT_ARG_STRING,&leftimg_filename,0,"Left image prefix","STR" }, { "rightimg_filename",'r',POPT_ARG_STRING,&rightimg_filename,0,"Right image prefix","STR" }, { "out_file",'o',POPT_ARG_STRING,&out_file,0,"Output calibration filename (YML)","STR" }, POPT_AUTOHELP { NULL, 0, 0, NULL, 0, NULL, NULL } }; POpt popt(NULL, argc, argv, options, 0); int c; while((c = popt.getNextOpt()) >= 0) {} load_image_points(board_width, board_height, square_size, num_imgs, img_dir, leftimg_filename, rightimg_filename); printf("Starting Calibration\n"); cv::Matx33d K1, K2, R; cv::Vec3d T; cv::Vec4d D1, D2; int flag = 0; flag |= cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC; flag |= cv::fisheye::CALIB_CHECK_COND; flag |= cv::fisheye::CALIB_FIX_SKEW; //flag |= cv::fisheye::CALIB_FIX_K2; //flag |= cv::fisheye::CALIB_FIX_K3; //flag |= cv::fisheye::CALIB_FIX_K4; cv::fisheye::stereoCalibrate(object_points, left_img_points, right_img_points, K1, D1, K2, D2, img1.size(), R, T, flag, cv::TermCriteria(3, 12, 0)); cv::FileStorage fs1(out_file, cv::FileStorage::WRITE); fs1 << "K1" << Mat(K1); fs1 << "K2" << Mat(K2); fs1 << "D1" << D1; fs1 << "D2" << D2; fs1 << "R" << Mat(R); fs1 << "T" << T; printf("Done Calibration\n"); printf("Starting Rectification\n"); cv::Mat R1, R2, P1, P2, Q; cv::fisheye::stereoRectify(K1, D1, K2, D2, img1.size(), R, T, R1, R2, P1, P2, Q, CV_CALIB_ZERO_DISPARITY, img1.size(), 0.0, 1.1); fs1 << "R1" << R1; fs1 << "R2" << R2; fs1 << "P1" << P1; fs1 << "P2" << P2; fs1 << "Q" << Q; printf("Done Rectification\n"); return 0; }
Void Mat::SetSize(const Mat &m) { SetSize(m.Rows(), m.Cols()); }
int main(int argc, char** argv) { bool ok = false; Size boardSize, subPixSize; string xmlImages, savePath, ymlIntrinsic1, ymlIntrinsic2; vector<string> fileList; vector<Mat> images; vector<Mat> undistortedImages; Mat cameraMatrix1, cameraMatrix2, distCoeffs1, distCoeffs2; vector<vector<Point2f> > cornersCam1, cornersCam2; vector<Point3f> idealCorners; Mat R, T; Mat noDist = Mat::zeros(5,1, CV_32F); Size imageSize; vector<Mat> rvecs1, tvecs1, rvecs2, tvecs2; Mat R1, R2, P1, P2, Q; Rect validRoi[2]; float squareSize; ok = readStereoParams(string(argv[1]), boardSize, squareSize, xmlImages, subPixSize, savePath, ymlIntrinsic1, ymlIntrinsic2); if (ok) { cout << "[Extrinsic] load image list!" << endl; ok = loadImageList(xmlImages, fileList); if (!ok) cout << "[Extrinsic] error! could not load image list!" << endl; } if (ok) { cout << "[Extrinsic] load images!" << endl; ok = loadImages(fileList, images); if (!ok) cout << "[Extrinsic] error! could not load images!" << endl; } if (ok) { boost::filesystem::path dir(savePath); boost::filesystem::create_directories(dir); cout << "[Extrinsic] load intrinsics and undistort images!" << endl; loadStereoIntrinsics(ymlIntrinsic1, ymlIntrinsic2, cameraMatrix1, cameraMatrix2,distCoeffs1, distCoeffs2); ok = undistortStereoImages(images, undistortedImages, cameraMatrix1, cameraMatrix2,distCoeffs1, distCoeffs2); if (!ok) cout << "[Extrinsic] error! could not load intrinsics and undistort images!" << endl; } if (ok) { cout << "[Extrinsic] find corners!" << endl; ok = findCornersStereo(boardSize, subPixSize, undistortedImages, cornersCam1, cornersCam2); if (!ok) cout << "[Extrinsic] error! could not find corners!" << endl; } if (ok) { rvecs1.resize(cornersCam1.size()); tvecs1.resize(cornersCam1.size()); rvecs2.resize(cornersCam1.size()); tvecs2.resize(cornersCam1.size()); cout << "[Extrinsic] calculate extrinsic with " << cornersCam1.size() << " pairs!" << endl; calcBoardCornerPositions(boardSize, squareSize, idealCorners); for (int i = 0; i < cornersCam1.size(); i++) { solvePnP(idealCorners, cornersCam1[i], cameraMatrix1, noDist, rvecs1[i], tvecs1[i]); solvePnP(idealCorners, cornersCam2[i], cameraMatrix2, noDist, rvecs2[i], tvecs2[i]); } ok = calculateRT(tvecs1, tvecs2, R, T); if (!ok) cout << "[Extrinsic] error! could not calculate extrinsic!" << endl; } if (ok) { cout << "[Extrinsic] save extrinsic parameters!" << endl; imageSize = ((Mat)images[0]).size(); stereoRectify(cameraMatrix1, noDist, cameraMatrix2, noDist, imageSize, R, T, R1, R2, P1, P2, Q, CALIB_ZERO_DISPARITY, 1, imageSize, &validRoi[0], &validRoi[1]); string ymlPath(savePath); ymlPath += "extrinsics.yml"; FileStorage fS(ymlPath.c_str(), FileStorage::WRITE); fS << "R" << R << "T" << T << "R1" << R1 << "R2" << R2 << "P1" << P1 << "P2" << P2 << "Q" << Q << "roi1" << validRoi[0] << "roi2" << validRoi[1]; fS.release(); Mat rmap[2][2]; initUndistortRectifyMap(cameraMatrix1, noDist, R1, P1, imageSize, CV_16SC2, rmap[0][0], rmap[0][1]); initUndistortRectifyMap(cameraMatrix2, noDist, R2, P2, imageSize, CV_16SC2, rmap[1][0], rmap[1][1]); #ifdef DEBUG // Show rectified images bool isVerticalStereo = fabs(P2.at<double>(1, 3)) > fabs(P2.at<double>(0, 3)); Mat canvas; double sf; int w, h; if( !isVerticalStereo ) { sf = 600./MAX(imageSize.width, imageSize.height); w = cvRound(imageSize.width*sf); h = cvRound(imageSize.height*sf); canvas.create(h, w*2, CV_8UC3); } else { sf = 300./MAX(imageSize.width, imageSize.height); w = cvRound(imageSize.width*sf); h = cvRound(imageSize.height*sf); canvas.create(h*2, w, CV_8UC3); } for(int i = 0; i < (undistortedImages.size() / 2); i++) { for(int k = 0; k < 2; k++) { Mat img = undistortedImages[i*2+k], rimg; remap(img, rimg, rmap[k][0], rmap[k][1], CV_INTER_LINEAR); stringstream filename; filename << savePath << ((k == 0)? "1": "2") << "_image_" << i << ".jpg"; imwrite(filename.str(), rimg); Mat canvasPart = !isVerticalStereo ? canvas(Rect(w*k, 0, w, h)) : canvas(Rect(0, h*k, w, h)); resize(rimg, canvasPart, canvasPart.size(), 0, 0, CV_INTER_AREA); Rect vroi(cvRound(validRoi[k].x*sf), cvRound(validRoi[k].y*sf), cvRound(validRoi[k].width*sf), cvRound(validRoi[k].height*sf)); rectangle(canvasPart, vroi, Scalar(0,0,255), 3, 8); } if( !isVerticalStereo ) for(int j = 0; j < canvas.rows; j += 16 ) line(canvas, Point(0, j), Point(canvas.cols, j), Scalar(0, 255, 0), 1, 8); else for(int j = 0; j < canvas.cols; j += 16 ) line(canvas, Point(j, 0), Point(j, canvas.rows), Scalar(0, 255, 0), 1, 8); imshow("rectified", canvas); char c = (char)waitKey(); if( c == 27 || c == 'q' || c == 'Q' ) break; } #endif Point2i rectCorner1(max(validRoi[0].x, validRoi[1].x), max(validRoi[0].y, validRoi[1].y)); Point2i rectCorner2(min(validRoi[0].x + validRoi[0].width, validRoi[1].x + validRoi[1].width), min(validRoi[0].y + validRoi[0].height, validRoi[1].y + validRoi[1].height)); Rect roi(rectCorner1.x, rectCorner1.y, rectCorner2.x - rectCorner1.x, rectCorner2.y - rectCorner1.y); for(int i = 0; i < (undistortedImages.size() / 2); i++) { for(int k = 0; k < 2; k++) { Mat img = undistortedImages[i*2+k], remapImg, rectImg; remap(img, remapImg, rmap[k][0], rmap[k][1], cv::INTER_LINEAR); stringstream filename; filename << savePath << "image" << i << ((k == 0)? "_1": "_2") << ".jpg"; rectImg = remapImg(roi); imwrite(filename.str(), rectImg); } } } return EXIT_SUCCESS; }
void setPixelValue(Mat &M,Point p,T value) { M.data+ M.step*p.y + p.x*M.elemSize()=value; }
void BOW::computeVideoFeatures( std::string fileName, vector<STFeature>& featureVec ) { const int procUnitLen = 20; const int intervalLen = 5; const int width = 320; const int height = 240; CProfiler prof; //Open video VideoCapture cap( fileName ); Mat frame; Mat frameg; Mat frameg2; Mat hardCopy; vector<STFeature> tempFeat; vector<cv::Mat> frames; vector<cv::Mat> frameQueue; int frameCpt = 0; int nbFrameTotal = cap.get(CV_CAP_PROP_FRAME_COUNT); featureVec.clear(); featureVec.reserve( 10000 ); cout << "Processing " << fileName << "..." << endl; cout << "Total number of frames: " << nbFrameTotal << endl; int lastPercent = 0; double lastFrameRate = 0; double accFrameRate = 0; int accFrameRateCpt = 0; for(;;) { cap >> frame; if( frame.empty() ) break; /* double pos = cap.get( CV_CAP_PROP_POS_AVI_RATIO ); if( lastPercent != (int)(ceil(pos*100)) ) { double meanFrameRate = accFrameRate/(double)accFrameRateCpt; accFrameRate = 0; accFrameRateCpt = 0; lastPercent = (int)(ceil(pos*100)); cout << lastPercent << "%" << " - " << meanFrameRate << " fps" << endl; } else { accFrameRate += lastFrameRate; accFrameRateCpt++; } */ cv::cvtColor( frame, frameg, CV_RGB2GRAY ); cv::resize( frameg, frameg2, cv::Size(width,height)); hardCopy = frameg2.clone(); //we reached processing unit length if( frameQueue.size() == procUnitLen ) { prof.start(); tempFeat.clear(); /* * new arch */ if( _extractor != 0 ) { _extractor->computeFeatures( frameQueue, frameCpt ); tempFeat = _extractor->getFeatures(); } /* */ featureVec.insert(featureVec.end(), tempFeat.begin(), tempFeat.end()); prof.stop(); frameQueue.clear(); lastFrameRate = 1.0/prof.getSeconds(); } frameQueue.push_back( hardCopy ); // frameCpt++; } cout << "Done!" << endl; }
//-------------------------------------------------------------- void ofApp::update(){ #ifdef __arm__ frame = cam.grab(); if(!frame.empty()) finder.update(frame); #else cam.update(); if(cam.isFrameNew()) { finder.update(cam); } #endif if(finder.size() > 0) { // Person present watchdog presentTimer.reset(); personPresent = true; // ofRectangle object = finder.getObjectSmoothed(0); // aFactor = aFactorLast - object.width; // ofDrawBitmapStringHighlight(ofToString(aFactor), 210, 120); // aFactorLast = object.width; } // prevent factedetection fails to be treated as person leaving if(presentTimer.check()) { personPresent = false; } if(fpsOutTimer.check()) { cout << "FPS:" << ofGetFrameRate() << endl; } sceneBlend.update( 1.0f / FPS ); // Person present if( personPresentLastFrame != personPresent ) { cout << "Person present changed!" << endl ; personPresentChanged = true; // person found if(personPresent){ sceneBlend.animateTo(1); // person lost } else { sceneBlend.animateTo(0); } } else { personPresentChanged = false; } personPresentLastFrame = personPresent; // State changes // just came to idle mode if((sceneBlend.val() == 0)) { if(SM.sceneChange) { SM.sceneChange = false; if(!SM.alwaysOn){ SM.mirror.setRandomImage(); SM.mirror.generateMatrixFromImage(); } cout << "Scene Idle entered!" << endl; } // just came to mirror mode } else if ((sceneBlend.val() == 1) ) { if(SM.sceneChange) { SM.sceneChange = false; sawSomeone = true; cout << "Scene Mirror entered!" << endl; } } else { SM.sceneChange = true; } SM.scenes[0]->update(); if(sawSomeone){ SM.scenes[1]->update(); } SM.getSceneBlend((float) sceneBlend.val() ,SM.mirror.pixelMatrix, SM.intro.pixelMatrix); // unsigned char * pixels = myImg.getPixels(); }
// // thanks again, Haris. i wouldn't be anywhere without your mind here. // MatResults project3d(const Mat & test, const Mat & testColor) const { PROFILEX("project3d"); MatResults result = MatResults(); int mid = mdl.cols/2; int midi = test.cols/2; Rect R(mid-crop/2,mid-crop/2,crop,crop); Rect Ri(midi-crop/2,midi-crop/2,crop,crop); cout << "R: " << R << endl; cout << "Ri: " << Ri << endl; cout << "mdl.size(): " << mdl.size() << endl; // get landmarks vector<Point2d> pts2d; getkp2d(test, pts2d, Ri); // get pose mat for our landmarks Mat KP = pnp(test.size(), pts2d); // Store the projection matrix. result.matrices.push_back(KP); // project img to head, count occlusions Mat_<uchar> test2(mdl.size(),127); Mat_<uchar> counts(mdl.size(),0); Mat test2Color(mdl.rows, mdl.cols, CV_8UC3); for (int i=R.y; i<R.y+R.height; i++) { PROFILEX("proj_1"); for (int j=R.x; j<R.x+R.width; j++) { Mat1d p = project_vec(KP, i, j); int x = int(p(0) / p(2)); int y = int(p(1) / p(2)); if (y < 0 || y > test.rows - 1) continue; if (x < 0 || x > test.cols - 1) continue; // stare hard at the coord transformation ;) test2(i, j) = test.at<uchar>(y, x); // Copy a color pixel (BGR) test2Color.at<Vec3b>(i, j) = testColor.at<Vec3b>(y, x); // each point used more than once is occluded counts(y, x) ++; } } // project the occlusion counts in the same way Mat_<uchar> counts1(mdl.size(),0); for (int i=R.y; i<R.y+R.height; i++) { PROFILEX("proj_2"); for (int j=R.x; j<R.x+R.width; j++) { Mat1d p = project_vec(KP, i, j); int x = int(p(0) / p(2)); int y = int(p(1) / p(2)); if (y < 0 || y > test.rows - 1) continue; if (x < 0 || x > test.cols - 1) continue; counts1(i, j) = counts(y, x); } } blur(counts1, counts1, Size(9,9)); counts1 -= eyemask; counts1 -= eyemask; // count occlusions in left & right half Rect left (0, 0,mid,counts1.rows); Rect right(mid,0,mid,counts1.rows); double sleft=sum(counts1(left))[0]; double sright=sum(counts1(right))[0]; // fix occlusions with soft symmetry Mat_<double> weights; Mat_<uchar> sym = test2.clone(); Mat symColor = test2Color.clone(); if (abs(sleft-sright)>symThresh) { PROFILEX("proj_3"); // make weights counts1.convertTo(weights,CV_64F); Point p,P; double m,M; int eyeMask_y_offset = -5; minMaxLoc(weights,&m,&M,&p,&P); double *wp = weights.ptr<double>(); for (size_t i=0; i<weights.total(); ++i) wp[i] = (1.0 - 1.0 / exp(symBlend+(wp[i]/M))); // cerr << weights(Rect(mid,mid,6,6)) << endl; for (int i=R.y; i<R.y+R.height; i++) { if (sleft-sright>symThresh) // left side needs fixing { for (int j=R.x; j<mid; j++) { int k = mdl.cols-j-1; sym(i,j) = test2(i,j) * (1-weights(i,j)) + test2(i,k) * (weights(i,j)); if( i + eyeMask_y_offset >= 0 && eyemask.at<double>(i + eyeMask_y_offset ,j) == 0 ) { // symColor.at<Vec3b>(i, j) = test2Color.at<Vec3b>(i,j) * (1-weights(i,j)) + test2Color.at<Vec3b>(i,k) * (weights(i,j)); symColor.at<Vec3b>(i, j) = test2Color.at<Vec3b>(i,k); } // symColor.at<Vec3b>(i, j) = test2Color.at<Vec3b>(i,j) * (1-weights(i,j)) + test2Color.at<Vec3b>(i,k) * (weights(i,j)); } } if (sright-sleft>symThresh) // right side needs fixing { for (int j=mid; j<R.x+R.width; j++) { int k = mdl.cols-j-1; sym(i,j) = test2(i,j) * (1-weights(i,j)) + test2(i,k) * (weights(i,j)); if( i + eyeMask_y_offset >= 0 && eyemask.at<double>(i + eyeMask_y_offset ,j) == 0 ) { // symColor.at<Vec3b>(i, j) = test2Color.at<Vec3b>(i,j) * (1-weights(i,j)) + test2Color.at<Vec3b>(i,k) * (weights(i,j)); symColor.at<Vec3b>(i, j) = test2Color.at<Vec3b>(i,k); } // symColor.at<Vec3b>(i, j) = test2Color.at<Vec3b>(i,j) * (1-weights(i,j)) + test2Color.at<Vec3b>(i,k) * (weights(i,j)); } } } } if (DEBUG_IMAGES) { cerr << (sleft-sright) << "\t" << (abs(sleft-sright)>symThresh) << endl; imshow("proj",test2); if (abs(sleft-sright)>symThresh) imshow("weights", weights); Mat t = test.clone(); rectangle(t,Ri,Scalar(255)); for (size_t i=0; i<pts2d.size(); i++) circle(t, pts2d[i], 1, Scalar(0)); imshow("test3",t); imshow("eyemask", eyemask); } Mat gray; Mat colorImg = symColor(R); sym.convertTo(gray,CV_8U); result.matrices.push_back(sym(R)); result.matrices.push_back(colorImg); return result; }
bool forward_ocl(InputArrayOfArrays inputs_, OutputArrayOfArrays outputs_, OutputArrayOfArrays internals_) { std::vector<UMat> inputs; std::vector<UMat> outputs; std::vector<UMat> internals; inputs_.getUMatVector(inputs); outputs_.getUMatVector(outputs); internals_.getUMatVector(internals); CV_Assert(inputs.size() == 1 && outputs.size() == 1); CV_Assert(inputs[0].total() == outputs[0].total()); const UMat& inp0 = inputs[0]; UMat& buffer = internals[0]; size_t num = inp0.size[0]; size_t channels = inp0.size[1]; size_t channelSize = inp0.total() / (num * channels); for (size_t i = 0; i < num; ++i) { MatShape s = shape(channels, channelSize); UMat src = inputs[i].reshape(1, s.size(), &s[0]); UMat dst = outputs[i].reshape(1, s.size(), &s[0]); UMat abs_mat; absdiff(src, cv::Scalar::all(0), abs_mat); pow(abs_mat, pnorm, buffer); if (acrossSpatial) { // add eps to avoid overflow float absSum = sum(buffer)[0] + epsilon; float norm = pow(absSum, 1.0f / pnorm); multiply(src, 1.0f / norm, dst); } else { Mat norm; reduce(buffer, norm, 0, REDUCE_SUM); norm += epsilon; // compute inverted norm to call multiply instead divide cv::pow(norm, -1.0f / pnorm, norm); repeat(norm, channels, 1, buffer); multiply(src, buffer, dst); } if (!blobs.empty()) { // scale the output Mat scale = blobs[0]; if (scale.total() == 1) { // _scale: 1 x 1 multiply(dst, scale.at<float>(0, 0), dst); } else { // _scale: _channels x 1 CV_Assert(scale.total() == channels); repeat(scale, 1, dst.cols, buffer); multiply(dst, buffer, dst); } } } return true; }
int main() { cout<< "Begin My Stitcher" << endl; vector<string> img_names; if(!ReadImg_names(img_names)){return -1;} // Check if have enough images int num_images = static_cast<int>(img_names.size()); if (num_images < 2) { cout<< "Need more images" << endl; return -1; } int64 t = getTickCount(); // find features cout<< "START_ Find features " << endl; Ptr<FeaturesFinder> finder; #if defined(HAVE_OPENCV_NONFREE) && defined(HAVE_OPENCV_GPU) if (try_gpu && gpu::getCudaEnabledDeviceCount() > 0) finder = new SurfFeaturesFinderGpu(); else #endif finder = new SurfFeaturesFinder(); // Load images // step 1 : find features Mat img; vector<ImageFeatures> features(num_images); vector<Mat> images(num_images); vector<Size> img_sizes(num_images); for(int i = 0; i < num_images; i++) { img = imread(img_names[i]); images[i] = img.clone(); img_sizes[i] = img.size(); if(img.empty()) { cout<< "Connot open image " << img_names[i] << endl; return -1; } (*finder)(img, features[i]); features[i].img_idx = i; cout<< "Features in image # " << i << " : " << features[i].keypoints.size() << endl; } finder->collectGarbage(); img.release(); cout<< "END_ Find all features, time:" << (getTickCount()-t)/getTickFrequency() << "sec" << endl; // step 2 : match features cout<< "START_ Partwise match" << endl; vector<MatchesInfo> pairwise_matches; BestOf2NearestMatcher matcher(try_gpu, match_conf); matcher(features, pairwise_matches); matcher.collectGarbage(); ofstream f("graph_match.txt"); f << matchesGraphAsString(img_names, pairwise_matches, conf_thresh); cout<< "END_ Pairwise matching, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec" << endl; // step 3 : Homography cout<< "START_ Homography" << endl; vector<Mat> HA; // step 4 : wrap /*Ptr<WarperCreator> warper_creator; warper_creator = new cv::FisheyeWarper();*/ return 0; }
int main(int argc, const char** argv) { cv::CommandLineParser cmd(argc, argv, "{ c camera | false | use camera }" "{ f file | 768x576.avi | input video file }" "{ m method | mog | method (mog, mog2) }" "{ h help | false | print help message }"); if (cmd.has("help")) { cout << "Usage : bgfg_segm [options]" << endl; cout << "Available options:" << endl; cmd.printMessage(); return EXIT_SUCCESS; } bool useCamera = cmd.get<bool>("camera"); string file = cmd.get<string>("file"); string method = cmd.get<string>("method"); if (method != "mog" && method != "mog2") { cerr << "Incorrect method" << endl; return EXIT_FAILURE; } int m = method == "mog" ? M_MOG : M_MOG2; VideoCapture cap; if (useCamera) cap.open(0); else cap.open(file); if (!cap.isOpened()) { cout << "can not open camera or video file" << endl; return EXIT_FAILURE; } Mat frame; cap >> frame; oclMat d_frame(frame); cv::ocl::MOG mog; cv::ocl::MOG2 mog2; oclMat d_fgmask, d_fgimg, d_bgimg; d_fgimg.create(d_frame.size(), d_frame.type()); Mat fgmask, fgimg, bgimg; switch (m) { case M_MOG: mog(d_frame, d_fgmask, 0.01f); break; case M_MOG2: mog2(d_frame, d_fgmask); break; } for (;;) { cap >> frame; if (frame.empty()) break; d_frame.upload(frame); int64 start = cv::getTickCount(); //update the model switch (m) { case M_MOG: mog(d_frame, d_fgmask, 0.01f); mog.getBackgroundImage(d_bgimg); break; case M_MOG2: mog2(d_frame, d_fgmask); mog2.getBackgroundImage(d_bgimg); break; } double fps = cv::getTickFrequency() / (cv::getTickCount() - start); std::cout << "FPS : " << fps << std::endl; d_fgimg.setTo(Scalar::all(0)); d_frame.copyTo(d_fgimg, d_fgmask); d_fgmask.download(fgmask); d_fgimg.download(fgimg); if (!d_bgimg.empty()) d_bgimg.download(bgimg); imshow("image", frame); imshow("foreground mask", fgmask); imshow("foreground image", fgimg); if (!bgimg.empty()) imshow("mean background image", bgimg); if (27 == waitKey(30)) break; } return EXIT_SUCCESS; }
int main(int argc, char** argv) { const char* keys = "{ i | input | | specify input image }" "{ o | output | squares_output.jpg | specify output save path}" "{ h | help | false | print help message }"; CommandLineParser cmd(argc, argv, keys); string inputName = cmd.get<string>("i"); string outfile = cmd.get<string>("o"); if(cmd.get<bool>("help")) { cout << "Usage : squares [options]" << endl; cout << "Available options:" << endl; cmd.printMessage(); return EXIT_SUCCESS; } int iterations = 10; namedWindow( wndname, WINDOW_AUTOSIZE ); vector<vector<Point> > squares_cpu, squares_ocl; Mat image = imread(inputName, 1); if( image.empty() ) { cout << "Couldn't load " << inputName << endl; return EXIT_FAILURE; } int j = iterations; int64 t_ocl = 0, t_cpp = 0; //warm-ups cout << "warming up ..." << endl; findSquares(image, squares_cpu); findSquares_ocl(image, squares_ocl); #ifdef ACCURACY_CHECK cout << "Checking ocl accuracy ... " << endl; cout << (checkPoints(squares_cpu, squares_ocl) ? "Pass" : "Failed") << endl; #endif do { int64 t_start = cv::getTickCount(); findSquares(image, squares_cpu); t_cpp += cv::getTickCount() - t_start; t_start = cv::getTickCount(); findSquares_ocl(image, squares_ocl); t_ocl += cv::getTickCount() - t_start; cout << "run loop: " << j << endl; } while(--j); cout << "cpp average time: " << 1000.0f * (double)t_cpp / getTickFrequency() / iterations << "ms" << endl; cout << "ocl average time: " << 1000.0f * (double)t_ocl / getTickFrequency() / iterations << "ms" << endl; Mat result = drawSquaresBoth(image, squares_cpu, squares_ocl); imshow(wndname, result); imwrite(outfile, result); waitKey(0); return EXIT_SUCCESS; }
void RobotVision::applyWaterRecognition(Mat &imgIni, Mat &imgRes) { imgRes.setTo(cv::Scalar(0)); canRecognition->segmentation_HSV(imgIni, imgRes, P->minH_water, P->maxH_water, P->minS_water, P->maxS_water, P->minV_water, P->maxV_water); }
void Puppets(){ //this is where the magic happens! Sort of. if(!clmModel._clm._plocal.empty()){ //Just in case there's nothing there... Mat localShape; Mat newshape; Mat globalShape; double mouth = double(gtk_adjustment_get_value(gtk_range_get_adjustment( GTK_RANGE(hscale)))); double eyebrows = double(gtk_adjustment_get_value(gtk_range_get_adjustment( GTK_RANGE(hscale2)))); double smile = double(gtk_adjustment_get_value(gtk_range_get_adjustment( GTK_RANGE(hscale3)))); //weight of expression parameters double headmovement = double(gtk_adjustment_get_value(gtk_range_get_adjustment( GTK_RANGE(hscale5))))/100.0; Mat oldshape; clmModel._clm._plocal.copyTo(localShape); clmModel._clm._pglobl.copyTo(globalShape); globalShape.db(1,0) *= headmovement; globalShape.db(2,0) *= headmovement; globalShape.db(3,0) *= headmovement; clmModel._clm._pdm.CalcShape2D(oldshape, localShape, globalShape); //calculate old shape localShape.db(0,0) *= (mouth/100.0); localShape.db(1,0) *= (eyebrows/100.0); localShape.db(2,0) *= (smile/100.0); clmModel._clm._pdm.CalcShape2D(newshape, localShape, globalShape); //calculate new shape ParseToPAW(newshape, localShape, globalShape); Mat teethimg; cvtColor(faceimg, teethimg, CV_RGB2BGR); cv::Mat neutralshape(newshape.rows, 1, CV_64FC1); Vec3d orientation; orientation(0) = globalShape.at<double>(1); orientation(1) = globalShape.at<double>(2); orientation(2) = globalShape.at<double>(3); int viewid = clmModel._det_valid.GetViewId(orientation); bool toggleERI = ERIon; if(viewid != 0){ toggleERI = 1; } sendOptions(writeToFile,toggleERI, choiceavatar); //send writeto and usesave to avatar clmModel._det_valid._fcheck[viewid]._paw.WarpToNeutral(faceimg, newshape, neutralshape); //warp to neutral (in CLM/PAW.cc) //***************// if(avatarWarpedHead2.empty()){ string imagefileloc, filelocleft, filelocright; if(avatarfile2.empty()){ avatarfile2 = "../images/shape_central.yml"; } cout << "avatar file " << avatarfile2 << endl; FileStorage fs(avatarfile2, FileStorage::READ); fs["shape"] >> avatarS2; fs["filename"] >> imagefileloc; fs["fileleft"] >> filelocleft; fs["fileright"] >> filelocright; fs.release(); avatarWarpedHead2 = imread( imagefileloc); cvtColor(avatarWarpedHead2, avatarWarpedHead2, CV_RGB2BGR); //warp to neutral on read of a new face, not every loop: so you only have to do it once clmModel._det_valid._fcheck[0]._paw.WarpToNeutral(avatarWarpedHead2, avatarS2, neutralshape); }
void RobotVision::applyDepthThresholding(Mat &world, Mat &imgMaxDepth) { imgMaxDepth.setTo(cv::Scalar(0)); }
static void DCT_1D( const Mat& _src, Mat& _dst, int flags, const Mat& _wave=Mat() ) { _dst.create( _src.size(), _src.type() ); int i, j, n = _dst.cols + _dst.rows - 1; Mat wave = _wave; int srcstep = 1, dststep = 1; double* w; CV_Assert( _src.cols + _src.rows - 1 == n); if( wave.empty() ) wave = initDCTWave( n, (flags & DFT_INVERSE) != 0 ); w = wave.ptr<double>(); if( !_src.isContinuous() ) srcstep = (int)(_src.step/_src.elemSize()); if( !_dst.isContinuous() ) dststep = (int)(_dst.step/_dst.elemSize()); if( _src.type() == CV_32FC1 ) { float *dst = _dst.ptr<float>(); for( i = 0; i < n; i++, dst += dststep ) { const float* src = _src.ptr<float>(); double sum = 0; for( j = 0; j < n; j++, src += srcstep ) sum += src[0]*w[j]; w += n; dst[0] = (float)sum; } } else if( _src.type() == CV_64FC1 ) { double *dst = _dst.ptr<double>(); for( i = 0; i < n; i++, dst += dststep ) { const double* src = _src.ptr<double>(); double sum = 0; for( j = 0; j < n; j++, src += srcstep ) sum += src[0]*w[j]; w += n; dst[0] = sum; } } else assert(0); }
void removeCol(InputOutputArray _matIn, int col, int method = METHOD::CV_RECT) { CV_Assert( col >= 0 && col < _matIn.getMat().cols ); Mat matIn = _matIn.getMat(); cv::Size size = matIn.size(); Mat matOut( matIn.rows, matIn.cols - 1, matIn.type()); switch(method) { case(METHOD::STD_MEMCPY) : { int rowInInBytes = size.width * matIn.elemSize(); int rowOutInBytes = ( size.width - 1 ) * matIn.elemSize(); if ( col > 0 ) { int matInOffset = 0; int matOutOffset = 0; int numCols = col; int numBytes = numCols * matIn.elemSize(); for ( int y = 0; y < size.height; ++y ) { std::memcpy( matOut.data + matOutOffset, matIn.data + matInOffset, numBytes ); matInOffset += rowInInBytes; matOutOffset += rowOutInBytes; } } if ( col < size.width - 1 ) { int matInOffset = ( col + 1 ) * matIn.elemSize(); int matOutOffset = col * matIn.elemSize(); int numCols = size.width - ( col + 1 ); int numBytes = numCols * matIn.elemSize(); for ( int y = 0; y < size.height; ++y ) { std::memcpy( matOut.data + matOutOffset, matIn.data + matInOffset, numBytes ); matInOffset += rowInInBytes; matOutOffset += rowOutInBytes; } } } break; case(METHOD::CV_RANGE) : { if(matIn.data != matOut.data || matIn.step != matOut.step) { if(col == 0) { matIn(Range(0, matIn.rows), Range(1, matIn.cols)).copyTo(matOut); } else if (col == matIn.cols - 1) { matIn(Range(0, matIn.rows), Range(0, col)).copyTo(matOut); } else { Mat a, b; matIn(Range(0, matIn.rows), Range(0, col)).copyTo(a); matIn(Range(0, matIn.rows), Range(col + 1, matIn.cols)).copyTo(b); // cout << "dst: " << a << endl; // cout << "temp: " << b << endl; hconcat(a, b, matOut); } } } break; default : if ( col > 0 ) { cv::Rect rect( 0, 0, col, size.height ); matIn( rect ).copyTo( matOut( rect ) ); } if ( col < size.width - 1 ) { cv::Rect rect1( col + 1, 0, size.width - col - 1, size.height ); cv::Rect rect2( col, 0, size.width - col - 1, size.height ); matIn( rect1 ).copyTo( matOut( rect2 ) ); } break; } matOut.copyTo(_matIn); }
static void DFT_1D( const Mat& _src, Mat& _dst, int flags, const Mat& _wave=Mat()) { _dst.create(_src.size(), _src.type()); int i, j, k, n = _dst.cols + _dst.rows - 1; Mat wave = _wave; double scale = (flags & DFT_SCALE) ? 1./n : 1.; size_t esz = _src.elemSize(); size_t srcstep = esz, dststep = esz; const uchar* src0 = _src.data; uchar* dst0 = _dst.data; CV_Assert( _src.cols + _src.rows - 1 == n ); if( wave.empty() ) wave = initDFTWave( n, (flags & DFT_INVERSE) != 0 ); const Complexd* w = wave.ptr<Complexd>(); if( !_src.isContinuous() ) srcstep = _src.step; if( !_dst.isContinuous() ) dststep = _dst.step; if( _src.type() == CV_32FC2 ) { for( i = 0; i < n; i++ ) { Complexf* dst = (Complexf*)(dst0 + i*dststep); Complexd sum(0,0); int delta = i; k = 0; for( j = 0; j < n; j++ ) { const Complexf* src = (const Complexf*)(src0 + j*srcstep); sum.re += src->re*w[k].re - src->im*w[k].im; sum.im += src->re*w[k].im + src->im*w[k].re; k += delta; k -= (k >= n ? n : 0); } dst->re = (float)(sum.re*scale); dst->im = (float)(sum.im*scale); } } else if( _src.type() == CV_64FC2 ) { for( i = 0; i < n; i++ ) { Complexd* dst = (Complexd*)(dst0 + i*dststep); Complexd sum(0,0); int delta = i; k = 0; for( j = 0; j < n; j++ ) { const Complexd* src = (const Complexd*)(src0 + j*srcstep); sum.re += src->re*w[k].re - src->im*w[k].im; sum.im += src->re*w[k].im + src->im*w[k].re; k += delta; k -= (k >= n ? n : 0); } dst->re = sum.re*scale; dst->im = sum.im*scale; } } else CV_Error(CV_StsUnsupportedFormat, ""); }
void removeRow(InputOutputArray _matIn, int row, int method = METHOD::CV_RECT) { CV_Assert( row >= 0 && row < _matIn.getMat().rows ); Mat matIn = _matIn.getMat(); cv::Size size = matIn.size(); Mat matOut( matIn.rows - 1, matIn.cols, matIn.type()); switch(method) { case(METHOD::STD_MEMCPY) : { int rowSizeInBytes = size.width * matIn.elemSize(); if ( row > 0 ) { int numRows = row; int numBytes = rowSizeInBytes * numRows; std::memcpy( matOut.data, matIn.data, numBytes ); } if ( row < size.height - 1 ) { int matOutOffset = rowSizeInBytes * row; int matInOffset = matOutOffset + rowSizeInBytes; int numRows = size.height - ( row + 1 ); int numBytes = rowSizeInBytes * numRows; std::memcpy( matOut.data + matOutOffset , matIn.data + matInOffset, numBytes ); } } break; case(METHOD::CV_RANGE) : { if(matIn.data != matOut.data || matIn.step != matOut.step) { if(row == 0) { matIn(Range(1, matIn.rows), Range(0, matIn.cols)).copyTo(matOut); } else if (row == matIn.rows - 1) { matIn(Range(0, row), Range(0, matIn.cols)).copyTo(matOut); } else { Mat a, b; matIn(Range(0, row), Range(0, matIn.cols)).copyTo(a); matIn(Range(row +1, matIn.rows), Range(0, matIn.cols)).copyTo(b); // cout << "dst: " << a << endl; // cout << "temp: " << b << endl; vconcat(a, b, matOut); } } } break; default : if ( row > 0 ) { cv::Rect rect( 0, 0, size.width, row ); matIn( rect ).copyTo( matOut( rect ) ); } if ( row < size.height - 1 ) { cv::Rect rect1( 0, row + 1, size.width, size.height - row - 1 ); cv::Rect rect2( 0, row, size.width, size.height - row - 1 ); matIn( rect1 ).copyTo( matOut( rect2 ) ); } break; } matOut.copyTo(_matIn); }
inline void op_diagmat::apply(Mat<typename T1::elem_type>& out, const Op<T1, op_diagmat>& X) { arma_extra_debug_sigprint(); typedef typename T1::elem_type eT; const Proxy<T1> P(X.m); const uword n_rows = P.get_n_rows(); const uword n_cols = P.get_n_cols(); const bool P_is_vec = (n_rows == 1) || (n_cols == 1); if(P.is_alias(out) == false) { if(P_is_vec) // generate a diagonal matrix out of a vector { const uword N = (n_rows == 1) ? n_cols : n_rows; out.zeros(N, N); if(Proxy<T1>::prefer_at_accessor == false) { typename Proxy<T1>::ea_type P_ea = P.get_ea(); for(uword i=0; i < N; ++i) { out.at(i,i) = P_ea[i]; } } else { if(n_rows == 1) { for(uword i=0; i < N; ++i) { out.at(i,i) = P.at(0,i); } } else { for(uword i=0; i < N; ++i) { out.at(i,i) = P.at(i,0); } } } } else // generate a diagonal matrix out of a matrix { out.zeros(n_rows, n_cols); const uword N = (std::min)(n_rows, n_cols); for(uword i=0; i < N; ++i) { out.at(i,i) = P.at(i,i); } } } else // we have aliasing { if(P_is_vec) // generate a diagonal matrix out of a vector { const uword N = (n_rows == 1) ? n_cols : n_rows; podarray<eT> tmp(N); eT* tmp_mem = tmp.memptr(); if(Proxy<T1>::prefer_at_accessor == false) { typename Proxy<T1>::ea_type P_ea = P.get_ea(); for(uword i=0; i < N; ++i) { tmp_mem[i] = P_ea[i]; } } else { if(n_rows == 1) { for(uword i=0; i < N; ++i) { tmp_mem[i] = P.at(0,i); } } else { for(uword i=0; i < N; ++i) { tmp_mem[i] = P.at(i,0); } } } out.zeros(N, N); for(uword i=0; i < N; ++i) { out.at(i,i) = tmp_mem[i]; } } else // generate a diagonal matrix out of a matrix { const uword N = (std::min)(n_rows, n_cols); if( (Proxy<T1>::has_subview == false) && (Proxy<T1>::fake_mat == false) ) { // NOTE: we have aliasing and it's not due to a subview, hence we're assuming that the output matrix already has the correct size for(uword i=0; i < n_cols; ++i) { if(i < N) { const eT val = P.at(i,i); arrayops::fill_zeros(out.colptr(i), n_rows); out.at(i,i) = val; } else { arrayops::fill_zeros(out.colptr(i), n_rows); } } } else { podarray<eT> tmp(N); eT* tmp_mem = tmp.memptr(); for(uword i=0; i < N; ++i) { tmp_mem[i] = P.at(i,i); } out.zeros(n_rows, n_cols); for(uword i=0; i < N; ++i) { out.at(i,i) = tmp_mem[i]; } } } } }
void setKernel(Mat kernel,bool normalize=true) { kernel.copyTo (_kernel); }