MATLAB 边缘检测代码只显示小白点?如何修改代码获取更多边缘信息?
根据代码的描述,这段代码的目的是进行边缘检测并显示结果。根据代码中的注释,可以看到图像的边缘检测结果被保存在变量bw_image中,而亚像素边缘检测结果被保存在变量subpixel_image中。
如果运行代码后只显示一个小白点,可能是因为图像的边缘检测结果很小,只有一个像素点。你可以尝试调整代码中的参数,例如修改order的值,增加边缘检测的阶数,从而获得更多的边缘信息。
另外,你可以尝试使用其他的边缘检测算法,例如Sobel算子、Laplacian算子等,看看是否能够获得更好的边缘检测结果。你可以在edge函数的第二个参数中指定其他的边缘检测算法名称,例如edge(image, 'sobel')。
最后,你还可以尝试调整图像的亮度和对比度,看看是否能够改善边缘检测结果。你可以使用imadjust函数来调整图像的亮度和对比度,例如adjusted_image = imadjust(image, [low_in high_in], [low_out high_out])。其中,[low_in high_in]是输入图像的亮度范围,[low_out high_out]是输出图像的亮度范围。你可以根据具体的图像情况调整这些参数,以获得更好的边缘检测结果。
% 读取图像 image = imread('lena.jpg'); image = rgb2gray(image); % 使用Canny边缘检测算法获取二值化图像 bw_image = edge(image, 'canny'); % 计算图像的Zernike矩 order = 10; % Zernike矩的阶数 moments = zernike_moments(bw_image, order); % 亚像素边缘检测 subpixel_image = subpixel_edge_detection(image, moments); % 显示结果 figure; subplot(1, 2, 1); imshow(bw_image); title('Binary Edge Image'); subplot(1, 2, 2); imshow(subpixel_image); title('Subpixel Edge Detection'); function moments = zernike_moments(image, order) [rows, cols] = size(image); moments = zeros(order+1, order+1); % 计算图像的归一化矩 for p = 0:order for q = 0:order if mod(p+q, 2) == 0 for x = 1:rows for y = 1:cols if image(x, y) > 0 rho = sqrt((2x-rows-1)^2 + (2y-cols-1)^2) / sqrt(rows^2 + cols^2); theta = atan2((2y-cols-1), (2x-rows-1)); moments(p+1, q+1) = moments(p+1, q+1) + image(x, y) * zernike_polynomial(p, q, rho, theta); end end end moments(p+1, q+1) = moments(p+1, q+1) * ((p+1)/(pi*(rowscols))); end end end end function value = zernike_polynomial(p, q, rho, theta) value = 0; for s = 0:(p-q)/2 value = value + (-1)^s * factorial(p-s) / (factorial(s) * factorial((p+q)/2-s) * factorial((p-q)/2-s)) * rho^(p-2s); end value = value * sqrt((p+1)/pi) * cos(qtheta); end function subpixel_image = subpixel_edge_detection(image, moments) [rows, cols] = size(image); subpixel_image = zeros(rows, cols); for x = 1:rows for y = 1:cols if image(x, y) > 0 rho = sqrt((2x-rows-1)^2 + (2y-cols-1)^2) / sqrt(rows^2 + cols^2); theta = atan2((2y-cols-1), (2*x-rows-1)); % 使用moments参数计算亚像素边缘位置 subpixel_x = (rows+1)/2 + rho * cos(theta) + moments(1, 1); subpixel_y = (cols+1)/2 + rho * sin(theta) + moments(1, 2); % 判断亚像素边缘位置是否在图像范围内 if subpixel_x >= 1 && subpixel_x <= rows && subpixel_y >= 1 && subpixel_y <= cols subpixel_image(round(subpixel_x), round(subpixel_y)) = 255; end end end end end
原文地址: https://www.cveoy.top/t/topic/fv88 著作权归作者所有。请勿转载和采集!