Skip to content

Instantly share code, notes, and snippets.

@vector4wang
Created September 8, 2020 01:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vector4wang/1c04608781496dea839964be7ddc5fb8 to your computer and use it in GitHub Desktop.
Save vector4wang/1c04608781496dea839964be7ddc5fb8 to your computer and use it in GitHub Desktop.
[opencv 融合图]
public class OpenCVUtils {
private final static List<List<Integer>> SIX_SIZE = new ArrayList<>();
private final static List<List<Integer>> TEN_SIZE = new ArrayList<>();
static {
SIX_SIZE.add(new ArrayList<Integer>() {{
add(0);
add(1);
}});
SIX_SIZE.add(new ArrayList<Integer>() {{
add(2);
add(3);
}});
SIX_SIZE.add(new ArrayList<Integer>() {{
add(4);
add(5);
}});
TEN_SIZE.add(new ArrayList<Integer>() {{
add(0);
add(1);
add(2);
}});
TEN_SIZE.add(new ArrayList<Integer>() {{
add(3);
add(4);
add(5);
add(6);
}});
TEN_SIZE.add(new ArrayList<Integer>() {{
add(7);
add(8);
add(9);
}});
}
private final static String ZH = "zh";
private final static String ZWB = "zwb";
private final static String ZZC = "zzc";
private final static String SRC = "src"; // 原图目录
private final static String ECO = "eco"; // 切图目录
private final static String YOLO_FUSE = "yolo_fuse"; // 融合图目录
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
/**
* private int xmin = 862;
* private int ymin = 635;
* private int xmax = 1235;
* private int ymax = 873;
*/
// /nfsc/INC_VSAP_CLOS/argus-automl/merge
String rootPath = "/sadf/merge";
initTypePath(rootPath);
//862 635 1235 873
RectPoint rectPoint = RectPoint.builder().xmin(862).ymin(635).xmax(1235).ymax(873).build();
screenShot(rootPath, "zh", "sdf", "0", 0, rectPoint);
imageMerge(rootPath, "zh", "sdf");
}
public static void initTypePath(String rootPath) {
File mergePath = FileUtil.mkdir(rootPath);// 初始化根目录
File mkdir = FileUtil.mkdir(mergePath + File.separator + ZH);
initSubPath(mkdir.getAbsolutePath());
log.info(mkdir.getAbsolutePath());
File mkdir1 = FileUtil.mkdir(mergePath + File.separator + ZWB);
initSubPath(mkdir1.getAbsolutePath());
File mkdir2 = FileUtil.mkdir(mergePath + File.separator + ZZC);
initSubPath(mkdir2.getAbsolutePath());
log.info("initTypePath success");
}
private static void initSubPath(String absolutePath) {
FileUtil.mkdir(absolutePath + File.separator + SRC);
FileUtil.mkdir(absolutePath + File.separator + ECO);
File mkdir = FileUtil.mkdir(absolutePath + File.separator + YOLO_FUSE);
log.info(mkdir.getAbsolutePath());
}
/**
* @param rootPath 存放原图、切图和融合图的根目录
* @param type 原图的来源类型
* @param srcPathName 来源图的文件名
* @param lable 标注中的label值
* @param index 标注中的坐标点的下标值如,0 1 2 3
* @param rectPoint 标注中的坐标
*/
public static void screenShot(String rootPath, String type, String srcPathName, String lable, int index, RectPoint rectPoint) {
String srcPath = rootPath+ File.separator + type + File.separator + SRC + File.separator + srcPathName;
List<File> files = FileUtil.loopFiles(srcPath);
String formatPrefix = "";
if ("zh".equals(type) || "wd".equals(type)) {
formatPrefix = "roi_image_%05d.png";
} else {
formatPrefix = "ROI_image_%05d.jpg";
}
String destPath = rootPath+ File.separator + type + File.separator + ECO + File.separator + lable + File.separator + srcPathName + "_" + index;
File destFileDir = FileUtil.mkdir(destPath);
for (int i = 0; i < files.size(); i++) {
String imageName = String.format("image_%05d.png", (i + 1));
String absPath = srcPath + File.separator + imageName;
Mat imread = Imgcodecs.imread(absPath);
Rect rect = new Rect(new Point(rectPoint.getXmin(), rectPoint.getYmin()), new Point(rectPoint.getXmax(), rectPoint.getYmax()));
Mat cutImage = cutImage(imread, rect);
String adjustROIName = String.format(formatPrefix, (i + 1));
String ecoImagePath = destFileDir + File.separator + adjustROIName;
Imgcodecs.imwrite(ecoImagePath, cutImage);
}
}
/**
* @param rootPath 存放原图、切图和融合图的根目录
* @param type 原图的来源类型
* @param srcPathName 来源图的文件名
*/
public static void imageMerge(String rootPath, String type, String srcPathName) {
String srcPath = rootPath +File.separator + type + File.separator + SRC + File.separator + srcPathName;
String imageSuffix = "";
if ("zh".equals(type) || "wd".equals(type)) {
imageSuffix = ".png";
} else {
imageSuffix = ".jpg";
}
List<File> files = FileUtil.loopFiles(srcPath);
List<List<Integer>> listYolo = new ArrayList<>();
if (files.size() != 6 && files.size() != 10) {
return;
}
if (files.size() == 6) {
listYolo = SIX_SIZE;
}
if (files.size() == 10) {
listYolo = TEN_SIZE;
}
List<Mat> imageListForyolo = new ArrayList<>();
for (int i = 0; i < files.size(); i++) {
String imageName = String.format("image_%05d.png", (i + 1));
String absPath = srcPath + File.separator + imageName;
Mat imread = Imgcodecs.imread(absPath);
Imgproc.cvtColor(imread, imread, Imgproc.COLOR_RGB2GRAY);
imageListForyolo.add(imread);
}
String tempPath = files.get(0).getAbsolutePath();
Mat imread = Imgcodecs.imread(tempPath);
int width = imread.width();
int height = imread.height();
List<Mat> mv = new ArrayList<>();
long s = System.currentTimeMillis();
for (List<Integer> integers : listYolo) {
Mat temp = Mat.zeros(height, width, CV_32FC1);
for (Integer index : integers) {
Mat mat = imageListForyolo.get(index);
mat.convertTo(mat, CV_32FC1);
Core.add(temp, mat, temp);
}
Mat temp2 = Mat.zeros(height, width, imageListForyolo.get(0).type());
double alpha = (double) 1 / integers.size();
Core.scaleAdd(temp, alpha, temp2, temp);
temp.convertTo(temp, CV_8UC1);
mv.add(temp);
}
Mat dest = new Mat();
Core.merge(mv, dest);
String destPath = rootPath + File.separator + type + File.separator + YOLO_FUSE;
Imgcodecs.imwrite(destPath + File.separator + srcPathName + imageSuffix, dest);
long e = System.currentTimeMillis();
log.info("imageMerge 耗时: {} ms", (e - s));
}
/**
* 根据坐标截取目标图
*
* @param src
* @param rect
* @return
*/
public static Mat cutImage(Mat src, Rect rect) {
//图片裁剪
Mat src_roi = new Mat(src, rect);
Mat cutImage = new Mat();
src_roi.copyTo(cutImage);
return cutImage;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment