本次主要是为了记录下学习和复习MATLAB中的知识点,以此来巩固一下自己薄弱的知识体系,MATLAB前面基础零散的小知识点就暂时先不管,这次直接奔向画图模块,事先声明,本人是跟着的B站上的教程视频 MATLAB教程_台大郭彦甫(14课)原视频补档,所以博客中的大部分案例也都来自郭老师得教案。
图片转换成二进制图
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.
选择你要扣出的区域,然后右键