MATLAB:Image Processing Toolbox工具箱入门实战(1),超详细讲解

本文介绍了MATLAB中的Image Processing Toolbox,通过读取图像、直方图均衡化、imfindcircles函数检测圆形对象,展示了如何提升图像对比度和寻找暗色及亮色圆。此外,还涉及形态学操作如开闭操作,以及二值化、连通分量分析等,适合初学者入门。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I = imread(“1.jpg”);
whos I % 使用 whos 命令,检查 imread 函数如何在工作区中存储图像数据。
% 调用 imhist 函数创建直方图。
% 请在调用 imhist 之前使用 figure 命令,这样直方图就不会覆盖当前图窗窗口中显示的图像 I。
imhist(I)
I2 = histeq(I); % 使用 histeq 函数提高图像的对比度
imshow(I2)
imwrite(I2, ‘2.jpg’); % 使用 imwrite 函数,将刚刚经过调整的图像 I2 写入磁盘文件
imfinfo(‘2.jpg’) % imfinfo 函数返回文件中图像的相关信息


![](https://img-blog.csdnimg.cn/direct/5ee7be33c41d44c7b05d2996d136f141.png)


#### 2.实战项目一:利用imfindcircles()函数检测和测量图像中的圆形目标


[Detect and Measure Circular Objects in an Image- MATLAB & Simulink ExampleThis example shows how to automatically detect circular objects in an image and visualize the detected circles.![icon-default.png?t=N7T8](https://csdnimg.cn/release/blog_editor_html/release2.3.6/ckeditor/plugins/CsdnLink/icons/icon-default.png?t=N7T8)https://www.mathworks.com/help/releases/R2021b/images/detect-and-measure-circular-objects-in-an-image.html](https://www.mathworks.com/help/releases/R2021b/images/detect-and-measure-circular-objects-in-an-image.html "Detect and Measure Circular Objects in an Image- MATLAB & Simulink Example")imfindcircles()使用基于圆形 Hough 变换 (CHT) 的算法在图像中寻找圆形。之所以使用这种方法,是因为当存在噪声、遮挡和变化的光照条件时该方法表现稳健。


有关imfindcircles()的详细信息,请参阅帮助文档:


[Find circles using circular Hough transform - MATLAB imfindcirclesThis MATLAB function finds the circles in image A whose radii are approximately equal to radius.![icon-default.png?t=N7T8](https://csdnimg.cn/release/blog_editor_html/release2.3.6/ckeditor/plugins/CsdnLink/icons/icon-default.png?t=N7T8)https://www.mathworks.com/help/releases/R2021b/images/ref/imfindcircles.html](https://www.mathworks.com/help/releases/R2021b/images/ref/imfindcircles.html "Find circles using circular Hough transform - MATLAB imfindcircles")本项目旨在演示调参来寻求圆目标的过程。


![](https://img-blog.csdnimg.cn/direct/7feb7f0d4db64bcfb7aa0e5a7fa3e7e9.png)



rgb = imread(‘coloredChips.png’);
imshow(rgb)

% 此段代码是为了清楚对象是比背景亮还是比背景暗,输出灰度图片看一看
% gray_image = rgb2gray(rgb);
% imshow(gray_image)

% 此段代码用来确定imfindcircles函数里的radiusRange,测出来应该是[25 30]
% d = drawline; % 画一条线,大致画出圆的直径
% pos = d.Position % 线的位置
% diffPos = diff(pos); % 各行之间的一阶差分,也就是delta x和delta y
% diameter = hypot(diffPos(1),diffPos(2)) % 平方和的平方根(斜边)

% 这里开始找圆,用的是imfindcircles()函数
% 背景相当亮,大多数塑料片比背景暗,将参数 ‘ObjectPolarity’ 设置为 ‘dark’ 以搜索较暗的圆。
% imfindcircles 有两种不同寻找圆的方法:默认方法(称为相位编码方法)/两阶段方法,这里指定使用两阶段方法
% 两种方法都能准确找到部分可见(遮挡)塑料片的中心和半径。
[centers,radii] = imfindcircles(rgb,[25 30],‘ObjectPolarity’,‘dark’, …
‘Sensitivity’,0.92,‘Method’,‘twostage’);

% 注意到黄色圆都没有被检测到
% 与背景相比,黄色塑料片的强度几乎相同,甚至更亮。因此,要检测黄色塑料片,‘ObjectPolarity’ 改为 ‘bright’。
% [centersBright,radiiBright] = imfindcircles(rgb,[25 30], …
% ‘ObjectPolarity’,‘bright’,‘Sensitivity’,0.95)
% 找到了三个原先未检测到的黄色塑料片&

### 扩展MATLAB Image Processing Toolbox功能的方法 为了增强MATLAB Image Processing Toolbox的功能,可以通过多种方式来实现这一目标。主要途径包括利用附加的产品包、编写自定义函数以及集成其他编程环境中的库。 #### 使用附加产品包 MathWorks提供了多个附加产品包,这些工具箱能够显著扩展图像处理能力[^1]。例如,Computer Vision System Toolbox 和 Deep Learning Toolbox 可以为用户提供更高级别的算法支持,适用于计算机视觉和深度学习领域内的复杂任务。 #### 编写自定义M文件或类 通过创建新的 M 文件 (脚本/函数) 或者定义自己的类,可以根据特定需求开发定制化的解决方案。这允许用户针对具体应用场景设计独特的滤波器、变换操作或者其他类型的图像分析流程。 ```matlab function outputImage = customFilter(inputImage, filterSize) % CUSTOMFILTER Applies a user-defined averaging filter to an input image. % % INPUTS: % inputImage - The original grayscale or color image matrix. % filterSize - Size of the square neighborhood over which to compute means. if nargin < 2 || isempty(filterSize), filterSize = 3; end % Default value h = fspecial('average', filterSize); outputImage = imfilter(double(inputImage), h); end ``` #### 集成第三方软件接口 除了内部资源外,还可以考虑与其他开源项目相结合。比如 OpenCV 是一个广泛使用的计算机视觉库,在 Python 中非常流行;然而它同样拥有 C++ API 并且能被 MATLAB 调用。借助 mex 函数编译外部代码片段并将其链接到 MATLAB 工作空间内执行,从而获得额外的能力[^3]. ```cpp // Example Mex function that calls OpenCV functions from within MATLAB #include "mex.h" #include <opencv2/core.hpp> #include <opencv2/imgproc.hpp> void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { // Convert MATLAB array into cv::Mat object... } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值