此程序目的是读取raw文件,并检测圆并计算圆心的位置,将位置显示在图片左上角,
对函数用法和解析和记录
常用函数
imshow
1 2
| imshow(I) imshow(I,[low high]) %可指定显示灰度范围
|
fprintf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| formatSpec = 'X is %4.2f meters or %8.3f mm\n'; fprintf(formatSpec,A1,A2); %将表格数据写入文本文件 x = 0:.1:1; A = [x; exp(x)]; fileID = fopen('exp.txt','w'); fprintf(fileID,'%6s %12s\n','x','exp(x)'); fprintf(fileID,'%6.2f %12.8f\n',A); %fprintf(fileID,'%6s %12s\r\n','x','exp(x)'); %fprintf(fileID,'%6.2f %12.8f\r\n',A); % 记事本打开格式 fclose(fileID); type exp.txt %查看文件
|
text
向数据点添加文本说明
1 2 3 4 5
| text(pi,0,'\leftarrow sin(\pi)'); text(2,8,['X:',num2str(Xmax-posX+stX-45.4144)],'Color','red','FontSize',14); text(2,27,['Y:',num2str(Ymax-posY+stY-37.1615)],'Color','red','FontSize',14); text(2,46,['YD:',num2str(YD1)],'Color','red','FontSize',14);
|
绘图相关
1 2 3 4 5 6 7
| figure('Name','Measured Data');% 设置标题名称 figure('Name','Measured Data','NumberTitle','off');% 关闭图窗编号 f1 = figure; f2 = figure; figure(f1); % 跳转f1 scatter((1:20),rand(1,20));
|
title
1 2 3 4 5 6 7 8
| title('My Title') title(['Temperature is ',num2str(c),' C']) % 包含变量 title('Case number # 3','Color', 'm') % 改变颜色 title('y = \ite^{\lambda t}','Color','b') % 使用TeX标记显示特殊符号 title('\alpha^2 and X_1') % 显示下标 title({'First line';'Second line'}) % 显示多行
|
plot
1 2 3 4
| plot(ones(201,1)*posY-stY,((posX-100):(posX+100))-stX,'g-'); plot(((posY-100):(posY+100))-stY,ones(201,1)*posX-stX,'g-'); plot(Ymax,Xmax,'r+'); plot(Ymax,Xmax,'ro');
|
'g-''r+''ro'
代表线型与颜色
hold
1 2
| hold on; %开启维持,禁止axes刷新 hold off; %关闭维持,
|
max
数组中的最大元素
M = max(A)
1.A是一维向量,返回最大值 2.A是二维数组,返回一行各列最大值
M = max(A,[],2)
返回各行最大值
[M,I] = max(A)
加上最大值的索引
B = 5;C = max(A,B)
最大元素比较
[M,I] = max(A(:))
返回全数组最大值及索引
round
1 2
| Y = round(X) % 四舍五入最近的整数 Y = round(X,N) % 保留N位小数
|
mod
b = mod(a,m) % 除后余数
reshape
B = reshape(A,[5,2]) % 重构为5x2矩阵
矩阵操作
zt = fliplr(zt); % 左右镜像
zt = flipud(zt); % 上下镜像
zt = rot90(zt,2); % 旋转两次90°
zt = zt' % 转置
fopen
打开文件返回文件标识符
fileID = fopen(filename)
fileID = fopen(filename,permission)
fileID = fopen(filename,permission,machinefmt,encodingIn)
返回文件打开出错信息
[fileID,errmsg] = fopen( ___ )
获取已打开文件信息
fIDs = fopen(‘all’)
filename = fopen(fileID)
[filename,permission,machinefmt,encodingOut] = fopen(fileID)
1 2 3 4 5 6 7 8 9
| fin = fopen('3.raw','r'); [fileID,errmsg] = fopen(filename); disp(errmsg); fileID = fopen('japanese_out.txt','w','UTF-8'); fIDs = fopen('all'); [filename,~,~,encoding] = fopen(fIDs);
|
fread
读取二进制文件中的数据
I = fread(fileID,'uint16=>uint16');
A = fread(fileID,[3 3],'double')
A = fread(fileID,'*char')';
一参文件标识符,其余为读取格式
imbinarize
转为二值图像
1 2 3
| BW = imbinarize(I) BW = imbinarize(I, 'adaptive'); %适应性阈值 BW = imbinarize(I,method)
|
regionprops
可计算二值图像上区域的质心等度量属性
计算圆形物体中心和半径
1 2 3 4 5 6
| Ipart2 = im2bw(Ipart1,0.3);%转为二值 stats = regionprops(Ipart2,'centroid','MajorAxisLength','MinorAxisLength','Eccentricity'); centers = cat(1, stats.Centroid); diameters = (cat(1,stats.MajorAxisLength)+cat(1,stats.MinorAxisLength))/2; radii = diameters/2; YD= cat(1,stats.MinorAxisLength)./cat(1,stats.MajorAxisLength);
|
或者
1 2 3
| centers = stats.Centroid; diameters = mean([stats.MajorAxisLength stats.MinorAxisLength],2); radii = diameters/2;
|
mean
取数组均值
radii
半径
YD
圆度
'MajorAxisLength','MinorAxisLength'
表示横竖两条直径
imshowpair
比较图像之间的差异
imshowpair(original, distorted, 'montage')
imshowpair(original, distorted,'ColorChannels','red-cyan')
obj = imshowpair(A,B)
obj = imshowpair(A,RA,B,RB)
obj = imshowpair(,method)
obj = imshowpair(,Name,Value)
上一篇:安装OpenCV扩展库contrib
下一篇:QImage与Mat的相互转换