简体   繁体   English

如何在C ++ OpenCV中显示带有putText的计时器的结果?

[英]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? 如何在OpenCV Android应用中使用带putText的计时器显示结果? The is detecting features on the view from a camera and the main algorithm and the timer is written in C++. 可以通过相机和主要算法检测视图上的特征,并且计时器使用C ++编写。 The full code of my C++ JNI file: 我的C ++ JNI文件的完整代码:

#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+'". 问题与putText一致:我收到一个错误“类型为'double'和'char const [15]'的无效操作数对二进制'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. “ t”属于double类,常量“检测时间”被视为字符串。 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. 在上面的代码中,stringstream类具有“ <<”运算符内置的所有重载,因此它知道如何处理双精度数和整数以及字符串以及如何将它们混搭在一起。 With a little more research in to the various attributes, you can get it to format the precision of decimel points and such. 通过对各种属性进行更多的研究,您可以获取它来格式化分米点等的精度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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