%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%测量交角(线性拟合的思想)
%Step 1: Load image %Step 2: Extract the region of
interest %Step 3: Threshold the image %Step 4: Find initial point on each
boundary %Step 5: Trace the boundaries %Step 6: Fit lines to the boundaries %Step 7: Find the angle of
intersection %Step 8: Find the point of
intersection %Step 9: Plot the results.
clc;clear;close all;
%Step 1: Load image RGB = imread('gantrycrane.png');
imshow(RGB);
%Step 2: Extract the region of interest
% pixel information displayed by imview
start_row = 34;
start_col = 208;
cropRGB = RGB(start_row:163, start_col:400, :);
figure, imshow(cropRGB)
% Store (X,Y) offsets for later use; subtract 1 so that each offset
will
% correspond to the last pixel before the region of interest
offsetX = start_col-1;
offsetY = start_row-1;
%Step 3: Threshold the image
I = rgb2gray(cropRGB);
threshold = graythresh(I);
BW = im2bw(I,threshold);
BW = ~BW; %
complement the image (objects of interest must be white)
figure, imshow(BW)
%Step 4: Find initial point on each
boundary dim = size(BW);
% horizontal beam
col1 = 4;
row1 = min(find(BW(:,col1)));
% angled beam
row2 = 12;
col2 = min(find(BW(row2,:)));
%Step 5: Trace the boundaries boundary1 = bwtraceboundary(BW, [row1, col1], 'N', 8, 70);
% set the search direction to counterclockwise, in order to trace
downward.
boundary2 = bwtraceboundary(BW, [row2, col2], 'E', 8,
90,'counter');
figure, imshow(RGB); hold on;
% apply offsets in order to draw in the original image
plot(offsetX+boundary1(:,2),offsetY+boundary1(:,1),'g','LineWidth',2);
plot(offsetX+boundary2(:,2),offsetY+boundary2(:,1),'g','LineWidth',2);
%Step 6: Fit lines to the boundaries ab1 = polyfit(boundary1(:,2), boundary1(:,1), 1);
ab2 = polyfit(boundary2(:,2), boundary2(:,1), 1);
%Step 7: Find the angle of
intersection vect1 = [1 ab1(1)]; % create a vector based on the line
equation
vect2 = [1 ab2(1)];
dp = dot(vect1, vect2);
% compute vector lengths
length1 = sqrt(sum(vect1.^2));
length2 = sqrt(sum(vect2.^2));
% obtain the larger angle of intersection in degrees
angle = 180-acos(dp/(length1*length2))*180/pi
%Step 8: Find the point of
intersection intersection = [1 ,-ab1(1); 1, -ab2(1)] [ab1(2); ab2(2)];
% apply offsets in order to compute the location in the
original,
% i.e. not cropped, image.
intersection = intersection + [offsetY; offsetX]
%Step 9: Plot the results.
inter_x = intersection(2);
inter_y = intersection(1);
% draw an "X" at the point of intersection
plot(inter_x,inter_y,'yx','LineWidth',2);
text(inter_x-60, inter_y-30,
[sprintf('%1.3f',angle),'{circ}'],...
'Color','y','FontSize',14,'FontWeight','bold');
interString = sprintf('(%2.1f,%2.1f)', inter_x, inter_y);
text(inter_x-10, inter_y+20, interString,...
'Color','y','FontSize',14,'FontWeight','bold');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%检测一段圆弧的半径(圆形拟合)
%Step 1: Read image %Step 2: Threshold the image %Step 3: Extract initial boundary point
location %Step 4: Trace the boundaries %Step 5: Fit a circle to the boundary
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
06.6.23基于特征的与运算
>> load imdemos dots box
>> imshow(dots)
>> figure,imshow(box)
>>
logical_and=box&dots;
>> imshow(logical_and);
>> [r,c]=find(logical_and);
>>
feature_and=bwselect(dots,c,r);
>> figure,imshow(feature_and);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%利用逻辑运算,提取含有细胞核的细胞
load imdemos bacteria;
imshow(bacteria);
bact_bw=(bacteria>=100);
%figure,imshow(bact_bw);
bact_bw=~bact_bw;
figure,imshow(bact_bw); %直接二值取反
figure,imshow(bact_bw)
%细胞和细胞核重叠在一起,无法区分
%进行Laplace算子滤波,突出核与胞的区别
filtered=filter2(fspecial('laplacian'),bacteria);
>>
figure,imshow(filtered) %先拉氏滤波
>>
bact_granules=(filtered>-4);
figure,imshow(bact_granules); %“-4”运算
bact_granules=bact_granules&bact_bw; %特征与运算
figure,imshow(bact_granules);
erode_bw=erode(bact_bw); %
%figure,imshow(erode_bw);
nobord=imclearborder(erode_bw); %
fill_bw=imfill(nobord,'holes');
figure,imshow(fill_bw);
%fill_bw=bwfill(erode_bw,'holes');
%figure,imshow(fill_bw);
bact_granules_0=(bact_granules==0); %判0运算的结果,等价与
figure,imshow(bact_granules_0); %下边的取反运算!
�ct_granules_anti=~bact_granules;
%figure,imshow(bact_granules_anti);
>>
granules=fill_bw&bact_granules_0;
%尽可能多地把“核”提取出来
>> figure,imshow(granules)
>> [r,c]=find(granules);
>>
result=bwselect(bact_bw,c,r);
>> figure,imshow(bacteria);
>> figure,imshow(result)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
06.7.15
%按顺序标记每一个连通区域
clc;clear;close all;
%Step 1: Read image %Step 2: Threshold the image %Step 3: Remove the noise %Step 4: Find the boundaries %Step 5: Determine which objects are round
>> RGB =
imread('pillsetc.png');
>> imshow(RGB)
>> I = rgb2gray(RGB);
>> threshold = graythresh(I);
>> bw = im2bw(I,threshold);
>> bw = bwareaopen(bw,30);
>> se = strel('disk',2);
>> bw = imclose(bw,se);
>> bw = imfill(bw,'holes');
>> [B,L] =
bwboundaries(bw,'noholes');
>> figure,imshow(label2rgb(L, @jet,
[.5 .5 .5]))
>> hold on
>> data=regionprops(L,'all');
>> centr=[data.Centroid]; %标记在重心上
>>
nums=1:length(B); %需要标记的物体个数
for k = 1:length(B)
boundary =
B{k};
plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2)
signal=num2str(nums(k));
text(centr(2*k-1),centr(2*k),signal) %按序标记物体
end
for k=1:length(B) %画出每个区域的最小凸边形
plot(data(k).ConvexHull(:,1),data(k).ConvexHull(:,2),'b','LineWidth',2)
end
>> stats =
regionprops(L,'Area','Centroid');
>> stats =
regionprops(L,'Area','Centroid');
threshold = 0.94;
for k = 1:length(B)
boundary =
B{k};
delta_sq =
diff(boundary).^2; perimeter =
sum(sqrt(sum(delta_sq,2)));
area =
stats(k).Area;
metric =
4*pi*area/perimeter^2;
metric_string =
sprintf('%2.2f',metric);
if metric
> threshold
centroid = stats(k).Centroid;
plot(centroid(1),centroid(2),'ko');
end
text(boundary(1,2)-35,boundary(1,1)+13,metric_string,'Color','y',...
'FontSize',14,'FontWeight','bold');
end
>> title(['Metrics closer to 1
indicate that ',...
'the object is approximately round']);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
06.7.17
%果穗梗、花椒籽分离
%d:matlab7toolboximagesimagestruesize.m*(308行)
clear;close all;clc
>>
i=imread('huaj_600_rgb.tif');
>> figure,imshow(i);
>> ig=rgb2gray(i);
>> imed=medfilt2(ig);
>>
imedcanny=edge(imed,'canny');
>> se90=strel('line',2,90);
se0=strel('line',2,0);
bwsdil=imdilate(imedcanny,[se90 se0]);
%>> figure,imshow(bwsdil)
>>
ifill=imfill(bwsdil,'holes');
>> bwero=imerode(ifill,[se90
se0]);
>> nosmall=bwareaopen(bwero,3000,4);
>>
nobord=imclearborder(nosmall,4);
figure,imshow(nobord);
>>
[labeled,numobjects]=bwlabel(nobord,4);
rgb_label=label2rgb(labeled,@spring,'c','shuffle');
figure,imshow(rgb_label);
mexdata=regionprops(labeled,'all');
hold on;
centr=[mexdata.Centroid]; %寻找重心位置
nums=1:numobjects;
for k = 1:numobjects
soli=mexdata(k).Solidity;
soli_string=sprintf('%2.2f',soli); %等价于转字符串
% signal=num2str(nums(k));
signal=sprintf('%d',k); %直接使用打印语句打印序号
text(centr(2*k-1),centr(2*k),signal) %按序标记物体
text(centr(2*k-1)-30,centr(2*k)-30,soli_string)
%标注每个Solidity值
end
%画最小凸多边形
>> for k=1:numobjects
plot(mexdata(k).ConvexHull(:,1),mexdata(k).ConvexHull(:,2),...
'b','Linewidth',2)
end
hold off;
%作出Solidity值的分布图,以之选定阈值
>>
figure,hist([mexdata.Solidity],255)
%只显示Solidity>0.92的物体的图像
>> idx = find([mexdata.Solidity]
> 0.95);
>> nut = ismember(labeled,idx);
>> figure,imshow(nut)
>>
[labnut,numnut]=bwlabel(nut,4);
>> numnut %数出花椒籽的粒数
>>
%只显示Solidity<0.92的物体的图像
idx = find([mexdata.Solidity] < 0.75);
peduncle = ismember(labeled,idx);
figure,imshow(peduncle)
>>
[labped,numped]=bwlabel(peduncle,4);
>> numped %数出含果穗梗的花椒数
>> %只显示无果穗梗的花椒图像
idx =
find([mexdata.Solidity]>=0.75&[mexdata.Solidity]<=0.95);
pure = ismember(labeled,idx);
figure,imshow(pure)
>>
[labpure,numpure]=bwlabel(pure,4);
>> numpure %纯净花椒的粒数
%分别对籽,皮,梗使用regionprops函数
>>
nutdata=regionprops(labnut,'all');
>>
peddata=regionprops(labped,'all');
>>
puredata=regionprops(labpure,'all');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%封装成一个函数
function huaj();
%对花椒图像进行处理
imname=input('input an image name:','s');
i=imread(imname);
figure,imshow(i);
ig=rgb2gray(i);
imed=medfilt2(ig);
imedcanny=edge(imed,'canny');
se90=strel('line',2,90);
se0=strel('line',2,0);
bwsdil=imdilate(imedcanny,[se90 se0]);
%figure,imshow(bwsdil)
ifill=imfill(bwsdil,'holes');
bwero=imerode(ifill,[se90 se0]);
nosmall=bwareaopen(bwero,3000,4);
nobord=imclearborder(nosmall,4);
figure,imshow(nobord);
[labeled,numobjects]=bwlabel(nobord,4);
rgb_label=label2rgb(labeled,@spring,'c','shuffle');
figure,imshow(rgb_label);
mexdata=regionprops(labeled,'all');
hold on;
centr=[mexdata.Centroid]; %寻找重心位置
nums=1:numobjects;
for k = 1:numobjects
soli=mexdata(k).Solidity;
soli_string=sprintf('%2.2f',soli); %等价于转字符串
% signal=num2str(nums(k));
signal=sprintf('%d',k); %直接使用打印语句打印序号
text(centr(2*k-1),centr(2*k),signal) %按序标记物体
text(centr(2*k-1)-30,centr(2*k)-30,soli_string)
%标注每个Solidity值
end
%画最小凸多边形
for k=1:numobjects
plot(mexdata(k).ConvexHull(:,1),mexdata(k).ConvexHull(:,2),...
'b','Linewidth',2)
end
hold off;
%作出Solidity值的分布图,以之选定阈值
figure,hist([mexdata.Solidity],255)
%只显示Solidity>0.92的物体的图像
idx = find([mexdata.Solidity] > 0.92);
nut = ismember(labeled,idx);
figure,imshow(nut)
[labnut,numnut]=bwlabel(nut,4);
numnut %数出花椒籽的粒数
%只显示Solidity<0.75的果穗梗图像
idx = find([mexdata.Solidity] < 0.75);
peduncle = ismember(labeled,idx);
figure,imshow(peduncle)
[labped,numped]=bwlabel(peduncle,4);
numped %数出含果穗梗的花椒数
%只显示无果穗梗的花椒果皮图像
idx =
find([mexdata.Solidity]>=0.75&[mexdata.Solidity]<=0.92);
pure = ismember(labeled,idx);
figure,imshow(pure)
[labpure,numpure]=bwlabel(pure,4);
numpure %纯净花椒的粒数
%分别对籽,皮,梗使用regionprops函数
nutdata=regionprops(labnut,'all');
peddata=regionprops(labped,'all');
puredata=regionprops(labpure,'all');
%封装成一个函数
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
06.7.18
%命令文件
%rice_1.m
>> i=imread('rice.png');
>> imshow(i);
>>
background=imopen(i,strel('disk',15));
>> i2=imsubtract(i,background);
>> figure,imshow(i2);
>> i3=imadjust(i2,stretchlim(i2),[0
1]);
>> figure,imshow(i3);
>> level=graythresh(i3);
>> bw=im2bw(i3,level);
>> figure,imshow(bw);
>>
[labeled,numobjects]=bwlabel(bw,4);
%函数文件
%rice.m
function y=rice;
str=input('enter a image name:','s');
i=imread(str);
imshow(i);
background=imopen(i,strel('disk',15));
i2=imsubtract(i,background);
figure,imshow(i2);
i3=imadjust(i2,stretchlim(i2),[0 1]);
figure,imshow(i3);
level=graythresh(i3);
bw=im2bw(i3,level);
figure,imshow(bw);
[labeled,numobjects]=bwlabel(bw,4);
label_rgb=label2rgb(labeled,@spring,'c','shuffle');
y=label_rgb;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%用if-else-end结构来控制循环,使之跳出或中断for或while循环
a(1)=1;a(2)=2;
for i=2:50
a(i+1)=a(i-1)+a(i);
i=i+1;
if
a(i)>10000 %查找第一个大于10000的元素
break %用if语句控制for循环
end
end
i,a(i)
%用递归调用形式计算n!
%(1)编写递归调用函数文件factor.m
function f=factor(n);
�ctor.m 计算n!
if n==1
f=1;
return;
else
f=n*factor(n-1);
return;
end
%(2)运行函数文件
factor(6)
%函数参数传递
function sa=circle(r,s);
%CIRCLE PLOT A CIRCLE OF RADIUS R IN THE LINE SPECIFIED BY S.
%r
%s
%sa
%
%circle(r)
%circle(r,s)
%sa=circle(r)
%sa=circle(r,s)
if nargin==1
%若输入参数数目为1,则该输入参数是指定的半径数值,
%默认的圆周线颜色是蓝色
s='b';
end;
clf;
t=0:pi/100:2*pi;
x=r*exp(i*t);
if nargout==0
plot(x,s);
else
sa=pi*r*r;
fill(real(x),imag(x),s)
end
axis('square')