mirror of
https://gitee.com/mymagicpower/AIAS.git
synced 2024-11-30 03:08:24 +08:00
update image util
This commit is contained in:
parent
a90680c8f5
commit
845e3d9425
@ -33,57 +33,25 @@ import java.util.stream.Collectors;
|
|||||||
**/
|
**/
|
||||||
public class ImageUtils {
|
public class ImageUtils {
|
||||||
|
|
||||||
public static Image bufferedImage2DJLImage(BufferedImage img) {
|
/**
|
||||||
|
* BufferedImage图片格式转DJL图片格式
|
||||||
|
* BufferedImage to DJL Image
|
||||||
|
* @author Calvin
|
||||||
|
*/
|
||||||
|
public static Image convert(BufferedImage img) {
|
||||||
return ImageFactory.getInstance().fromImage(img);
|
return ImageFactory.getInstance().fromImage(img);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void saveImages(List<Image> input, List<Image> generated, String path) throws IOException {
|
/**
|
||||||
Path outputPath = Paths.get(path);
|
* 保存BufferedImage图片
|
||||||
Files.createDirectories(outputPath);
|
* Save BufferedImage
|
||||||
|
* @author Calvin
|
||||||
save(generated, "image", outputPath);
|
*/
|
||||||
save(group(input, generated), "stitch", outputPath);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void save(List<Image> images, String name, Path path) throws IOException {
|
|
||||||
for (int i = 0; i < images.size(); i++) {
|
|
||||||
Path imagePath = path.resolve(name + i + ".png");
|
|
||||||
images.get(i).save(Files.newOutputStream(imagePath), "png");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static List<Image> group(List<Image> input, List<Image> generated) {
|
|
||||||
NDList stitches = new NDList(input.size());
|
|
||||||
|
|
||||||
try (NDManager manager = Engine.getInstance().newBaseManager()) {
|
|
||||||
for (int i = 0; i < input.size(); i++) {
|
|
||||||
int scale = 4;
|
|
||||||
int width = scale * input.get(i).getWidth();
|
|
||||||
int height = scale * input.get(i).getHeight();
|
|
||||||
|
|
||||||
NDArray left = input.get(i).toNDArray(manager);
|
|
||||||
NDArray right = generated.get(i).toNDArray(manager);
|
|
||||||
|
|
||||||
left = NDImageUtils.resize(left, width, height, Image.Interpolation.BICUBIC);
|
|
||||||
left = left.toType(DataType.UINT8, false);
|
|
||||||
|
|
||||||
right = right.toType(DataType.UINT8, false);
|
|
||||||
|
|
||||||
stitches.add(NDArrays.concat(new NDList(left, right), 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
return stitches.stream()
|
|
||||||
.map(array -> array.toType(DataType.UINT8, false))
|
|
||||||
.map(array -> ImageFactory.getInstance().fromNDArray(array))
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void saveImage(BufferedImage img, String name, String path) {
|
public static void saveImage(BufferedImage img, String name, String path) {
|
||||||
ai.djl.modality.cv.Image djlImg = ImageFactory.getInstance().fromImage(img);
|
Image djlImg = ImageFactory.getInstance().fromImage(img); // 支持多种图片格式,自动适配
|
||||||
Path outputDir = Paths.get(path);
|
Path outputDir = Paths.get(path);
|
||||||
Path imagePath = outputDir.resolve(name);
|
Path imagePath = outputDir.resolve(name);
|
||||||
|
// OpenJDK 不能保存 jpg 图片的 alpha channel
|
||||||
try {
|
try {
|
||||||
djlImg.save(Files.newOutputStream(imagePath), "png");
|
djlImg.save(Files.newOutputStream(imagePath), "png");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@ -91,9 +59,15 @@ public class ImageUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 保存DJL图片
|
||||||
|
* Save DJL Image
|
||||||
|
* @author Calvin
|
||||||
|
*/
|
||||||
public static void saveImage(Image img, String name, String path) {
|
public static void saveImage(Image img, String name, String path) {
|
||||||
Path outputDir = Paths.get(path);
|
Path outputDir = Paths.get(path);
|
||||||
Path imagePath = outputDir.resolve(name);
|
Path imagePath = outputDir.resolve(name);
|
||||||
|
// OpenJDK 不能保存 jpg 图片的 alpha channel
|
||||||
try {
|
try {
|
||||||
img.save(Files.newOutputStream(imagePath), "png");
|
img.save(Files.newOutputStream(imagePath), "png");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@ -101,23 +75,47 @@ public class ImageUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 保存图片,含检测框
|
||||||
|
* Save BoundingBox Image
|
||||||
|
* @author Calvin
|
||||||
|
*/
|
||||||
public static void saveBoundingBoxImage(
|
public static void saveBoundingBoxImage(
|
||||||
Image img, DetectedObjects detection, String name, String path) throws IOException {
|
Image img, DetectedObjects detection, String name, String path) throws IOException {
|
||||||
|
// Make image copy with alpha channel because original image was jpg
|
||||||
img.drawBoundingBoxes(detection);
|
img.drawBoundingBoxes(detection);
|
||||||
Path outputDir = Paths.get(path);
|
Path outputDir = Paths.get(path);
|
||||||
Files.createDirectories(outputDir);
|
Files.createDirectories(outputDir);
|
||||||
Path imagePath = outputDir.resolve(name);
|
Path imagePath = outputDir.resolve(name);
|
||||||
|
// OpenJDK can't save jpg with alpha channel
|
||||||
img.save(Files.newOutputStream(imagePath), "png");
|
img.save(Files.newOutputStream(imagePath), "png");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 绘制人脸关键点
|
||||||
|
* Draw Face Landmark
|
||||||
|
* @author Calvin
|
||||||
|
*/
|
||||||
|
public static void drawLandmark(Image img, BoundingBox box, float[] array) {
|
||||||
|
for (int i = 0; i < array.length / 2; i++) {
|
||||||
|
int x = getX(img, box, array[2 * i]);
|
||||||
|
int y = getY(img, box, array[2 * i + 1]);
|
||||||
|
Color c = new Color(0, 255, 0);
|
||||||
|
drawImageRect((BufferedImage) img.getWrappedImage(), x, y, 1, 1, c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 画检测框
|
||||||
|
* Draw image rectangle
|
||||||
|
* @author Calvin
|
||||||
|
*/
|
||||||
public static void drawImageRect(BufferedImage image, int x, int y, int width, int height) {
|
public static void drawImageRect(BufferedImage image, int x, int y, int width, int height) {
|
||||||
// 将绘制图像转换为Graphics2D
|
// 将绘制图像转换为Graphics2D
|
||||||
// Convert the drawing image to Graphics2D
|
|
||||||
Graphics2D g = (Graphics2D) image.getGraphics();
|
Graphics2D g = (Graphics2D) image.getGraphics();
|
||||||
try {
|
try {
|
||||||
g.setColor(new Color(246, 96, 0));
|
g.setColor(new Color(246, 96, 0));
|
||||||
// 声明画笔属性 :粗 细(单位像素)末端无修饰 折线处呈尖角
|
// 声明画笔属性 :粗 细(单位像素)末端无修饰 折线处呈尖角
|
||||||
// Declare the pen attribute: thick, no decoration at the end, sharp angle at the intersection
|
|
||||||
BasicStroke bStroke = new BasicStroke(4, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);
|
BasicStroke bStroke = new BasicStroke(4, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);
|
||||||
g.setStroke(bStroke);
|
g.setStroke(bStroke);
|
||||||
g.drawRect(x, y, width, height);
|
g.drawRect(x, y, width, height);
|
||||||
@ -127,15 +125,18 @@ public class ImageUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 画检测框
|
||||||
|
* Draw image rectangle
|
||||||
|
* @author Calvin
|
||||||
|
*/
|
||||||
public static void drawImageRect(
|
public static void drawImageRect(
|
||||||
BufferedImage image, int x, int y, int width, int height, Color c) {
|
BufferedImage image, int x, int y, int width, int height, Color c) {
|
||||||
// 将绘制图像转换为Graphics2D
|
// 将绘制图像转换为Graphics2D
|
||||||
// Convert the drawing image to Graphics2D
|
|
||||||
Graphics2D g = (Graphics2D) image.getGraphics();
|
Graphics2D g = (Graphics2D) image.getGraphics();
|
||||||
try {
|
try {
|
||||||
g.setColor(c);
|
g.setColor(c);
|
||||||
// 声明画笔属性 :粗 细(单位像素)末端无修饰 折线处呈尖角
|
// 声明画笔属性 :粗 细(单位像素)末端无修饰 折线处呈尖角
|
||||||
// Declare the pen attribute: thick, no decoration at the end, sharp angle at the intersection
|
|
||||||
BasicStroke bStroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);
|
BasicStroke bStroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);
|
||||||
g.setStroke(bStroke);
|
g.setStroke(bStroke);
|
||||||
g.drawRect(x, y, width, height);
|
g.drawRect(x, y, width, height);
|
||||||
@ -145,6 +146,11 @@ public class ImageUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 显示文字
|
||||||
|
* Draw image text
|
||||||
|
* @author Calvin
|
||||||
|
*/
|
||||||
public static void drawImageText(BufferedImage image, String text) {
|
public static void drawImageText(BufferedImage image, String text) {
|
||||||
Graphics graphics = image.getGraphics();
|
Graphics graphics = image.getGraphics();
|
||||||
int fontSize = 100;
|
int fontSize = 100;
|
||||||
@ -159,28 +165,25 @@ public class ImageUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 返回外扩人脸 factor = 1, 100%, factor = 0.2, 20%
|
/**
|
||||||
* Returns the enlarged face with factor = 1, 100%, factor = 0.2, 20%
|
* 返回外扩人脸 factor = 1, 100%, factor = 0.2, 20%
|
||||||
* @param img
|
* Returns the enlarged outer face. factor = 1, 100%, factor = 0.2, 20%
|
||||||
* @param box
|
* @author Calvin
|
||||||
* @param factor
|
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public static Image getSubImage(Image img, BoundingBox box, float factor) {
|
public static Image getSubImage(Image img, BoundingBox box, float factor) {
|
||||||
Rectangle rect = box.getBounds();
|
Rectangle rect = box.getBounds();
|
||||||
// 左上角坐标 - Upper left corner coordinates
|
// 左上角坐标 - Top-left corner coordinates
|
||||||
int x1 = (int) (rect.getX() * img.getWidth());
|
int x1 = (int) (rect.getX() * img.getWidth());
|
||||||
int y1 = (int) (rect.getY() * img.getHeight());
|
int y1 = (int) (rect.getY() * img.getHeight());
|
||||||
// 宽度,高度 - width, height
|
// 宽度,高度 - Width, height
|
||||||
int w = (int) (rect.getWidth() * img.getWidth());
|
int w = (int) (rect.getWidth() * img.getWidth());
|
||||||
int h = (int) (rect.getHeight() * img.getHeight());
|
int h = (int) (rect.getHeight() * img.getHeight());
|
||||||
// 左上角坐标 - Upper right corner coordinates
|
// 左上角坐标 - Top-left corner coordinates
|
||||||
int x2 = x1 + w;
|
int x2 = x1 + w;
|
||||||
int y2 = y1 + h;
|
int y2 = y1 + h;
|
||||||
|
|
||||||
// 外扩大100%,防止对齐后人脸出现黑边
|
// 外扩大100%,防止对齐后人脸出现黑边
|
||||||
// Expand by 100% to prevent black edges after alignment
|
// Enlarge by 100% to prevent black edges from appearing on the aligned face
|
||||||
int new_x1 = Math.max((int) (x1 + x1 * factor / 2 - x2 * factor / 2), 0);
|
int new_x1 = Math.max((int) (x1 + x1 * factor / 2 - x2 * factor / 2), 0);
|
||||||
int new_x2 = Math.min((int) (x2 + x2 * factor / 2 - x1 * factor / 2), img.getWidth() - 1);
|
int new_x2 = Math.min((int) (x2 + x2 * factor / 2 - x1 * factor / 2), img.getWidth() - 1);
|
||||||
int new_y1 = Math.max((int) (y1 + y1 * factor / 2 - y2 * factor / 2), 0);
|
int new_y1 = Math.max((int) (y1 + y1 * factor / 2 - y2 * factor / 2), 0);
|
||||||
@ -191,45 +194,23 @@ public class ImageUtils {
|
|||||||
return img.getSubImage(new_x1, new_y1, new_w, new_h);
|
return img.getSubImage(new_x1, new_y1, new_w, new_h);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void drawJoints(Image img, Image subImg, int x, int y, Joints joints) {
|
private static int getX(Image img, BoundingBox box, float x) {
|
||||||
BufferedImage image = (BufferedImage) img.getWrappedImage();
|
|
||||||
Graphics2D g = (Graphics2D) image.getGraphics();
|
|
||||||
int stroke = 2;
|
|
||||||
g.setStroke(new BasicStroke((float) stroke));
|
|
||||||
int imageWidth = subImg.getWidth();
|
|
||||||
int imageHeight = subImg.getHeight();
|
|
||||||
Iterator iterator = joints.getJoints().iterator();
|
|
||||||
|
|
||||||
while (iterator.hasNext()) {
|
|
||||||
Joints.Joint joint = (Joints.Joint) iterator.next();
|
|
||||||
g.setPaint(randomColor().darker());
|
|
||||||
int newX = x + (int) (joint.getX() * (double) imageWidth);
|
|
||||||
int newY = y + (int) (joint.getY() * (double) imageHeight);
|
|
||||||
g.fillOval(newX, newY, 10, 10);
|
|
||||||
}
|
|
||||||
|
|
||||||
g.dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Color randomColor() {
|
|
||||||
return new Color(RandomUtils.nextInt(255));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void drawBoundingBoxImage(Image img, DetectedObjects detection) {
|
|
||||||
img.drawBoundingBoxes(detection);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int getX(Image img, BoundingBox box) {
|
|
||||||
Rectangle rect = box.getBounds();
|
Rectangle rect = box.getBounds();
|
||||||
// 左上角x坐标 - Upper left corner x coordinate
|
// 左上角坐标 - Top-left corner coordinates
|
||||||
int x = (int) (rect.getX() * img.getWidth());
|
int x1 = (int) (rect.getX() * img.getWidth());
|
||||||
return x;
|
// 宽度 - Width
|
||||||
|
int w = (int) (rect.getWidth() * img.getWidth());
|
||||||
|
|
||||||
|
return (int) (x * w + x1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int getY(Image img, BoundingBox box) {
|
private static int getY(Image img, BoundingBox box, float y) {
|
||||||
Rectangle rect = box.getBounds();
|
Rectangle rect = box.getBounds();
|
||||||
// 左上角y坐标 - Upper left corner y coordinate
|
// 左上角坐标 - Top-left corner coordinates
|
||||||
int y = (int) (rect.getY() * img.getHeight());
|
int y1 = (int) (rect.getY() * img.getHeight());
|
||||||
return y;
|
// 高度 - Height
|
||||||
|
int h = (int) (rect.getHeight() * img.getHeight());
|
||||||
|
|
||||||
|
return (int) (y * h + y1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user