简体   繁体   中英

How can I display results of a timer with a putText in C++ OpenCV?

How can I display results od a timer with a putText in my OpenCV Android app? The is detecting features on the view from a camera and the main algorithm and the timer is written in C++. The full code of my C++ JNI file:

#include <jni.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <vector>

using namespace std; 
using namespace cv;

extern "C" {
JNIEXPORT void JNICALL  
Java_org_opencv_samples_tutorial3_Sample3View_FindFeatures(JNIEnv* env, jobject, jint 
width, jint height, jbyteArray yuv, jintArray bgra)   
{
jbyte* _yuv  = env->GetByteArrayElements(yuv, 0);
jint*  _bgra = env->GetIntArrayElements(bgra, 0);

Mat myuv(height + height/2, width, CV_8UC1, (unsigned char *)_yuv);
Mat mbgra(height, width, CV_8UC4, (unsigned char *)_bgra);
Mat mgray(height, width, CV_8UC1, (unsigned char *)_yuv);

//Please make attention about BGRA byte order
//ARGB stored in java as int array becomes BGRA at native level
cvtColor(myuv, mbgra, CV_YUV420sp2BGR, 4);

vector<KeyPoint> v;

OrbFeatureDetector  detector(1);
double t = (double)getTickCount();
detector.detect(mgray, v);
t = ((double)getTickCount() - t)/getTickFrequency();
putText(mbgra, t+" detection time", Point2f(100,100), FONT_HERSHEY_PLAIN, 2, Scalar(0,0,255,255), 2);
for( size_t i = 0; i < v.size(); i++ )
    circle(mbgra, Point(v[i].pt.x, v[i].pt.y), 10, Scalar(0,0,255,255));
env->ReleaseIntArrayElements(bgra, _bgra, 0);
env->ReleaseByteArrayElements(yuv, _yuv, 0); }}

The problem is in the line with putText: I get an error "invalid operands of types 'double' and 'char const [15]' to binary 'operator+'". Is my timer OK? How else can I display the results of it? I will be grateful for your help.

't' is of class double and the constant " detection time" is treated as a string. String + double is something that the compiler doesn't understand, which is why it pukes on you.

Instead, try this approach:

std::stringstream s;
s << t;
s << " detection time";

putText(mbgra, s.str(), Point2f(100,100), FONT_HERSHEY_PLAIN, 2, Scalar(0,0,255,255), 2);

In the above code, the stringstream class has all the overloads built in to the "<<" operator such that it knows what to do with doubles and integers and strings and how to mash them together. With a little more research in to the various attributes, you can get it to format the precision of decimel points and such.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM