Fast image processing

I have a 10X10 array with values ​​from 1 to 10. Now say that I want to give each value a unique color (Say 1 gets blue 2 gets red, etc.). I am using qt qimage to represent an image. That's what I'm doing

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)

Is this the fastest way to do this? I have a large image, scanning each pixel, finding its value in a hash table, setting the pixel is a bit slow. Is there a faster way?

+3
source share
2 answers

There is a really faster way: create an array of unsigned characters and directly change the pixel values. Then create a QImage from this array. Calling setPixel () is very expensive.

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;
 }
}

QImage:: format_RGB32 paintEvent() :

void paintEvent(QPaintEvent* event){
//...
QImage image(buffer_, w, h, QImage::Format_RGB32);
painter.drawImage(QPoint(0, 0), image);
}
+9

10 , -. . a[10][10]. image.setPixel, .

, RGB, . QImage ( uchar * data, int width, int height, Format format ). , .

+4

All Articles