halcon求图像灰度最大值和最小值——min_max_gray

前言

我们在图像处理中有时候需要求图像的最小灰度和最大灰度,本文就来介绍如何实现。

1、halcon算子应用

csharp 复制代码
dev_clear_window()
gen_image_const (Image, 'byte', 3, 3)
get_image_size (Image, Width, Height)
for rowIndex := 1 to Height by 1
    for columnIndex := 1 to Width by 1
        if(rowIndex=1 and columnIndex=1)
             set_grayval (Image, rowIndex-1, columnIndex-1,1)
        endif
        if(rowIndex=1 and columnIndex=2)
             set_grayval (Image, rowIndex-1, columnIndex-1,2)
        endif
        if(rowIndex=1 and columnIndex=3)
             set_grayval (Image, rowIndex-1, columnIndex-1,30)
        endif
        if(rowIndex=2 and columnIndex=1)
             set_grayval (Image, rowIndex-1, columnIndex-1,100)
        endif
        if(rowIndex=2 and columnIndex=2)
             set_grayval (Image, rowIndex-1, columnIndex-1,150)
        endif
        if(rowIndex=2 and columnIndex=3)
             set_grayval (Image, rowIndex-1, columnIndex-1,190)
        endif
        if(rowIndex=3 and columnIndex=1)
             set_grayval (Image, rowIndex-1, columnIndex-1,210)
        endif
        if(rowIndex=3 and columnIndex=2)
             set_grayval (Image, rowIndex-1, columnIndex-1,225)
        endif
        if(rowIndex=3 and columnIndex=3)
             set_grayval (Image, rowIndex-1, columnIndex-1,250)
        endif
    endfor
endfor

threshold (Image, Region, 0, 255)
min_max_gray (Region, Image, 20, Min, Max, Range)

min_max_gray算子用于计算图像最小灰度和最大灰度,该算子有6的参数

参数1是指定计算的区域范围

参数2是输入图像

参数3是截断百分比,取值范围 用于控制从灰度直方图两端移除的像素比例,比如上面的代码的图像总共有9个像素,参数3是20,则灰度直方图两边需要移除的像素个数是9*20%=1.8个像素,4舍5入,需要移除2个像素,灰度直方图中像素从小到大的排列是1、2、30、100、150、190、210、225、250,移除掉灰度小的一边的2个像素1、2,移除掉灰度大的一边的2个像素225、250,所以最终得到的灰度最小值是30,灰度最大值210,灰度范围是210-30=180.

参数4是灰度最小值,参考参数3中描述的计算方式

参数5是灰度最大值,参考参数3中描述的计算方式

参数6是灰度范围,参考参数3中描述的计算方式

2、算子应用后效果


马工撰写的年入30万+C#上位机项目实战必备教程(点击下方链接即可访问文章目录)

1、《C#串口通信从入门到精通》

2、《C#与PLC通信从入门到精通 》

3、《C# Modbus通信从入门到精通》

4、《C#Socket通信从入门到精通 》

5、《C# MES通信从入门到精通》

6、《winform控件从入门到精通》

7、《C#操作MySql数据库从入门到精通》

以上教程CSDN订阅已超过2000+~

相关推荐
开开心心就好17 小时前
自动生成小学数学题库支持导出Word
人工智能·安全·leetcode·贪心算法·ocr·音视频·语音识别
chinesegf17 小时前
模型如何自主判断调用工具
人工智能·自动化
专注搞钱17 小时前
用GPT-4o自动生成SPC报告:省了每月2天重复劳动
人工智能·半导体
Marion15817 小时前
【无标题】
java·人工智能·ai
qingyulee17 小时前
Python自然语言处理:从分词到词向量
人工智能·自然语言处理
老徐聊GEO17 小时前
2026年:巧妙引导,让AI回答中自然融入你的品牌
大数据·人工智能·python
Cx330❀17 小时前
【Linux网络】从零定制应用层协议:黏包问题、全双工缓冲区与 Jsoncpp 序列化深度解析
linux·运维·服务器·开发语言·网络·c++·人工智能
冷小鱼17 小时前
SAP:从ERP巨头到AI+时代的智能引擎
人工智能
Skrrapper17 小时前
从 DeepSeek、Qwen 到 GPT:一次企业级 AI 知识库项目的模型选型复盘
人工智能·gpt·算法
未来和明天17 小时前
领嵌iLeadE-588边缘计算盒子4路AHD、4路千兆网接多个摄像头多路AI视频分析
人工智能·边缘计算