package cz.vutbr.feec.imageprocessing.op.visualize; import ij.ImagePlus; import ij.process.ImageConverter; import ij.process.ImageProcessor; import java.awt.Color; import java.util.Collections; import java.util.List; import java.util.Vector; import java.lang.String; import com.rapidminer.operator.Operator; import com.rapidminer.operator.OperatorDescription; import com.rapidminer.operator.OperatorException; import com.rapidminer.operator.ports.InputPort; import com.rapidminer.operator.ports.OutputPort; import com.rapidminer.operator.ports.metadata.GenerateNewMDRule; import com.rapidminer.operator.ports.metadata.MetaData; import com.rapidminer.operator.ports.metadata.SimplePrecondition; import com.rapidminer.parameter.ParameterTypeCategory; import com.rapidminer.parameter.ParameterTypeInt; import cz.vutbr.feec.imageprocessing.IOObject.ImagePlusBaseIOObject; import cz.vutbr.feec.imageprocessing.IOObject.ImagePlusClrIOObject; import cz.vutbr.feec.imageprocessing.IOObject.PointsIOObject; import cz.vutbr.feec.imageprocessing.IOObject.innerObject.InterestArea; public class GroupVisualizer extends Operator{ protected InputPort interestPoints = getInputPorts().createPort("Points"); protected InputPort inImg = getInputPorts().createPort("Image Plus"); protected OutputPort outImg = getOutputPorts().createPort("Image plus"); private static final String PARAM_MIN_PEOPLE = "Min. people"; private static final String PARAM_DISTANCE = "Distance"; private static final String[] PARAM_DISPLAY = { "All", "Group", "Objects"}; private static final String[] PARAM_COLOR = { "Black", "Blue", "Green", "Orange", "Red", "White", "Yellow"}; private static final String PARAM_COLOR_OBJECTS = "Object color"; private static final String PARAM_COLOR_GROUP = "Group color"; private static final String PARAM_DISPLAY_D = "Display"; public GroupVisualizer(OperatorDescription description) { super(description); interestPoints.addPrecondition(new SimplePrecondition(interestPoints, new MetaData(PointsIOObject.class))); inImg.addPrecondition(new SimplePrecondition(inImg, new MetaData( ImagePlusBaseIOObject.class))); getTransformer().addRule( new GenerateNewMDRule(outImg, ImagePlusBaseIOObject.class)); } Vector body = new Vector(); Vector novebody = new Vector(); Vector bodyX = new Vector(4); Vector bodyY = new Vector(4); public void doWork() throws OperatorException { PointsIOObject points = interestPoints.getData(); ImagePlusBaseIOObject imp = inImg.getData(); if (!imp.isColor()) { // Convert to color ImageConverter ic = new ImageConverter(imp.getImage()); ic.convertToRGB(); } ImageProcessor ip = imp.getImage().getProcessor(); switch (getParameterAsInt(PARAM_COLOR_OBJECTS)) { case 0: ip.setColor(Color.BLACK); break; case 1: ip.setColor(Color.BLUE); break; case 2: ip.setColor(Color.GREEN); break; case 3: ip.setColor(Color.ORANGE); break; case 4: ip.setColor(Color.RED); break; case 5: ip.setColor(Color.WHITE); break; case 6: ip.setColor(Color.YELLOW); break; } InterestArea moje = null, moje2 =null,moje3 = null; int kapacita = 0,Ax =0, Ay = 0, Bx =0, By=0 , x =0, y =0, minheight =0; double vysledek = 0; double VABx = 0, VABy = 0; int minX = Integer.MAX_VALUE, minY = Integer.MAX_VALUE, maxX =0, maxY =0, minXy =0, minYx =0, maxYx =0, maxXy = 0; int minXwidth = 0, maxYheight = 0; String pocet = "Number of objects in the group: "; for (InterestArea point : points) { body.add(new InterestArea(point.x,point.y,point.height,point.width)); //zápis všech bodů zájmu } for (int m = 0; m < body.size(); m++ ){ //algoritmus porovnávající vektorové vzdálenosti for (int n = 0; n < body.size(); n++ ){ if (n != m){ moje = body.get(m); Ax = moje.getX(); Ay = moje.getY(); System.out.print("\n"); System.out.print("Ax: "); System.out.print(Ax); System.out.print("\n"); System.out.print("Ay: "); System.out.print(Ay); moje2 = body.get(n); Bx = moje2.getX(); By = moje2.getY(); System.out.print("\n"); System.out.print("Bx: "); System.out.print(Bx); System.out.print("\n"); System.out.print("By: "); System.out.print(By); System.out.print("\n"); VABx = Bx - Ax; VABy = By - Ay; vysledek = Math.sqrt(Math.pow(VABx,2) + Math.pow(VABy,2)); //výpočet vektorů a velikosti vektoru System.out.print("Vector length: "); System.out.print(vysledek); System.out.print("\n"); if (vysledek < getParameterAsDouble(PARAM_DISTANCE) && vysledek != 0) { novebody.add(new InterestArea(Ax, Ay,moje.getWidth(),moje.getHeight())); //zápis bodů zájmu patřících do skupiny break; } } } } if (novebody.size() >= getParameterAsInt(PARAM_MIN_PEOPLE)) {// podmínka parametru min. people for (int i=0; i < novebody.size(); i++) { // porovnávání nejmenších a největších hodnot bodů zájmu na osách x a y moje3 = novebody.get(i); x = moje3.getX(); if (x < minX){ minX = x; minXy = moje3.getY(); minXwidth = moje3.getWidth(); minheight = moje3.getHeight(); } if (x > maxX){ maxX = x; maxXy = moje3.getY(); } y = moje3.getY(); if (y < minY){ minY = y; minYx = moje3.getX(); } if (y > maxY){ maxY = y; maxYx = moje3.getX(); maxYheight = moje3.getHeight(); } if (getParameterAsInt(PARAM_DISPLAY_D) == 0 || (getParameterAsInt(PARAM_DISPLAY_D) == 2 )) { ip.drawRect(x - moje3.getHeight()/2, y - moje3.getWidth()/2, moje3.getHeight(), moje3.getWidth()); } //vykreslení bodů zájmu patřících do skupiny,pokud metody getHeight() a getWidth() nefungují, je třeba je implementovat do InterestArea.java } /*System.out.print("Minimalni body"); System.out.print("\n"); System.out.print("minX: "); System.out.print(minX); System.out.print("\n"); System.out.print("minXy: "); System.out.print(minXy); System.out.print("\n"); System.out.print("minY: "); System.out.print(minY); System.out.print("\n"); System.out.print("minYx: "); System.out.print(minYx); System.out.print("\n"); System.out.print("Maximalni body"); System.out.print("\n"); System.out.print("maxX: "); System.out.print(maxX); System.out.print("\n"); System.out.print("maxXy: "); System.out.print(maxXy); System.out.print("\n"); System.out.print("maxY: "); System.out.print(maxY); System.out.print("\n"); System.out.print("maxYx: "); System.out.print(maxYx); System.out.print("\n"); System.out.print(vykresli); System.out.print("\n");*/ kapacita = novebody.size(); pocet = pocet.concat(Integer.toString(kapacita)); System.out.print(pocet); bodyX.add(minX); bodyX.add(minYx); bodyX.add(maxX); bodyX.add(maxYx); bodyY.add(minY); bodyY.add(minXy); bodyY.add(maxY); bodyY.add(maxXy); Collections.sort(bodyX); Collections.sort(bodyY); //seřazení hodnot na osách x a y int ABx = bodyX.get(0) - bodyX.get(0); int ABy = bodyY.get(3) - bodyY.get(0); int ACx = bodyX.get(3) - bodyX.get(0); int ACy = bodyY.get(0) - bodyY.get(0); double width = Math.sqrt(Math.pow(ABx,2) + Math.pow(ABy,2)); int Intwidth = (int)width; double height = Math.sqrt(Math.pow(ACx,2) + Math.pow(ACy,2)); int Intheight = (int)height; switch (getParameterAsInt(PARAM_COLOR_GROUP)) { case 0: ip.setColor(Color.BLACK); break; case 1: ip.setColor(Color.BLUE); break; case 2: ip.setColor(Color.GREEN); break; case 3: ip.setColor(Color.ORANGE); break; case 4: ip.setColor(Color.RED); break; case 5: ip.setColor(Color.WHITE); break; case 6: ip.setColor(Color.YELLOW); break; } if (getParameterAsInt(PARAM_DISPLAY_D) <= 1){ ip.drawRect(bodyX.get(0) - minheight/2 , bodyY.get(0) - minXwidth/2, Intheight + maxYheight, Intwidth + minXwidth); ip.drawString(pocet, ip.getWidth() - ip.getStringWidth(pocet), ip.getHeight()); } //vykreslení skupiny } bodyX.clear(); bodyY.clear(); novebody.clear(); body.clear(); ImagePlusClrIOObject resultImage = null; ImagePlus imgPlus = new ImagePlus("ip_visual", ip); resultImage = new ImagePlusClrIOObject("ip_visual", imgPlus, imp.getFitnessMask()); outImg.deliver(resultImage); } @Override public List getParameterTypes() { List types = super.getParameterTypes(); types.add(new ParameterTypeCategory(PARAM_DISPLAY_D, "Display configuration", PARAM_DISPLAY, 0)); types.add(new ParameterTypeCategory(PARAM_COLOR_OBJECTS, "Color of detected objects", PARAM_COLOR, 0)); types.add(new ParameterTypeCategory(PARAM_COLOR_GROUP, "Color of detected group", PARAM_COLOR, 0)); types.add(new ParameterTypeInt(PARAM_MIN_PEOPLE, "Minimum people in the group", 2, Integer.MAX_VALUE, 2)); types.add(new ParameterTypeInt(PARAM_DISTANCE, "Distance between people in px", 0, Integer.MAX_VALUE, 300)); return types; } }