Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp
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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Bear Bibeault
• Paul Clapham
• Jeanne Boyarsky
Sheriffs:
• Devaka Cooray
• Junilu Lacar
• Tim Cooke
Saloon Keepers:
• Tim Moores
• Ron McLeod
• Tim Holloway
• Claude Moore
• Stephan van Hulst
Bartenders:
• Winston Gutkowski
• Carey Brown
• Frits Walraven

# Average Neighbors of Any Given 2D Array

Ranch Hand
Posts: 203
Hey there! How do you average any given 2D array?
So far I have a method to countNeighbors but how do you use countNeighbors to make a method averageNeighbors?

Bartender
Posts: 5641
56
Well, to average any set of numbers you need two things: a sum, and a count.

Ana Yo
Ranch Hand
Posts: 203
Sorry, I meany to say average the cells neighbors. For example,  average[0][0] is
the average of 4 values and average[0][2] is the average of 6 values.

Carey Brown
Bartender
Posts: 5641
56
Yes, and do you also have the sum of those neighbors?

Ana Yo
Ranch Hand
Posts: 203
Image
this is what i have so far, i want to check if it's going to be the top right, top left, bottom right, or bottom left corner

Ana Yo
Ranch Hand
Posts: 203
but i'm not sure how to

Carey Brown
Bartender
Posts: 5641
56
You'll, of course, need the count of neighbors. You'll also need to figure out which neighbors to count so that you can sum up their values.

Ana Yo
Ranch Hand
Posts: 203
Is this right so far?

Ana Yo
Ranch Hand
Posts: 203
other than the triple = signs ;)

Sheriff
Posts: 13174
219

Ana Yo wrote:Sorry, I meany to say average the cells neighbors. For example,  average[0][0] is
the average of 4 values and average[0][2] is the average of 6 values.

Think about what you're not telling other people about this problem. You seem to assume that other people know everything that you know. They don't, so your description is really vague and somewhat confusing.

From what you described, I can deduce that your examples assume an array with dimensions that are at least [2][4]. That's the only way it would make sense to say that average[0][0] is the average of 4 values and average[0][2] is the average of 6 values. Moreover, if there are 6 values used for average[0][2], then you have to be counting the cell [0][2] also. Otherwise, if you're truly only counting neighbors, there are going to be only 5 values because you can't consider a cell as its own neighbor. That just would not make sense at all.

When describing problems, you have to assume other people are totally ignorant of the context. You have to give the whole context, otherwise, you will either not be understood or you will be misunderstood and you will get answers that may not be entirely correct.

Ana Yo
Ranch Hand
Posts: 203
Can you show me how you would do the top left corner so I understand how to the rest of the corners?

Ana Yo
Ranch Hand
Posts: 203

Junilu Lacar wrote:

Ana Yo wrote:Sorry, I meany to say average the cells neighbors. For example,  average[0][0] is
the average of 4 values and average[0][2] is the average of 6 values.

Think about what you're not telling other people about this problem. You seem to assume that other people know everything that you know. They don't, so your description is really vague and somewhat confusing.

From what you described, I can deduce that your examples assume an array with dimensions that are at least [2][4]. That's the only way it would make sense to say that average[0][0] is the average of 4 values and average[0][2] is the average of 6 values. Moreover, if there are 6 values used for average[0][2], then you have to be counting the cell [0][2] also. Otherwise, if you're truly only counting neighbors, there are going to be only 5 values because you can't consider a cell as its own neighbor. That just would not make sense at all.

When describing problems, you have to assume other people are totally ignorant of the context. You have to give the whole context, otherwise, you will either not be understood or you will be misunderstood and you will get answers that may not be entirely correct.

I'm so sorry! Let me describe this lab:
A gray-level image is sometimes stored as a list of int values. The values represent the intensity of light
at discrete positions in the image.
An image may be smoothed by replacing each element with the average of the element's neighboring
elements.
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].
Assume that the image is rectangular, that is, all rows have the same number of locations. Use integer
arithmetic for this so that the values in smooth are integers.
NOTE:
1. For each location in the image, average together only those values that exist. E.g. smooth[0][0] is
the average of 4 values and smooth[0][2] is the average of 6 values.

Ana Yo
Ranch Hand
Posts: 203
This is the shell + countNeighbors(which I made):

Carey Brown
Bartender
Posts: 5641
56
Most neighborhoods are sized in odd numbers of NxN, such as 3x3, 5x5, 7x7, etc.. Because they are odd they have a central value horizontally and vertically. You haven't said what size neighborhood you are going to average. If you are given a r,c for a pixel in your image then figuring out which pixels in your neighborhood fall inside you image array. Figure out how you'd do this with pencil and paper then see if you can implement the logic for those steps.

Ana Yo
Ranch Hand
Posts: 203

Carey Brown wrote:Most neighborhoods are sized in odd numbers of NxN, such as 3x3, 5x5, 7x7, etc.. Because they are odd they have a central value horizontally and vertically. You haven't said what size neighborhood you are going to average. If you are given a , rarc for a pixel in your image then figuring out which pixels in your neighborhood fall inside you image array. Figure out how you'd do this with pencil and paper then see if you can implement the logic for those steps.

I'm going to be averaging a 6x6 array and then putting the averages into another array called smallImage.

Ana Yo
Ranch Hand
Posts: 203
First things first. I'm kind pf confused on what the lab is asking us to do. What is smallImage and why is it only a 4x4 array?

Ana Yo
Ranch Hand
Posts: 203
Oops I'm sorry for the confusion, I get what the lab is asking. Can someone delete my previous message of "first things first"

Ana Yo
Ranch Hand
Posts: 203
UPDATED CODE:

Ana Yo
Ranch Hand
Posts: 203
I keep on getting the addresses as ouputs instead of the actual array.

Carey Brown
Bartender
Posts: 5641
56
Try

Not sure how/if this works for 2D arrays.

Ana Yo
Ranch Hand
Posts: 203
How do we do the average of the neighbors if we can't even use 2D arrays and only use 1D arrays?!?!

Ana Yo
Ranch Hand
Posts: 203
This was the error message when I tried to use you sop:
Smooth.java:73: error: method toString in class Object cannot be applied to given types;
System.out.println(image.toString(image));
^
required: no arguments
found: int[][]
reason: actual and formal argument lists differ in length
1 error

Carey Brown
Bartender
Posts: 5641
56
You did not enter what I sent you.

Ana Yo
Ranch Hand
Posts: 203
Smooth.java:73: error: cannot find symbol
System.out.println( Arrays.toString( array ) );
^
symbol:   variable array
location: class Smooth
Smooth.java:73: error: cannot find symbol
System.out.println( Arrays.toString( array ) );
^
symbol:   variable Arrays
location: class Smooth
2 errors

Carey Brown
Bartender
Posts: 5641
56
Look up the Javadocs for the class "Arrays", and find the explanation of the method I gave you.

Ana Yo
Ranch Hand
Posts: 203
Did I get the compile time error becuase I entered it wrong or becuase you were wrong? Also, what explanation?

Carey Brown
Bartender
Posts: 5641
56
I feel like you're wanting to be spoon fed. Do you not know that class names begin with an upper case letter? So "Arrays" is a class. And which method did I tell you to call? And what parameters is the method expecting?
Arrays#toString()

Note that if this doesn't work for 2D arrays you'll have to write your own.

Ana Yo
Ranch Hand
Posts: 203
I know that class names start with capital letters and you want me to call the toString method, so therefore I have to make a toString method?

Ana Yo
Ranch Hand
Posts: 203
Thank you for being patient with me, but I just want to let you know I started java this year. I sometimes forget some bits and pieces, so sorry if I make you keep on reminding me things. I'm not very experienced as like other moderators and coders on this website. Thanks for understanding.

Carey Brown
Bartender
Posts: 5641
56
OK, I checked and 2D arrays are not supported by Arrays#toString(), my appologies. So you'll have to write your own. Since it will be yours you can call it anything you like, e.g.

Ana Yo
Ranch Hand
Posts: 203
No problem, it is you who have been helping me tremendously!

Ana Yo
Ranch Hand
Posts: 203
I'm still a bit confused because for the smooth method, our teacher wants us to use 1D arrays and not "auxillary 2D arrays". How do we do that?

Carey Brown
Bartender
Posts: 5641
56
I revised previous post.

Carey Brown
Bartender
Posts: 5641
56

Ana Yo wrote:I'm still a bit confused because for the smooth method, our teacher wants us to use 1D arrays and not "auxillary 2D arrays". How do we do that?

Not sure why the 1D requirement. I've written this before for some image processing work and I had a source image (a 2D array) and a destination image (also a 2D array). It's not clear why the instance on a 1D array or how to use one in this context.

Ana Yo
Ranch Hand
Posts: 203
This is what my teacher said:
NOTE: When writing this method, you are not permitted
//       to use any auxiliary 2-D array. You can use a 1-D
//       array.

Ana Yo
Ranch Hand
Posts: 203
Is it possible if you can ask other bartenders(if that's what they are called) if they know how to do this?

Ana Yo
Ranch Hand
Posts: 203
Hey Carey Brown! So apparently, we can just replace each cell with the averged neighbor values. Don't worry about the auxillary 2D array and what not!

Marshal
Posts: 24195
54
So you don't even need a 1-D array, then.

Ana Yo
Ranch Hand
Posts: 203
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?

Ana Yo
Ranch Hand
Posts: 203

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

exactly:)

 A day job? In an office? My worst nightmare! Comfort me tiny ad! Become a Java guru with IntelliJ IDEA https://www.jetbrains.com/idea/