Ask Your Question
0

Can't process image using cv_bridge

asked 2013-05-06 01:20:55 -0500

Starsky Wong gravatar image

updated 2013-05-06 01:31:06 -0500

I'm using a library called 'tpofinder' to recognize textured objects,but it can only work using opencv's videocapture() function,and it's very slow,about 1 fps or less. I'm using CMake to build it,in the same project,the one using videocapture can process image,but the other one using cv_bridge can't process,it just return the origin image.

The one using videocapture

//Include headers for OpenCV Image processing
#include <opencv2/imgproc/imgproc.hpp>
//Include headers for OpenCV GUI handling
#include <opencv2/highgui/highgui.hpp>

#include <boost/foreach.hpp>
#include <boost/format.hpp>
#include <iostream>

#include "tpofinder/configure.h"
#include "tpofinder/detect.h"
#include "tpofinder/visualize.h"

using namespace cv;
using namespace tpofinder;
using namespace std;
VideoCapture *capture = NULL;
bool verbose = true;

Mat image;
Detector detector;

void openCamera() {
    capture = new VideoCapture(0);
    if (!capture->isOpened()) {
        cerr << "Could not open default camera." << endl;
        exit(-1);
    }
}

void loadModel(Modelbase& modelbase, const string& path) {
    if (verbose) {
        cout << boost::format("Loading object %-20s ... ") % path;
    }
    modelbase.add(path);
    if (verbose) {
        cout << "[DONE]" << endl;
    }
}

void processImage(Detector& detector) {
    if (!image.empty()) {
        cout << "Detecting objects on image          ... ";
        Scene scene = detector.describe(image);
        vector<Detection> detections = detector.detect(scene);
        cout << "[DONE]" << endl;

        BOOST_FOREACH(Detection d, detections) {
        cout << "Drawing                             ..."; 
            drawDetection(image, d);
        cout << "[Done]" << endl;
        }
    }
}

void nextImage() 
{
    if (verbose) {
        cout << "Reading from webcam                 ... ";
    }
    *capture >> image;
    if (verbose) {
        cout << "[DONE]" << endl;
    }
}


/**
* This tutorial demonstrates simple image conversion between ROS image message and OpenCV formats and image processing
*/
int main(int argc, char **argv)
{

    // TODO: adapt to OpenCV 2.4.
    // TODO: remove duplication
    // TODO: support SIFT
    Ptr<FeatureDetector> fd = new OrbFeatureDetector(1000, 1.2, 8);
    Ptr<FeatureDetector> trainFd = new OrbFeatureDetector(250, 1.2, 8);
    Ptr<DescriptorExtractor> de = new OrbDescriptorExtractor(1000, 1.2, 8);
    Ptr<flann::IndexParams> indexParams = new flann::LshIndexParams(15, 12, 2);
    Ptr<DescriptorMatcher> dm = new FlannBasedMatcher(indexParams);

    Feature trainFeature(trainFd, de, dm);

    Modelbase modelbase(trainFeature);

    loadModel(modelbase, PROJECT_BINARY_DIR + "/data/adapter");
    loadModel(modelbase, PROJECT_BINARY_DIR + "/data/blokus");
    loadModel(modelbase, PROJECT_BINARY_DIR + "/data/book1");
    loadModel(modelbase, PROJECT_BINARY_DIR + "/data/book2");
    loadModel(modelbase, PROJECT_BINARY_DIR + "/data/book3");
    loadModel(modelbase, PROJECT_BINARY_DIR + "/data/coffee");

    Feature feature(fd, de, dm);

    Ptr<DetectionFilter> filter = new AndFilter(
            Ptr<DetectionFilter> (new EigenvalueFilter(-1, 4.0)),
            Ptr<DetectionFilter> (new InliersRatioFilter(0.30)));

    Detector detector(modelbase, feature, filter);


    openCamera();

    while (true) {
        nextImage();
        processImage(detector);
    //Display the image using OpenCV
     cv::imshow("image processed", image);
    //Add some delay in miliseconds. The function only works if there is at least one HighGUI window created and the window is active. If there are several HighGUI windows, any of them can be active.
     cv::waitKey(3);
    }


    //OpenCV HighGUI call to destroy a display window on shut-down.
    cv::destroyWindow("image processed");


}

The one using cv_bridge

//Includes all the headers necessary to use the most common public pieces of the ROS system.
#include <ros/ros.h>
//Use image_transport for publishing and subscribing to images in ROS
#include <image_transport/image_transport.h>
//Use cv_bridge to convert between ROS and OpenCV Image formats
#include <cv_bridge/cv_bridge.h>
//Include some useful constants for image encoding. Refer to: http://www ...
(more)
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2016-06-17 09:27:24 -0500

pablocesar gravatar image

You "use" Rosbridge, but you show the images different than what I normally do:

std_msgs::Header h;
h.stamp = ros::Time::now();
.....
cv_bridge::CvImage cv_ptr(h, sensor_msgs::image_encodings::BGR8, img);
cv::imshow(OPENCV_WINDOW_IMAGE,cv_ptr.image);//cv::imshow(WINDOW,cv_ptr.image); for you

Sorry, but your solution takes too much time to be solved in an easy way (too many lines of code), I hope someone else will do ;-)

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2013-05-06 01:20:55 -0500

Seen: 1,012 times

Last updated: Jun 17 '16