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

news/2024/7/5 21:26:30

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();

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;

}

原图:
膨胀后:


http://www.niftyadmin.cn/n/3876809.html

相关文章

Laravel 中像Thinkphp中一样使用全局自定义函数

2019独角兽企业重金招聘Python工程师标准>>> 创建文件 app/helpers.php<?php // 示例函数 function foo() {return time();}2.修改composer.json。 {..."autoload": {"files": ["app/helpers.php"]}... }3.运行composer dump-aut…

【App】app中获取ro属性的方法

参看博客&#xff1a; https://my.oschina.net/chaselinfo/blog/213393?p1 String version SystemPropertiesProxy.get(this,"ro.mediatek.version.release");Log.d("QYC", "version " version);

pages/another/another 出现脚本错误或者未正确调用 Page()

出现该问题一定是js文件中忘了定义onLoad 可以温习一下页面生命周期知识 Page({ data: { }, onLoad: function () { }, })

译:SQL Server的Missing index DMV的 bug可能会使你失去理智---慎重看待缺失索引DMV中的信息...

注&#xff1a; 本文译自https://www.sqlskills.com/blogs/paul/missing-index-dmvs-bug-that-could-cost-your-sanity/  原文作者是在SQL Server 2008 SP1下面说的这个问题&#xff0c;本人在SQL Server 2014 SP2下测试仍有有这个问&#xff0c;因此记录了下来  本人原本打…

【App】蓝牙Bluetooth,app的编写总结

蓝牙四大必须任务&#xff1a;1.设置蓝牙2.查找局部区域内的配对设备&#xff0c;或可用设备3.连接设备4.再设备之间传输数据蓝牙权限&#xff1a;BLUETOOTH,ACCESS_FINE_LOCATION //android 9, API 28ACCESS_COARSE_LOCATION // < android9借助远程设备的已知 MAC 地址&a…

微信小程序selectable=‘true‘无复制效果解决

<text selectable></text>或<text selectable{{true}}></text>

20个高效正则表达式

2019独角兽企业重金招聘Python工程师标准>>> 1 . 校验密码强度 密码的强度必须是包含大小写字母和数字的组合&#xff0c;不能使用特殊字符&#xff0c;长度在8-10之间。 ^(?.*\\d)(?.*[a-z])(?.*[A-Z]).{8,10}$ 2. 校验中文 字符串仅能是中文。 ^[\\u4e00-\\u9f…

【androidstudio】下载gradle和sdk慢,更新不了的问题

下不下来&#xff0c;网络那些问题的解决 改hosts文件vim /etc/hosts#qyc add 2020/10/19 google app engine 203.208.41.32 appengine.google.com 203.208.50.167 dl.google.com 203.208.50.167 dl-ssl.google.com 这个快速的IP是怎么知道的&#xff1a; 这个IP是怎么发现…