% https://stackoverflow.com/questions/31776434/non-maximal-suppressionthinning-algorithm
function [ marks ] = nonMaxSupp( mag,dir )
	[m,n]=size(mag);
	marks=mag;
	for i=2:m-1
		for j=2:n-1
			if mag(i,j) ~=0
				angle=dir(i,j)+180;
				if (angle>=340 || angle<=22.5) || (angle>=157.5 && angle<=202.5)  %horizontal
					if (mag(i,j+1)>mag(i,j)) || (mag(i,j-1)>mag(i,j))
						marks(i,j)=0;
					end	
				elseif (angle>22.5 && angle<=67.5) || (angle>202.5 && angle<=247.5) %45
					if (mag(i+1,j+1)>mag(i,j)) || (mag(i-1,j-1)>mag(i,j))
						marks(i,j)=0;
					end
				elseif (angle>67.5 && angle<=112.5) || (angle>247.5 && angle<=292.5) %vertical
					if (mag(i-1,j)>mag(i,j)) ||(mag(i+1,j)>mag(i,j))
						marks(i,j)=0;
					end
				else %135
					if (mag(i-1,j+1)>mag(i,j)) || (mag(i+1,j-1)>mag(i,j))
						marks(i,j)=0;
					end
				end
			end
		end
	end	
end	