OpenCV以一种有效的方式从uchar数组创建序列
OpenCV create a sequence from an array of uchar in an efficient way
我正在移植一些使用OpenCV的代码,只是为了适合椭圆。我以后可能会重写它,但现在我需要使用OpenCV例程。这段代码只有几行,但我不得不改变几乎所有的东西,因为cvFitEllipse2()的配置文件已经改变了。
我的问题是以下一个:转换点的数组(对uchar)成的东西,我可以饲料cvFitEllipse2()与
我没有找到很多代码样本,我发现OpenCV的文档记录很差(但因为我是OpenCV的新手,也许这种感觉是错误的)。无论如何,这是我写的:
/* centers is the input uchar array */
CvBox2D box; //output
CvMemStorage* storage = cvCreateMemStorage(0);
CvSeq* seq = cvCreateSeq(CV_32FC2, sizeof(CvSeq), sizeof(CvPoint2D32f), storage);
for(int h=0; h<mm; ++h)
{
CvPoint2D32f p;
p.x = (float)centers[h].x;
p.y = (float)centers[h].y;
cvSeqPush(seq, &p);
}
// Fits ellipse to current contour.
box = cvFitEllipse2(seq);
问题:这段代码比原来的代码慢。
- 是否有任何方法可以预先分配seq所需的内存?(使用CvMemStorage对象)
- 有没有更好(更简单)的?容器比CvSeq吗?
- 我可以用更快的东西代替cvSeqPush吗?
谢谢你的帮助!
我假设centers
是点的向量。
如果你乐于使用c++接口而不是旧的,那么你的痛苦就结束了。
根据中心的声明方式,可以:
vector<cv::Point> centers;
RotatedRect rect = fitEllipse(centers);
或核心方法:
struct {
uchar x, y
} myPoint;
vector<myPoint> centers;
...
int sz = centers.size();
// if you have a different type for x and y, modify here to CV_MY_TYPE
// Here I have taken a pointer to the vector data storage,
// and fed it into the Mat.
// Check Mat documentation on constructors
Mat pts(1,sz,CV_8UC2, (void*)¢ers[0].x);
RotatedRect rect = fitEllipse(pts);
...
// take care not to change/ delete your vector
// before finishing with the pts Mat.
编辑
我已经将类型修改为UCHAR
相关文章:
- 如何在c++中为模板函数实例创建快捷方式
- 在c代码之间共享数据的最佳方式
- 在C++中将函数压缩为两种方式
- 以螺旋方式打印矩阵的程序.(工作不好)
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- 创建引用向量的优雅方式
- Constexpr替代了新的放置方式,可以让内存中的对象保持未初始化状态
- 使用QQuickFramebufferObject时同步数据的最佳方式是什么
- 不同/较旧的处理器运行c++代码的方式是否不同
- 从嵌套在std::映射中的std::列表中删除元素的最佳方式
- 如果条件为TRUE(最佳方式?),则在do while循环中后置增量
- 重载方法的方式会在使用临时调用时生成编译器错误
- 在reactor中存储eventHandlers的最佳方式是什么
- 如何以优化的方式同时迭代两个间距不相等的数组
- 以线程安全的方式调用"QQuickPaintedItem::updateImage(const QImage&image)"(no QThread)
- 为什么C++有不同的变量初始化方式?
- 在AVX通道中混洗的最佳方式
- 如何重新定义MPI_FLOAT,MPI_DOUBLE以 typedef 的方式
- 如何以静态代码分析友好的方式使用 #define 防护?
- OpenCV以一种有效的方式从uchar数组创建序列