我编写了一个函数,通过仅占用高位字节,将具有两个字节的图像转换为8位图像。
我仍然需要原始图像,因此无法修改原始数组。
调用此函数来处理视频流的每个帧。
您知道优化速度功能的可能性吗?
谢谢!
QImage* createQImage(uchar *ptr, uint width, uint height, uint channels)
{
static uint size = width * height * channels;
static uchar *buffer = new uchar[size];
//Take every second byte (upper byte)
for(uint i=0; i<size; i++) {
buffer[i] = (*(ptr+1));
ptr = ptr+2;
}
static QImage img = QImage(buffer, width, height, QImage::Format_BGR888);
static QImage *ptr_img = &img;
return ptr_img;
}
最佳答案
如果只是将输入缓冲区移到输出缓冲区,一次执行一个字节并不是最快的。简单地将uchar *ptr
转换为uint64_t*
并使用位移/掩码拉出字节可能是优化此方法的方法。最后,您将需要以与已经处理过的相同方式在“末尾”剩下的一些字节进行“手动”处理。假设您的图像是16位,然后16/64 == 4,则速度提高了4倍。
假设您的硬件一次可以加载64位,则可以使用uint32_t
将此代码一次加载32位,但这是大多数人都拥有64位硬件的合理假设。该代码假定您使用的是低端字节序硬件。
代码(未经测试)。
QImage* createQImage(uchar* ptr, uint width, uint height, uint channels) {
uint64_t* nptr = (uint64_t*)ptr;
static uint size = width * height * channels;
static uchar *buffer = new uchar[size];
static uint bytesize = size*2;
uint idx = 0;
uint i = 0;
for(; i + sizeof(uint64_t) <= bytesize; i+=sizeof(uint64_t)) {
//get 64 bits at a time
uint64_t val = nptr[i/sizeof(uint64_t)];
//get 4 bytes at a time
//note inverted due to little endian assumed (maybe wrong)
buffer[idx] = (uchar)(val >> (8 * 6));
++idx;
buffer[idx] = (uchar)(val >> (8 * 4));
++idx;
buffer[idx] = (uchar)(val >> (8 * 2));
++idx;
buffer[idx] = (uchar)(val >> (8 * 0));
++idx;
}
//We have some bytes left at the end, process these a byte at a time
//Take every second byte (upper byte)
for(; i < bytesize; i+=2) {
buffer[idx] = (*(ptr + i + 1));
++idx;
}
static QImage img = QImage(buffer, width, height, QImage::Format_BGR888);
static QImage *ptr_img = &img;
return ptr_img;
}
关于c++ - C++:具有缓冲区和指针循环的功能-是否有可能进行优化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61347383/