Msdn code
- Colorref pixel;
- Int Maxy = imgoriginal. getheight (), Maxx = imgoriginal. getwidth ();
- Byte R, G, B, AVG;
- For (INT y = 0; y <Maxy; y ++ ){
- For (INT x = 0; x <Maxx; X ++ ){
- Pixel = imgoriginal. getpixel (x, y );
- R = getrvalue (pixel );
- G = getgvalue (pixel );
- B = getbvalue (pixel );
- AVG = (R + G + B)/3;
- Imgoriginal. setpixelrgb (X, Y, AVG, AVG, avg );
- }}
This method is very inefficient, because every call to getpixel contains the program's stack entry and exit. Therefore, in the face of a large amount of data to be processed, the method of direct access to the memory address is used.
- Byte * prealdata;
- Prealdata = (byte *) imgoriginal. getbits ();
- Int Pit = imgoriginal. getpitch ();
- Int bitcount = imgoriginal. getbpp ()/8;
- For (INT y = 0; y <Maxy; y ++ ){
- For (INT x = 0; x <Maxx; X ++ ){
- Int grayval = (INT) (* (prealdata + pit * Y + x * bitcount) * 0.3
- + (INT) (* (prealdata + pit * Y + x * bitcount + 1) * 0.59
- + (INT) (* (prealdata + pit * Y + x * bitcount + 2) * 0.11;
- * (Prealdata + pit * Y + x * bitcount) = grayval;
- * (Prealdata + pit * Y + x * bitcount + 1) = grayval;
- * (Prealdata + pit * Y + x * bitcount + 2) = grayval;
- // If it is an 8-bit grayscale image, read a byte bit directly as the grayscale value.
- // For 24-bit RGB Images, read pixaddr, pixaddr + 1 in sequence, and pixaddr + 2 is the B, G, and r component values.
- }}
Two methods are used to process the same image (3264*2448 pixels). The former takes 1 minute, and the latter takes about 1 second.
Therefore, the latter is at least 60 times faster than the former.
Another method for direct access to the memory address:
Int I, j, temp; int pixel [4]; int width = yuantu. getwidth (); int Height = yuantu. getheight (); int widthbytes = yuantu. getpitch (); bianyuantu. create (width, height, yuantu. getbpp (); If (yuantu. isindexed () {yuantu. getcolortable (0,256, colortable); bianyuantu. setcolortable (0,256, colortable);} byte * pyuantudata = (byte *) yuantu. getbits (); byte * pbianyuantudata = (byte *) bianyuantu. getbits (); For (j = 0; j
How to convert a color image to a grayscale image
// Convert a true color image to a grayscale image and directly modify the pixel value
- Void pixelschangedtogray (cimage * pimage)
- {
- Int nbyte, J, I, nwidth, nheight, nbytesperpixel;
- Byte * ppixelline, cnewpixelvalue;
- Nwidth = pimage-> getwidth (); nheight = pimage-> getheight ();
- Nbytesperpixel = pimage-> getbpp ()/8;
- For (I = 0; I <nheight; I ++ ){
- Ppixelline = (byte *) pimage-> getpixeladdress (0, I );
- Nbyte = 0;
- For (j = 0; j <nwidth; j ++) {cnewpixelvalue = (byte) (0.11 * ppixelline [nbyte]
- + 0.59 * ppixelline [nbyte + 1]
- + 0.30 * ppixelline [nbyte + 2]);
- Ppixelline [nbyte] = ppixelline [nbyte + 1] = ppixelline [nbyte + 2]
- = Cnewpixelvalue;
- Nbyte + = nbytesperpixel;
- }
- }
- }
// Changes a non-real color image to a grayscale image and modifies the color palette information.
- Void palettechangedtogray (cimage * pimage)
- {
- Rgbquad colortabs [256];
- Int I, ncolortableentries, nnewgraycolor;
- Ncolortableentries = pimage-> getmaxcolortableentries ();
- Pimage-> getcolortable (0, ncolortableentries, colortabs );
- For (I = 0; I <ncolortableentries; I ++ ){
- Nnewgraycolor = (INT) (0.11 * colortabs [I]. rgbblue
- + 0.59 * colortabs [I]. rgbgreen
- + 0.30 * colortabs [I]. rgbred );
- Colortabs [I]. rgbblue = (byte) nnewgraycolor;
- Colortabs [I]. rgbgreen = (byte) nnewgraycolor;
- Colortabs [I]. rgbred = (byte) nnewgraycolor;
- }
- Pimage-> setcolortable (0, ncolortableentries, colortabs );
- }
Cimage access pixel and pixel Operation Summary