Demo entry 6769761

1

   

Submitted by anonymous on Nov 07, 2018 at 23:54
Language: Matlab. Code size: 3.2 kB.

x = 1;
while x>0

    excel_path = input('输出的excel文件存储路径 ','s');
    picture_edge = input('需要裁剪的边缘像素点 ');     
    hole_min = input('需要忽略的最大孔隙面积 ');
    new_picture_math = input('处理后照片保存路径 ','s');     %控制台输入内容
    
    [picture_name,picture_path,FilterIndex] = uigetfile({'*.tif';'*.jpg'},'select images','MultiSelect','on');     %读取图片
    if FilterIndex == 1
       picture_number = length(picture_name);
       if (iscell(picture_name) == 0)     %判断如只有一张图片则直接读取数组,否则转向else
           picture_number = 1;
           picture_matri = imread([picture_path,picture_name]);
           picture_size = size(picture_matri);
            if numel(picture_size)>2
                picture_matrix = rgb2gray(picture_matri);
            else
                picture_matrix = picture_matri;
            end

            picture_matrix = picture_matrix(1:picture_edge,:);     %裁剪
            picture_matrix = medfilt2(picture_matrix);     %中值滤波
            best_parameter = graythresh(picture_matrix);     %确定最佳二值化参数
            picture_matrix = im2bw(picture_matrix,best_parameter);      %二值化
            picture_matrix = (~picture_matrix);
            picture_matrix = bwareaopen(picture_matrix,hole_min,4);
            picture_matrix = (~picture_matrix);
            picture_matrix = bwareaopen(picture_matrix,hole_min,4);
            picture_matrix = (~picture_matrix);     %消除小面积孔隙
            imwrite(picture_matrix,[new_picture_math,picture_name,'.jpg']);
            picture_matrix = bwlabel(picture_matrix,4);     %标记区域
            hole_character = regionprops(picture_matrix,'Area','Eccentricity','Orientation');     %计算孔隙特征数据
            xlswrite(excel_path, struct2cell(hole_character),picture_name,'B1');     %输出到excel
            
       else     %如有不止一张图片,则读取为元胞数组
            for no = 1:picture_number
                picture_matri = imread([picture_path,picture_name{no}]);     %读取图片
                picture_size = size(picture_matri);
                if numel(picture_size)>2
                    picture_matrix = rgb2gray(picture_matri);
                else
                    picture_matrix = picture_matri;
                end

                picture_matrix = picture_matrix(1:picture_edge,:);     %裁剪
                picture_matrix = medfilt2(picture_matrix);     %中值滤波
                best_parameter = graythresh(picture_matrix);     %确定最佳二值化参数
                picture_matrix = im2bw(picture_matrix,best_parameter);      %二值化
                picture_matrix = (~picture_matrix);
                picture_matrix = bwareaopen(picture_matrix,hole_min,4);
                picture_matrix = (~picture_matrix);
                picture_matrix = bwareaopen(picture_matrix,hole_min,4);
                picture_matrix = (~picture_matrix);     %消除小面积孔隙
                imwrite(picture_matrix,[new_picture_math,picture_name{no},'.jpg']);
                picture_matrix = bwlabel(picture_matrix,4);     %标记区域
                hole_character = regionprops(picture_matrix,'Area','Eccentricity','Orientation');     %计算孔隙特征数据
                xlswrite(excel_path, struct2cell(hole_character),picture_name{no},'B1');     %输出到excel
            end;
       end;
     end;
end;

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).