-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathImageLightness.java
74 lines (64 loc) · 2.8 KB
/
ImageLightness.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
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 下午9:12
* @description:图像明度
* @email: [email protected]
* @version:
*/
public class ImageLightness {
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 = doLightness(img, 50);
Frame targetFrame = converter.convert(target);
CanvasFrame targetCanvas = new CanvasFrame("处理后", 1);
targetCanvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
targetCanvas.showImage(targetFrame);
}
private static Mat doLightness(Mat img, float percent) {
float alpha = percent / 100;
alpha = Math.max(-1.f, Math.min(1.f, alpha));
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);
if(alpha >= 0) {
int B = (int) (b * (1 - alpha) + 255 * alpha);
int G = (int) (g * (1 - alpha) + 255 * alpha);
int R = (int) (r * (1 - alpha) + 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 {
int B = (int) (b * (1 + alpha));
int G = (int) (g * (1 + alpha));
int R = (int) (r * (1 + 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;
}
}