1.批量二值化及保存
clc;
clear;
file_path = 'E:\spyder\spineunet-1\data\train\image\';% 图像文件夹路径
img_path_list = dir(strcat(file_path,'*.jpg'));%获取该文件夹中所有jpg格式的图像
img_num = length(img_path_list);%获取图像总数量
if img_num > 0 %有满足条件的图像
for j = 1:img_num %逐一读取图像
image_name = img_path_list(j).name;% 图像名
IM = imread(strcat(file_path,image_name));
%fprintf('%d %d %s',i,j,strcat(file_path,image_name));% 显示正在处理的图像名
%自适应滤波
im=wiener2(IM,[3,3]);
%二值化
thresh=graythresh(im);
im=im2bw(im,thresh);
% %****平滑滤波****
% h=ones(3,3)/5;
% h(1,1)=0;h(1,3)=0;h(3,1)=0;h(1,3)=0;
% im=imfilter(IM,h);
% %****中值滤波****
% im=medfilt2(IM,[3,3]);
% %****均值滤波****
% fsp=fspecial('average',3);
% im=imfilter(IM,fsp);
%figure;
%imshow(im);
saveddir='E:\matlab\gxy\';
savedname=fullfile(saveddir,image_name);
imwrite(im,savedname);
end
end
`2.滤波、开闭运算
I=imread('test16.bmp');
thresh=graythresh(I);
bw=im2bw(I,thresh);
%****平滑滤波****
h=ones(3,3)/5;
h(1,1)=0;h(1,3)=0;h(3,1)=0;h(1,3)=0;
I1=imfilter(I,h);
thresh1=graythresh(I1);
bw1=im2bw(I1,thresh1);
se1=strel('line',5,200);%直线长度7,角度85
img1=imopen(bw1,se1);%开运算
img2=imclose(bw1,se1);%闭运算
img3=imclose(img1,se1);%先开后闭运算
img4=imopen(img2,se1);%先闭后开运算
img5=imdilate(bw1,se1);%膨胀操作
img6=imerode(bw1,se1);%腐蚀
figure(1);
subplot(331),imshow(I1);title('平滑滤波');
subplot(332),imshow(bw1);title('平滑滤波二值化');
subplot(333),imshow(img1);title('开运算');
subplot(334),imshow(img2);title('闭运算');
subplot(335),imshow(img3);title('先开后闭运算');
subplot(336),imshow(img4);title('先闭后开运算');
subplot(337),imshow(img5);title('膨胀操作');
subplot(338),imshow(img6);title('腐蚀');
subplot(339),imshow(bw);title('原图二值化');
%****自适应滤波****
I2=wiener2(I,[3,3]);
thresh2=graythresh(I2);
bw2=im2bw(I2,thresh2);
se2=strel('line',5,215);%直线长度7,角度85
img11=imopen(bw2,se2);%开运算
img12=imclose(bw2,se2);%闭运算
img13=imclose(img11,se2);%先开后闭运算
img14=imopen(img12,se2);%先闭后开运算
img15=imdilate(bw2,se2);%膨胀操作
img16=imerode(bw2,se2);%腐蚀
figure(2);
subplot(331),imshow(I2);title('自适应滤波');
subplot(332),imshow(bw2);title('自适应滤波二值化');
subplot(333),imshow(img11);title('开运算');
subplot(334),imshow(img12);title('闭运算');
subplot(335),imshow(img13);title('先开后闭运算');
subplot(336),imshow(img14);title('先闭后开运算');
subplot(337),imshow(img15);title('膨胀操作');
subplot(338),imshow(img16);title('腐蚀');
subplot(339),imshow(bw);title('原图二值化');
%****中值滤波****
I3=medfilt2(I,[3,3]);
thresh3=graythresh(I3);
bw3=im2bw(I3,thresh3);
se3=strel('line',5,90);%直线长度7,角度85
img111=imopen(bw3,se3);%开运算
img112=imclose(bw3,se3);%闭运算
img113=imclose(img111,se3);%先开后闭运算
img114=imopen(img112,se3);%先闭后开运算
img115=imdilate(bw3,se3);%膨胀操作
img116=imerode(bw3,se3);%腐蚀
figure(3);
subplot(331),imshow(I3);title('中值滤波');
subplot(332),imshow(bw3);title('中值滤波二值化');
subplot(333),imshow(img111);title('开运算');
subplot(334),imshow(img112);title('闭运算');
subplot(335),imshow(img113);title('先开后闭运算');
subplot(336),imshow(img114);title('先闭后开运算');
subplot(337),imshow(img115);title('膨胀操作');
subplot(338),imshow(img116);title('腐蚀');
subplot(339),imshow(bw);title('原图二值化');
%****均值滤波****
fsp=fspecial('average',3);
I4=imfilter(I,fsp);
thresh4=graythresh(I4);
bw4=im2bw(I4,thresh4);
se4=strel('line',5,90);%直线长度7,角度85
img1111=imopen(bw4,se4);%开运算
img1112=imclose(bw4,se4);%闭运算
img1113=imclose(img1111,se4);%先开后闭运算
img1114=imopen(img1112,se4);%先闭后开运算
img1115=imdilate(bw4,se4);%膨胀操作
img1116=imerode(bw4,se4);%腐蚀
figure(4);
subplot(331),imshow(I4);title('均值滤波');
subplot(332),imshow(bw4);title('均值滤波二值化');
subplot(333),imshow(img1111);title('开运算');
subplot(334),imshow(img1112);title('闭运算');
subplot(335),imshow(img1113);title('先开后闭运算');
subplot(336),imshow(img1114);title('先闭后开运算');
subplot(337),imshow(img1115);title('膨胀操作');
subplot(338),imshow(img1116);title('腐蚀');
subplot(339),imshow(bw);title('原图二值化');n
都是一些基础操作,没有牛皮的东西写。。。
3.计算灰度、面积
image1=imread('test.jpg') %图片的名字是test,格式为jpg
%方法一
image1=wiener2(image,[3,3]);%滤波
thresh1=graythresh(image1);%自动阈值
bw=im2bw(image1,thresh1);%二值化
% se=strel('line',5,90); %结构元素
% bw1=imopen(bw,se); %开运算
% bw2=imclose(bw1,se); %闭运算
figure; %打印图片
imshow(bw); %展示图片
s_white=bwarea(bw); %一个2*2的领域计算图像中像素为1的个数即白色
sum=numel(bw); %统计总像素
s_black=sum-s_white; %黑色
p= s_white/s_black;
result_area(j)=s_white;
fprintf('; s_white=%f p=%f',s_white,p);% 显示正在处理的图像名
%方法二
count_black=numel(bw,(bw==0)); %统计像素为0的个数
%*******************计算平均灰度********************%
[m,n]=size(image);
min=256;
max=0;
sumGray=0;
for i=1:n
for j=1:m
if image(i,j)<min
min=image(i,j);
end
if image(i,j)>max
max=image(i,j);
end
sumGray=sumGray+double(image(i,j));
end
end
avgGray=sumGray/(m*n);
fprintf('; avgGray=%f\n',avgGray);
end
end
4.插值、绘制圆滑曲线图,拟合曲线,曲线标点
(1)
clc;
clear;
%YC3-2品种x1
%实验使用品种时间129,222,301,310,320
%原始数据
t=[19, 29, 53, 60, 69, 79];%原始时间
y=[0, 752, 9926, 404, 240, 0];%对应像素值
% values=spcrv([[t2(1) t2 t2(end)];[y2(1) y2 y2(end)]],3); %拟原始数据的曲线图
% plot(values(1,:),values(2,:),'b');%绿色%YC3-2品种x1
%模拟的原始数据
t2=[19, 25, 29, 34, 44, 53, 63, 69, 79];%原始时间
y2=[0, 825,752,4889, 11569, 9926, 404, 100, 0];%对应像素值
t3=[19, 22, 25, 29, 34, 37, 41, 44, 47, 50, 53, 56, 60, 63, 66, 69, 72, 75, 79]; %需要插值时间的标准
%插值数据
n1=interp1(t2,y2,22,'nearest');%邻近插值
n2=interp1(t2,y2,37,'pchip'); %立方插值
n3=interp1(t2,y2,41,'pchip');
n4=interp1(t2,y2,47,'spline');%三次样条插值
n5=interp1(t2,y2,50,'spline');
n6=interp1(t2,y2,56,'spline');
n7=interp1(t2,y2,60,'spline');
n8=interp1(t2,y2,66,'linear');%线性插值
n9=interp1(t2,y2,72,'linear');
n10=interp1(t2,y2,75,'linear');
result_interpl=[0,n1,825,752,4889,n2,n3,11569,n4,n5,9926,n6,n7,404,n8,100,n9,n10,0];
values=spcrv([[t3(1) t3 t3(end)];[result_interpl(1) result_interpl result_interpl(end)]],3); %拟原始数据的曲线图
plot(values(1,:),values(2,:),'b');%绿色%YC3-2品种x1,b参数表示颜色
(2)
如果要拟合曲线函数在命令行窗口输入:cftool
选择对应项出现如下图所示:
(3)
clear;
clc;
a1 = 1.09e+04;
b1 = 54.58;
c1 = 12.67;
x=30:80;
y = a1*exp(-((x-b1)/c1).^2);
plot(x,y);
title('油菜开花时序图');
text(43,a1*exp(-((43-b1)/c1).^2),'*初花期花临界点');
text(53,a1*exp(-((53-b1)/c1).^2),'*盛花临界点');
text(60,a1*exp(-((60-b1)/c1).^2),'*终花期花临界点');
xlabel('油菜开花时间:天数')
ylabel('油菜花面积:像素')
整合了一下平常经常用到的一些基本操作,待续。。。