Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
77 lines (64 loc) · 2.11 KB

File metadata and controls

77 lines (64 loc) · 2.11 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package image2d;
import java.awt.Color;
import java.awt.image.BufferedImage;
/**
*
* @author pratchaya
*/
public class Threshold {
public static BufferedImage apply(BufferedImage _image) {
int _r, p, r, g, b;
double threshold = otsuTreshold(_image);
BufferedImage imageOutput = new BufferedImage(_image.getWidth(), _image.getHeight(), BufferedImage.TYPE_3BYTE_BGR); // Set initial BufferedImage
for (int i = 0; i < _image.getWidth(); i++) {
for (int j = 0; j < _image.getHeight(); j++) {
// Get pixels
r = RGB.getRGBW(_image, i, j);
r = ((r >> 16) & 0xff);
if (r > threshold) {
p = 255;
} else {
p = 0;
}
p = (p << 16) | (p << 8) | (p);
imageOutput.setRGB(i, j, p);
}
}
return imageOutput;
}
public static int otsuTreshold(BufferedImage _image) {
int _histogram[] = Histogram.histogtam(_image);
int total = _image.getWidth() * _image.getHeight();
float sum = 0;
for (int i = 0; i < 256; i++) {
sum += i * _histogram[i];
}
float sum_bg = 0;
int wight_bg = 0, wight_fg = 0;
float varMax = 0;
int threshold = 0;
for (int i = 0; i < 256; i++) {
wight_bg += _histogram[i];
if (wight_bg == 0) {
continue;
}
wight_fg = total - wight_bg;
if (wight_fg == 0) {
break;
}
sum_bg += (float) (i * _histogram[i]);
float mean_bg = sum_bg / wight_bg;
float mean_fg = (sum - sum_bg) / wight_fg;
float varBetween = (float) wight_bg * (float) wight_fg * (mean_bg - mean_fg) * (mean_bg - mean_fg);
if (varBetween > varMax) {
varMax = varBetween;
threshold = i;
}
}
return threshold;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.