【图像检测】基于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