# detecting edge direction using Julia-language

Let say we have following image:

using Images
using PyPlot
using ImageFiltering

function chessboardimg(m::Int, n::Int, blkwidth::Int, blkheight::Int, )
img = zeros(m, n)

togglex = true
toggley = !togglex
for i=1:m
togglex = true
for j=1:n
img[i, j] = abs(togglex - toggley)
if j % blkwidth <= 0
togglex = !togglex
end
end
if i % blkheight <= 0
toggley = !toggley
end
end
return img
end

### creating image ####
img1 = transpose(triu(ones(20, 20)))
img2 = chessboardimg(20, 20, 10, 10)
img = [img1 img2]


Now i want to get the edge directions in this image as below:

gx, gy = imgradients(img)


These are the images of each calculated step:

as you can see in the "Gradient Direction"-image twoh vertical lines are missing. which should actually indicate the transition from black to white region in the y-direction.

1) what am i doing wrong?

• What's the x- and what's the y-axis in your plots? It looks like you swapped the plots that say "y derivative" and "x derivative" – Marcus Müller Feb 14 '17 at 13:17
• more importantly, are you sure that it's wrong that this line is missing? does imshow deal with the values you feed it like you think it should do it, or does it just behave like it's specified to deal with them? – Marcus Müller Feb 14 '17 at 13:18
• @MarcusMüller sorry the plot was swapped! i edited again! independently from $imshow$ the values for two transitions are missing in the "Gradient Direction"-Matrix. – arash javan Feb 14 '17 at 13:27
• are you 100% sure? these don't happen to be the one where angle is negative? – Marcus Müller Feb 14 '17 at 13:31
• @MarcusMüller yes i just checked the whole Gradient-Direction-Matrix these transitions are not in the matrix, not even as a $\pm180; deg$ ! – arash javan Feb 14 '17 at 13:44

• exactly correct and that is why i am saying that this is wrong because, there are two vertical transitions as you see in x-derivative (those marked with white line in x-derivative) which have the value $\frac{\partial{I}}{\partial{x}}(i,j)= 1.0$ correspending values in y-derivative image are $\frac{\partial{I}}{\partial{y}}(i,j)= 0.0$ which yields to $\theta=atan2(0,1) = 0$ so you can not distinguish betwenn regions with no edge and those with vertical edges! – arash javan Feb 14 '17 at 14:52
• actually both ;), i agree with you in case that where angle is 0 we have an horizontally running edge beacue in that case you have $\theta=atan2(1,0)=0$. And i do not agree with you because there is still an edge there but because of the nature of $atan2$ which gives you $0$ for both $$\theta=atan2(0,0)=atan2(1,0)=0$$ and ecatly here we are loosing those edges. – arash javan Feb 14 '17 at 19:49
• @arashjavan you're mixing up directions again. $\text{atan2}(1,0)\ne 0$. Instead, $0=\text{atan2}(0,0) = \text{atan2}(0,\alpha),\,\alpha\ge0$ (but $\text{atan2}(0,-\alpha) \ne 0$, too). – Marcus Müller Feb 14 '17 at 19:56
• sorry for the typo, of course i meant $atan2(0,\alpha)=0=atan2(0,0)$, which as you wrote too would mean that we do lose edges transition from $0 \rightarrow 1$ in the $x$-Direction. right? – arash javan Feb 15 '17 at 7:26