简体   繁体   English

高效编辑图像中的像素

[英]Efficiently editing Pixels in an Image

I have an 8bit Image (stored in an Array) containing black(0) and white(255) pixels. 我有一个包含黑色(0)和白色(255)像素的8位图像(存储在数组中)。 Say I want to change all Black pixels in the image to grey(say 120) pixels. 假设我想将图像中的所有“黑色”像素更改为灰色(例如120)像素。 What is the fastest way I can change Black to Grey. 将Black更改为Grey的最快方法是什么?

I thought of two approaches- 我想到了两种方法-

  1. Start checking every single pixel in the image. 开始检查图像中的每个像素。 As soon as a black pixel is found change it to grey. 一旦发现黑色像素,请将其更改为灰色。 Continue till end of image. 继续直到图像结束。 (Slower but easier) (慢但容易)

  2. Start checking pixels. 开始检查像素。 When a black pixel is found maintain a counter to track it. 当找到黑色像素时,请维护一个计数器以对其进行跟踪。 Continue incrementing the counter till the next white pixel. 继续增加计数器,直到下一个白色像素。 Then goto the counter and use a fast function like memset to change a group of black pixels to grey. 然后转到计数器并使用像memset这样的快速功能将一组黑色像素更改为灰色。 (Not sure but I think this may be faster) (不确定,但我认为这可能会更快)

I have a huge 1GB image therefore approach 1 is pretty slow. 我有一个巨大的1GB映像,因此方法1相当慢。 Is there a better(faster) way to change/edit pixels? 有没有更好(更快)的方式来更改/编辑像素?

Probably quicker to do it a word at a time (using word aligned accesses). 一次完成一个单词大概更快(使用单词对齐访问)。

You can just bitwise OR with 0x78787878 (assuming 32 bits). 您可以使用0x78787878(假设32位)按位或。 This will not affect white pixels but will set black pixels to the required value. 这不会影响白色像素,但会将黑色像素设置为所需值。

I think the problem with the first approach is that you read and write the same 32/64/x bits (depending on memory architecture/bus width) muliple times. 我认为第一种方法的问题是,您读取和写入相同的32/64 / x位(取决于内存体系结构/总线宽度)的多次时间。 It should be faster if you read and write the bits corresponding to the bus width once. 如果一次读写与总线宽度相对应的位,应该会更快。

In the following snippet, getPixelsSizeOfLong returns the bites according to the width of the bus (say 4 bytes) and there reduces transfering bits between cache and cpu. 在以下代码段中, getPixelsSizeOfLong根据总线的宽度(例如4个字节)返回位,并减少了缓存和cpu之间的传输位。

// Forward declaration:
unsigned long getPixelsSizeOfLong(byteInImage unsigned int);
void          setPixelsSizeOfLong(byteInImage unsigned int, newBitValue unsigned long);

unsinged long l;

for (a=0; a+=sizeof(l); a<nof_pixels) {
  l  = getPixelsSizeOfLong(a);
  l |= 120;
  setPixelsSizeOfLong(a, l);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM