Thread: Caught In The Convolution Matrix

  1. #1
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273

    Red face Caught In The Convolution Matrix

    Hello,

    Although this isn't strictly to do with game programming per se (although it may now be possible to do this in real-time, I haven't checked ), I was wondering if someone could help with a spot of image processing that I'm trying to do.

    I'm currently trying to implement a simple edge detection algorithm using the following convolution matrix:-
    Code:
    -1 -1 -1
    -1  8 -1
    -1 -1 -1
    This is a fairly standard operator used for the detection of edges within an image, and it works great (I thought it was a lot harder than it turned out!), comparing the results with Paint Shop Pro's efforts show them to be nearly identical.

    Nearly, because unfortunately I can't work out how best to handle the left, top, right and bottom edges of the rectangle that is the image itself. The problem is, for example, when processing the pixel at the top-left corner of the bitmap (0, 0), part of the matrix is acting outside of the image and so has no effect. To illustrate using a mask:-
    Code:
    X is the position of pixel (0, 0)
    -1 -1 -1      0 0 0
    -1  8 -1 into 0 X 1
    -1 -1 -1      0 1 1
    Now, what I'm currently doing is testing if the calculated pixel to read is outside the range of the image. If it is, then a count is incremented and nothing is added to the output sample. At the end of the matrix operation, if there were any pixels clipped then the output sample is:-
    Code:
    sample = (int)(sample * ((double)numclipped / 9));
    However, this doesn't compare favourably to what PSP does. The outer edge is much brighter on mine, suggesting that I'm not doing it right. Any ideas?

  2. #2
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    I would handle edges and corners the following way:
    *In the interior of the image, the middle of the matrix is 8.
    *Along an edge, the middle element of the matrix is 5.
    *At a corner, the middle element is 3.
    Then use 0 for 'non-existent' elements outside the image.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  3. #3
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Thanks Sang-Drax, after including your suggestion the output now looks better and is the same "shape" as the output Paint Shop Pro provides, although the actual values along the edge are more distinct in the PSP version. It must be using a similar algorithm, doing it slightly differently on the edges.

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Well, you could also replicate nonexistent values with the nearest existing value.

    But think about it: edge detection along an edge? It doesn't make sense to care about the value there. I suggest that you only do edge detection along the x-direction at the top and bottom and only along the y-direction at the left and right edge of the image.

    I.e.
    0 1 0
    0 -2 0
    0 1 0
    along the left and right border.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  2. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 05:57 PM
  3. What is a matrix's purpose in OpenGL
    By jimboob in forum Game Programming
    Replies: 5
    Last Post: 11-14-2004, 12:19 AM
  4. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 06:36 AM