快速图像处理
我有一个 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)
这是我能做到的最快的方法吗?我有一个大图像,扫描每个像素,在哈希表中搜索其值,设置像素有点慢。有更快的方法吗?
I have an array 10X10 with values between 1 to 10. Now say I want to give each value a unique color (Say 1 gets blue 2 gets red etc). I'm using qt qimage to represent the image.
Here'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 I can do this? I have a big image, scanning each pixel, searching its value in a hash table, setting pixel is a bit slow. Is there a faster way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确实有一种更快的方法:创建一个无符号字符数组并直接修改像素值。然后从该数组创建一个 QImage。调用 setPixel() 的开销非常大。
这是 QImage::format_RGB32 的,你的 PaintEvent() 看起来像这样:
There is indeed a faster way: Create an array of unsigned chars and modify the pixels values directly. Then create a QImage from this array. Calling setPixel() is very expensive.
That's for QImage::format_RGB32 and your paintEvent() would look something like this:
如果只有 10 种不同的颜色,则不需要使用哈希表。简单的数组就足够了。您也不需要
[10][10]
数组。只需在从磁盘读取图像时调用 image.setPixel 即可。如果您有许多不同的颜色,请将它们存储为 RGB 值而不是索引。您可以一次读取所有数据并使用
QImage ( uchar * data, int width, int height, Format format )
创建图像。它比单独设置每个像素要快得多。If you have only 10 different colors you don't need to use hash table. Simple array would be sufficient. You don't need
a[10][10]
array either. Just callimage.setPixel
as you are reading it from disk.If you have many different colors store them as RGB values instead of indexes. You can read all data at once and create your image with
QImage ( uchar * data, int width, int height, Format format )
. It will be much faster than setting each pixel individually.