1.图像转换为数字矩阵

将图像读取为一个矩阵



data = imread('C:\Users\10378\Desktop\学习\test\test.jpg');


2.灰度化处理

rgb2gray :通过消除图像色调和饱和度信息同时保留亮度实现将将RGB图像或彩色图转换为灰度图像,即灰度化处理的功能



gdata = rgb2gray(data);


3.二值化

graythresh :最大类间方差法是一种自适应的阈值确定的方法,又叫大津法,简称OTSU。它是按图像的灰度特性,将图像分成背景和目标2部分。在使用im2bw函数将灰度图像转换为二值图像时,需要设定一个阈值,这个函数可以帮助我们获得一个合适的阈值。



n = graythresh(data);


 



BW = im2bw(data, n);


完整代码:



data = imread('C:\Users\10378\Desktop\学习\test\test.jpg');

data

subplot(221);

imshow(data);

gdata = rgb2gray(data);

subplot(222);

imshow(gdata);

n = graythresh(gdata);

subplot(223);

imshow(bw);

bw=im2bw(data,n);