加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 快速图像处理

发布时间:2020-12-16 10:29:18 所属栏目:百科 来源:网络整理
导读:我有一个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
我有一个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)

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

解决方法

确实有一种更快的方法:创建一个无符号字符数组并直接修改像素值.然后从这个数组创建一个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;
 }
}

那是QImage :: format_RGB32,你的paintEvent()看起来像这样:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读