MATLAB中Hough Transform绘图不符预期,如何调整设置实现目标效果?
Hey there! Let's work through getting your Hough transform plot looking exactly how you want it. I’ve dealt with this exact head-scratcher before when plotting Hough accumulators in MATLAB, so here are the key tweaks to fix up your current code:
Key Adjustments to Try
Fix the color mapping & intensity range
The default grayscale colormap often makes Hough peaks hard to spot, and unadjusted intensity limits can flatten out important details. Add these lines right afterimshow:colormap(gca, 'jet'); % Swap to a high-contrast colormap (hot, parula also work) clim([0 max(h(:))]); % Lock color range to match your accumulator's min/max valuesTweak axis settings for clarity
axis normalcan sometimes stretch or misalign your theta/rho axes. Replace it with:axis tight; % Removes extra whitespace around the accumulator xlabel('Theta (degrees)'); ylabel('Rho (pixels)');This ensures your axes fit the exact range of
thetaandrhovalues, plus adds clear labels to make the plot interpretable.Optional: Mark Hough peaks (if your target style includes this)
If you want to highlight the strongest lines detected, add peak markers after enablinghold on:peaks = houghpeaks(h, 5); % Grab top 5 peaks (adjust number as needed) plot(theta(peaks(:,2)), rho(peaks(:,1)), 'rs', 'MarkerSize', 8, 'MarkerFaceColor', 'r');
Full Updated Code Example
Here’s how all these pieces fit together:
% Assuming you already ran: [h, theta, rho] = hough(edge_detected_image); figure; imshow(h, [], 'XData', theta, 'YData', rho, 'InitialMagnification', 'fit'); colormap(gca, 'jet'); clim([0 max(h(:))]); axis on; axis tight; xlabel('Theta (degrees)'); ylabel('Rho (pixels)'); title('Hough Transform Accumulator'); hold on; % Add peak markers (optional) peaks = houghpeaks(h, 5); plot(theta(peaks(:,2)), rho(peaks(:,1)), 'rs', 'MarkerSize', 8, 'MarkerFaceColor', 'r'); hold off;
These changes should make your Hough accumulator plot match the typical, easy-to-read style—with clear peaks, properly aligned axes, and intuitive color coding.
内容的提问来源于stack exchange,提问作者Ravi Shankar

