MATLAB学习笔记——影像处理进阶

本文详细介绍了使用MATLAB进行图像处理的方法,包括图像转换、背景去除、连通区域标记及上色等高级技巧。通过具体案例,展示了如何利用MATLAB内置函数如graythresh、im2bw、imopen、imsubtract等实现图像的二值化、形态学操作及连通组件分析。

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

前言

本次主要是为了记录下学习和复习MATLAB中的知识点,以此来巩固一下自己薄弱的知识体系,MATLAB前面基础零散的小知识点就暂时先不管,这次直接奔向画图模块,事先声明,本人是跟着的B站上的教程视频 MATLAB教程_台大郭彦甫(14课)原视频补档,所以博客中的大部分案例也都来自郭老师得教案。

MATLAB对图像进阶处理

图片转换成二进制图

I = imread('rice.png'); level=graythresh(I); 
bw=im2bw(I, level); subplot(1,2,1); imshow(I); 
subplot (1,2,2); imshow(bw);

graythresh - Global image threshold(阈值) using Otsu’s method
This MATLAB function computes a global threshold T from grayscale image I, using Otsu’s method [1].
im2bw - Convert(转换) image to binary image, based on threshold
This MATLAB function converts the grayscale image I to binary image BW, by replacing all pixels in the input image with luminance greater than level with the value 1 (white) and replacing all other pixels with the value 0 (black).
在这里插入图片描述
背景图估计

I = imread('rice.png'); 
BG = imopen(I, strel('disk', 15));
imshow(BG);

imopen - Morphologically(心态) open image
This MATLAB function performs morphological opening on the grayscale or binary image I, returning the opened image, J.
在这里插入图片描述
去除背景

I = imread('rice.png'); 
subplot(1,3,1); imshow(I); 
BG = imopen(I, strel('disk', 15));
subplot(1,3,2); imshow(BG);
I2 = imsubtract(I, BG);
subplot(1,3,3); imshow(I2);

imsubtract - Subtract one image from another or subtract constant from image(减法)
This MATLAB function subtracts each element in array Y from the corresponding element in array X and returns the difference in the corresponding element of the output array Z
在这里插入图片描述
基于阈值把背景去除(更精确)

I = imread('rice.png'); level=graythresh(I);
bw = im2bw(I, level); subplot (1,2,1); 
imshow(bw); BG = imopen(I, strel('disk', 15));
I2 = imsubtract(I, BG); level=graythresh(I2); 
bw2 = im2bw(I2, level); 
subplot(1,2,2); imshow(bw2);

在这里插入图片描述
通过连通区域标记

I=imread('rice.png');
BG=imopen(I, strel('disk', 15));
I2=imsubtract(I, BG); level=graythresh(I2); 
BW=im2bw(I2, level); 
[labeled, numObjects]=bwlabel(BW, 8);

bwlabel - Label connected components(区域) in 2-D binary image
This MATLAB function returns the label matrix(矩阵) L that contains labels for the 8-connected objects found in BW.
在这里插入图片描述
可以看出,最后计算出来的numObjects为99,所以连通的区域(白色部分)有99,可应用于图像识别的计数。
给图片上色

I=imread('rice.png');
BG=imopen(I, strel('disk', 15));
I2=imsubtract(I, BG); level=graythresh(I2); 
BW=im2bw(I2, level); 
[labeled, numObjects]=bwlabel(BW, 8);
RGB_label=label2rgb(labeled); imshow(RGB_label);

label2rgb - Convert label matrix into RGB image
This MATLAB function converts a label matrix, L, such as those returned by labelmatrix, bwlabel, bwlabeln, or watershed, into an RGB color image for the purpose of visualizing the labeled regions.
在这里插入图片描述
交互式选择

I=imread('rice.png'); level=graythresh(I);
BG=imopen(I, strel('disk', 15));
I2=imsubtract(I, BG); BW=im2bw(I2, graythresh(I2)); 
ObjI = bwselect(BW); imshow(ObjI);

bwselect - Select objects in binary image
This MATLAB function returns a binary image containing the objects that overlap the pixel (r,c), where n specifies the connectivity.
在这里插入图片描述
选择你要扣出的区域,然后右键
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值