Exemplo n.º 1
0
    void Frame::FindKeypoints(const cv::Mat& img)
    {
        std::chrono::steady_clock::time_point t1 = std::chrono::steady_clock::now();
        scanimg = img;
        int kPcount = 0;

        zbar::ImageScanner scanner;
        scanner.set_config( zbar::ZBAR_NONE,  zbar::ZBAR_CFG_ENABLE, 1);

        Image qrimage(scanimg.cols, scanimg.rows, "Y800", scanimg.data, scanimg.cols* scanimg.rows);

        std::chrono::steady_clock::time_point t2 = std::chrono::steady_clock::now();

        if( scanner.scan(qrimage) > 0){
            for(Image::SymbolIterator symbol = qrimage.symbol_begin();symbol !=
                                                                      qrimage.symbol_end();
                ++symbol)
            {
                kPcount++;
                for(int k = 0; k < 4; k++)if(symbol->get_location_size()==4){

                        keyFeature kfeature;

                        cv::KeyPoint kp;
                        kp.pt.x = (float)symbol->get_location_x(k);
                        kp.pt.y = (float)symbol->get_location_y(k);

                        kfeature.kp = kp;
                        kfeature.marker_id = k;
                        kfeature.code_id =  stoi(symbol->get_data());
                        frameKeyFeatures.push_back(kfeature);

                        //std::cout << "locations are "  <<  symbol->get_location_x(k) <<" "<<symbol->get_location_y(k)<<std::endl;
                        cv::Point sympos =  cv::Point( symbol->get_location_x(k), symbol->get_location_y(k));
                        cv::circle( scanimg, sympos, 4, cv::Scalar( 0, 0, 255 ), 3, 8 );
                    }


                std::string out =  "the id of code is " + symbol->get_data();
                //std::cout << out<< std::endl;
                //cv::imshow("123", scanimg);
            }

            std::chrono::steady_clock::time_point t3 = std::chrono::steady_clock::now();

            double ttrack1 = std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
            double ttrack2 = std::chrono::duration_cast<std::chrono::microseconds>(t3 - t2).count();
            double ttrack3 = std::chrono::duration_cast<std::chrono::microseconds>(t3 - t1).count();
            //std::cout <<"ttrack1 "<< ttrack1 <<"us ttrack2 "<< ttrack2 <<"us ttrack3 is "<< ttrack3 <<"us"<< std::endl;
            isKeyframe = true;
        }
    }
Exemplo n.º 2
0
/**
 * This is a (modified) test program written by Michael Young
 * (https://github.com/ayoungprogrammer/WebcamCodeScanner). It was modified to
 * work with the Raspicam.
 */
int main(int argc, char* argv[])
{
	PiCamera cam; // open the video camera no. 0
	ImageScanner scanner;  
	scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);  

	namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"

	while (1)
	{
		Mat frame = cam.Snap();
		Mat grey;
		cvtColor(frame,grey,CV_BGR2GRAY);

		int width = frame.cols;  
		int height = frame.rows;  
		uchar *raw = (uchar *)grey.data;  
		// wrap image data  
		zbar::Image image(width, height, "Y800", raw, width * height);  
		// scan the image for barcodes  
		int n = scanner.scan(image);  
		// extract results  
		for(Image::SymbolIterator symbol = image.symbol_begin();  
				symbol != image.symbol_end();  
				++symbol) {  
			vector<Point> vp;  
			// do something useful with results  
			cout << "decoded " << symbol->get_type_name()  << " symbol \"" << symbol->get_data() << '"' <<" "<< endl;  
			int n = symbol->get_location_size();  
			for(int i=0;i<n;i++){  
				vp.push_back(Point(symbol->get_location_x(i),symbol->get_location_y(i))); 
			}  
			RotatedRect r = minAreaRect(vp);  
			Point2f pts[4];  
			r.points(pts);  
			for(int i=0;i<4;i++){  
				line(frame,pts[i],pts[(i+1)%4],Scalar(255,0,0),3);  
			}  
		}  

		imshow("MyVideo", frame); //show the frame in "MyVideo" window

		if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
		{
			cout << "esc key is pressed by user" << endl;
			break; 
		}
	}
	return 0;

}
Exemplo n.º 3
0
void QrTracker::track(Mat frame, int &size_x, int &size_y, string &data, Point2f &p1, Point2f &p2, Point2f &p3, Point2f &p4)
{
    Mat frame_grayscale;
    cvtColor(frame, frame_grayscale, COLOR_BGR2HSV);

    // wrap image
    int width = frame_grayscale.cols;
    int height = frame_grayscale.rows;
    uchar *raw = (uchar *)(frame_grayscale.data);
    Image image(width, height, "Y800", raw, width * height);

    //Scan the image for QRcodes
    scanner.scan(image);
    for (Image::SymbolIterator symbol = image.symbol_begin(); symbol != image.symbol_end(); ++symbol)
    {
        //Size
        size_x = symbol->get_location_x(2) - symbol->get_location_x(1);
        size_y = symbol->get_location_y(1) - symbol->get_location_y(0);

        //Data
        data = symbol->get_data().c_str();

        //Points
        if (symbol->get_location_size() == 4)
        {
            p1 = Point2f(symbol->get_location_x(0), symbol->get_location_y(0));
            p2 = Point2f(symbol->get_location_x(1), symbol->get_location_y(1));
            p3 = Point2f(symbol->get_location_x(2), symbol->get_location_y(2));
            p4 = Point2f(symbol->get_location_x(3), symbol->get_location_y(3));
        }
        else
        {
            p1 = Point2f(-1,-1);
            p2 = Point2f(-1,-1);
            p3 = Point2f(-1,-1);
            p4 = Point2f(-1,-1);
        }
    }
}
Exemplo n.º 4
0
int main(int argc, char **argv) {
    int cam_idx = 0;

    if (argc == 2) {
        cam_idx = atoi(argv[1]);
    }

    VideoCapture cap(cam_idx);
    if (!cap.isOpened()) {
        cerr << "Could not open camera." << endl;
        exit(EXIT_FAILURE);
    }
    //cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
    //cap.set(CV_CAP_PROP_FRAME_HEIGHT, 480);

    namedWindow("captured", CV_WINDOW_AUTOSIZE);
    
    // Create a zbar reader
    ImageScanner scanner;
    
    // Configure the reader
    scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);

    for (;;) {
        // Capture an OpenCV frame
        cv::Mat frame, frame_grayscale;
        cap >> frame;

        // Convert to grayscale
        cvtColor(frame, frame_grayscale, CV_BGR2GRAY);

        // Obtain image data
        int width = frame_grayscale.cols;
        int height = frame_grayscale.rows;
        uchar *raw = (uchar *)(frame_grayscale.data);

        // Wrap image data
        Image image(width, height, "Y800", raw, width * height);

        // Scan the image for barcodes
        //int n = scanner.scan(image);
        scanner.scan(image);

        // Extract results
        int counter = 0;
        for (Image::SymbolIterator symbol = image.symbol_begin(); symbol != image.symbol_end(); ++symbol) {
            time_t now;
            tm *current;
            now = time(0);
            current = localtime(&now);

            // do something useful with results
            cout    << "[" << current->tm_hour << ":" << current->tm_min << ":" << setw(2) << setfill('0') << current->tm_sec << "] " << counter << " "
                    << "decoded " << symbol->get_type_name()
                    << " symbol \"" << symbol->get_data() << '"' << endl;

            //cout << "Location: (" << symbol->get_location_x(0) << "," << symbol->get_location_y(0) << ")" << endl;
            //cout << "Size: " << symbol->get_location_size() << endl;
            
            // Draw location of the symbols found
            if (symbol->get_location_size() == 4) {
                //rectangle(frame, Rect(symbol->get_location_x(i), symbol->get_location_y(i), 10, 10), Scalar(0, 255, 0));
                line(frame, Point(symbol->get_location_x(0), symbol->get_location_y(0)), Point(symbol->get_location_x(1), symbol->get_location_y(1)), Scalar(0, 255, 0), 2, 8, 0);
                line(frame, Point(symbol->get_location_x(1), symbol->get_location_y(1)), Point(symbol->get_location_x(2), symbol->get_location_y(2)), Scalar(0, 255, 0), 2, 8, 0);
                line(frame, Point(symbol->get_location_x(2), symbol->get_location_y(2)), Point(symbol->get_location_x(3), symbol->get_location_y(3)), Scalar(0, 255, 0), 2, 8, 0);
                line(frame, Point(symbol->get_location_x(3), symbol->get_location_y(3)), Point(symbol->get_location_x(0), symbol->get_location_y(0)), Scalar(0, 255, 0), 2, 8, 0);
            }
            
            // Get points
            /*for (Symbol::PointIterator point = symbol.point_begin(); point != symbol.point_end(); ++point) {
                cout << point << endl;
            } */
            counter++;
        }

        // Show captured frame, now with overlays!
        imshow("captured", frame);
                                                                                                                                                          
        // clean up
        image.set_data(NULL, 0);
        
        waitKey(30);
    }

    return 0;
}
Exemplo n.º 5
0
JNIEXPORT void JNICALL Java_com_example_qr_MainActivity_nativeDetect(
		JNIEnv * jenv, jclass, jlong imageGray) {
	LOGD(
			"Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeDetect enter");
	try {
		vector<Rect> RectFaces;
		//       ((DetectionBasedTracker*)thiz)->process(*((Mat*)imageGray));
		//       ((DetectionBasedTracker*)thiz)->getObjects(RectFaces);
		//       vector_Rect_to_Mat(RectFaces, *((Mat*)faces));

		Mat img = *((Mat*) imageGray);
		Mat imgout;
		cvtColor(img, imgout, CV_GRAY2RGB);
		int width = img.cols;
		int height = img.rows;
		uchar *raw = (uchar *) img.data;
//		uchar *raw = (uchar *) imageGray;
		// wrap image data
		Image image(width, height, "Y800", raw, width * height);
		// scan the image for barcodes
		ImageScanner scanner;
		scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);

		int n = scanner.scan(image);
		// extract results
		for (Image::SymbolIterator symbol = image.symbol_begin();
				symbol != image.symbol_end(); ++symbol) {
			vector<Point> vp;
			// do something useful with results
			cout << "decoded " << symbol->get_type_name() << " symbol \""
					<< symbol->get_data() << '"' << " " << endl;
			int n = symbol->get_location_size();
			for (int i = 0; i < n; i++) {
				vp.push_back(
						Point(symbol->get_location_x(i),
								symbol->get_location_y(i)));

			}
			RotatedRect r = minAreaRect(vp);
			Point2f pts[4];
			r.points(pts);
			for (int i = 0; i < 4; i++) {
				line(imgout, pts[i], pts[(i + 1) % 4], Scalar(255, 0, 0), 3);
			}
			cout << "Angle: " << r.angle << endl;
		}
		imshow("imgout.jpg", imgout);
		// clean up
		image.set_data(NULL, 0);
		waitKey();

	} catch (cv::Exception& e) {
		LOGD("nativeCreateObject caught cv::Exception: %s", e.what());
		jclass je = jenv->FindClass("org/opencv/core/CvException");
		if (!je)
			je = jenv->FindClass("java/lang/Exception");
		jenv->ThrowNew(je, e.what());
	} catch (...) {
		LOGD("nativeDetect caught unknown exception");
		jclass je = jenv->FindClass("java/lang/Exception");
		jenv->ThrowNew(je,
				"Unknown exception in JNI code DetectionBasedTracker.nativeDetect()");
	}
	LOGD(
			"Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeDetect exit");
}