-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathImageSaturate.java
97 lines (82 loc) · 3.48 KB
/
ImageSaturate.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com.biubiu.example;
import org.bytedeco.javacpp.BytePointer;
import org.bytedeco.javacpp.opencv_core.Mat;
import org.bytedeco.javacv.CanvasFrame;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.OpenCVFrameConverter;
import javax.swing.*;
import static org.bytedeco.javacpp.opencv_imgcodecs.IMREAD_COLOR;
import static org.bytedeco.javacpp.opencv_imgcodecs.imread;
/**
* @author :张音乐
* @date :Created in 2021/11/10 下午8:33
* @description:图像饱和度
* @email: [email protected]
* @version:
*/
public class ImageSaturate {
public static void main(String[] args) {
String filepath = "/home/yinyue/opencv/113958-153525479855be.jpg";
Mat img = imread(filepath, IMREAD_COLOR);
if(img.empty()) {
System.out.println("cannot open file");
return;
}
OpenCVFrameConverter.ToMat converter = new OpenCVFrameConverter.ToMat();
// 显示原图
Mat origin = img.clone();
CanvasFrame originCanvas = new CanvasFrame("原始图", 1);
originCanvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Frame originFrame = converter.convert(origin);
originCanvas.showImage(originFrame);
Mat target = doSaturate(img, 50);
Frame targetFrame = converter.convert(target);
CanvasFrame targetCanvas = new CanvasFrame("处理后", 1);
targetCanvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
targetCanvas.showImage(targetFrame);
}
private static Mat doSaturate(Mat img, int percent) {
float increment = percent * 1.0f / 100;
int height = img.rows();
int width = img.cols();
for(int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
BytePointer ptr = img.ptr(i, j);
int b = ptr.get(0) < 0 ? (ptr.get(0) + 256) : ptr.get(0);
int g = ptr.get(1) < 0 ? (ptr.get(1) + 256) : ptr.get(1);
int r = ptr.get(2) < 0 ? (ptr.get(2) + 256) : ptr.get(2);
float max = Math.max(b, Math.max(g, r));
float min = Math.min(b, Math.min(g, r));
float delta = (max - min) / 255;
if(delta == 0) {
continue;
}
float value = (max + min) / 255;
float L = value / 2;
float S = 0f, alpha;
if(L < 0.5) {
S = delta / value;
}
if(increment >= 0) {
if((increment + S) >= 1) {
alpha = S;
} else {
alpha = 1 - increment;
}
alpha = 1 / alpha - 1;
int B = (int) (b + (b - L * 255) * alpha);
int G = (int) (g + (g - L * 255) * alpha);
int R = (int) (r + (r - L * 255) * alpha);
ptr.put((byte) Math.max(0, Math.min(B, 255)), (byte) Math.max(0, Math.min(G, 255)), (byte) Math.max(0, Math.min(R, 255)));
} else {
alpha = increment;
int B = (int) (b + (b - L * 255) * alpha);
int G = (int) (g + (g - L * 255) * alpha);
int R = (int) (r + (r - L * 255) * alpha);
ptr.put((byte) Math.max(0, Math.min(B, 255)), (byte) Math.max(0, Math.min(G, 255)), (byte) Math.max(0, Math.min(R, 255)));
}
}
}
return img;
}
}