图像腐蚀与膨胀(用CImage处理),使用快速处理图像灰度值

1.用快速读写进行颜色转变

#include<iostream>

#include<atlimage.h>

using namespace std;

int main()

{

const char *srcFilePath = "21a.jpg", *destFilePath = "21b.jpg";

CImage srcImage;

srcImage.Load(srcFilePath);

int width = srcImage.GetWidth(), height = srcImage.GetHeight();

int bpp = srcImage.GetBPP(), pitch = srcImage.GetPitch();

扫描二维码关注公众号,回复: 5032266 查看本文章

BYTE *pData = (BYTE *)srcImage.GetBits();

for (int y = 0; y < height; y++)

{

for (int x = 0; x < width; x++)

{

BYTE b = *(pData + pitch * y + x * bpp / 8 + 0);

BYTE g = *(pData + pitch * y + x * bpp / 8 + 1);

BYTE r = *(pData + pitch * y + x * bpp / 8 + 2);

*(pData + pitch * y + x * bpp / 8 + 0) = r;

*(pData + pitch * y + x * bpp / 8 + 1) = g;

*(pData + pitch * y + x * bpp / 8 + 2) = b;

}

}

srcImage.Save(destFilePath);

return 0;

}

 

 

 

 

2.用快速读写改变颜色灰度值

#include<iostream>

#include<atlimage.h>

using namespace std;

int main()

{

const char *srcFilePath = "22a.jpg", *destFilePath = "22b.jpg";

CImage srcImage;

srcImage.Load(srcFilePath);

int width = srcImage.GetWidth(), height = srcImage.GetHeight();

int bpp = srcImage.GetBPP(), pitch = srcImage.GetPitch();

BYTE *pData = (BYTE *)srcImage.GetBits();

for (int y = 0; y < height; y++)

{

for (int x = 0; x < width; x++)

{

BYTE b = *(pData + pitch * y + x * bpp / 8 + 0);

BYTE g = *(pData + pitch * y + x * bpp / 8 + 1);

BYTE r = *(pData + pitch * y + x * bpp / 8 + 2);

BYTE gray = (BYTE)round(r*0.299 + g * 0.587 + b * 0.114);

*(pData + pitch * y + x * bpp / 8 + 0) = gray;

*(pData + pitch * y + x * bpp / 8 + 1) = gray;

*(pData + pitch * y + x * bpp / 8 + 2) = gray;

}

}

srcImage.Save(destFilePath);

return 0;

}

 

 

 

 

 

 

3.实现腐蚀和膨胀函数

腐蚀:

#include<iostream>

#include<atlimage.h>

using namespace std;

int main()

{

const char *srcFilePath = "23a.jpg", *destFilePath = "23b2.jpg";

CImage srcImage;

srcImage.Load(srcFilePath);

int width = srcImage.GetWidth(), height = srcImage.GetHeight();

int bpp = srcImage.GetBPP(), pitch = srcImage.GetPitch();

BYTE *pData = (BYTE *)srcImage.GetBits();

int **a = new int*[height];

for (int i = 0; i < height; i++)

{

a[i] = new int[width];

 

}

for (int y = 0; y < height; y++)

{

for (int x = 0; x < width; x++)

{

BYTE b = *(pData + pitch * y + x * bpp / 8 + 0);

BYTE g = *(pData + pitch * y + x * bpp / 8 + 1);

BYTE r = *(pData + pitch * y + x * bpp / 8 + 2);

BYTE gray = (BYTE)round(r*0.299 + g * 0.587 + b * 0.114);

*(pData + pitch * y + x * bpp / 8 + 0) = gray;

*(pData + pitch * y + x * bpp / 8 + 1) = gray;

*(pData + pitch * y + x * bpp / 8 + 2) = gray;

a[y][x] = gray;

}

}

 

for (int y = 3; y < height - 3; y++) //忽略一点点边缘

{

for (int x = 3; x <width - 3; x++)

{

 

if (a[y][x - 1] < 20 && a[y][x + 1] < 20)

{

if (a[y - 1][x]< 20 && a[y + 1][x] < 20)

{

if (a[y + 1][x - 1] < 20 && a[y - 1][x - 1] < 20)

{

if (a[y - 1][x + 1] <20 && a[y + 1][x + 1] < 20)

{

if (a[y][x - 3] < 20 && a[y][x + 3] < 20)

{

if (a[y - 3][x] < 20 && a[y + 3][x] < 20)

{

if (a[y + 3][x - 3] < 20 && a[y - 3][x - 3] < 20)

{

if (a[y - 3][x + 3] < 20 && a[y + 3][x + 3] < 20)

{

*(pData + pitch * y + x * bpp / 8 + 0) = 1;

*(pData + pitch * y + x * bpp / 8 + 1) = 1;

*(pData + pitch * y + x * bpp / 8 + 2) = 1;

}

}

}

}

}

}

}

}

else

{

*(pData + pitch * y + x * bpp / 8 + 0) = 255;

*(pData + pitch * y + x * bpp / 8 + 1) = 255;

*(pData + pitch * y + x * bpp / 8 + 2) = 255;

}

 

 

}

}

srcImage.Save(destFilePath);

return 0;

}

腐蚀前:

腐蚀后:

 

膨胀:

#include<iostream>

#include<atlimage.h>

using namespace std;

int main()

{

const char *srcFilePath = "23a.jpg", *destFilePath = "23c.jpg";

CImage srcImage;

srcImage.Load(srcFilePath);

int width = srcImage.GetWidth(), height = srcImage.GetHeight();

int bpp = srcImage.GetBPP(), pitch = srcImage.GetPitch();

BYTE *pData = (BYTE *)srcImage.GetBits();

int **a = new int*[height];

for (int i = 0; i < height; i++)

{

a[i] = new int[width];

 

}

for (int y = 0; y < height; y++)

{

for (int x = 0; x < width; x++)

{

BYTE b = *(pData + pitch * y + x * bpp / 8 + 0);

BYTE g = *(pData + pitch * y + x * bpp / 8 + 1);

BYTE r = *(pData + pitch * y + x * bpp / 8 + 2);

BYTE gray = (BYTE)round(r*0.299 + g * 0.587 + b * 0.114);

*(pData + pitch * y + x * bpp / 8 + 0) = gray;

*(pData + pitch * y + x * bpp / 8 + 1) = gray;

*(pData + pitch * y + x * bpp / 8 + 2) = gray;

a[y][x] = gray;

}

}

 

for (int y = 2; y < height - 2; y++) //忽略一点点边缘,防止溢出

{

for (int x = 2; x <width- 2; x++)

{

 

if (a[y][x - 1] > 30 && a[y][x + 1] > 30)

{

if (a[y - 1][x] > 30 && a[y + 1][x] > 30)

{

if (a[y + 1][x - 1] > 30 && a[y - 1][x - 1] > 30)

{

if (a[y - 1][x + 1] > 30 && a[y + 1][x + 1] > 30)

{

if (a[y][x - 2] > 30 && a[y][x + 2] > 30)

{

if (a[y - 2][x] > 30 && a[y + 2][x] > 30)

{

if (a[y + 2][x - 2] > 30 && a[y - 2][x - 2] > 30)

{

if (a[y - 2][x + 2] > 30 && a[y + 2][x + 2] > 30)

{

*(pData + pitch * y + x * bpp / 8 + 0) = 255;

*(pData + pitch * y + x * bpp / 8 + 1) = 255;

*(pData + pitch * y + x * bpp / 8 + 2) = 255;

}

}

}

}

}

}

}

}

else

{

*(pData + pitch * y + x * bpp / 8 + 0) = 1;

*(pData + pitch * y + x * bpp / 8 + 1) = 1;

*(pData + pitch * y + x * bpp / 8 + 2) = 1;

}

 

 

}

}

srcImage.Save(destFilePath);

return 0;

}

原图:
膨胀后:

猜你喜欢

转载自blog.csdn.net/a66666_/article/details/81355582
今日推荐