• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Average Neighbors of Any Given 2D Array  RSS feed

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 5771
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
 
Ana Yo
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 5771
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
 
Ana Yo
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but i'm not sure how to
 
Carey Brown
Bartender
Posts: 5771
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this right so far?
 
Ana Yo
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
other than the triple = signs ;)
 
Marshal
Posts: 13441
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the shell + countNeighbors(which I made):
 
Carey Brown
Bartender
Posts: 5771
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
UPDATED CODE:
 
Ana Yo
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I keep on getting the addresses as ouputs instead of the actual array.
[[I@3e2ad6a0
[[I@3e2ad6a0
 
Carey Brown
Bartender
Posts: 5771
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try

Not sure how/if this works for 2D arrays.
 
Ana Yo
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ana Yo
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 5771
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look up the Javadocs for the class "Arrays", and find the explanation of the method I gave you.
 
Ana Yo
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did I get the compile time error becuase I entered it wrong or becuase you were wrong? Also, what explanation?
 
Carey Brown
Bartender
Posts: 5771
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 5771
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem, it is you who have been helping me tremendously!
 
Ana Yo
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 5771
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Sheriff
Posts: 24366
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you don't even need a 1-D array, then.
 
Ana Yo
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

exactly:)
 
Normally trees don't drive trucks. Does this tiny ad have a license?
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!