Esempio n. 1
0
void Calibration::setDefaults()
{
	views.n = 0;
	views.prompt = false;
	views.save_views = false;

	find_chessboard.flags = 0;
	find_chessboard.grid = Size(0, 0);

	sub_pixel.win = WIN_SIZE;
	sub_pixel.zz = ZERO_ZNE;
	sub_pixel.crit = ERR_TOL;

	calib_cam.flags = 0;

	solve_pnp.useExtGuess = false;

	polka_dots.dilate = 0;
	polka_dots.erode = 0;
	polka_dots.thr1 = 0;
	polka_dots.thr2 = 0;

	intrinsic_params.file = "";
	extrinsic_params.file = "";
}
Esempio n. 2
0
void find_in_image() {
	Setting conf("application.cfg");
	string base = conf.getString("application.res_dir");
	Mat haystack = read(base + "/car_features/01.jpg");
	Mat needle = toGrayscale(cv::imread(base + "/car_features/01.jpg"));
	Mat needle64x128;
	cv::resize(needle, needle64x128, Size(64, 128));

	vector<float> hog = computeHog(needle64x128);

	cout << hog.size() << endl;
}
Esempio n. 3
0
 void resize(Mat& img, float fx) {
     Mat dst;
     int inter = (fx > 1.0) ? CV_INTER_CUBIC : CV_INTER_AREA;
     cv::resize(img, dst, Size(), fx, 1.0, inter);
     assert(img.rows == dst.rows);
     if (img.cols > dst.cols) {
         dst.copyTo(img(Range::all(), Range(0, dst.cols)));
         img(Range::all(), Range(dst.cols, img.cols)) = cv::Scalar::all(0);
     } else {
         dst(Range::all(), Range(0, img.cols)).copyTo(img);
     }
 }
Esempio n. 4
0
void EnhancedStereo::createBuffer()
{
//    cout << "create" << endl;
    int bufferWidth = smallWidth()*dispMax;
    if (errorBuffer.cols != bufferWidth or errorBuffer.rows != smallHeight())
    {
        errorBuffer = Mat_<uint8_t>(Size(bufferWidth, smallHeight()));
    }
    if (tableauLeft.cols != bufferWidth or tableauLeft.rows != smallHeight())
    {
        tableauLeft = Mat_<int>(Size(bufferWidth, smallHeight()));
    }
    if (tableauRight.cols != bufferWidth or tableauRight.rows != smallHeight())
    {
        tableauRight = Mat_<int>(Size(bufferWidth, smallHeight()));
    }
    if (tableauTop.cols != bufferWidth or tableauTop.rows != smallHeight())
    {
        tableauTop = Mat_<int>(Size(bufferWidth, smallHeight()));
    }
    if (tableauBottom.cols != bufferWidth or tableauBottom.rows != smallHeight())
    {
        tableauBottom = Mat_<int>(Size(bufferWidth, smallHeight()));
    }
    if (smallDisparity.cols != smallWidth() or smallDisparity.rows != smallHeight())
    {
        smallDisparity = Mat_<uint8_t>(Size(smallWidth(), smallHeight()));
    }
}
Esempio n. 5
0
GaussianMixture* HogPeopleDetector::process(int offsetX=0, int offsetY=0, double scale=1) {

    detectedROI.clear();
    detectedCENTER.clear();
    detectedSCALES.clear();
    detectedIDS.clear();

    //gets the detections
    vector<cv::Rect> rois;
    vector<cv::Point> points;
    HOGDescriptor hogDesc;//the detector
    hogDesc.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());//we use a people detector

    //cv::Mat m (this->imgBank->imgSRC);
    cv::gpu::GpuMat gpuMat(this->imgBank->imgSRC);

    hogDesc.detectMultiScale(gpuMat, rois, 0, Size(8, 8),
                             Size(32,32), 1.05, 2.5);

    //fills the gaussian mixture with detections
    for(int i = 0 ; i < rois.size() ; i++) {
        cv::Rect r = rois[i];
        detectedROI.push_back(r);
        cv::Point p;
        p.x = r.x + r.width*0.5;
        p.y = r.y + r.height*0.5;
        detectedCENTER.push_back(p);
        this->gm->glist[i]->mean[0] = p.x;
        this->gm->glist[i]->mean[1] = p.y;
    }
    this->nbdetected = rois.size();

    gm->curnb = rois.size();
    return gm;

}
Esempio n. 6
0
void FingerTracker::Start()
{
	cvDestroyAllWindows();
	VideoCapture capture(0);
	if (!capture.isOpened())
	{
		throw std::runtime_error("Could not start camera capture");
	}

	Mat frame;
    while (true)
	{
		if (capture.read(frame))
		{
			if (frame.rows && frame.cols)
			{
				flip(frame, frame, 1);
				pyrDown(frame, frame, Size(frame.cols / 2, frame.rows / 2));

				Process(frame);

				auto const& candidate = GetCandidate();

#if ENABLE_LINE_DRAWING
				if (candidate.m_found)
				{
					m_points.push_back(candidate.m_fingerPosition);
				}
				if (m_points.size() > LINE_HISTORY)
				{
					m_points.pop_front();
				}
#endif

				Display(frame, candidate);
			}
		}
					
		auto key = cvWaitKey(10);
		if (char(key) == 27)
		{
			break;
		}
	}
	capture.release();
}
void DeepPyramid::detect(const Mat &img, vector<BoundingBox> &objects, bool isBoundingBoxRegressor) const {
    CV_Assert(img.channels() == 3);
    vector<FeatureMap> maps;
    cout << "here!" << endl;
    constructFeatureMapPyramid(img, maps);
    cout << "filter" << endl;
    detect(maps, objects);
    cout << "group rectangle" << endl;
    calculateOriginalRectangle(objects, Size(img.cols, img.rows));
    groupRectangle(objects);
    if (isBoundingBoxRegressor) {
        cout << "boundbox regressor: TODO" << endl;
    } else {
        cout << "bounding box regressor switch off" << endl;
    }
    cout << "Object count:" << objects.size() << endl;
}
void DeepPyramid::detect(const Mat& img, vector<Rect>& detectedObjects, vector<float>& confidence, bool isBoundingBoxRegressor) const {
    CV_Assert(img.channels() == 3);
    vector<FeatureMap> maps;
    constructFeatureMapPyramid(img, maps);
    cout << img.cols << "," << img.rows << "," << rootFilter.size() << endl;
    vector<BoundingBox> objects;
    detect(maps, objects);
    cout << "group rectangle" << endl;
    calculateOriginalRectangle(objects, Size(img.cols, img.rows));
    groupRectangle(objects);
    if (isBoundingBoxRegressor) {
        cout << "boundbox regressor: TODO" << endl;
    } else {
        cout << "bounding box regressor switch off" << endl;
    }
    cout << "Object count:" << objects.size() << endl;
    for (size_t i = 0; i < objects.size(); i++) {
        detectedObjects.push_back(objects[i].originalImageBox);
        confidence.push_back(objects[i].confidence);
    }
}
Esempio n. 9
0
void EnhancedStereo::computeCost(const Mat_<uint8_t> & img1, const Mat_<uint8_t> & img2)
{
    double T1 = 0, T2 = 0; // time profiling
//    cout << "cost" << endl;
    Mat_<uint8_t> img2remap(Size(blockSize - 1 + dispMax, blockSize));
    Mat_<int> integral1, integral2;
    integral(img1, integral1);
    int blockSize2 = blockSize * blockSize;
    for (int v = 0; v < smallHeight(); v++)
    {
        for (int u = 0; u < smallWidth(); u++)
        {
            int idx = getLinearIdx(vBig(v), uBig(u));
            uint8_t * outPtr = errorBuffer.row(v).data + u*dispMax;
            
            Point pinf = pinfPxVec[idx];
            CurveRasterizer<Polynomial2> raster(pinf.x, pinf.y, epipolePx.x, epipolePx.y, epipolarVec[idx]);
            
            // the remap Mat
            img2remap.setTo(0);
            for (int i = 0; i < img2remap.cols; i++, raster.step())
            {
                int u2 = raster.x + halfBlockSize();
                if (u2 < 0 or u2 >= img2.cols) continue;
                for (int j = -halfBlockSize(); j <= halfBlockSize(); j++)
                {
                    int v2 = raster.y + j;
                    if (v2 < 0 or v2 >= img2.rows) continue;
                    img2remap(halfBlockSize() + j, i) = img2(v2, u2);
                }
            }
            
            //compute bias
            int u1 = uBig(u) - halfBlockSize();
            int v1 = vBig(v) - halfBlockSize();
            int bias1 = integral1(v1, u1) + integral1(v1 + blockSize, u1 + blockSize) -
                         integral1(v1 + blockSize, u1) - integral1(v1, u1 + blockSize);
//            
//            
            integral(img2remap, integral2);
            
            // compute the actual error
            for (int i = 0; i < dispMax; i++, outPtr++)
            {
                int bias = integral2(blockSize, i + blockSize) - integral2(blockSize, i);
                bias = (bias - bias1) / blockSize2;
                bias = min(10, max(-10, bias));
//                cout << bias << " ";
                int acc = 0;
                for (int x2 = -halfBlockSize(); x2 <= halfBlockSize(); x2++)
                {
                    for (int x1 = -halfBlockSize(); x1 <= halfBlockSize(); x1++)
                    {
                        acc += abs(img1(vBig(v) + x2, uBig(u)- x1) - 
                            img2remap(halfBlockSize() + x2, i + halfBlockSize() + x1) + bias);
                    }
                }
                
                *outPtr = acc / blockSize2;
            }
//            cout << endl;
        }
    }
//    cout << "read " << T1 / CLOCKS_PER_SEC << endl;
//    cout << "write " << T2 / CLOCKS_PER_SEC << endl;
} 
Esempio n. 10
0
Size Camera::getSize() {
    return Size((int)getWidth(), (int)getHeight());
}
Esempio n. 11
0
void FingerTracker::Setup()
{
	VideoCapture capture(0);
	if (!capture.isOpened())
	{
		throw std::runtime_error("Could not start camera capture");
	}

	int windowSize = 25;
	int Xpos = 200;
	int Ypos = 50;
	int update = 0;
	int buttonClicked = 0;
	namedWindow("RGB", CV_WINDOW_AUTOSIZE);
	createTrackbar("X", "RGB",  &Xpos, 320, TrackBarCallback, (void*)&update);
	createTrackbar("Y", "RGB",  &Ypos, 240, TrackBarCallback, (void*)&update);
	createTrackbar("Size", "RGB",  &windowSize, 100, TrackBarCallback, (void*)&update);
	setMouseCallback("RGB", MouseCallback, (void*)&buttonClicked);
	Mat fingerWindowBackground, fingerWindowBackgroundGray;

	m_calibrationData.reset(new CalibrationData());
	bool ticking = false;
	std::chrono::system_clock::time_point start = std::chrono::system_clock::now();
    while (true)
	{  		
		Mat frame, frameHSV;

		if (capture.read(frame))
		{
			flip(frame, frame, 1);

			pyrDown(frame, frame, Size(frame.cols / 2, frame.rows / 2));

			Rect fingerWindow(Point(Xpos, Ypos), Size(windowSize, windowSize*3));
			if (Xpos + windowSize >= frame.cols || Ypos + windowSize*3 >= frame.rows)
			{
				windowSize = 20;
				Xpos = 200;
				Ypos = 50;
				update = 0;
			}
			else if (buttonClicked == 1)
			{
				frame(fingerWindow).copyTo(fingerWindowBackground);
				cvtColor(fingerWindowBackground, fingerWindowBackgroundGray, CV_BGR2GRAY);
				buttonClicked  = 0;
				update = 0;
				cvDestroyAllWindows();
			}

			if (fingerWindowBackgroundGray.rows && !m_calibrationData->m_ready)
			{
				Mat diff, thd;
				absdiff(frame(fingerWindow), fingerWindowBackground, diff);
				std::vector<Mat> ch;
				split(diff, ch);
				threshold(ch[0], ch[0], m_calibrationDiffThreshold, 255, 0);
				threshold(ch[1], ch[1], m_calibrationDiffThreshold, 255, 0);
				threshold(ch[2], ch[2], m_calibrationDiffThreshold, 255, 0);
				thd = ch[0];
				add(thd, ch[1], thd);
				add(thd, ch[2], thd);
				medianBlur(thd, thd, 5);

				Mat top, middle, bottom;
				Rect r1 = Rect(0, 0, thd.cols, thd.rows/3);
				Rect r2 = Rect(0, thd.rows / 3 + 1, thd.cols, thd.rows/3);
				Rect r3 = Rect(0, thd.rows * 2 / 3 + 1, thd.cols, thd.rows -  thd.rows * 2 / 3 - 1);
				top = thd(r1);
				middle = thd(r2);
				bottom = thd(r3);			

				auto percentageTop = countNonZero(top) * 100.0 / top.size().area();
				auto percentageMiddle = countNonZero(middle) * 100.0 / middle.size().area();
				auto percentageBottom = countNonZero(bottom) * 100.0 / bottom.size().area();

				bool topReady = false;
				bool middleReady = false;
				bool bottomReady = false;

				Scalar c1, c2, c3;
				if (percentageTop > m_calibrationTopLowerThd && percentageTop < m_calibrationTopUpperThd)
				{
					topReady = true;
					c1 = Scalar(0, 255, 255);
				}
				else
				{
					c1 = Scalar(0, 0, 255);
				}

				if (percentageMiddle > m_calibrationMiddleLowerThd && percentageMiddle < m_calibrationMiddleUppperThd)
				{
					middleReady = true;					
					c2 = Scalar(0, 255, 255);
				}
				else
				{
					c2 = Scalar(0, 0, 255);
				}

				if (percentageBottom > m_calibrationBottomLowerThd && percentageBottom < m_calibrationBottomUpperThd)
				{
					bottomReady = true;
					c3 = Scalar(0, 255, 255);
				}
				else
				{
					c3 = Scalar(0, 0, 255);
				}

				bool readyToGo = false;
				if (middleReady && topReady && bottomReady)
				{
					c1 = Scalar(0, 255, 0);
					c2 = Scalar(0, 255, 0);
					c3 = Scalar(0, 255, 0);
					if (!ticking)
					{
						start = std::chrono::system_clock::now();
						ticking = true;
					}
					if (std::chrono::system_clock::now() - start > std::chrono::seconds(1))
					{
						readyToGo = true;
					}
				}
				else
				{
					ticking = false;
				}

#if ENABLE_DEBUG_WINDOWS
				std::stringstream ss;
				ss << percentageTop << ", " << percentageMiddle << ", " << percentageBottom;
				putText(frame, ss.str(), Point(0, getTextSize(ss.str(), 0, 0.5, 1, nullptr).height), 0, 0.5, Scalar::all(255), 1);
				cv::imshow("Thresholded", thd);
#endif

				if (percentageTop >= m_calibrationTopUpperThd && percentageBottom >= m_calibrationBottomUpperThd && percentageMiddle >= m_calibrationMiddleUppperThd)
				{
					putText(frame, "Move finger away from camera", Point(0, getTextSize("Move finger away from camera", 0, 0.5, 1, nullptr).height), 0, 0.5, Scalar::all(255), 1);
				}
				else if (percentageTop <= m_calibrationTopLowerThd && percentageBottom <= m_calibrationBottomLowerThd && percentageMiddle <= m_calibrationMiddleLowerThd)
				{
					putText(frame, "Move finger closer to camera", Point(0, getTextSize("Move finger closer to camera", 0, 0.5, 1, nullptr).height), 0, 0.5, Scalar::all(255), 1);
				}
				
				if (readyToGo)
				{
					Mat framePatchHSV;
					cvtColor(frame(fingerWindow), framePatchHSV, CV_BGR2HSV);
					cvtColor(frame, frameHSV, CV_BGR2HSV);

					MatND hist;
					calcHist(&framePatchHSV, 1, m_calibrationData->m_channels, thd, hist, 2, m_calibrationData->m_histSize, (const float**)m_calibrationData->m_ranges, true, false);
					m_calibrationData->m_hist = hist;
					normalize(m_calibrationData->m_hist, m_calibrationData->m_hist, 0, 255, NORM_MINMAX, -1, Mat());

#if ENABLE_DEBUG_WINDOWS

					double maxVal=0;
					minMaxLoc(m_calibrationData->m_hist, 0, &maxVal, 0, 0);
					int scale = 10;
					Mat histImg = Mat::zeros(m_calibrationData->m_sbins*scale, m_calibrationData->m_hbins*10, CV_8UC3);
					for( int h = 0; h < m_calibrationData->m_hbins; h++)
					{
						for( int s = 0; s < m_calibrationData->m_sbins; s++ )
						{
							float binVal = m_calibrationData->m_hist.at<float>(h, s);
							int intensity = cvRound(binVal*255/maxVal);
							rectangle( histImg, Point(h*scale, s*scale),
										Point( (h+1)*scale - 1, (s+1)*scale - 1),
										Scalar::all(intensity),
										CV_FILLED );
						}
					}

					imshow("H-S Histogram", histImg);
#endif
					m_calibrationData->m_ready = true;
					frame(fingerWindow).copyTo(m_calibrationData->m_fingerPatch);
					m_calibrationData->m_fingerRect = fingerWindow;
					m_currentCandidate.m_windowRect = fingerWindow;
					m_currentCandidate.m_fingerPosition = fingerWindow.tl();
					return;
				}

				rectangle(frame, r1.tl() + fingerWindow.tl(), r1.br() + fingerWindow.tl(), c1);
				rectangle(frame, r2.tl() + fingerWindow.tl(), r2.br() + fingerWindow.tl(), c2);
				rectangle(frame, r3.tl() + fingerWindow.tl(), r3.br() + fingerWindow.tl(), c3);
				imshow("Calibration", frame);
			}
			else
			{
				int baseline = 0;
				putText(frame, "Adjust calibration window, click when ready", Point(0, getTextSize("Adjust calibration window", 0, 0.4, 2, &baseline).height), 0, 0.4, Scalar::all(255), 1);
				rectangle(frame, fingerWindow.tl(), fingerWindow.br(), Scalar(0, 0, 255));
				imshow("RGB", frame);
			}
			auto key = cvWaitKey(10);
			if (char(key) == 27)
			{
				break;
			}
		}
	}
	capture.release();
}
Esempio n. 12
0
void FingerTracker::Process(Mat frame)
{
#if ENABLE_DEBUG_WINDOWS
	Mat img_display;
	frame.copyTo(img_display);
#endif
	
	//	Process only Region of Interest i.e. region around current finger position
	Rect roi = Rect(		
		Point(std::max(m_currentCandidate.m_windowRect.tl().x - m_roiSpanX, 0), std::max(m_currentCandidate.m_windowRect.tl().y - m_roiSpanY, 0)), 		
		Point(std::min(m_currentCandidate.m_windowRect.tl().x + m_roiSpanX + m_calibrationData->m_fingerPatch.cols, frame.cols), 
			  std::min(m_currentCandidate.m_windowRect.tl().y + m_roiSpanY + m_calibrationData->m_fingerPatch.rows, frame.rows)));

	Mat frameRoi;
	frame(roi).copyTo(frameRoi);

	//================TEMPLATE MATCHING
	int result_cols =  frameRoi.cols - m_calibrationData->m_fingerPatch.cols + 1;
	int result_rows = frameRoi.rows - m_calibrationData->m_fingerPatch.rows + 1;
	assert(result_cols > 0 && result_rows > 0);

	Mat scoreMap;
	scoreMap.create(result_cols, result_rows, CV_32FC1);

	//	Compare current frame roi region to known candidate
	//	Using OpenCV matchTemplate function with correlation coefficient matching method
	matchTemplate(frameRoi, m_calibrationData->m_fingerPatch, scoreMap, 3);

	//================HISTOGRAM BACK PROJECTION
	MatND backProjection;
	Mat frameHSV;
	cvtColor(frameRoi, frameHSV, CV_BGR2HSV);

	calcBackProject(&frameHSV, 1, m_calibrationData->m_channels, m_calibrationData->m_hist, backProjection, (const float**)(m_calibrationData->m_ranges), 1, true);

	Mat backProjectionThresholded;
	threshold(backProjection, backProjectionThresholded, m_backProjectionThreshold, 255, 0);
	erode(backProjectionThresholded, backProjectionThresholded, getStructuringElement(MORPH_RECT, Size(2 * m_erosionSize + 1, 2 * m_erosionSize + 1), Point(m_erosionSize, m_erosionSize)));
	dilate(backProjectionThresholded, backProjectionThresholded, getStructuringElement(MORPH_RECT, Size(2 * m_dilationSize + 1, 2 * m_dilationSize + 1), Point(m_dilationSize, m_dilationSize)));

	Mat backProjectionThresholdedShifted;
	Rect shifted(Rect(m_calibrationData->m_fingerPatch.cols - 1, m_calibrationData->m_fingerPatch.rows - 1, scoreMap.cols, scoreMap.rows));
	backProjectionThresholded(shifted).copyTo(backProjectionThresholdedShifted);

	Mat maskedOutScoreMap(scoreMap.size(), CV_8U);
	scoreMap.copyTo(maskedOutScoreMap, backProjectionThresholdedShifted);

	//====================Localizing the best match with minMaxLoc
	double minVal; double maxVal; Point minLoc; Point maxLoc;
	Point matchLoc;
	minMaxLoc(maskedOutScoreMap, &minVal, &maxVal, &minLoc, &maxLoc, Mat());
	matchLoc = maxLoc + roi.tl();

	m_currentCandidate.m_confidence = static_cast<float>(maxVal);

	if (maxVal > m_candidateDetecionConfidenceThreshold)
	{
		m_currentCandidate.m_found = true;
		m_currentCandidate.m_windowRect = Rect(matchLoc, Point(matchLoc.x + m_calibrationData->m_fingerPatch.cols , matchLoc.y + m_calibrationData->m_fingerPatch.rows));		

		//================Find finger position
		Mat fingerWindowThresholded;
		backProjectionThresholded(Rect(maxLoc, Point(maxLoc.x + m_calibrationData->m_fingerPatch.cols , maxLoc.y + m_calibrationData->m_fingerPatch.rows))).copyTo(fingerWindowThresholded);
		
		m_currentCandidate.m_fingerPosition = GetFingerTopPosition(fingerWindowThresholded) + matchLoc;

#if ENABLE_DEBUG_WINDOWS
		rectangle(img_display, m_currentCandidate.m_windowRect.tl(), m_currentCandidate.m_windowRect.br(), Scalar(255,0,0), 2, 8, 0 );
		rectangle(scoreMap, m_currentCandidate.m_windowRect.tl(), m_currentCandidate.m_windowRect.br(), Scalar::all(0), 2, 8, 0 );
		rectangle(img_display, m_currentCandidate.m_fingerPosition, m_currentCandidate.m_fingerPosition + Point(5,5), Scalar(255,0,0));
#endif
	}
	else
	{
		m_currentCandidate.m_found = false;
	}

#if ENABLE_DEBUG_WINDOWS
	std::stringstream ss;
	ss << maxVal;
	putText(img_display, ss.str(), Point(50, 50), 0, 0.5, Scalar(255,255,255), 1);

	imshow("Overlays", img_display);
	imshow("Results", scoreMap);
	imshow("ResultMasked", maskedOutScoreMap);
#endif
}
/*
 * Class:     io_github_melvincabatuan_fullbodydetection_MainActivity
 * Method:    predict
 * Signature: (Landroid/graphics/Bitmap;[B)V
 */
JNIEXPORT void JNICALL Java_io_github_melvincabatuan_fullbodydetection_MainActivity_predict
  (JNIEnv * pEnv, jobject clazz, jobject pTarget, jbyteArray pSource){

   AndroidBitmapInfo bitmapInfo;
   uint32_t* bitmapContent; // Links to Bitmap content

   if(AndroidBitmap_getInfo(pEnv, pTarget, &bitmapInfo) < 0) abort();
   if(bitmapInfo.format != ANDROID_BITMAP_FORMAT_RGBA_8888) abort();
   if(AndroidBitmap_lockPixels(pEnv, pTarget, (void**)&bitmapContent) < 0) abort();

   /// Access source array data... OK
   jbyte* source = (jbyte*)pEnv->GetPrimitiveArrayCritical(pSource, 0);
   if (source == NULL) abort();

   /// cv::Mat for YUV420sp source and output BGRA 
    Mat srcGray(bitmapInfo.height, bitmapInfo.width, CV_8UC1, (unsigned char *)source);
    Mat mbgra(bitmapInfo.height, bitmapInfo.width, CV_8UC4, (unsigned char *)bitmapContent);

/***********************************************************************************************/
    /// Native Image Processing HERE... 
    if(DEBUG){
      LOGI("Starting native image processing...");
    }

    if (full_body_cascade.empty()){
       t = (double)getTickCount();
       sprintf( full_body_cascade_path, "%s/%s", getenv("ASSETDIR"), "haarcascade_fullbody.xml");       
    
      /* Load the face cascades */
       if( !full_body_cascade.load(full_body_cascade_path) ){ 
           LOGE("Error loading cat face cascade"); 
           abort(); 
       };

       t = 1000*((double)getTickCount() - t)/getTickFrequency();
       if(DEBUG){
       LOGI("Loading full body cascade took %lf milliseconds.", t);
     }
    }
            
 
     std::vector<Rect> fbody;


       //-- Detect full body
       t = (double)getTickCount();
 
       /// Detection took cat_face_cascade.detectMultiScale() time = 655.334471 ms
      // cat_face_cascade.detectMultiScale( srcGray, faces, 1.1, 2 , 0 , Size(30, 30) ); // Scaling factor = 1.1;  minNeighbors = 2 ; flags = 0; minimumSize = 30,30

      // cat_face_cascade.detectMultiScale() time = 120.117185 ms
      // cat_face_cascade.detectMultiScale( srcGray, faces, 1.2, 3 , 0 , Size(64, 64));

 
      
      full_body_cascade.detectMultiScale( srcGray, fbody, 1.2, 2 , 0 , Size(14, 28));  // Size(double width, double height) 

      // scalingFactor parameters determine how much the classifier will be scaled up after each run.
      // minNeighbors parameter specifies how many positive neighbors a positive face rectangle should have to be considered a possible match; 
      // when a potential face rectangle is moved a pixel and does not trigger the classifier any more, it is most likely that it’s a false positive. 
      // Face rectangles with fewer positive neighbors than minNeighbors are rejected. 
      // If minNeighbors is set to zero, all potential face rectangles are returned. 
      // The flags parameter is from the OpenCV 1.x API and should always be 0. 
      // minimumSize specifies the smallest face rectangle we’re looking for. 

       t = 1000*((double)getTickCount() - t)/getTickFrequency();
       if(DEBUG){
          LOGI("full_body_cascade.detectMultiScale() time = %lf milliseconds.", t);
      }


       // Iterate through all faces and detect eyes
       t = (double)getTickCount();

       for( size_t i = 0; i < fbody.size(); i++ )
       {
          Point center(fbody[i].x + fbody[i].width / 2, fbody[i].y + fbody[i].height / 2);
          ellipse(srcGray, center, Size(fbody[i].width / 2, fbody[i].height / 2), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0);
       }//endfor
  
       t = 1000*((double)getTickCount() - t)/getTickFrequency();
       if(DEBUG){
          LOGI("Iterate through all faces and detecting eyes took %lf milliseconds.", t);
       }

       /// Display to Android
       cvtColor(srcGray, mbgra, CV_GRAY2BGRA);


      if(DEBUG){
        LOGI("Successfully finished native image processing...");
      }
   
/************************************************************************************************/ 
   
   /// Release Java byte buffer and unlock backing bitmap
   pEnv-> ReleasePrimitiveArrayCritical(pSource,source,0);
   if (AndroidBitmap_unlockPixels(pEnv, pTarget) < 0) abort();
}
Esempio n. 14
0
void open_imgs_dir(const char* dir_name, std::vector<cv::Mat>& images, std::vector<std::string>& images_names, double downscale_factor) {
	if (dir_name == NULL) {
		return;
	}

	string dir_name_ = string(dir_name);
	vector<string> files_;

#ifndef WIN32
//open a directory the POSIX way

	DIR *dp;
	struct dirent *ep;     
	dp = opendir (dir_name);
	
	if (dp != NULL)
	{
		while ((ep = readdir (dp))) {
			if (ep->d_name[0] != '.')
				files_.push_back(ep->d_name);
		}
		
		(void) closedir (dp);
	}
	else {
		cerr << ("Couldn't open the directory");
		return;
	}

#else
//open a directory the WIN32 way
	HANDLE hFind = INVALID_HANDLE_VALUE;
	WIN32_FIND_DATA fdata;

	if(dir_name_[dir_name_.size()-1] == '\\' || dir_name_[dir_name_.size()-1] == '/') {
		dir_name_ = dir_name_.substr(0,dir_name_.size()-1);
	}

	hFind = FindFirstFile(string(dir_name_).append("\\*").c_str(), &fdata);	
	if (hFind != INVALID_HANDLE_VALUE)
	{
		do
		{
			if (strcmp(fdata.cFileName, ".") != 0 &&
				strcmp(fdata.cFileName, "..") != 0)
			{
				if (fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
				{
					continue; // a diretory
				}
				else
				{
					files_.push_back(fdata.cFileName);
				}
			}
		}
		while (FindNextFile(hFind, &fdata) != 0);
	} else {
		cerr << "can't open directory\n";
		return;
	}

	if (GetLastError() != ERROR_NO_MORE_FILES)
	{
		FindClose(hFind);
		cerr << "some other error with opening directory: " << GetLastError() << endl;
		return;
	}

	FindClose(hFind);
	hFind = INVALID_HANDLE_VALUE;
#endif
	
	for (unsigned int i=0; i<files_.size(); i++) {
		if (files_[i][0] == '.' || !(hasEndingLower(files_[i],"jpg")||hasEndingLower(files_[i],"png"))) {
			continue;
		}
		cv::Mat m_ = cv::imread(string(dir_name_).append("/").append(files_[i]));
		if(downscale_factor != 1.0)
			cv::resize(m_,m_,Size(),downscale_factor,downscale_factor);
		images_names.push_back(files_[i]);
		images.push_back(m_);
	}
		

}
Esempio n. 15
0
int main(int argc, char **argv)
{
    if (argc < 2)
    {
        cout << "Usage: " << argv[0] << " <raw_file> [frame_id]" << endl;
        return 1;
    }

    ifstream in{argv[1]};
    struct stat st;
    char *buf{nullptr};
    size_t file_size;
    size_t i{0};
    array<char, 160 * 120 * 2> raw;
    vector<Packet> pkts;
    vector<uint8_t> ids;

    if (stat(argv[1], &st) != 0)
    {
        cout << "Could not stat " << argv[1] << endl;
        return 1;
    }

    file_size = static_cast<size_t>(st.st_size);
    buf = new char[file_size];
    in.read(buf, file_size);

    if (static_cast<size_t>(in.tellg()) != file_size)
    {
        cout << "Did not reach EOF" << endl;
        cout << "read " << in.tellg() << "/" << file_size << endl;
        cout << "file is good: " << static_cast<int>(in.good()) << endl;
        return 1;
    }

    uint8_t id = 0;
    while (i < file_size)
    {
        Packet tmp;

        try
        {
            tmp.parse(reinterpret_cast<uint8_t*>(buf + i), file_size - i);
        }
        catch (const ParseError &e)
        {
            cout << "Caught a parsing error - \"" << e.getMessage() << "\"" <<
                endl;
            break;
        }

        if (id != tmp.getID())
        {
            id = tmp.getID();
            cout << "id: " << static_cast<int>(tmp.getID()) << " ";
            cout << "len: " << static_cast<int>(tmp.getLength()) << endl;
        }
        pkts.push_back(tmp);

        i += tmp.getRawLength();
    }

    cout << "Parsed " << pkts.size() << " packets" << endl;

    for (int k = 2; k < argc; k++)
    {
        ids.push_back(static_cast<uint8_t>(std::stoi(string{argv[k]})));
    }

    if (ids.size() == 0)
    {
        return 0;
    }

    cout << "examining IDs ";
    for (const auto &id : ids)
    {
        cout << static_cast<int>(id) << " ";
    }
    cout << endl;

    for (const auto &id : ids)
    {
        vector<uint8_t> raw;
        PacketType type;

        cout << "examining " << static_cast<int>(id) << endl;

        for (const auto &pkt : pkts)
        {
            if (pkt.getID() == id)
            {
                size_t new_length = pkt.getOffset() +
                    static_cast<size_t>(pkt.getLength());

                type = pkt.getType();

                if (new_length > raw.size())
                {
                    raw.resize(new_length, 0);
                }

                for (size_t k = 0; k < pkt.getLength(); k++)
                {
                    raw[pkt.getOffset() + k] = pkt.getData()[k];
                }
            }
        }

        cout << "type: " << type << endl;

        cout << hex;
        for (size_t j = 0; j < 512; j++)
        {
            if (j > 0 && (j % 32) == 0)
            {
                cout << endl;
            }
            cout << (static_cast<unsigned int>(raw[j]) & 0xFF) << " ";
        }
        cout << endl;

        Mat img{Size(160, 120), CV_8UC3, raw.data()};
        Mat dst;

        //cvtColor(img, dst, CV_YUV2GRAY_UYVY);
        //cvtColor(img, dst, CV_YUV2GRAY_YUY2);
        //cvtColor(img, dst, CV_YUV2BGR_UYVY);
        //cvtColor(img, dst, CV_YUV2BGRA_UYVY);
        //cvtColor(img, dst, CV_YUV2BGR_YUY2);
        //cvtColor(img, dst, CV_YUV2BGR_YVYU);
        //cvtColor(img, dst, CV_YUV2BGRA_YUY2);
        //cvtColor(img, dst, CV_YUV2BGRA_YVYU);
        //cvtColor(img, dst, CV_YUV2GRAY_NV12);
        //cvtColor(img, dst, CV_YUV2BGR_NV12);
        cvtColor(img, dst, CV_YCrCb2BGR);

        imshow("Police Video", dst);
        waitKey(0);

        ofstream out_jpeg{"out.jpg", ofstream::binary};
        for (size_t j = 0; j < raw.size(); j++)
        {
            if (raw[j] == 0xFF && raw[j + 1] == 0xD8)
            {
                cout << "hit the start" << endl;
                out_jpeg.write(reinterpret_cast<char *>(raw.data() + j), 2);
                j++;
            }
            else if (raw[j] == 0xFF && raw[j + 1] == 0xDB)
            {
                cout << "hit the start 2" << endl;
                out_jpeg.write(reinterpret_cast<char *>(raw.data() + j), 2);
                j++;
            }
            else if (raw[j] == 0xFF && raw[j + 1] == 0xC0)
            {
                cout << "hit the start 3" << endl;
                out_jpeg.write(reinterpret_cast<char *>(raw.data() + j), 2);
                j++;
            }
            else if (raw[j] == 0xFF && raw[j + 1] == 0xD9)
            {
                cout << "hit the end" << endl;
                out_jpeg.write(reinterpret_cast<char *>(raw.data() + j), 2);
                return 0;
            }
            else
            {
                out_jpeg.write(reinterpret_cast<char *>(raw.data() + j), 1);
            }
        }
    }

    return 0;
}