技术心得:图像处理——(源)腐蚀(eroded)、膨胀(dilated)函数编程实现

简介: 技术心得:图像处理——(源)腐蚀(eroded)、膨胀(dilated)函数编程实现

"

1 #include

2 #include

3 #include

4 #include

5 #include

6 using namespace cv;

7 using namespace std;

8

9 //自定义膨胀函数

10 void dilated_my( Mat img1,Mat bin,int k)

11 {

12 k=(k-1)/2;

13 for (int i = 0; i < bin.rows; ++i){

14 for (int j = 0; j < bin.cols; ++j){

15

16 uchar maxV = 0;

17 //遍历周围最大像素值

18 for (int yi = i-k; yi <= i+k; yi++) {

19 for (int xi = j-k; xi <= j+k; xi++) {

20 if (xi[span style=""color: rgba(128, 0, 128, 1)"">0||xi>= bin.cols|| yi[span style=""color: rgba(128, 0, 128, 1)"">0 || yi >= bin.rows){

21 continue;

22 }

23

24 maxV = (std::max)(maxV, bin.at(yi, xi));

25 }

26 }

27 img1.at(i, j) = maxV;

28 }

29 }

30 }

31 //自定义腐蚀函数

32 void eroded_my( Mat img1,Mat bin ,int k)

33 {

34 k=(k-1)/2;

35 for (int i = 0; i < bin.rows; ++i){

36 for (int j = 0; j < bin.cols; ++j){

37 uchar minV = 255;

38

39 //遍历周围最小像素值

40 for (int yi = i-k; yi <= i+k; yi++) {

41 for (int xi = j-k; xi <= j+k; xi++) { //代码效果参考:https://v.youku.com/v_show/id_XNjQwNjYzNDk0NA==.html

42 if (xi[span style=""color: rgba(128, 0, 128, 1)"">0||xi>= bin.cols|| yi[span style=""color: rgba(128, 0, 128, 1)"">0 || yi >= bin.rows){

43 continue;

44 }

45 minV = (std::min)(minV, bin.at(yi, xi));

46

47 }

48 }

49 img1.at(i, j) = minV;

50 }

51 }

52 }

53 int main()

54 {

55 //从文件中读取成灰度图像

56

57

58 Mat img1 = imread("""",0);

59 imshow(""gray"",img1);

60 Mat dst1;

61 Mat dst2;

62 Mat bin;

63 threshold(img1,bin,200,255,CV_THRESH_BINARY);

64

65 //opencv函数法

66 Mat structElement1 = getStructuringElement(MORPH_RECT, Size(5,5));

67 dilate(bin,dst1,structElement1//代码效果参考:https://v.youku.com/v_show/id_XNjQwNjY0NTcyMA==.html

);

68 imshow(""opencv_dilated"", dst1);

69 erode(bin,dst2,structElement1);

70 imshow(""opencv_eroded"", dst2);

71

72 //自定义方法

73 dilated_my(img1,bin,5);

74 imshow(""my_dilated"",img1);

75 eroded_my(img1,bin,5);

76 imshow(""my_eroded"",img1);

77

78 waitKey(0);

79 return 0;

80

81 }

原图:

opencv函数法:

自己函数:

萍水相逢逢萍水,浮萍之水水浮萍!


"
image.png
相关文章
|
并行计算 PyTorch 算法框架/工具
【已解决】RuntimeError: CuDA error: no kernel image is available for execution on the device
【已解决】RuntimeError: CuDA error: no kernel image is available for execution on the device
|
1天前
|
人工智能 运维 安全
|
3天前
|
SpringCloudAlibaba 负载均衡 Dubbo
微服务架构下Feign和Dubbo的性能大比拼,到底鹿死谁手?
本文对比分析了SpringCloudAlibaba框架下Feign与Dubbo的服务调用性能及差异。Feign基于HTTP协议,使用简单,适合轻量级微服务架构;Dubbo采用RPC通信,性能更优,支持丰富的服务治理功能。通过实际测试,Dubbo在调用性能、负载均衡和服务发现方面表现更出色。两者各有适用场景,可根据项目需求灵活选择。
371 123
微服务架构下Feign和Dubbo的性能大比拼,到底鹿死谁手?
|
6天前
|
人工智能 JavaScript 测试技术
Qwen3-Coder入门教程|10分钟搞定安装配置
Qwen3-Coder 挑战赛简介:无论你是编程小白还是办公达人,都能通过本教程快速上手 Qwen-Code CLI,利用 AI 轻松实现代码编写、文档处理等任务。内容涵盖 API 配置、CLI 安装及多种实用案例,助你提升效率,体验智能编码的乐趣。
581 107
|
2天前
|
Java 数据库 数据安全/隐私保护
Spring 微服务和多租户:处理多个客户端
本文介绍了如何在 Spring Boot 微服务架构中实现多租户。多租户允许单个应用实例为多个客户提供独立服务,尤其适用于 SaaS 应用。文章探讨了多租户的类型、优势与挑战,并详细说明了如何通过 Spring Boot 的灵活配置实现租户隔离、动态租户管理及数据源路由,同时确保数据安全与系统可扩展性。结合微服务的优势,开发者可以构建高效、可维护的多租户系统。
193 127
|
2天前
|
Web App开发 前端开发 API
在折叠屏应用中,如何处理不同屏幕尺寸和设备类型的样式兼容性?
在折叠屏应用中,如何处理不同屏幕尺寸和设备类型的样式兼容性?
222 124
|
2天前
|
人工智能 数据可视化 测试技术
Coze平台指南(3):核心功能-创建智能体与设计角色
Coze 智能体是由大语言模型驱动,通过提示词设定角色,并借助知识库、插件和工作流扩展能力,以执行特定任务的AI助手。对测试工程师而言,精心设计的智能体可显著提升测试效率与质量,关键是要准确理解测试需求,并将其转化为智能体的角色设定和功能配置。建议进一步学习知识库与工作流,以深化应用。

热门文章

最新文章