在代码中,错误提示为"位置 1 处的索引超出数组边界(不能超出 1)",具体是在第51行的q21 = pixelValues(2, 1);出现了问题。

这个错误的原因是在计算亚像素边缘位置时,subpixelX和subpixelY的值可能超出了边界范围。在计算subpixelX和subpixelY时,使用了错误的公式,应该是subpixelX = round(x - dx / (2*dx + dx));和subpixelY = round(y - dy / (2*dy + dy));。

修改后的代码如下:

function [subpixelEdgeImg, edgeStrength] = subpixel_edge(edgeImg)
    % 获取边缘图像的尺寸
    [height, width] = size(edgeImg);
    % 初始化亚像素边缘图像和边缘强度矩阵
    subpixelEdgeImg = zeros(height, width);
    edgeStrength = zeros(height, width);
    % 对于每个像素点
    for y = 2:height-1
        for x = 2:width-1
            % 如果当前像素是边缘点
            if edgeImg(y, x) == 1
                % 计算亚像素边缘位置和边缘强度
                % 计算x方向和y方向的梯度
                dx = (edgeImg(y, x+1) - edgeImg(y, x-1)) / 2;
                dy = (edgeImg(y+1, x) - edgeImg(y-1, x)) / 2;
                % 计算亚像素边缘位置
                subpixelX = round(x - dx / (2*dx + dx));
                subpixelY = round(y - dy / (2*dy + dy));
                % 计算亚像素边缘位置的四个邻近像素
                x1 = floor(subpixelX);
                x2 = ceil(subpixelX);
                y1 = floor(subpixelY);
                y2 = ceil(subpixelY);
                % 添加边界条件
                y1 = max(1, y1);
                y2 = min(height, y2);
                x1 = max(1, x1);
                x2 = min(width, x2);
                % 使用双线性插值计算亚像素边缘强度
                edgeStrength(y, x) = bilinear_interpolation(subpixelX, subpixelY, edgeImg(y1:y2, x1:x2), height, width);
                % 设置亚像素边缘图像的像素值
                subpixelEdgeImg(y, x) = 1;
            end
        end
    end
end

function value = bilinear_interpolation(x, y, pixelValues, height, width)
    % 获取邻近像素的位置
    x1 = floor(x);
    x2 = ceil(x);
    y1 = floor(y);
    y2 = ceil(y);
    % 添加边界条件
    y1 = max(1, y1);
    y2 = min(height, y2);
    x1 = max(1, x1);
    x2 = min(width, x2);
    % 获取邻近像素的值
    q11 = pixelValues(1, 1);
    q12 = pixelValues(1, 2);
    q21 = pixelValues(2, 1);
    q22 = pixelValues(2, 2);
    % 计算插值结果
    value = (1/(x2-x1)/(y2-y1)) * (q11*(x2-x)*(y2-y) + q21*(x-x1)*(y2-y) + q12*(x2-x)*(y-y1) + q22*(x-x1)*(y-y1));
end

请尝试运行修改后的代码,看看是否能够解决问题。

matlab中此段代码错误提示为:位置 1 处的索引超出数组边界不能超出 1。出错 subpixel_edgebilinear_interpolation 第 51 行 q21 = pixelValues2 1;出错 subpixel_edge 第 30 行 edgeStrengthy x = bilinear_interpolationsubpixelX su

原文地址: https://www.cveoy.top/t/topic/hDf1 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录