欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

图像处理

程序员文章站 2022-03-21 13:53:06
...
package com.fokantech.mian;

import com.fokantech.util.CoreUtil;
import com.fokantech.util.CreateXmlUtil;
import com.fokantech.util.FormatConversion;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

/**
 *  输入背景
 */
public class SmartSiteAtomization {
    static{
        System.load(CoreUtil.getCore());
    }


    static ArrayList<String> imageArray = new ArrayList<>();
    static ArrayList<String> backgroundArray = new ArrayList<>();

    public static ArrayList<String> getImagePaht(String imgpath){
        File file = new File(imgpath);
        String[] list = file.list();
        for (String s : list) {
            String path = file + "\\" + s ;
            imageArray.add(path);
        }
        return imageArray;
    }
    public static ArrayList<String> getBackgroundPaht(String BackgroundPaht){
        File file = new File(BackgroundPaht);
        String[] list = file.list();
        for (String s : list) {
            String path = file + "\\" + s ;
            backgroundArray.add(path);
        }
        return backgroundArray;
    }

    public static int getMax(){
        Integer [] items = new Integer[]{450,470,480,500,520,550};
        int a= (int) Math.floor(Math.random()*items.length);
        int i = items[a];
        return i;
    }
    public static int getMin(){
        Integer [] items = new Integer[]{350,370,380,400,420,450};
        int a= (int) Math.floor(Math.random()*items.length);
        int i = items[a];
        return i;
    }
    public static int getSclace(){
        Integer [] items = new Integer[]{5,6,7,8,9,10};
        int a= (int) Math.floor(Math.random()*items.length);
        int i = items[a];
        return i;
    }
    public static double getAlpha(){
        double [] items = new double[]{0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8};
        int a= (int) Math.floor(Math.random()*items.length);
        double i = items[a];
        return i;
    }

    public static void main(String[] args) throws IOException {
        for (int i = 1; i <= 3000; i++) {
            int sclace = getSclace();
            double alpha = getAlpha();
            ArrayList<String> imageArray1 = getImagePaht("C:\\Users\\FKT00093\\Desktop\\image");
            ArrayList<String> backgroundPathArray = getBackgroundPaht("C:\\Users\\FKT00093\\Desktop\\background");

            Random random = new Random();
            String backgroundImage = backgroundPathArray.get(random.nextInt(backgroundPathArray.size()));

            Collections.shuffle(imageArray1);
            int randomNum = 5;
            List<String> strings = imageArray1.subList(0, randomNum);
            BufferedImage bufferedImage = ImageIO.read(new File(backgroundImage));
            Graphics2D bgG2 = bufferedImage.createGraphics();


            Document document = CreateXmlUtil.createXML();
            Element annotation = document.addElement("annotation");
            Element folder = annotation.addElement("folder");
            folder.setText("0");
            Element filename = annotation.addElement("filename");
            filename.setText(String.valueOf(i)+".jpg");
            Element size = annotation.addElement("size");
            Element width1 = size.addElement("width");
            Element height1 = size.addElement("height");
            width1.setText(String.valueOf(bufferedImage.getWidth()));
            height1.setText(String.valueOf(bufferedImage.getHeight()));

            for (int j = 0; j <strings.size() ; j++) {
                String minImage = strings.get(j);
                //Mat mat = ImageReSize.getReSize(minImage);
                int i1 = minImage.indexOf("image\\");
                int i2 = minImage.indexOf("_");
                String labelName = minImage.substring(i1 + 6, i2);
                System.out.println(labelName);
                BufferedImage buff = ImageIO.read(new File(minImage));//ImageforMat.Mat2BufImg(mat, ".jpg");
                Graphics2D g = (Graphics2D) bufferedImage.getGraphics();

                if (!labelName.equalsIgnoreCase("safetyhat")){
                    int width = getMax();
                    int hieght = getMin();

                    Random b = new Random();
                    int min = 0;
                    int max = bufferedImage.getWidth()  - 1;
                    int randomNum2 = b.ints(min, (max + 1)).findFirst().getAsInt();

                    int mi = 0;
                    int ma = bufferedImage.getHeight() - (hieght * 2) - 1;
                    int randomNum1 = b.ints(mi, (ma + 1)).findFirst().getAsInt();

                    int y = randomNum1;
                    int x = bufferedImage.getWidth() / 6 * j;
                    g.drawImage(buff, x, y, width, hieght, null);
                    g.dispose();

                    Element object = annotation.addElement("object");
                    Element name = object.addElement("name");
                    name.setText(labelName);
                    Element bndbox = object.addElement("bndbox");
                    Element xmin = bndbox.addElement("xmin");
                    xmin.setText(String.valueOf(x));
                    Element ymin = bndbox.addElement("ymin");
                    ymin.setText(String.valueOf(y));
                    Element xmax = bndbox.addElement("xmax");
                    xmax.setText(String.valueOf(x+width));
                    Element ymax = bndbox.addElement("ymax");
                    ymax.setText(String.valueOf(y+hieght));
                }else {
                    int width = 150;
                    int height = 150;
                    Random b = new Random();
                    int mi = 0;
                    int ma = bufferedImage.getHeight()  - 1;
                    int randomNum1 = b.ints(mi, (ma + 1)).findFirst().getAsInt();

                    int x = bufferedImage.getWidth() / 6 * j;
                    int y = randomNum1;

                    g.drawImage(buff, x, y, width, height, null);
                    g.dispose();
                    Element object = annotation.addElement("object");
                    Element name = object.addElement("name");
                    name.setText(labelName);
                    Element bndbox = object.addElement("bndbox");
                    Element xmin = bndbox.addElement("xmin");
                    xmin.setText(String.valueOf(x));
                    Element ymin = bndbox.addElement("ymin");
                    ymin.setText(String.valueOf(y));
                    Element xmax = bndbox.addElement("xmax");
                    xmax.setText(String.valueOf(x+width));
                    Element ymax = bndbox.addElement("ymax");
                    ymax.setText(String.valueOf(y+height));

                }
            }
            //遮罩层大小
            int coverWidth = bufferedImage.getWidth();
            int coverHeight = bufferedImage.getHeight();
            //遮罩层位置
            int coverX = 0;
            int coverY = bufferedImage.getHeight() - coverHeight;

            //创建黑色遮罩层
            BufferedImage cover = new BufferedImage(coverWidth, coverHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D coverG2 = (Graphics2D)cover.getGraphics();
            coverG2.setColor(new Color(131,141,143));
            coverG2.fillRect(0,0, coverWidth, coverHeight);
            coverG2.dispose();
            //开启透明度
            bgG2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, (float) alpha));
            //描绘
            bgG2.drawImage(cover, coverX, coverY, coverWidth, coverHeight, null);
            //结束透明度
            bgG2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
            bgG2.dispose();
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("UTF-8");
            File file = new File("C:\\Users\\FKT00093\\Desktop\\newImage3\\"+"2_"+i+".xml");
            XMLWriter writer = new XMLWriter(new FileOutputStream(file), format);
            writer.write(document);
            writer.close();
            Mat mat = FormatConversion.BufImg2Mat(bufferedImage, BufferedImage.TYPE_INT_RGB, CvType.CV_8UC3);
            Mat dst = mat.clone();
            Imgproc.blur(mat,dst,new Size(sclace,sclace));
            Imgcodecs.imwrite("C:\\Users\\FKT00093\\Desktop\\newImage3\\"+"2_"+i+".jpg",dst);
        }
    }
}
相关标签: Java绘制图像