Reputation: 123
Write a function called "smooth", which blurs the picture - a double array represents the group of pixels of the picture. Smoothing a picture for a parameter n, is taking all pixel in the picture to be the average of nn neighbors. Meaning that is looking at a nn square which the current pixel is his center, and replace the pixel with the average of the neighbors.
My attempt: So I have written the following:
public void smooth(int n) {
int N = (n-1)/2;
for (int x = N; x<frame.length-N;x++){
for (int y=N;y<frame[x].length-N;y++){
frame[x][y]=average(x,y,N,this.frame);
}
}
//for edges:
for (int x = 0; x<frame.length;x++) {
for (int y = 0; y < frame[0].length; y++) {
if (FilterOutOfBounds(x,y,this.frame,N)!=-1) frame[x][y] = FilterOutOfBounds(x, y, this.frame, N);
}
}
}
public static int average(int x, int y, int N,int[][] frame){
int sum = 0, cnt=0;
for (int i = x-N;i<x+N;i++){
for (int j = y-N;j<y+N;j++){
sum+=frame[i][j]; cnt++;
}
}
return sum/cnt;
}
public static int FilterOutOfBounds(int x, int y, int[][] frame, int N){
int cnt = 0,sum=0;
if (frame[0].length-1-y<N && x!=frame.length-1){
for (int j = y;j>=y-1;j--){
for (int i = x;i<=x+1;i++){
cnt++;
sum+=frame[i][j];
}
}
}
else if (y<N && x!=frame.length-1 ){
for (int j = y;j<=y+1;j++){
for (int i = x;i<=x+1;i++){
cnt++;
sum+=frame[i][j];
}
}
}
else if(frame.length-x<N && y!=0){
for (int i = x;i>=x-1;i--){
for (int j = y;j>=y-1;j--){
cnt++;
sum+=frame[i][j];
}
}
}
else if (x<N && y!=frame.length-1){
for (int i = x;i<=x+1;i++){
for (int j = y;j<=y+1;j++){
cnt++;
sum+=frame[i][j];
}
}
}
if (cnt==0) return -1;
return sum/cnt;
}
However, it doesn't blur the edges for a certain reason, and I can't think why. Therefore, I will be glad for some help. Thanks!
Upvotes: 0
Views: 551
Reputation: 86
I think there are some issues with your code. First, you are directly updating the frame
variable, which will accumulate the average values for your calculation. I believe this is also an unwanted side-effect.
An other thing is, that the FilterOutOfBounds
method uses frame.length
against y
, which shall be frame[0].length
, as this refers to y
for a rectangle. Also these inner calculations does not handle N
, so the x+1
and y+1
in the for loops are probably misconcepts, they should be x+N
and y+N
or something similar.
I have created a more simple solution for your question. Basically I have modified the avarage
method, to be able to handle edge cases and let the smooth
for loop to iterate over every cell of frame
. Worked for me:
public void smooth( int n ) {
int N = (n - 1) / 2;
int[][] blurred = new int[frame.length][frame[0].length];
for ( int x = 0; x < frame.length; x++ ) {
for ( int y = 0; y < frame[x].length; y++ ) {
blurred[x][y] = average(x, y, N, frame);
}
}
frame = blurred;
}
public static int average( int x, int y, int N, int[][] frame ) {
int sum = 0, cnt = 0;
for ( int i = Math.max(x - N, 0); i <= Math.min(x + N, frame.length - 1); i++ ) {
for ( int j = Math.max(y - N, 0); j <= Math.min(y + N, frame[i].length - 1); j++ ) {
sum += frame[i][j];
cnt++;
}
}
return sum / cnt;
}
Upvotes: 1