如何在我的OpenCV Android应用程序中显示带有putText的计时器的结果?正在检测来自相机的视图上的特征,主要算法和计时器是用C++编写的。我的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); }}
问题出在 putText 的行中:我收到一个错误"类型'double'和'char const [15]'的无效操作数到二进制'operator+'"。我的计时器可以吗?我还能如何显示它的结果?我将感谢您的帮助。
't' 是双精度类,常数 "检测时间" 被视为字符串。 字符串 + double 是编译器不理解的东西,这就是为什么它会吐到你身上。
相反,请尝试此方法:
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);
在上面的代码中,stringstream 类将所有重载内置到 "<<" 运算符中,以便它知道如何处理双精度、整数和字符串以及如何将它们混合在一起。 通过对各种属性进行更多研究,您可以让它格式化小数点等的精度。