目录
2.实战项目一:利用imfindcircles()函数检测和测量图像中的圆形目标
1.基本图像导入、处理和导出
Basic Image Import, Processing, and Export- MATLAB & SimulinkThis example shows how to read an image into the workspace, adjust the contrast in the image, and then write the adjusted image to a file.https://www.mathworks.com/help/releases/R2021b/images/image-import-and-export.html以下是输入的缺陷检测图片1.jpg(左),和经过histeq(直方图均衡使强度值扩展分布到了图像的完整范围内)函数提高对比度的2.jpg(右)。
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 函数返回文件中图像的相关信息
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.https://www.mathworks.com/help/releases/R2021b/images/detect-and-measure-circular-objects-in-an-image.htmlimfindcircles()使用基于圆形 Hough 变换 (CHT) 的算法在图像中寻找圆形。之所以使用这种方法,是因为当存在噪声、遮挡和变化的光照条件时该方法表现稳健。
有关imfindcircles()的详细信息,请参阅帮助文档:
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', ...
'Sensit