-
Notifications
You must be signed in to change notification settings - Fork 0
/
MedianFilter.java
58 lines (48 loc) · 1.88 KB
/
MedianFilter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package sample.use.my.sprawozdanie1;
import sample.use.my.sprawozdanie1.SaltPepper;
import java.util.Arrays;
import kimage.image.Image;
import kimage.plugin.Plugin;
import kimage.utils.gui.ImageFrame;
/**
*
* @author Weronika
*/
public class MedianFilter extends Plugin {
private int dimension = 5;
public void process(Image imgIn, Image imgOut) {
int[][] pic = new int[imgIn.getWidth()][imgIn.getHeight()];
int width = imgIn.getWidth();
int height = imgIn.getHeight();
// szerokość "obwódki" maski
int frame_size = dimension / 2;
// ilość pikseli w masce
int size=dimension*dimension;
// iteracja po wszystkich pikselach, które mają pełną maskę
for (int i = frame_size; i < width - frame_size; ++i) {
for (int j = frame_size; j < height - frame_size; ++j) {
int[] tab = new int[size];
int x = 0;
for (int a = i - frame_size; a <= i + frame_size; ++a) {
for (int b = j - frame_size; b <= j + frame_size; ++b) {
// zapamiętywanie w pikseli wartości z maski
tab[x++] = imgIn.getRed(a, b);
}
}
// sortowanie maski i wybór mediany jako nowej wartości piksela
Arrays.sort(tab);
pic[i][j] = tab[size/2+1];
}
}
for (int i = frame_size; i < width - frame_size; ++i) {
for (int j = frame_size; j < height - frame_size; ++j) {
imgOut.setRGB(i, j, pic[i][j], pic[i][j], pic[i][j]);
}
}
}
}