Let say we have following image:


made with following Julia-code:

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
    if i % blkheight <= 0
      toggley = !toggley
  return img

### 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)
mag, grad_angle = magnitude_phase(gx, gy)
grad_angle *= 180 / pi
imshow(grad_angle, "gray", interpolation="none")

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?

  • $\begingroup$ 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" $\endgroup$ – Marcus Müller Feb 14 '17 at 13:17
  • $\begingroup$ 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? $\endgroup$ – Marcus Müller Feb 14 '17 at 13:18
  • $\begingroup$ @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. $\endgroup$ – arash javan Feb 14 '17 at 13:27
  • $\begingroup$ are you 100% sure? these don't happen to be the one where angle is negative? $\endgroup$ – Marcus Müller Feb 14 '17 at 13:31
  • $\begingroup$ @MarcusMüller yes i just checked the whole Gradient-Direction-Matrix these transitions are not in the matrix, not even as a $\pm180; deg$ ! $\endgroup$ – arash javan Feb 14 '17 at 13:44

The edges "missing" from the picture are actually those that have angle=0; which is the same as the phase you get for "there's no edge here".

So, nothing wrong.

  • $\begingroup$ 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! $\endgroup$ – arash javan Feb 14 '17 at 14:52
  • $\begingroup$ I don't understand, @arashjavan, are you agreeing with me or contradicting me? $\endgroup$ – Marcus Müller Feb 14 '17 at 18:01
  • $\begingroup$ 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. $\endgroup$ – arash javan Feb 14 '17 at 19:49
  • $\begingroup$ @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). $\endgroup$ – Marcus Müller Feb 14 '17 at 19:56
  • $\begingroup$ 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? $\endgroup$ – arash javan Feb 15 '17 at 7:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.