CImage访问像素及其像素操作总结
阅读原文时间:2023年07月08日阅读:2

MSDN的代码

这种方式效率很低, 因为每次调用getpixel,都包含着程序的进栈和出栈。所以,面对大量需要处理的数据,采用直接访问内存地址的方法。

用两种方法对同一张图片(3264*2448像素)进行处理,前者需要1分钟,后者只需1秒左右。

所以,后者比前者至少快60倍

直接访问内存地址的另一种方式:

    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<height-1;j++)  
{  
    for(i=0;i<width-1;i++)  
    {  
        pixel\[0\]=pYuantuData\[j\*widthBytes+i\];  
        pixel\[1\]=pYuantuData\[j\*widthBytes+i+1\];  
        pixel\[2\]=pYuantuData\[(j+1)\*widthBytes+i\];  
        pixel\[3\]=pYuantuData\[(j+1)\*widthBytes+i+1\];  
        temp=(int)sqrt((double)((pixel\[0\]-pixel\[3\])\*(pixel\[0\]-pixel\[3\])  
            +(pixel\[1\]-pixel\[2\])\*(pixel\[1\]-pixel\[2\]))); //罗伯特算子  
        pBianyuantuData\[j\*widthBytes+i\]=temp;  
    }  
}

彩色图像转化为灰度图的处理方式

//真彩色图像变为灰度图,直接修改像素点的值

//非真彩色图像变为灰度图,修改调色板信息

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器