sle*_*nja 3 c c++ qt image-processing qimage
我有一个10X10的数组,其值介于1到10之间.现在说我想给每个值一个独特的颜色(Say 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)
Run Code Online (Sandbox Code Playgroud)
这是我能做到这一点的最快方式吗?我有一个大图像,扫描每个像素,在哈希表中搜索其值,设置像素有点慢.有更快的方法吗?
确实有一种更快的方法:创建一个无符号字符数组并直接修改像素值.然后从这个数组创建一个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;
}
}
Run Code Online (Sandbox Code Playgroud)
那是QImage :: format_RGB32,你的paintEvent()看起来像这样:
void paintEvent(QPaintEvent* event){
//...
QImage image(buffer_, w, h, QImage::Format_RGB32);
painter.drawImage(QPoint(0, 0), image);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3537 次 |
最近记录: |