matlab重要函数

####matlab重要函数

  • 图片读取
imgsrc=imread('imgdir/imgname.jpg');
  • 转灰度图
grayImg=rgb2gray(imgsrc);
  • 二值化
imgbin=imgsrc<120;
thresh=graythresh(imgsrc);
imgbin=im2bw(imgsrc,thresh);//0
  • 显示图片
figure;imshow(imgsrc);title('imgsrc');
figure('Name','imgsrc');imshow(imgsrc);
  • 边缘检测
imgsobel=edge(grayImg,'sobel');
  • 腐蚀膨胀
se=strel('square',3);//Structuring element
imgdilate=imdilate(imgsobel,se);
imgerode=imerode(imgsobel,se);
  • 去除小的连通域的操作
imbwop=bwareaopen(imgdilate,300);
  • 连通域的求取
[L,num]=bwlabel(imbwop);
regionprops(L,'BoundingBox');//regionprops == get the properties of region
regionprops(L,'Centroid');
regionprops(L,'Area');
regionprops(L,'all');
  • 循环函数
for i=1:num
end
  • 判断语句
if area(i).Area>500 && status(i).BoundingBox(3)>30
    '语句'
end
  • 横向轴向投影
图像灰度x方向投影:
        img_x=sum(f,2)/n;
        plot(img_x,'r','LineWidth',1.5); %以序号为横坐标,img_x的值为纵坐标画出的折线
        
    图像灰度y方向投影:
        img_y=sum(f,1)/m;
        plot(img_y,'r','LineWidth',1.5);
  • 三维图显示
    mesh(tu3);
  • 单向梯度
    A=[1 3 4 5 7 9]
    diff(A)=[2,1,1,2,2];横向的间隔差值

####matlab 从txt中读取文件

clc;close all;clear all;
fin=fopen('/media/hhg/DWorkplace/2019-11-18/imglists.txt','r');
i=1;
while ~feof(fin)
    str=fgetl(fin);
    str2=strcat('/media/hhg/DWorkplace/2019-11-18/',str);
    img=imread(str2);
    imgGrey=rgb2gray(img);
%     figure;imshow(imgGrey);
    subplot(3,6,i);imshow(imgGrey);
    imgroi1=imgGrey(116:1020,1254:1628);
    imgroi2=imgGrey(116:1020,1628:1898);
%     figure;imshow(imgroi1);
%     figure;imshow(imgroi2);
    imgroi1_bin=imgroi1>220;
    imgroi2_bin=imgroi2>220;
    subplot(3,6,i+6);imshow(imgroi1_bin);
    subplot(3,6,i+6*2);imshow(imgroi2_bin);
%     figure;imshow(imgroi1_bin);
%     figure;imshow(imgroi2_bin);
%     savepath1=strcat('/media/hhg/DWorkplace/2019-11-18/',int2str(i),'_roi1.jpg');
%     savepath2=strcat('/media/hhg/DWorkplace/2019-11-18/',int2str(i),'_roi2.jpg');
%     imwrite(imgroi1_bin,savepath1);
%     imwrite(imgroi2_bin,savepath2);
    i=i+1;
end

####matlab中读取cvs

fin=fopen('/home/hhg/Projects/SVN/upper_control/bin/filletHist.csv','r');
while ~feof(fin)
   str=fgetl(fin);   % 读取一行, str是字符串
   temp=str2num(str);
    figure;plot(temp>140);
end
fclose(fin);

你可能感兴趣的:(Matlab,matlab,图像处理)