Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 快速图像处理_C++_C_Qt_Image Processing_Qimage - Fatal编程技术网

C++ 快速图像处理

C++ 快速图像处理,c++,c,qt,image-processing,qimage,C++,C,Qt,Image Processing,Qimage,我有一个数组10X10,值在1到10之间。现在假设我想给每个值一个唯一的颜色(假设1得到蓝色2得到红色等等)。我用qt-qimage来表示图像。 这就是我要做的 read array from disk. store in a[10][10] generate a hash table in which each value in the array has a corresponding qRGB for entire array get value (say a[0][0])

我有一个数组10X10,值在1到10之间。现在假设我想给每个值一个唯一的颜色(假设1得到蓝色2得到红色等等)。我用qt-qimage来表示图像。 这就是我要做的

read array from disk. store in a[10][10]
generate a hash table in which each value in the array has a corresponding qRGB
for entire array
    get value (say a[0][0])
    search hashtable, get equivalent qRGB
    image.setPixel(coord,qRGB)

这是我能做到的最快的方法吗?我有一个大图像,扫描每个像素,在哈希表中搜索它的值,设置像素有点慢。有没有更快的方法?

如果只有10种不同的颜色,则不需要使用哈希表。简单的数组就足够了。您也不需要
一个[10][10]
数组。从磁盘读取图像时,只需调用
image.setPixel


如果有许多不同的颜色,则将它们存储为RGB值而不是索引。您可以一次读取所有数据,并使用
QImage(uchar*数据、int-width、int-height、Format-Format)
创建图像。这比单独设置每个像素要快得多。

确实有一种更快的方法:创建一个无符号字符数组,直接修改像素值。然后从这个数组创建一个QImage。调用setPixel()非常昂贵

unsigned char* buffer_;
buffer_ = new unsigned char[4 * w * h];
//...


for(int i = 0; i < h; i++){
 for(int j = 0; j < w; j++){

  unsigned char r, g, b;
  //...

  buffer_[4 * (i * w + j)    ] = r;
  buffer_[4 * (i * w + j) + 1] = g;
  buffer_[4 * (i * w + j) + 2] = b;
 }
}

不幸的是,我不能用rgb值替换现有索引。我无法预测使用的颜色数量。它的颜色范围可以从1种颜色到2^32种颜色。一个更好的解决方案?@sleeping.ninja如果不知道你到底在做什么,很难说出来。如果您只需要不同的颜色,请将索引直接映射到RGB。如果您需要它们在视觉上不同,请使用一些散列函数。如果您需要以某种特定的方式映射它们,并且这比从哈希表中获取它们要慢,我不知道您能做些什么。谢谢。第二种方法是使用QImage扫描线函数,该函数返回指向QImage像素数据的指针。拥有此指针后,可以直接编辑QRgb格式->0xAARGGBB的像素值
void paintEvent(QPaintEvent* event){
//...
QImage image(buffer_, w, h, QImage::Format_RGB32);
painter.drawImage(QPoint(0, 0), image);
}