I am trying to use Roberts edge detection to process an image. Do I just apply both of the masks to the image and perform convolution as normal? Could someone give me the breakdown of how to use this edge detection method, as I am trying to program it to process a greyscale image. I convoluted the image using both kernels separately but the image dent' look right.



1 Answer 1


Robert's Cross is a little tricky because it's not an odd size (2x2 rather than 3x3 or 5x5). I've done it using numpy+scipy using a padded 3x3 convolution mask.

import sys
import numpy as np
from scipy import ndimage
import Image

roberts_cross_v = np.array( [[ 0, 0, 0 ],
                             [ 0, 1, 0 ],
                             [ 0, 0,-1 ]] )

roberts_cross_h = np.array( [[ 0, 0, 0 ],
                             [ 0, 0, 1 ],
                             [ 0,-1, 0 ]] )
def load_image( infilename ) :
    img = Image.open( infilename )
    # note signed integer
    return np.asarray( img, dtype="int32" )

def save_image( data, outfilename ) :
    img = Image.fromarray( np.asarray( np.clip(data,0,255), dtype="uint8"), "L" )
    img.save( outfilename )

def roberts_cross( infilename, outfilename ) :
    image = load_image( infilename )

    vertical = ndimage.convolve( image, roberts_cross_v )
    horizontal = ndimage.convolve( image, roberts_cross_h )

    output_image = np.sqrt( np.square(horizontal) + np.square(vertical))

    save_image( output_image, outfilename )

infilename = sys.argv[1]
outfilename = sys.argv[2]
roberts_cross( infilename, outfilename )

From the Wikipedia entry on Robert's Cross. http://en.wikipedia.org/wiki/Roberts_Cross

Image from Robert's Cross Entry on Wikipedia

My script's output.

My script output

  • $\begingroup$ Do you square every pixel value in the image? $\endgroup$ Dec 7, 2011 at 20:28
  • $\begingroup$ Yes. The "np.sqrt( np.square(horizontal) + np.square(vertical))" gives the vector magnitude between the horizontal, vertical directions. $\endgroup$ Dec 8, 2011 at 14:16
  • $\begingroup$ @DavidPoole Robert's edge detector wiki is fantastic - simple, to the point, and illustrative. How/why is it sensitive to noise VS other gradient measures? Isnt there only one 'true' gradient measure? $\endgroup$
    – Spacey
    Mar 28, 2012 at 21:22
  • $\begingroup$ I am trying to implement this function in python (Spyder version) but I am having trouble understanding the arguments it takes and how to feed them? what is infilename and outfilename? Thanks Sam $\endgroup$
    – user19959
    Mar 10, 2016 at 2:17
  • $\begingroup$ infilename is a grayscale (1 plane) image. outfilename is just an output file the script will write to. The input/output images can be jpeg, png, tif, etc, the Image library (now Pillow) will interpret the image format based on the file extension. Example: python3 rcross.py bicycle.jpg out.tif $\endgroup$ Mar 10, 2016 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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