简单图像处理 MATLAB

1、图像读取

I = imread('C:\Users\Administrator\Desktop\123\A\001.jpg');%变量名=imread('路径\名称')
subplot(121);%原图展示
imshow(I); % 输出原图

 2、将RGB图像转换为灰度图像

X=rgb2gray(I);
imshow(X);

3、将灰度图转化为二值图像,0.4为阈值参数取值0-1

BW=im2bw(X,0.4);
figure,imshow(BW);

 4、检测图像中的线段

I = imread('C:\Users\Administrator\Desktop\123\A\001.jpg');
I = rgb2gray(I);
h1 = [-1,-1,-1;2,2,2;-1,-1,-1];
h2 = [-1,-1,2;-1,2,-1;2,-1,-1];
h3 = [-1,2,-1;-1,2,-1;-1,2,-1];
h4 = [2,-1 -1;-1,2,-1;-1,-1,2];
J1 = imfilter(I,h1);
J2 = imfilter(I,h2);
J3 = imfilter(I,h3);
J4 = imfilter(I,h4);
J = J1+J2+J3+J4;
figure;
subplot(121),imshow(I);
subplot(122),imshow(J);

5、图像分割

I = imread('C:\Users\Administrator\Desktop\123\A\001.jpg');
I = rgb2gray(I);
figure;
subplot(121),imshow(I);
subplot(122),imhist(I,200);%显示灰度图像的直方图
%采用全局阈值对图像进行分割
J=I>120;
[width,height]=size(I);
for i = 1:width
    for j = 1:height
        if (I(i,j)>130)
            K(i,j)=1;
        else
            K(i,j)=0;
        end
    end
end
figure;
subplot(121),imshow(J);
subplot(122),imshow(K);

 

 6、图像裁剪

%图片裁剪
I = imread('C:\Users\Administrator\Desktop\123\A\001.jpg');
K=rgb2gray(I);
rect = [10 15 50 50];%定义剪切区域
X = imcrop(K,rect);%进行图像剪切
subplot(121),imshow(K);
rectangle('Position',rect,'LineWidth',2,'EdgeColor','r');%显示图像裁剪区域
subplot(122),imshow(X);%显示裁剪图像

猜你喜欢

转载自blog.csdn.net/weixin_64589302/article/details/130735380
今日推荐