matlab实验问题

img


使用matlab 根据图中要求 进行数据拟合并预测人口数量 完成实验

(1)问题分析
从图中摘取数据之后,可以先通过matlab的polyfit函数分别得到二次、三次、四次多项式的最小二乘拟合系数,再通过polyval计算1978年到2021年的人口预测值,从而计算拟合误差,最后通过拟合误差最小的拟合多项式预测2022年的人口数量。PS. 问题中2022年的人口数量有误,根据国家统计局数据,2022年全国人口141175万人。
(2)问题的matlab解决代码

%Load data
data=[1978, 96259, 18.25, 6.25, 12.00, 0;
1979, 97542, 17.82, 6.21, 11.61, 1283;
1980, 98705, 18.21, 6.34, 11.87, 1163;
1981, 100072, 20.91, 6.36, 14.55, 1367;
1982, 101654, 22.28, 6.60, 15.68, 1582;
1983, 103008, 20.19, 6.90, 13.29, 1354;
1984, 104357, 19.90, 6.82, 13.08, 1349;
1985, 105851, 21.04, 6.78, 14.26, 1494;
1986, 107507, 22.43, 6.86, 15.57, 1656;
1987, 109300, 23.33, 6.72, 16.61, 1793;
1988, 111026, 22.37, 6.64, 15.73, 1726;
1989, 112704, 21.58, 6.54, 15.04, 1678;
1990, 114333, 21.06, 6.67, 14.39, 1629;
1991, 115823, 19.68, 6.70, 12.98, 1490;
1992, 117171, 18.24, 6.64, 11.60, 1348;
1993, 118517, 18.09, 6.64, 11.45, 1346;
1994, 119850, 17.70, 6.49, 11.21, 1333;
1995, 121121, 17.12, 6.57, 10.55, 1271;
1996, 122389, 16.98, 6.56, 10.42, 1268;
1997, 123626, 16.57, 6.51, 10.06, 1237;
1998, 124761, 15.64, 6.50, 9.14, 1135;
1999, 125786, 14.64, 6.46, 8.18, 1025;
2000, 126743, 14.03, 6.45, 7.58, 957;
2001, 127627, 13.38, 6.43, 6.95, 884;
2002, 128453, 12.86, 6.41, 6.45, 826;
2003, 129227, 12.41, 6.40, 6.01, 774;
2004, 129988, 12.29, 6.42, 5.87, 761;
2005, 130756, 12.40, 6.51, 5.89, 768;
2006, 131448, 12.09, 6.81, 5.28, 692;
2007, 132129, 12.10, 6.93, 5.17, 681;
2008, 132802, 12.14, 7.06, 5.08, 673;
2009, 133450, 11.95, 7.08, 4.87, 648;
2010, 134091, 11.90, 7.11, 4.79, 641;
2011, 134916, 13.27, 7.14, 6.13, 825;
2012, 135922, 14.57, 7.13, 7.43, 1006;
2013, 136726, 13.03, 7.13, 5.90, 804;
2014, 137646, 13.83, 7.12, 6.71, 920;
2015, 138326, 11.99, 7.07, 4.93, 680;
2016, 139232, 13.57, 7.04, 6.53, 906;
2017, 140011, 12.64, 7.06, 5.58, 779;
2018, 140541, 10.86, 7.08, 3.78, 530;
2019, 141008, 10.41, 7.09, 3.32, 467;
2020, 141212, 8.52, 7.07, 1.45, 204;
2021, 141260, 7.52, 7.18, 0.34, 48];

%Do polynomial fitting 
warning off;
p2=polyfit(data(:,1),data(:,2),2);
p3=polyfit(data(:,1),data(:,2),3);
p4=polyfit(data(:,1),data(:,2),4);
warning on;

%Calculate error
p2_predict=polyval(p2,data(:,1));
p3_predict=polyval(p3,data(:,1));
p4_predict=polyval(p4,data(:,1));
p2_error=sum((p2_predict-data(:,2)).^2);
p3_error=sum((p3_predict-data(:,2)).^2);
p4_error=sum((p4_predict-data(:,2)).^2);
fprintf('2次、3次、4次多项式的拟合偏差平方和分别为:%d, %d, %d\n',...
    p2_error,p3_error,p4_error);

%Predict the population size of 2022
error_vec=[p2_error,p3_error,p4_error];
[~,min_error_ind]=min(error_vec);
switch min_error_ind
    case 1
        fprintf('最佳拟合多项式是2次。\n');
        population_2022=polyval(p2,2022);
    case 2
        fprintf('最佳拟合多项式是3次。\n');
        population_2022=polyval(p3,2022);
    case 3
        fprintf('最佳拟合多项式是4次。\n');
        population_2022=polyval(p4,2022);
end
fprintf('根据最佳拟合多项式预测的2022年人口数量为:%d万人\n',int32(population_2022));

(3)代码运行结果

img

你发可以复制的内容吧,这种截图的估计没人会弄。

  • 这有个类似的问题, 你可以参考下: https://ask.csdn.net/questions/7515029
  • 这篇博客你也可以参考下:matlab在循环时如何跳过几个数,matlab如何得到一个数组的行数和列数, matlab判断数组的长度
  • 你还可以看下matlab参考手册中的 matlab 访问和更改 MAT 文件中的变量,而不必将文件加载到内存中 matfile
  • 除此之外, 这篇博客: matlab图像处理实现简单机器视觉中的 使用matlab对图像进行简单处理并分析不同处理方法的特点 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 对不同曝光程度的图像进行均衡化处理

    数据代码段

    %直方图均衡化
    figure;
    srcimage=imread('C:\Users\27019\Desktop\机器视觉\图1-2.jpg');
    info=imfinfo('C:\Users\27019\Desktop\机器视觉\图1-2.jpg');
    subplot(2,3,1);
    imshow(srcimage);
    title('原灰度图像');
    subplot(2,3,2);
    imhist(srcimage);
    title('灰度直方图');
    subplot(2,3,3);
    H1=histeq(srcimage);
    imhist(H1);
    title('直方图均衡化');
    subplot(2,3,4);
    histeq(H1);
    title('均衡化处理后的图像');
    

    figure;
    srcimage=imread(‘C:\Users\27019\Desktop\机器视觉\图1-2.jpg’);
    info=imfinfo(‘C:\Users\27019\Desktop\机器视觉\图1-2.jpg’);
    subplot(2,3,1);
    imshow(srcimage);
    title(‘原灰度图像’);
    subplot(2,3,2);
    imhist(srcimage);
    title(‘灰度直方图’);
    subplot(2,3,3);
    H1=histeq(srcimage);
    imhist(H1);
    title(‘直方图均衡化’);
    subplot(2,3,4);
    histeq(H1);
    title(‘均衡化处理后的图像’);

    分别处理以下三幅图像
    图1-1
    图1-2
    图1-3
    这三幅图像分别是灰度范围分布较大,过度曝光和欠曝光的图像。

    处理结果如下
    图1-1的处理结果
    图1-2的处理结果
    图1-3的处理结果
    从图像处理的结果来看,主要分别是:过度曝光的图像的灰度直方图大部分集中在灰度值较高的区域,欠曝光的图像的灰度直方图主要集中在灰度值较低的区域,均衡化处理后的直方图分布较为均匀,且图像的亮度较为适中。经过处理后的图像边缘效果更好,图像的信息更明显,达到了增强图像的效果。

    对图像进行去噪声处理
    使用不同的方法对图像进行去噪声处理,得出各种处理方法的特点。
    图1-4

    1. 空间域模板卷积(不同模板、不同尺寸)
    2. 频域低通滤波器(不同滤波模型、不同截止频率)
    3. 中值滤波方法(不同窗口)
    4. 均值滤波方法(不同窗口)
      备注:对不同方法和同一方法的不同参数的实验结果进行分析和比较,如空间域卷积模板可有高斯型模板、矩形模板、三角形模板和自己根据需求设计的模板等;模板大小可以是3×3,5×5,7×7或更大。频域滤波可采用矩形或巴特沃斯等低通滤波器模型,截止频率也是可选的。
      数据代码段
    %空间域模板卷积
    clc
    clear all %清屏
    b=imread('C:\Users\27019\Desktop\机器视觉\图1-4.jpg'); %导入图像
    a1=double(b)/255;
    figure;
    subplot(5,3,1),imshow(a1);
    title('原图像');
     
    Q=ordfilt2(b,6,ones(3,3));%二维统计顺序滤波
    subplot(5,3,4),imshow(Q);
    title('二维顺序滤波,窗口为3')
     
    Q=ordfilt2(b,6,ones(5,5));%二维统计顺序滤波
    subplot(5,3,5),imshow(Q);
    title('二维顺序滤波,窗口为5')
     
    Q=ordfilt2(b,6,ones(7,7));%二维统计顺序滤波
    subplot(5,3,6),imshow(Q);
    title('二维顺序滤波,窗口为7')
     
    m=medfilt2(b,[3,3]);%中值滤波
    subplot(5,3,7),imshow(m);
    title('中值滤波,窗口为3')
     
    m=medfilt2(b,[5,5]);%中值滤波
    subplot(5,3,8),imshow(m);
    title('中值滤波,窗口为5')
     
    m=medfilt2(b,[7,7]);%中值滤波
    subplot(5,3,9),imshow(m);
    title('中值滤波,窗口为7')
     
    x=1/16*[0 1 1 1;1 1 1 1;1 1 1 1;1 1 1 0];
    a=filter2(x,a1);%邻域滤波*16
    subplot(5,3,10),imshow(a);
    title('邻域滤波*16')
     
    x=1/32*[0 1 1 1;1 1 1 1;1 1 1 1;1 1 1 0];
    a=filter2(x,a1);%邻域滤波*32
    subplot(5,3,11),imshow(a);
    title('邻域滤波*32')
     
    h = fspecial('average',[3,3]);
    A=filter2(h,a1)
    subplot(5,3,13),imshow(A);
    title('均值滤波,窗口为3')
     
    h = fspecial('average',[5,5]);
    A=filter2(h,a1)
    subplot(5,3,14),imshow(A);
    title('均值滤波,窗口为5')
     
    h = fspecial('average',[7,7]);
    A=filter2(h,a1)
    subplot(5,3,15),imshow(A);
    title('均值滤波,窗口为7')
    %高斯模板
    figure;
    A=imread('C:\Users\27019\Desktop\机器视觉\图1-4.jpg');
    subplot(2,3,1);
    imshow(A);
    title('原始图像');
    p=[3,3];
    h=fspecial('gaussian',p);
    B1=filter2(h,A)/255;
    subplot(2,3,2);
    imshow(B1);
    title('高斯模板,窗口为3');
    p=[5,5];
    h=fspecial('gaussian',p);
    B2=filter2(h,A)/255;
    subplot(2,3,3);
    imshow(B2);
    title('高斯模板,窗口为5');
    p=[7,7];
    h=fspecial('gaussian',p);
    B3=filter2(h,A)/255;
    subplot(2,3,4);
    imshow(B3);
    title('高斯模板,窗口为7');
    %均值模板
    figure;
    A=imread('C:\Users\27019\Desktop\机器视觉\图1-4.jpg');
    subplot(2,3,1);
    imshow(A);
    title('原始图像');
    B1=filter2(fspecial('average',3),A)/255;
    subplot(2,3,2);
    imshow(B1);
    title('均值滤波,窗口为3');
    B2=filter2(fspecial('average',5),A)/255;
    subplot(2,3,3);
    imshow(B2);
    title('均值滤波,窗口为5');
    B3=filter2(fspecial('average',7),A)/255;
    subplot(2,3,4);
    imshow(B3);
    title('均值滤波,窗口为7');
    %中值模板
    figure;
    A=imread('C:\Users\27019\Desktop\机器视觉\图1-4.jpg');
    subplot(2,3,1);
    imshow(A);
    title('原始图像');
    B0=medfilt2(A);
    subplot(2,3,2);
    imshow(B0);
    title('中值滤波,窗口为[3,3]');
    p=[5,5];
    B1=medfilt2(A,p);
    subplot(2,3,3);
    imshow(B1);
    title('中值滤波,窗口为[5,5]');
    p=[7,7];
    B2=medfilt2(A,p);
    subplot(2,3,4);
    imshow(B2);
    title('中值滤波,窗口为[7,7]');
    

    %空间域模板卷积
    clc
    clear all %清屏
    b=imread(‘C:\Users\27019\Desktop\机器视觉\图1-4.jpg’); %导入图像
    a1=double(b)/255;
    figure;
    subplot(5,3,1),imshow(a1);
    title(‘原图像’);

    Q=ordfilt2(b,6,ones(3,3));%二维统计顺序滤波
    subplot(5,3,4),imshow(Q);
    title(‘二维顺序滤波,窗口为3’)

    Q=ordfilt2(b,6,ones(5,5));%二维统计顺序滤波
    subplot(5,3,5),imshow(Q);
    title(‘二维顺序滤波,窗口为5’)

    Q=ordfilt2(b,6,ones(7,7));%二维统计顺序滤波
    subplot(5,3,6),imshow(Q);
    title(‘二维顺序滤波,窗口为7’)

    m=medfilt2(b,[3,3]);%中值滤波
    subplot(5,3,7),imshow(m);
    title(‘中值滤波,窗口为3’)

    m=medfilt2(b,[5,5]);%中值滤波
    subplot(5,3,8),imshow(m);
    title(‘中值滤波,窗口为5’)

    m=medfilt2(b,[7,7]);%中值滤波
    subplot(5,3,9),imshow(m);
    title(‘中值滤波,窗口为7’)

    x=1/16*[0 1 1 1;1 1 1 1;1 1 1 1;1 1 1 0];
    a=filter2(x,a1);%邻域滤波16
    subplot(5,3,10),imshow(a);
    title('邻域滤波
    16’)

    x=1/32*[0 1 1 1;1 1 1 1;1 1 1 1;1 1 1 0];
    a=filter2(x,a1);%邻域滤波32
    subplot(5,3,11),imshow(a);
    title('邻域滤波
    32’)

    h = fspecial(‘average’,[3,3]);
    A=filter2(h,a1)
    subplot(5,3,13),imshow(A);
    title(‘均值滤波,窗口为3’)

    h = fspecial(‘average’,[5,5]);
    A=filter2(h,a1)
    subplot(5,3,14),imshow(A);
    title(‘均值滤波,窗口为5’)

    h = fspecial(‘average’,[7,7]);
    A=filter2(h,a1)
    subplot(5,3,15),imshow(A);
    title(‘均值滤波,窗口为7’)
    %高斯模板
    figure;
    A=imread(‘C:\Users\27019\Desktop\机器视觉\图1-4.jpg’);
    subplot(2,3,1);
    imshow(A);
    title(‘原始图像’);
    p=[3,3];
    h=fspecial(‘gaussian’,p);
    B1=filter2(h,A)/255;
    subplot(2,3,2);
    imshow(B1);
    title(‘高斯模板,窗口为3’);
    p=[5,5];
    h=fspecial(‘gaussian’,p);
    B2=filter2(h,A)/255;
    subplot(2,3,3);
    imshow(B2);
    title(‘高斯模板,窗口为5’);
    p=[7,7];
    h=fspecial(‘gaussian’,p);
    B3=filter2(h,A)/255;
    subplot(2,3,4);
    imshow(B3);
    title(‘高斯模板,窗口为7’);
    %均值模板
    figure;
    A=imread(‘C:\Users\27019\Desktop\机器视觉\图1-4.jpg’);
    subplot(2,3,1);
    imshow(A);
    title(‘原始图像’);
    B1=filter2(fspecial(‘average’,3),A)/255;
    subplot(2,3,2);
    imshow(B1);
    title(‘均值滤波,窗口为3’);
    B2=filter2(fspecial(‘average’,5),A)/255;
    subplot(2,3,3);
    imshow(B2);
    title(‘均值滤波,窗口为5’);
    B3=filter2(fspecial(‘average’,7),A)/255;
    subplot(2,3,4);
    imshow(B3);
    title(‘均值滤波,窗口为7’);
    %中值模板
    figure;
    A=imread(‘C:\Users\27019\Desktop\机器视觉\图1-4.jpg’);
    subplot(2,3,1);
    imshow(A);
    title(‘原始图像’);
    B0=medfilt2(A);
    subplot(2,3,2);
    imshow(B0);
    title(‘中值滤波,窗口为[3,3]’);
    p=[5,5];
    B1=medfilt2(A,p);
    subplot(2,3,3);
    imshow(B1);
    title(‘中值滤波,窗口为[5,5]’);
    p=[7,7];
    B2=medfilt2(A,p);
    subplot(2,3,4);
    imshow(B2);
    title(‘中值滤波,窗口为[7,7]’);
    处理结果如下
    在这里插入图片描述
    在这里插入图片描述
    高斯滤波器模型
    使用高斯模板滤波得到的图像相比原图像更为清晰,达到了去除噪声的效果。同时当高斯模板的窗口越大,得到的图像会越模糊。
    在这里插入图片描述
    中值滤波方法,窗口为3,5,7
    中值滤波原理是使用邻域的中值来代替像素原来的值,得到的图像比较清晰。但是使用的中值滤波方法的窗口越大所得到的图像会越模糊。
    在这里插入图片描述
    均值滤波方法,窗口为3,5,7
    均值滤波可以看出对颗粒的噪声有很好的过滤作用,采用的是使用邻域的均值代替该像素的值。在均值滤波的方法种,窗口越大得到的图像越模糊。

    以下几种处理方式在后方处理中重复试用,此处避免累述,只显示结果,分析特点,避免累述

    在这里插入图片描述
    巴特沃斯滤波器模型
    在这里插入图片描述
    sobel模板
    Sobel模板水平和竖直方向上的去噪作用均能达到获取图像的边缘的作用。
    在这里插入图片描述
    Prewitt模板
    Prewitt得到的图像都有突出图像边缘的作用。

    图像去噪声结果分析

    1. 直方图均衡化对哪一类图像增强效果最明显?为什么均衡化后直方图并不平坦?
    2. 不同空间域卷积器模板的滤波效果有何不同?
    3. 空间域卷积器模板的大小的滤波效果有何影响?
    4. 不同频域滤波器的效果有何不同?

    回答:

    1. 对灰度范围分布区间较大的图像的增强效果最明显。这是因为在离散情况下由于灰度取值的离散性不可能把取同一个灰度值的像素变换到不同的像素。也就是说通过灰度直方图均衡化变换函数获得的带有小数的不同灰度值四舍五人取整后会出现归并现象。所以实际应用中就不可能得到完全平坦的直方图但结果图像的灰度直方图相比于原图像直方图要平坦得多。
    2. 效果最好的是均值滤波,最差的是二维顺序滤波。均值滤波得到的图像比较平滑,而二维顺序滤波得到的图像中还有较多的噪点。中值滤波的图像中仍有一些噪点,领域滤波的图像中亮度偏低。
    3. 模板越大,图像越模糊,而模板越小,图像越清晰。
    4. 不同的滤波器得到的图像的滤过的信息不同。

    获取内圆,计算距离
    图中一个I-kid机器人A在足球场上拍摄到另一个I-kid机器人B的图片。请首先利用特征提取算法提取地面半径为4.37m的内圆边缘,然后利用所学的摄像机投影模型求解此刻机器人A距离内圆圆心的距离||OA||。(假设机器人A的摄像机被垂直安装,光心位于机器人的中心线上,且其光心距离球场地面为0.5m;同时摄像机的分辨率为640*480像素,主点在其图像中心;摄像机的横向有效焦距为300像素,纵向有效焦距有1200像素)

    1. 利用Matlab图像工具箱中函数提取图2中球场标记线的内圆边缘。
    2. 已知图3是球场标志线及机器人A所处大致位置,请利用摄像机线性投影模型计算机器人A离内圆圆心点O的距离||OA||。
      在这里插入图片描述在这里插入图片描述
      备注:将机器人A的摄像机坐标系先在垂直方向下移0.5m至机器人A的足下,然后平移至圆心点O。从机器人足下平移到圆心O的距离就是待求的距离||OA||。
      数据代码段
    %Prewitt模板
    figure;
    A=imread('C:\Users\27019\Desktop\机器视觉\图2.jpg');
    subplot(2,3,1);
    imshow(A);
    title('原始图像');
    h=[1 1 1;0 0 0;-1 -1 -1];
    B1=filter2(h,A)/255;
    subplot(2,3,2);
    imshow(B1);
    title('Prewitt模板,水平方向');
    h=[1 0 -1;1 0 -1;1 0 -1];
    B2=filter2(h,A)/255;
    subplot(2,3,3)
    imshow(B2);
    title('Prewitt模板,垂直方向');
    %Sobel模板
    A=imread('C:\Users\27019\Desktop\机器视觉\图2.jpg');
    subplot(2,3,1);
    imshow(A);
    title('原始图像');
    h=[1 2 1;0 0 0;-1 -2 -1];
    B1=filter2(h,A)/255;
    subplot(2,3,2);
    imshow(B1);
    title('Prewitt模板,水平方向');
    h=[1 0 -1;2 0 -2;1 0 -1];
    B2=filter2(h,A)/255;
    subplot(2,3,3)
    imshow(B2);
    title('Prewitt模板,垂直方向');
    %低通滤波器
    a=imread('C:\Users\27019\Desktop\机器视觉\图1-4.jpg');%读入图像
    figure;
    subplot(2,4,1);
    imshow(a);
    z=double(a)/255;
    b=fft2(double(a));%对图片进行傅里叶变换
    c=log(1+abs(b));
    d=fftshift(b);%将变换的原点调整到频率矩阵的中心
    e=log(1+abs(d));
    %代入巴特沃斯公式进行滤波处理
    [m,n]=size(d);
    for i=1:256
        for j=1:256
            d1(i,j)=(1/(1+((i-128)^2+(j-128)^2)^0.5/500)^2)*d(i,j);%截至频率
        end;
    end;
    for i=1:256
        for j=1:256
            d2(i,j)=(1/(1+((i-128)^2+(j-128)^2)^0.5/1000)^2)*d(i,j);
        end;
    end;
    for i=1:256
        for j=1:256
            d3(i,j)=(1/(1+((i-128)^2+(j-128)^2)^0.5/2000)^2)*d(i,j);
        end;
    end;
    for i=1:256
        for j=1:256
            d4(i,j)=(1/(1+((i-128)^2+(j-128)^2)^0.5/4000)^2)*d(i,j);
        end;
    end;
    FF1=ifftshift(d1);
    FF2=ifftshift(d2);
    FF3=ifftshift(d3);
    FF4=ifftshift(d4);
    ff1=real(ifft2(FF1));%取傅里叶反变换
    ff2=real(ifft2(FF2));
    ff3=real(ifft2(FF3));
    ff4=real(ifft2(FF4));
    subplot(2,4,5);imshow(uint8(ff1));xlabel('截止频率为500');
    subplot(2,4,6);imshow(uint8(ff2));xlabel('截止频率为1000');
    subplot(2,4,7);imshow(uint8(ff3));xlabel('截止频率为2000');
    subplot(2,4,8);imshow(uint8(ff4));xlabel('截止频率为4000');
    %不同算子
    clear;clc;
    close all;
    I=imread('C:\Users\27019\Desktop\机器视觉\图2.jpg');
    imshow(I,[]);
    title('Original Image');
     
    sobelBW=edge(I,'sobel');
    figure;
    imshow(sobelBW);
    title('Sobel Edge');
     
    robertsBW=edge(I,'roberts');
    figure;
    imshow(robertsBW);
    title('Roberts Edge');
     
    prewittBW=edge(I,'prewitt');
    figure;
    imshow(prewittBW);
    title('Prewitt Edge');
     
    logBW=edge(I,'log');
    figure;
    imshow(logBW);
    title('Laplasian of Gaussian Edge');
     
    cannyBW=edge(I,'canny');
    figure;
    imshow(cannyBW);
    title('Canny Edge');
    
    %标记内圆
    img=imread('C:\Users\27019\Desktop\机器视觉\图2.jpg');%读取原图
    i=img;
    img=im2bw(img); %二值化
    [B,L]=bwboundaries(img);
    [L,N]=bwlabel(img);
    img_rgb=label2rgb(L,'hsv',[.5 .5 .5],'shuffle');
    imshow(i);
    title('end result')
    hold on;
    k = 109;
    boundary = B{k}; 
    plot(boundary(:,2),boundary(:,1),'y','LineWidth',1);%显示内圆标记线
    
    

    %Prewitt模板
    figure;
    A=imread(‘C:\Users\27019\Desktop\机器视觉\图2.jpg’);
    subplot(2,3,1);
    imshow(A);
    title(‘原始图像’);
    h=[1 1 1;0 0 0;-1 -1 -1];
    B1=filter2(h,A)/255;
    subplot(2,3,2);
    imshow(B1);
    title(‘Prewitt模板,水平方向’);
    h=[1 0 -1;1 0 -1;1 0 -1];
    B2=filter2(h,A)/255;
    subplot(2,3,3)
    imshow(B2);
    title(‘Prewitt模板,垂直方向’);
    %Sobel模板
    A=imread(‘C:\Users\27019\Desktop\机器视觉\图2.jpg’);
    subplot(2,3,1);
    imshow(A);
    title(‘原始图像’);
    h=[1 2 1;0 0 0;-1 -2 -1];
    B1=filter2(h,A)/255;
    subplot(2,3,2);
    imshow(B1);
    title(‘Prewitt模板,水平方向’);
    h=[1 0 -1;2 0 -2;1 0 -1];
    B2=filter2(h,A)/255;
    subplot(2,3,3)
    imshow(B2);
    title(‘Prewitt模板,垂直方向’);
    %低通滤波器
    a=imread(‘C:\Users\27019\Desktop\机器视觉\图1-4.jpg’);%读入图像
    figure;
    subplot(2,4,1);
    imshow(a);
    z=double(a)/255;
    b=fft2(double(a));%对图片进行傅里叶变换
    c=log(1+abs(b));
    d=fftshift(b);%将变换的原点调整到频率矩阵的中心
    e=log(1+abs(d));
    %代入巴特沃斯公式进行滤波处理
    [m,n]=size(d);
    for i=1:256
    for j=1:256
    d1(i,j)=(1/(1+((i-128)2+(j-128)2)0.5/500)2)*d(i,j);%截至频率
    end;
    end;
    for i=1:256
    for j=1:256
    d2(i,j)=(1/(1+((i-128)2+(j-128)2)0.5/1000)2)*d(i,j);
    end;
    end;
    for i=1:256
    for j=1:256
    d3(i,j)=(1/(1+((i-128)2+(j-128)2)0.5/2000)2)*d(i,j);
    end;
    end;
    for i=1:256
    for j=1:256
    d4(i,j)=(1/(1+((i-128)2+(j-128)2)0.5/4000)2)*d(i,j);
    end;
    end;
    FF1=ifftshift(d1);
    FF2=ifftshift(d2);
    FF3=ifftshift(d3);
    FF4=ifftshift(d4);
    ff1=real(ifft2(FF1));%取傅里叶反变换
    ff2=real(ifft2(FF2));
    ff3=real(ifft2(FF3));
    ff4=real(ifft2(FF4));
    subplot(2,4,5);imshow(uint8(ff1));xlabel(‘截止频率为500’);
    subplot(2,4,6);imshow(uint8(ff2));xlabel(‘截止频率为1000’);
    subplot(2,4,7);imshow(uint8(ff3));xlabel(‘截止频率为2000’);
    subplot(2,4,8);imshow(uint8(ff4));xlabel(‘截止频率为4000’);
    %不同算子
    clear;clc;
    close all;
    I=imread(‘C:\Users\27019\Desktop\机器视觉\图2.jpg’);
    imshow(I,[]);
    title(‘Original Image’);

    sobelBW=edge(I,‘sobel’);
    figure;
    imshow(sobelBW);
    title(‘Sobel Edge’);

    robertsBW=edge(I,‘roberts’);
    figure;
    imshow(robertsBW);
    title(‘Roberts Edge’);

    prewittBW=edge(I,‘prewitt’);
    figure;
    imshow(prewittBW);
    title(‘Prewitt Edge’);

    logBW=edge(I,‘log’);
    figure;
    imshow(logBW);
    title(‘Laplasian of Gaussian Edge’);

    cannyBW=edge(I,‘canny’);
    figure;
    imshow(cannyBW);
    title(‘Canny Edge’);

    %标记内圆
    img=imread(‘C:\Users\27019\Desktop\机器视觉\图2.jpg’);%读取原图
    i=img;
    img=im2bw(img); %二值化
    [B,L]=bwboundaries(img);
    [L,N]=bwlabel(img);
    img_rgb=label2rgb(L,‘hsv’,[.5 .5 .5],‘shuffle’);
    imshow(i);
    title(‘end result’)
    hold on;
    k = 109;
    boundary = B{k};
    plot(boundary(:,2),boundary(:,1),‘y’,‘LineWidth’,1);%显示内圆标记线
    处理结果
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里我只做到了标记内圈,计算距离我没能实现,日后实现在评论区补充
    结果分析

    1. 不同边缘提取算子效果有何不同?
    2. 内圆标记线在图像上为什么不能被拟合为一段标准圆弧?

    回答:

    1. Sobel算子检测方法对灰度渐变和噪声较多的图像处理效果较好。对噪声具有平滑作用,提供较为精确的边缘方向信息,边缘定位精度不够高,图像的边缘不止一个像素。当对精度要求不是很高时,是一种较为常用的边缘检测方法。
      Roberts算子检测方法对具有陡峭的低噪声的图像处理效果较好,但是利用roberts算子提取边缘的结果是边缘比较粗,因此边缘的定位不是很准确。
      Prewitt算子检测方法对灰度渐变和噪声较多的图像处理效果较好。但边缘较宽,而且间断点多。
      Laplacian算子法对噪声比较敏感,所以很少用该算子检测边缘,而是用来判断边缘像素视为与图像的明区还是暗区。
      Log算子是高斯滤波和拉普拉斯边缘检测结合在一- 起的产物,它具有Laplace 算子的所有优点,同时也克服了其对噪声敏感的缺点。
      Canny方法不容易受噪声干扰,能够检测到真正的弱边缘。优点在于,使用两种不同的阈直分别检测强边缘和弱边缘,并且当弱边缘和强边缘相连时,才将弱边缘包含在输出图像中。
    2. 图像拍摄的时候,镜头不是垂直于地面,使得得到的图像有一定的畸变。
  • 您还可以看一下 苏金明老师的MATLAB编译器课程中的 MATLAB编译器的基本功能和使用步骤小节, 巩固相关知识点

以下答案由GPT-3.5大模型与博主波罗歌共同编写:
这里提供一种常见的数据拟合方法——多项式拟合进行人口数量预测,并提供 MATLAB 代码实现。

  1. 数据准备:

根据题目给出的图形,我们需要先将数据导入 MATLAB 中。这里使用导入工具将数据保存为名为 population.csv 的文件。代码如下:

% 清空工作区变量
clear all;

% 导入数据
filename = 'population.csv';
delimiter = ',';
startRow = 2;
formatSpec = '%f%f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'HeaderLines' ,startRow-1, 'ReturnOnError', false);
fclose(fileID);
year = dataArray{:, 1};
popu = dataArray{:, 2};
clearvars filename delimiter startRow formatSpec fileID dataArray ans;
  1. 数据绘制:

将导入的人口数据先绘制到图像上,以便更好的观察数据特点。代码如下:

% 数据绘制
plot(year, popu, '-o');
xlabel('Year');
ylabel('Population');
title('Population Data');

绘制出的图像如下所示:

![image](https://n/