gpt4 book ai didi

c++ - OpenCV 前景检测速度慢

转载 作者:太空狗 更新时间:2023-10-29 23:20:08 24 4
gpt4 key购买 nike

我正在尝试实现概述的密码本前景检测算法 here学习 OpenCV 一书中。

该算法仅针对图片的每个像素描述了一种基于码本的方法。所以我采用了想到的最简单的方法 - 拥有一组代码簿,每个像素一个,很像 IplImage 的矩阵结构。数组的长度等于图像中的像素数。

我写了以下两个循环来学习背景和分割前景。它利用我对src图像内部矩阵结构的有限理解,使用指针算法遍历像素。

  void foreground(IplImage* src, IplImage* dst, codeBook* c, int* minMod, int* maxMod){

int height = src->height;
int width = src->width;

uchar* srcCurrent = (uchar*) src->imageData;
uchar* srcRowHead = srcCurrent;
int srcChannels = src->nChannels;
int srcRowWidth = src->widthStep;

uchar* dstCurrent = (uchar*) dst->imageData;
uchar* dstRowHead = dstCurrent;
// dst has 1 channel
int dstRowWidth = dst->widthStep;

for(int row = 0; row < height; row++){
for(int column = 0; column < width; column++){
(*dstCurrent) = find_foreground(srcCurrent, (*c), srcChannels, minMod, maxMod);
dstCurrent++;
c++;
srcCurrent += srcChannels;
}
srcCurrent = srcRowHead + srcRowWidth;
srcRowHead = srcCurrent;
dstCurrent = dstRowHead + dstRowWidth;
dstRowHead = dstCurrent;
}
}

void background(IplImage* src, codeBook* c, unsigned* learnBounds){

int height = src->height;
int width = src->width;

uchar* srcCurrent = (uchar*) src->imageData;
uchar* srcRowHead = srcCurrent;
int srcChannels = src->nChannels;
int srcRowWidth = src->widthStep;

for(int row = 0; row < height; row++){
for(int column = 0; column < width; column++){
update_codebook(srcCurrent, c[row*column], learnBounds, srcChannels);
srcCurrent += srcChannels;
}
srcCurrent = srcRowHead + srcRowWidth;
srcRowHead = srcCurrent;
}
}

该程序可以运行,但非常缓慢。有什么明显的东西在减慢它的速度吗?或者它是简单实现中的固有问题?我可以做些什么来加快速度吗?每个码本都没有按特定顺序排序,因此处理每个像素确实需要线性时间。因此,将背景样本加倍,对于每个像素,程序运行速度减慢 2,然后按像素数放大。但就实现而言,我没有看到任何清晰、合乎逻辑的方法来对代码元素条目进行排序。

我知道在 opencv 样本中有一个相同算法的示例实现。但是,该结构似乎要复杂得多。我希望更多地了解这种方法背后的原因,我知道我可以为现实生活中的应用程序修改示例。

谢谢

最佳答案

无论您如何实现,对图像中的每个像素进行操作都会很慢。

关于c++ - OpenCV 前景检测速度慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3654865/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com