【图像检测】基于LSD算法直线检测matlab代码
1 简介
提出了一种中国象棋棋盘角点检测的算法.首先采用LSD算法检测出棋盘灰度图像中的大部分直线,然后通过使用基于灰度值区域的投影直方图和基于LSD算法的直线交点检测两种方法,精确地检测出象棋棋盘的角点.最后通过实验,验证了算法的有效性和实时性,对于棋盘的亮度变化、棋盘畸变和边缘模糊具有良好的适应性.
通过观察 LSD 算法的结果可以发现,棋盘角点大部分都是位于棋盘边线的交点或者交点附近,所 以,可以通过检测出的直线求得这些直线的交点,从而确定象棋棋盘角点的位置.首先,在 LSD 算法检测出的棋盘上的直线后,从直线方向的分布上知道棋盘中信息点( 图 1( a) 中标出的两个点称之为信息点) 处于一些斜线的交点处,而且棋盘上其他角点处于方向位于 90 度,180 度左右直线的交点上,因此,可以通过对 LSD 算法检测出的直线进行分组,先求出这些斜线的交点即信息点的准确位置,然后,通过这两个点的位置可以大致确定棋盘其他角点的近似位置,由于这些直线的信息都是已知的,那么可以找到这些近似位置附近直线的交点,另外,由信息点得到的近似位置是比较接近真实棋盘角点的位置,所以,找到距离近似位置最近的交点的位置即为棋盘角点的位置.
2 部分代码
%{ Finds the angles and the gradients of the image. Generates a list of pseudo ordered points. * * @param scaled_image the image after guassian blur and resize * @param threshold The minimum value of the angle that is considered defined, otherwise NOTDEF * @param n_bins The number of bins with which gradients are ordered by, using bucket sort. * @param list Return: Vector of coordinate points that are pseudo ordered by magnitude. * Pixels would be ordered by norm value, up to a precision given by max_grad/n_bins. %} %list = [y, x, gradient] ordered by gradient %angles & modgrad: img_height * img_width matrix function [angles, modgrad, list] = ll_angle(scaled_image, threshold, n_bins) [height, width, dim] = size(scaled_image); % [modgrad,angles] = imgradient(scaled_image); % max_grad = max(max(modgrad)); % define arrays for store angles and gradient angles = zeros(height, width); modgrad = zeros(height, width); %Undefined the down and right boundaries angles(height, :) = -1024.0; angles(:, width) = -1024.0; max_grad = -1; %Computing gradient for remaining piexls for y = 1 : height-1 for addr = 1 : width-1 % DA = (scaled_image(y+1, addr+1) - scaled_image(y, addr)); % BC = (scaled_image(y+1, addr) - scaled_image(y, addr+1)); DA = (scaled_image(y+1, addr+1) - scaled_image(y, addr)); BC = (scaled_image(y+1, addr) - scaled_image(y, addr+1)); gx = DA + BC; % gx = double(gx); gy = DA - BC; % gy = double(gy); norm = sqrt((gx^2 + gy^2)/4); modgrad(y, addr) = norm; %set angles to nodefine for small gradient if norm <= threshold angles(y, addr) = -1024.0; else angles(y, addr) = atan2(gx,-gy); if norm > max_grad max_grad = norm; end end end end %computing histogram of gradient values count = 1; if max_grad > 0 bin_coef = (n_bins - 1) / max_grad; else bin_coef = 0; end unsorted_list = zeros(height*width,3); for y = 1 : height for x = 1 : width if x < width && y < height i = modgrad(y, x) * bin_coef; else i = -1024; end unsorted_list(count, 1) = y; unsorted_list(count, 2) = x; unsorted_list(count, 3) = i; count= count+1; end end % list = unsorted_list; % sort [a, index] = sort(unsorted_list(:,3),'descend'); list = unsorted_list(index, [1,2 ,3]); % % figure, imshow(angles); % figure, imshow(modgrad); %3 仿真结果
4 参考文献
[1]段汕, 贺兴, 娄联堂, 徐文, & 罗敬. (2013). 一种基于灰度投影的中国象棋棋盘角点检测算法. 中南民族大学学报:自然科学版, 32(4), 5.
**部分理论引用网络文献,若有侵权联系博主删除。**