• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Average Neighbors of Any Given 2D Array

 
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:So you don't even need a 1-D array, then.


all that confusion for nothing!
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ana Yo wrote:To prove to you that I'm actually trying and putting effort: this is what i have so far:
I think my logic is kinda wrong. Would you mind fixing it?



Here's a pseudo-code version of what your code should look like:



The code you had near the beginning of this thread for numberOfNeighbors looked pretty good to me.
 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do I have to make another method called sumOfNeigbors? Or can I use countNeigbor();
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Well, to average any set of numbers you need two things: a sum, and a count.

 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the updated code:


But I get this error:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Smooth.smoothImage(Smooth.java:57)
at Smooth.main(Smooth.java:29)                 ERROR IS IN THE SMOOTH METHOD
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay... the error message says your code is trying to divide by zero, which of course you can't do. And your code is trying to divide by "neighbors", which is what you got when you counted the number of neighbors of some cell.

So what can you conclude from those two things?
 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if(neighbor!=0)
{
  image[r][c]=sum/neighbors;
}

?
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like you have concluded that the number of neighbors is zero, so you should avoid doing the division. The first part of that is right... your code says the number of neighbors is zero.

Which of the cells in your 2-D can have zero neighbors?
 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
None
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ana Yo wrote:None



But the sumNeighbor method returned zero. And that isn't right because none of the cells have zero neighbors.

So you can conclude that there's something wrong with that method. You have this code which says "If (i,j) is a neighbor of (r,c)":



That's the key line of code in the method, isn't it? There must be something wrong with the six conditions it's testing. So to make sure you understand it (it's easy to get that sort of thing wrong and not notice it), write down the six conditions and what they are testing for:

i >= 0 ... i is inside the array

i <= m.length - 1 ... ?

j >= 0 ... ?

j <= m[0].length - 1 ... ?

m[i][j] == 1 ... ?

!(i == r && j == c) ... ?
 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ohhh so there is something wrong with my conditions.
 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what you said earlier, as a description of what you were supposed to implement.

Ana Yo wrote:Say that the original values are in the 2D array "image". Compute the smoothed array by doing this:
Each value smooth[r][c] is the average of nine values:
image[r-1][c-1], image[r-1][c ], image[r-1][c+1],
image[r ][c-1], image[r ][c ], image[r ][c+1],
image[r+1][c-1], image[r+1][c ], image[r+1][c+1].



Now yeah, you have to watch out because sometimes some of those nine values can be outside the array. The first four of your conditions take care of that. But what about the other two? How do they fit in with your requirement?
 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the values are not diagonals?
 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure because I got those statements from notes our teacher has given us.
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Here's a pseudo-code version of what your code should look like:


"sumOfNeighbors" has to access the 2D image array. If you then compute the average and put it back into the same image array you have a situation where the average you created is now used in a subsequent call to sumOfNeighbors(). This is why this algorithm usually has a source image and a destination image.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then maybe you should consult your teacher about them. I can't see those notes so I can't comment on them. All I can say is that the last two of your six conditions:

m[i][j] == 1 ... cell is 1

!(i == r && j == c) ... You're asking whether [i, j] is a neighbor of [r, c] and you're saying it can't be a neighbor if it's the same as [r, c]



don't seem to match what you were told to do.
 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Should I just leave the 4 conditional statements and take out the last 2?
 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the notes.
count.PNG
[Thumbnail for count.PNG]
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've been scratching my head about this 1D array requirement and so far I don't see how that's supposed to work.
 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I asked my friend and she said the teacher said don't worry about it.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your screen snapshot you have
In your source code you have
In which only two locations in the image meet that criteria.
 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, I see. It's because the source code is to find wherever 1 is. But in this actual lab, we're just finding the neighbors. So should I just get rid of the last two conditions?
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seems like the criteria of '1' and also looking to see if !(i==r && j==c) should both go away. Those are not a normal part of a blur algorithm.
 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For some reason, this is the output
1 2 3 4 5 6
2 5 5 5 5 5
3 5 5 5 5 4
4 5 5 5 5 3
5 5 5 5 5 2
6 5 4 3 2 1

1 2 3 4 5 6
2 5 5 5 5 5
3 5 5 5 5 4
4 5 5 5 5 3
5 5 5 5 5 2
6 5 4 3 2 1

16 19 10 20 15
6 8 19 17 14
6 17 13 9 15
15 12 18 18 6
11 10 11 11 17

16 19 10 20 15
6 8 19 17 14
6 17 13 9 15
15 12 18 18 6
11 10 11 11 17
 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Updated code:

 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Note that when you call this you pass "smallimage" in but get a "newimage" (or whatever) out. You then use "newimage" in subsequent operations. No changes are made to the source image.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might want to modify your printImage() to use printf() so that you can get columns to line up.
 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Outputs with fixed smooth()
1 2 3 4 5 6
2 5 5 5 5 5
3 5 5 5 5 4
4 5 5 5 5 3
5 5 5 5 5 2
6 5 4 3 2 1

1 2 3 4 5 6
2 5 5 5 5 5
3 5 5 5 5 4
4 5 5 5 5 3
5 5 5 5 5 2
6 5 4 3 2 1

8 12 18 10 17
8 12 18 15 11
15 13 18 10 11
15 8 18 16 11
6 17 7 7 9

8 12 18 10 17
8 12 18 15 11
15 13 18 10 11
15 8 18 16 11
6 17 7 7 9
 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even if something is wrong it should at least print out the smoothed image one. There's supposed to be 6 outputs but there are only 4.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are only making 4 calls to printImage().
 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes but after smooth() is executed, shouldn't it print out the smoothd one?
 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
printImage (image);  
     System.out.println();
     smoothImage (image);
     printImage(image);
     System.out.println();
  So basically you print image and then call smoothImage which averages all the neighbors then print the averaged image one and repeats that for smallimage
     
     createImage (smallImage);
     printImage (smallImage);
     System.out.println();
     smoothImage (smallImage);
     printImage (smallImage);
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have chosen to use the smoothImage() approach I suggested. It is different than the one you had before. This one does not modify the image passed in but instead passes a resulting image back out. So you would then need to make a minor change so that you are not just tossing the returned image in the bit-bucket.

 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1 2 3 4 5 6
2 5 5 5 5 5
3 5 5 5 5 4
4 5 5 5 5 3
5 5 5 5 5 2
6 5 4 3 2 1

1 2 3 4 5 6
2 5 5 5 5 5
3 5 5 5 5 4
4 5 5 5 5 3
5 5 5 5 5 2
6 5 4 3 2 1

13 9 9 17 11
11 8 11 14 17
12 7 12 6 17
11 6 8 12 6
9 20 8 6 16

13 9 9 17 11
11 8 11 14 17
12 7 12 6 17
11 6 8 12 6
9 20 8 6 16
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is not correct. Your smoothed image should not be exactly the same as the before data.
 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I modified my code but it still outputs the same outputs as the previous post.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sum was not calculated correctly.

 
Ana Smith
Ranch Hand
Posts: 373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I fixed the sum:
1 2 3 4 5 6
2 5 5 5 5 5
3 5 5 5 5 4
4 5 5 5 5 3
5 5 5 5 5 2
6 5 4 3 2 1

2 3 4 4 5 5
3 3 4 4 4 5
4 4 5 5 4 4
4 4 5 5 4 4
5 4 4 4 3 3
5 5 4 4 3 2

19 19 15 13 15
11 12 9 17 14
14 12 17 12 18
13 10 11 20 13
6 11 7 19 7

19 19 15 13 15
11 12 9 17 14
14 12 17 12 18
13 10 11 20 13
6 11 7 19 7
 
reply
    Bookmark Topic Watch Topic
  • New Topic