Skip to main content
gamedev.net
🔒 Locked

[java] Coding a function

Started by Mary Coory Dec 14, 2005 at 6:51 AM 0 replies 650+ views
Original Post
Mary Coory
Mary Coory
Hello everybody As a part of a project I need to code the following : If we Consider the following 8-neighborhood of a pixel p1[j] with value either 0 or 1 and which is a part of bigger array, say, image [j] http://cgm.cs.mcgill.ca/~godfried/teaching/projects97/azar/grid.gif A(p1) = number of 0,1 patterns in the sequence p2,p3,p4,p5,p6,p7,p8,p9,p2 For example : http://cgm.cs.mcgill.ca/~godfried/teaching/projects97/azar/grid2.gif has A(p1) = 1 and http://cgm.cs.mcgill.ca/~godfried/teaching/projects97/azar/grid3.gif has A(p1) = 2 I want to code this function , Any help please? btwn: I try to view the images using [img][/img] in my post but it seemed that it doesn't work !
haegarr
haegarr
Iterate the neighbourhood by sub-indices e.g. from an array, and check the difference:
int[] indices = {    0,-1, // dx,dy of p2   +1,-1, // dx,dy of p3   +1, 0, // dx,dy of p4   and so on until and inclusive    0,-1, // dx,dy of p2};count = 0;for(int neighbourIndex = 0; neighbourIndex < indices.length-2; ++neighbourIndex) {   int x1 = i+indices[neighbourIndex+0];   int y1 = j+indices[neighbourIndex+1];   int x2 = i+indices[neighbourIndex+2];   int y2 = j+indices[neighbourIndex+3];   int diff = image[y2][x2]-image[y1][x1];   if(diff==1) {      count++;   }}


Notice that both pixels must be inside the image, so you may either ignore the borders or else include border checks into the loop above (but then the definition of 0->1 transitions is somewhat spurious).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.