c++ MFC图像处理CImage类常用操作代码

 

原文作者:aircraft

原文地址:https://www.cnblogs.com/DOMLX/p/9598974.html

        MFC图像处理CImage类常用操作

 

 

CImage类头文件为#include<atlimage.h>

CImage类读取图片CImage.Load("src.bmp");

CImage类保存图片CImage.Save("dst.jpg");

 

一.CImage类实例拷贝数据到另外一个CImage类实例

bool ImageCopy(const CImage &srcImage, CImage &destImage)
{
    int i, j;//循环变量
    if (srcImage.IsNull())
        return FALSE;
    //源图像参数
    BYTE* srcPtr = (BYTE*)srcImage.GetBits();
    int srcBitsCount = srcImage.GetBPP();
    int srcWidth = srcImage.GetWidth();
    int srcHeight = srcImage.GetHeight();
    int srcPitch = srcImage.GetPitch();
    //销毁原有图像
    if (!destImage.IsNull())
    {
        destImage.Destroy();
    }
    //创建CImage类新图像并分配内存
    if (srcBitsCount == 32)   //支持alpha通道
    {
        destImage.Create(srcWidth, srcHeight, srcBitsCount, 1);
    }
    else
    {
        destImage.Create(srcWidth, srcHeight, srcBitsCount, 0);
    }
    //加载调色板
    if (srcBitsCount <= 8 && srcImage.IsIndexed())//需要调色板
    {
        RGBQUAD pal[256];
        int nColors = srcImage.GetMaxColorTableEntries();
        if (nColors>0)
        {
            srcImage.GetColorTable(0, nColors, pal);
            destImage.SetColorTable(0, nColors, pal);//复制调色板程序
        }
    }
    //目标图像参数
    BYTE *destPtr = (BYTE*)destImage.GetBits();
    int destPitch = destImage.GetPitch();
    //复制图像数据
    for (i = 0; i<srcHeight; i++)
    {
        memcpy(destPtr + i*destPitch, srcPtr + i*srcPitch, abs(srcPitch));
    }

    return TRUE;
}

 

 

二.CImage类实例处理图像间的腐蚀运算

//腐蚀运算
//width:图像宽;height:图像高;矩形掩膜的边长(2*r+1)void erosion(CImage image, int width, int height, int r)
{
    int i, j, m, n;
    int flag;
    //unsigned char * pBuff = tempBuff;
    CImage Buff;
    ImageCopy(image, Buff);
    //dataCopy(image, pBuff, width, height);
    byte *pImg = (byte *)image.GetBits();
    byte *pBuff = (byte *)Buff.GetBits();
    int step = image.GetPitch();
    //int height = image.GetHeight();
    //int width = image.GetWidth();
    for (i = r; i<height - r; i++)
    {
        for (j = r; j<width - r; j++)
        {
            flag = 1;
            for (m = i - r; m <= i + r; m++)
            {
                for (n = j - r; n <= j + r; n++)
                {
                    //if (!pBuff[i*width + j] || !pBuff[m*width + n])
                    if (!*(pBuff + i*step + j) || !*(pBuff + m*step + n))
                    {
                        flag = 0;
                        break;
                    }
                }
            }
            if (flag == 0)
            {
                *(pImg + i*step + j) = 0;
            }
            else
            {
                *(pImg + i*step + j) = 255;
            }
        }
    }
}

 

三.CImage类实例处理图像间的膨胀运算

 

//膨胀运算
//width:图像宽;height:图像高;矩形掩膜的边长(2*r+1)
void diate(CImage image, int width, int height, int r)
{
    int i, j, m, n;
    int flag;
    //unsigned char * pBuff = tempBuff;

    CImage Buff;
    ImageCopy(image, Buff);
    //dataCopy(image, pBuff, width, height);
    byte *pImg = (byte *)image.GetBits();
    byte *pBuff = (byte *)Buff.GetBits();
    int step = image.GetPitch();
    //int height = image.GetHeight();
    //int width = image.GetWidth();
    //dataCopy(image, pBuff, width, height);
    for (i = r; i<height - r; i++)
    {
        for (j = r; j<width - r; j++)
        {
            flag = 1;
            for (m = i - r; m <= i + r; m++)
            {
                for (n = j - r; n <= j + r; n++)
                {
                    if (255 == *(pBuff + i*step + j) || 255 == *(pBuff + m*step + n))
                    {
                        flag = 0;
                        break;
                    }
                }
            }
            if (flag == 0)
            {
                *(pImg + i*step + j) = 255;
            }
            else
            {
                *(pImg + i*step + j) = 0;
            }
        }
    }
}

 

 

四.CImage类实例处理图片遍历赋值操作

    byte *pImg = (byte *)imgSrc.GetBits();
    int step = imgSrc.GetPitch();
    int height = imgSrc.GetHeight();
    int width = imgSrc.GetWidth();
    int sum = 0;
    unsigned char val = 0;
    //初始化
    for (int i = 0; i<maxY; i++)
        for (int j = 0; j<maxX; j++)
            *(pDstImg + i*step + j) = 0;        

 

五.用CImage类实例遍历生成手指静脉边缘图

#define  mlen  9 //模板长度
//加长扩展的水平边缘检测模板
int upperEdgeOperator[mlen * 3] =
{
    -1, -1, -1, -1, -1, -1, -1, -1, -1,
    0, 0, 0, 0, 0, 0, 0, 0, 0,
    1, 1, 1, 1, 1, 1, 1, 1, 1
};
int lowerEdgeOperator[mlen * 3] =
{
    1, 1, 1, 1, 1, 1, 1, 1, 1,
    0, 0, 0, 0, 0, 0, 0, 0, 0,
    -1, -1, -1, -1, -1, -1, -1, -1, -1
};

int findEdgesHorizontal(CImage& imgSrc, CImage& imgDst)
{
    int maxY = imgSrc.GetHeight();
    int maxX = imgSrc.GetWidth();

    if (!imgDst.IsNull())
    {
        imgDst.Destroy();
    }
    imgDst.Create(maxX, maxY, 8, 0);//图像大小与imgSrc相同,每个像素占1字节

    if (imgDst.IsNull())
        return FALSE;

    byte *pImg = (byte *)imgSrc.GetBits();
    byte *pDstImg = (byte *)imgDst.GetBits();
    int step = imgSrc.GetPitch();
    int height = imgSrc.GetHeight();
    int width = imgSrc.GetWidth();
    int sum = 0;
    unsigned char val = 0;
    //初始化
    for (int i = 0; i<maxY; i++)
        for (int j = 0; j<maxX; j++)
            *(pDstImg + i*step + j) = 0;

    //找上边缘
    for (int i = 1; i <maxY / 2 - 1; i++)
    {
        for (int j = 4; j < maxX - 4; j++)
        {
            sum = 0;
            for (int m = -1; m <= 1; m++)
            {
                for (int n = -mlen / 2; n <= mlen / 2; n++)
                {
                    sum += *(pImg + (i + m)*step + (j + n))*upperEdgeOperator[(m + 1)*mlen + (n + mlen / 2)];
                }
            }
            sum = sum < 0 ? 0 : sum;
            sum = sum > 255 ? 255 : sum;
            val = unsigned char(sum);
            *(pDstImg + i*step + j) = val;
        }
    }
    //找下边缘
    for (int i = maxY / 2 + 1; i <maxY - 1; i++)
    {
        for (int j = 4; j < maxX - 4; j++)
        {
            sum = 0;
            for (int m = -1; m <= 1; m++)
            {
                for (int n = -mlen / 2; n <= mlen / 2; n++)
                {
                    sum += *(pImg + (i + m)*step + (j + n))*upperEdgeOperator[(m + 1)*mlen + (n + mlen / 2)];;
                }
            }
            sum = sum < 0 ? 0 : sum;
            sum = sum > 255 ? 255 : sum;
            val = unsigned char(sum);
            *(pDstImg + i*step + j) = val;
        }
    }

    return TRUE;
}

 

六.CImage图像类实例将RGB图转为灰度(gary)图

BOOL ImageToGray(CImage& imgSrc, CImage& imgDst)
{
    int maxY = imgSrc.GetHeight();
    int maxX = imgSrc.GetWidth();

    if (!imgDst.IsNull())
    {
        imgDst.Destroy();
    }
    imgDst.Create(maxX, maxY, 8, 0);//图像大小与imgSrc相同,每个像素占1字节

    if (imgDst.IsNull())
        return FALSE;

    //为imgDst构造256阶灰度调色表
    RGBQUAD ColorTab[256];
    for (int i = 0; i<256; i++)
    {
        ColorTab[i].rgbBlue = ColorTab[i].rgbGreen = ColorTab[i].rgbRed = i;
    }
    imgDst.SetColorTable(0, 256, ColorTab);

    byte* pDataSrc = (byte*)imgSrc.GetBits(); //获取指向图像数据的指针
    byte* pDataDst = (byte*)imgDst.GetBits();
    int pitchSrc = imgSrc.GetPitch(); //获取每行图像占用的字节数 +:top-down;-:bottom-up DIB
    int pitchDst = imgDst.GetPitch();
    int bitCountSrc = imgSrc.GetBPP() / 8;  // 获取每个像素占用的字节数
    int bitCountDst = imgDst.GetBPP() / 8;
    if ((bitCountSrc != 3) || (bitCountDst != 1))
        return FALSE;
    int tmpR, tmpG, tmpB, avg;
    for (int i = 0; i<maxX; i++)
    {
        for (int j = 0; j<maxY; j++)
        {
            tmpR = *(pDataSrc + pitchSrc*j + i*bitCountSrc);
            tmpG = *(pDataSrc + pitchSrc*j + i*bitCountSrc + 1);
            tmpB = *(pDataSrc + pitchSrc*j + i*bitCountSrc + 2);
            avg = (int)(tmpR + tmpG + tmpB) / 3;
            *(pDataDst + pitchDst*j + i*bitCountDst) = avg;
        }
    }
    return TRUE;
}

 

七.CImage类转opencv Mat类  以及Mat类转CImage类

#include "stdafx.h"
#include <opencv2/opencv.hpp>
#include "CimgMat.h"


void CimgMat::MatToCImage(Mat& mat, CImage& cimage)
{
    if (0 == mat.total())
    {
        return;
    }


    int nChannels = mat.channels();
    if ((1 != nChannels) && (3 != nChannels))
    {
        return;
    }
    int nWidth = mat.cols;
    int nHeight = mat.rows;


    //重建cimage
    cimage.Destroy();
    cimage.Create(nWidth, nHeight, 8 * nChannels);


    //拷贝数据


    uchar* pucRow;                                    //指向数据区的行指针
    uchar* pucImage = (uchar*)cimage.GetBits();        //指向数据区的指针
    int nStep = cimage.GetPitch();                    //每行的字节数,注意这个返回值有正有负


    if (1 == nChannels)                                //对于单通道的图像需要初始化调色板
    {
        RGBQUAD* rgbquadColorTable;
        int nMaxColors = 256;
        rgbquadColorTable = new RGBQUAD[nMaxColors];
        cimage.GetColorTable(0, nMaxColors, rgbquadColorTable);
        for (int nColor = 0; nColor < nMaxColors; nColor++)
        {
            rgbquadColorTable[nColor].rgbBlue = (uchar)nColor;
            rgbquadColorTable[nColor].rgbGreen = (uchar)nColor;
            rgbquadColorTable[nColor].rgbRed = (uchar)nColor;
        }
        cimage.SetColorTable(0, nMaxColors, rgbquadColorTable);
        delete[]rgbquadColorTable;
    }


    for (int nRow = 0; nRow < nHeight; nRow++)
    {
        pucRow = (mat.ptr<uchar>(nRow));
        for (int nCol = 0; nCol < nWidth; nCol++)
        {
            if (1 == nChannels)
            {
                *(pucImage + nRow * nStep + nCol) = pucRow[nCol];
            }
            else if (3 == nChannels)
            {
                for (int nCha = 0; nCha < 3; nCha++)
                {
                    *(pucImage + nRow * nStep + nCol * 3 + nCha) = pucRow[nCol * 3 + nCha];
                }
            }
        }
    }
}

void CimgMat::CImageToMat(CImage& cimage, Mat& mat)
{
    if (true == cimage.IsNull())
    {
        return;
    }


    int nChannels = cimage.GetBPP() / 8;
    if ((1 != nChannels) && (3 != nChannels))
    {
        return;
    }
    int nWidth = cimage.GetWidth();
    int nHeight = cimage.GetHeight();


    //重建mat
    if (1 == nChannels)
    {
        mat.create(nHeight, nWidth, CV_8UC1);
    }
    else if (3 == nChannels)
    {
        mat.create(nHeight, nWidth, CV_8UC3);
    }


    //拷贝数据


    uchar* pucRow;                                    //指向数据区的行指针
    uchar* pucImage = (uchar*)cimage.GetBits();        //指向数据区的指针
    int nStep = cimage.GetPitch();                    //每行的字节数,注意这个返回值有正有负


    for (int nRow = 0; nRow < nHeight; nRow++)
    {
        pucRow = (mat.ptr<uchar>(nRow));
        for (int nCol = 0; nCol < nWidth; nCol++)
        {
            if (1 == nChannels)
            {
                pucRow[nCol] = *(pucImage + nRow * nStep + nCol);
            }
            else if (3 == nChannels)
            {
                for (int nCha = 0; nCha < 3; nCha++)
                {
                    pucRow[nCol * 3 + nCha] = *(pucImage + nRow * nStep + nCol * 3 + nCha);
                }
            }
        }
    }
}

 

八.纯图像数据赋值给CImage后的初始化,并且写入调色板

bool InitalImage(CImage &image, int width, int height)
{
    if (image.IsNull())
        image.Create(width, height, 8);
    else
    {
        if (width <= 0 || height <= 0)
            return false;
        else if (image.GetHeight() == width && image.GetWidth() == height)
            return true;
        else
        {
            image.Destroy();
            image.Create(width, height, 8);
        }
    }
    //写入调色板
    RGBQUAD ColorTable[256];
    image.GetColorTable(0, 256, ColorTable);
    for (int i = 0; i < 256; i++)
    {
        ColorTable[i].rgbBlue = (BYTE)i;
        ColorTable[i].rgbGreen = (BYTE)i;
        ColorTable[i].rgbRed = (BYTE)i;
    }
    image.SetColorTable(0, 256, ColorTable);
    return true;
}

 

九.根据MFC控件大小CImage类实例图片显示

if(m_image2.IsNull())    //判断有无图像  
        return;  
  
// 取得客户区尺寸  
CRect zcRect;  
GetDlgItem(IDC_STATIC_PIC2)->GetClientRect(&zcRect);  
  
// 将图像显示在界面之上  
m_image2.Draw(GetDlgItem(IDC_STATIC_PIC2)->GetDC()->m_hDC,  
                            zcRect.left,  
                            zcRect.top,  
                            zcRect.Width(),  
                            zcRect.Height()); 

 

十.根据CImage类实例图片调整控件大小显示图片

    if(m_image1.IsNull())  
        return;  
      
    // 将整控件调整为与图像同一尺寸   
    GetDlgItem(IDC_STATIC_PIC)->SetWindowPos(NULL,  
                        0,0,m_image1.GetWidth(), m_image1.GetHeight(),   
                        SWP_NOMOVE);     
      
    CRect zcRect;  
    GetDlgItem(IDC_STATIC_PIC)->GetClientRect(&zcRect);  
      
    m_image1.Draw(GetDlgItem(IDC_STATIC_PIC)->GetDC()->m_hDC,  
                                zcRect.left,  
                                zcRect.top,  
                                zcRect.Width(),  
                                zcRect.Height());  

 

十一.CImage类与CBitmap转换

 

    CImage nImage;
    nImage.Load(imgFilePath);
 
    HBITMAP hBitmap=nImage.Detach(); // 获得位图句柄 用以转换
 
 
    // 转换方式一:
    CBitmap bmp;
    bmp.DeleteObject();
    bmp.Attach(hBitmap); //  转换为CBitmap对象
 
 
    // 转换方式二:
     
    CBitmap *pBitmap=CBitmap::FromHandle(nImage.m_hBitmap);

 

十二.CImage类实例实现图像二值化

 

void imgBinary(CImage image, int imgW, int imgH, int threshold)
{
    int i;
    int index = 0;
    byte *pImg = (byte *)image.GetBits();
    int step = image.GetPitch();
    int height = image.GetHeight();
    int width = image.GetWidth();
    for (i = 0; i<height*width; i++)
    {
        *(pImg + index) = *(pImg + index)>threshold ? 255 : 0;
        index++;
    }

}

 

 

十三.CImage实现自己的argmax函数----求图像一定高度区域中某一列遇到的第一个最大像素值得坐标并返回

 

int  argmax(CImage &image,int Top,int Bottom,int x)
{
    int max = 0;
    int tem;
    int pos = 0;
    byte *pImg = (byte *)image.GetBits();
    int step = image.GetPitch();
    int height = image.GetHeight();
    int width = image.GetWidth();
    if (Top > 0 && Top < height && Bottom > 0 && Bottom < height && x > 0 && x < width)
    {
        for (int i = Top; i < Bottom; ++i)
        {
            tem = *(pImg + i*step + x);
            if (tem > max)
            {
                max = tem;
                pos = i;
            }

        }
        return pos;
    }
    else
    {
        return FALSE;
    }
        
}

 

 

 

 

 十四.CImage类创建指定长宽图像并初始化调色板

 

bool InitalImage(CImage &image, int width, int height)
{
    if (image.IsNull())
        image.Create(width, height, 8);
    else
    {
        if (width <= 0 || height <= 0)
            return false;
        else if (image.GetHeight() == width && image.GetWidth() == height)
            return true;
        else
        {
            image.Destroy();
            image.Create(width, height, 8);
        }
    }
    //写入调色板
    RGBQUAD ColorTable[256];
    image.GetColorTable(0, 256, ColorTable);
    for (int i = 0; i < 256; i++)
    {
        ColorTable[i].rgbBlue = (BYTE)i;
        ColorTable[i].rgbGreen = (BYTE)i;
        ColorTable[i].rgbRed = (BYTE)i;
    }
    image.SetColorTable(0, 256, ColorTable);
    return true;
}

 

 

 

 十五.将存放在一维指针数组里的图像数据赋值给CImage类实例

void LoadImageData(CImage &image, unsigned char * data)
{
    if (data == nullptr)
        return;
    byte *pS;
    byte *pImg = (byte *)image.GetBits();
    int step = image.GetPitch();
    int height = image.GetHeight();
    int width = image.GetWidth();
    for (int i = 0; i < image.GetHeight(); ++i)
    {
        pS = data + i * width;
        for (int j = 0; j < image.GetWidth(); ++j)
        {
            *(pImg + i*step + j) = pS[j];
        }
    }
}

 

十六.CImage类自己实现图片的裁剪

 

//裁剪roi区域
void RoiCut(CImage &image, CImage &roiImg, int heightTop,int heightDown,int widthBegin,int widthEnd)
{
    InitalImage(roiImg, heightDown - heightTop + 1, widthBegin - widthEnd + 1);
    byte *pImg = (byte *)image.GetBits();
    byte *pRoi = (byte *)roiImg.GetBits();
    int step = image.GetPitch();
    int height = image.GetHeight();
    int width = image.GetWidth();
    int index = 0;
    for (int i = heightTop; i < heightDown; i++)
    {
        for (int j = widthBegin; j < widthEnd; j++)
        {
            *(pRoi + index) = *(pImg + i*step + j);
            index++;
        }
    }

}

 

 

 

 还有一些操作比较麻烦这里就先不写了,后面想到什么在写把嘿嘿。

 

 

 

 

 

 

 

 

参考博客:https://blog.csdn.net/ishallwin/article/details/4840180

参考博客:https://blog.csdn.net/shuilan0066/article/details/7080244

 

转载于:https://www.cnblogs.com/DOMLX/p/9598974.html

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
地进行查找、插入、删除等操作。哈希函数是将关键字映射到哈希表中的位置灰度图像的对数指数变换是一种常用的图像增强方法,可以通过以下代码实现: ```c++ // 对数指数变换函数 void Logarithmic_Exponential_Transform(CImage* pImage, double a的函数,常用的哈希函数有除留余数法、平方取中法、折叠法等。处理冲突的方法有开放地址法、链地址法、再哈希法等。 给定关键字序列(25,, double b, double c) { // 获取图像信息 int nWidth = pImage->GetWidth(); int nHeight = pImage->GetHeight(); int nPitch = pImage->GetPitch(); BYTE* pBits = (BYTE*)pImage18,3,21,65,98,31,19,22,12,0,29,101),采用除留余->GetBits(); // 逐像素进行变换 for (int y = 0; y < nHeight; y数法作为哈希函数、链地址法作为处理冲突的方法绘制哈希表: ``` 哈希表++) { BYTE* pRow = pBits + y * nPitch; for (int x = 0; x < n大小为13,将关键字除以13取余数作为哈希地址。用链表表示哈希表中的Width; x++) { BYTE gray = pRow[x]; double f = a * log(1 + b * gray) +每个位置。 0 -> 0 -> null 1 -> null 2 -> null 3 -> 3 -> null 4 -> null c; if (f < 0) f = 0; if (f > 255) f = 255; 5 -> null 6 -> 18 -> null 7 -> null 8 -> 98 -> null 9 -> 19 -> pRow[x] = (BYTE)f; } } } ``` 其中,a、b、c 是变换参数,可以29 -> 99 -> null 10 -> 21 -> 31 -> null 11 -> 22 -> null 12 -> 12 ->根据实际需求进行调整。调用该函数时,可以将 CImage 对象作为参数传入,表示对该图像进行处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值