Ejemplo n.º 1
0
  protected void init(int n) throws InvalidConfigException {
    double r[] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
    double g[] = {0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0};
    double b[] = {1.0, 0.95, 0.9, 0.85, 0.8, 0.75, 0.7, 0.65, 0.6, 0.55, 0.5};

    Mat X = ColorMap.linspace(0, 1, 11);
    Mat rMat = new MatOfDouble(r);
    Mat rMatF = new Mat();
    rMat.convertTo(rMatF, CvType.CV_32FC1);

    Mat gMat = new MatOfDouble(g);
    Mat gMatF = new Mat();
    gMat.convertTo(gMatF, CvType.CV_32FC1);

    Mat bMat = new MatOfDouble(b);
    Mat bMatF = new Mat();
    bMat.convertTo(bMatF, CvType.CV_32FC1);

    this._lut =
        ColorMap.linear_colormap(
            X,
            rMatF.clone(), // red
            gMatF.clone(), // green
            bMatF.clone(), // blue
            n); // number of sample points
  }
Ejemplo n.º 2
0
  /**
   * @param source
   * @param delta
   * @return Mat
   */
  public static Mat Sobel(Mat source, int delta) {

    Mat grey = new Mat();
    Imgproc.cvtColor(source, grey, Imgproc.COLOR_BGR2GRAY);
    Mat sobelx = new Mat();
    Imgproc.Sobel(grey, sobelx, CvType.CV_32F, 1, delta);

    double minVal, maxVal;
    Core.MinMaxLocResult minMaxLocResult = Core.minMaxLoc(sobelx);
    minVal = minMaxLocResult.minVal;
    maxVal = minMaxLocResult.maxVal;

    Mat draw = new Mat();
    sobelx.convertTo(
        draw, CvType.CV_8U, 255.0 / (maxVal - minVal), -minVal * 255.0 / (maxVal - minVal));
    return draw;
  }
Ejemplo n.º 3
0
  public static void extractQueryFeatures2HDFS(String filename, Job job) throws IOException {

    // Read the local image.jpg as a Mat
    Mat query_mat_float =
        Highgui.imread(LOCAL_USER_DIR + ID + INPUT + "/" + filename, CvType.CV_32FC3);
    // Convert RGB to GRAY
    Mat query_gray = new Mat();
    Imgproc.cvtColor(query_mat_float, query_gray, Imgproc.COLOR_RGB2GRAY);
    // Convert the float type to unsigned integer(required by SIFT)
    Mat query_mat_byte = new Mat();
    query_gray.convertTo(query_mat_byte, CvType.CV_8UC3);
    //        // Resize the image to 1/FACTOR both width and height
    //        Mat query_mat_byte = FeatureExtraction.resize(query_mat_byte);

    // Extract the feature from the (Mat)image
    Mat query_features = FeatureExtraction.extractFeature(query_mat_byte);

    System.out.println(PREFIX + "Extracting the query image feature...");
    System.out.println("query_mat(float,color):" + query_mat_float);
    System.out.println("query_mat(float,gray):" + query_gray);
    System.out.println("query_mat(byte,gray):" + query_mat_byte);
    System.out.println("query_mat_features:" + query_features);
    System.out.println();

    // Store the feature to the hdfs in order to use it later in different map tasks
    System.out.println(PREFIX + "Generating the feature file for the query image in HDFS...");
    FileSystem fs = FileSystem.get(job.getConfiguration());
    String featureFileName = filename.substring(0, filename.lastIndexOf(".")) + ".json";
    FSDataOutputStream fsDataOutputStream =
        fs.create(new Path(HDFS_HOME + USER + ID + INPUT + "/" + featureFileName));
    BufferedWriter bw =
        new BufferedWriter(new OutputStreamWriter(fsDataOutputStream, StandardCharsets.UTF_8));
    bw.write(FeatureExtraction.mat2json(query_features));
    bw.close();
    System.out.println(PREFIX + "Query feature extraction finished...");
    System.out.println();
  }
Ejemplo n.º 4
0
  /**
   * @param inputImg
   * @return Mat
   */
  public static Mat kmeans(Mat inputImg) {

    Mat rgba = inputImg;
    Mat tempMat = inputImg;
    rgba = new Mat(inputImg.cols(), inputImg.rows(), CvType.CV_8UC3);
    inputImg.copyTo(rgba);

    List<Mat> hsv_planes_temp = new ArrayList<Mat>(3);
    Core.split(tempMat, hsv_planes_temp);

    double threshValue1 = PreProcessingOperation.getHistAverage(inputImg, hsv_planes_temp.get(0));
    sample.util.Estimate.setFirstHistAverageValue(threshValue1);
    System.out.println("Defore eqau " + threshValue1);

    System.out.println(
        Estimate.getBlueAverage() + " ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;");

    if (threshValue1 > 140) {
      if (Estimate.getBlueAverage() > 110) {
        rgba.convertTo(rgba, -1, 10d * 31 / 100, 0);
        System.out.println("11");
      } else {
        rgba.convertTo(rgba, -1, 10d * 40 / 100, 0);
        System.out.println("12");
      }
    } else if (threshValue1 > 135) {
      rgba.convertTo(rgba, -1, 10d * 32 / 100, 0);
      System.out.println("21");
    } else if (threshValue1 > 125) {
      if (Estimate.getBlueAverage() > 110) {
        rgba.convertTo(rgba, -1, 10d * 30 / 100, 0);
        rgba = PreProcessing.Dilate(rgba, 5);
        System.out.println("31");
      } else {
        rgba.convertTo(rgba, -1, 10d * 37 / 100, 0);
        System.out.println("32");
      }
    } else if (threshValue1 > 120) {
      rgba.convertTo(rgba, -1, 10d * 35 / 100, 0);
      System.out.println("41");
    } else if (threshValue1 > 110) {
      if (Estimate.getBlueAverage() > 110) {
        rgba.convertTo(rgba, -1, 10d * 35 / 100, 0);
        rgba = PreProcessing.Dilate(rgba, 5);
        System.out.println("51");
      }
    } else if (threshValue1 > 100) {
      if (Estimate.getBlueAverage() > 107) {
        rgba.convertTo(rgba, -1, 10d * 24 / 100, 0);
        rgba = PreProcessing.Dilate(rgba, 5);
        System.out.println("61");
      } else if (Estimate.getBlueAverage() > 90) {
        rgba.convertTo(rgba, -1, 10d * 30 / 100, 0);
        rgba = PreProcessing.Dilate(rgba, 5);
        System.out.println("62");
      }
    } else if (threshValue1 > 50) {

      if (Estimate.getBlueAverage() > 160) {
        rgba.convertTo(rgba, -1, 10d * 30 / 100, 0);
        rgba = PreProcessing.Dilate(rgba, 3);
        System.out.println("81");
      } else if (Estimate.getBlueAverage() > 160) {
        rgba.convertTo(rgba, -1, 10d * 27 / 100, 0);
        rgba = PreProcessing.Dilate(rgba, 9);
        System.out.println("82");
      } else if (Estimate.getBlueAverage() > 130) {
        rgba.convertTo(rgba, -1, 10d * 30 / 100, 0);
        rgba = PreProcessing.Dilate(rgba, 9);
        System.out.println("83");
      } else if (Estimate.getBlueAverage() > 70) {
        rgba.convertTo(rgba, -1, 10d * 29 / 100, 0);
        rgba = PreProcessing.Dilate(rgba, 9);
        System.out.println("84");
      }
    } else if (threshValue1 > 30) {
      if (Estimate.getBlueAverage() > 190) {
        rgba.convertTo(rgba, -1, 10d * 25 / 100, 0);
        System.out.println("91");
      } else if (Estimate.getBlueAverage() > 160) {
        rgba.convertTo(rgba, -1, 10d * 35 / 100, 0);
        System.out.println("92");
      }
    } else {
      if (Estimate.getBlueAverage() > 240) {
        rgba.convertTo(rgba, -1, 10d * 24 / 100, 0);
        System.out.println("7");
      } else {
        rgba.convertTo(rgba, -1, 10d * 17 / 100, 0);
        System.out.println("7");
      }
    }
    tempMat.release();

    Mat mHSV = new Mat();
    Imgproc.cvtColor(rgba, mHSV, Imgproc.COLOR_RGBA2RGB, 3);
    Imgproc.cvtColor(rgba, mHSV, Imgproc.COLOR_RGB2HSV, 3);
    List<Mat> hsv_planes = new ArrayList<Mat>(3);
    Core.split(mHSV, hsv_planes);

    Mat channel = hsv_planes.get(0);
    channel = Mat.zeros(mHSV.rows(), mHSV.cols(), CvType.CV_8UC1);
    hsv_planes.set(2, channel);
    Core.merge(hsv_planes, mHSV);

    mHSV.convertTo(mHSV, CvType.CV_8UC1);
    mHSV = Histogram(mHSV);

    /*
    Mat clusteredHSV = new Mat();
    mHSV.convertTo(mHSV, CvType.CV_32FC3);
    TermCriteria criteria = new TermCriteria(TermCriteria.EPS + TermCriteria.MAX_ITER,100,0.1);
    Core.kmeans(mHSV, 1, clusteredHSV, criteria, 20, Core.KMEANS_PP_CENTERS);
    Mat hsvImg = new Mat();
    List<Mat> hsvPlanes = new ArrayList<>();
    Mat thresholdImg = new Mat();
    int thresh_type = Imgproc.THRESH_BINARY_INV;
    hsvImg.create(mHSV.size(), CvType.CV_8U);
    Imgproc.cvtColor(mHSV, hsvImg, Imgproc.COLOR_BGR2HSV);
    Core.split(hsvImg, hsvPlanes);
    Imgproc.threshold(hsvPlanes.get(1), thresholdImg, 0 , 200 , thresh_type);
    double threshValue = PreProcessingOperation.getHistAverage(hsvImg, hsvPlanes.get(0));
    Estimate.setSecondHistAverageValue(threshValue);
    System.out.println("After equa " + Estimate.getSecondHistAverageValue());*/

    Imgproc.threshold(mHSV, mHSV, 0, 150, Imgproc.THRESH_BINARY_INV);
    // mHSV.convertTo(mHSV, CvType.CV_8UC1);
    return mHSV;
  }
Ejemplo n.º 5
0
  public static void main(String[] args) {

    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

    /** *** Configuration Variables **** */
    int imgWidth = 200;
    int imgHeight = 200;
    int numPatch = 2000;
    int patchWidth = 40;
    int patchHeight = 40;
    int k = 200; // kmeans number of center
    int numBins = 8;

    String filePathRed = "base/Red/";
    String filePathBlack = "base/Black";
    String procPathRed = "base/ProcRed";
    String procPathBlack = "base/ProcBlack";
    /** ******************************** */
    ArrayList<String> fileNames = new ArrayList<String>();

    sources = new ArrayList<Mat>();

    /* Image IO */
    try {

      /* Read Red Staplers */
      File folder = new File(filePathRed);
      BufferedImage currentImage;
      for (final File fileEntry : folder.listFiles()) {
        if (!fileEntry.isDirectory()) {

          // Resize Image
          currentImage = ImageProc.resize(ImageIO.read(fileEntry), imgWidth, imgHeight);

          File outFile = new File(procPathRed + "/" + fileEntry.getName());
          ImageIO.write(currentImage, "JPG", outFile);
          sources.add(Highgui.imread(outFile.getPath()));
          fileNames.add(outFile.getName());
        }
      }

      /* Read Black Staplers */
      folder = new File(filePathBlack);
      for (final File fileEntry : folder.listFiles()) {
        if (!fileEntry.isDirectory()) {

          // Resize Image
          currentImage = ImageProc.resize(ImageIO.read(fileEntry), imgWidth, imgHeight);

          File outFile = new File(procPathBlack + "/" + fileEntry.getName());
          ImageIO.write(currentImage, "JPG", outFile);
          sources.add(Highgui.imread(outFile.getPath()));
          fileNames.add(outFile.getName());
        }
      }

    } catch (IOException e) {
      e.printStackTrace();
    }

    /** ************************************* */
    float[] p1 = new float[30];
    float[] p2 = new float[30];

    /* Create Image Patches and calculate color feature vector for each patch */
    Iterator<Mat> imgIter = sources.iterator();
    Mat thisImage;
    Mat featureMat = new Mat();
    List<Mat> imagePatches = null;
    Iterator<Mat> patchIter = null;

    while (imgIter.hasNext()) {

      thisImage = imgIter.next();

      // Randomly Sample Patches
      imagePatches = ImageProc.sampleImage(thisImage, patchWidth, patchHeight, numPatch);
      patchIter = imagePatches.iterator();

      // Create color feature vector for each patch
      while (patchIter.hasNext()) {
        featureMat.push_back(ImageProc.calBGRFeature(patchIter.next(), numBins));
      }
    }

    Mat centers = new Mat();
    Mat bestLabels = new Mat();
    Core.kmeans(
        featureMat,
        k,
        bestLabels,
        new TermCriteria(TermCriteria.EPS, 0, Math.pow(10, -5)),
        0,
        Core.KMEANS_RANDOM_CENTERS,
        centers);

    MatOfFloat bestLabelRange = new MatOfFloat(0, k);

    ArrayList<Mat> centerHist = new ArrayList<Mat>();
    Mat centerHistMat = new Mat(0, k, CvType.CV_32FC1);

    imgIter = sources.listIterator();
    Iterator<String> nameIter = fileNames.iterator();

    int ptr = 0;
    int cnt = 0;

    // Output CSV

    try {
      File outCSV = new File("output/res.csv");
      FileWriter fstream = new FileWriter(outCSV);
      BufferedWriter out = new BufferedWriter(fstream);
      StringBuilder sb;
      out.write("@relation staplers\n");
      for (int n = 0; n < 200; n++) {
        out.write("@attribute " + "a" + n + " real\n");
      }

      out.write("@attribute class {RedStapler, BlackStapler}\n\n");
      out.write("@data\n\n");

      while (imgIter.hasNext()) {

        Mat thisMat = new Mat(bestLabels, new Range(ptr, ptr + numPatch), new Range(0, 1));
        Mat mat = new Mat();
        thisMat.convertTo(mat, CvType.CV_32F);

        ArrayList<Mat> bestLabelList = new ArrayList<Mat>();
        bestLabelList.add(mat);

        Mat thisHist = new Mat();
        Imgproc.calcHist(
            bestLabelList, new MatOfInt(0), new Mat(), thisHist, new MatOfInt(k), bestLabelRange);

        centerHist.add(thisHist);

        // Create file
        sb = new StringBuilder();

        float[] histArr = new float[(int) thisHist.total()];
        thisHist.get(0, 0, histArr);

        for (int m = 0; m < histArr.length; m++) {
          sb.append(histArr[m] + ",");
        }

        if (cnt++ < 10) sb.append("RedStapler");
        else sb.append("BlackStapler");

        sb.append("\n");
        out.write(sb.toString());
        // Close the output stream

        centerHistMat.push_back(thisHist.t());
        ptr += numPatch;
        imgIter.next();
      }

      out.close();
    } catch (IOException e) { // Catch exception if any
      System.err.println("Error: " + e.getMessage());
      System.exit(-1);
    }

    /* Support Vector Machine Validation */
    Mat labelMat = new Mat(sources.size(), 1, CvType.CV_32FC1);

    double[] labels = new double[20];
    for (int i = 0; i < 10; i++) {
      labels[i] = 1;
      labels[i + 10] = -1;
    }
    labelMat.put(0, 0, labels);

    CvSVMParams params = new CvSVMParams();
    params.set_kernel_type(CvSVM.LINEAR);

    CvSVM svm = new CvSVM();
    svm.train(centerHistMat, labelMat, new Mat(), new Mat(), params);
    svm.save("base/haha.txt");
    String basePath = "base/predict/";

    try {
      File testCSV = new File("output/test.arff");
      FileWriter testStream = new FileWriter(testCSV);
      BufferedWriter testOut = new BufferedWriter(testStream);

      testOut.write("@relation staplers\n");
      for (int n = 0; n < 200; n++) {
        testOut.write("@attribute " + "a" + n + " real\n");
      }

      testOut.write("@attribute class {RedStapler, BlackStapler}\n\n");
      testOut.write("@data\n\n");

      for (int m = 0; m < 21; m++) {

        // System.out.println(basePath + m + ".jpg");
        Mat testImg = Highgui.imread(basePath + m + ".jpg");

        List<Mat> patches = ImageProc.sampleImage(testImg, patchWidth, patchHeight, numPatch);
        List<Mat> features = new ArrayList<Mat>();

        for (int i = 0; i < patches.size(); i++) {

          Mat testVector = ImageProc.calBGRFeature(patches.get(i), numBins);
          features.add(testVector);
        }

        Mat testData = ImageProc.calFeatureVector(features, centers);

        StringBuilder testsb = new StringBuilder();
        // String name = nameIter.next();
        // sb.append(name + ",");

        float[] data = new float[testData.cols()];
        testData.get(0, 0, data);

        for (int o = 0; o < data.length; o++) {
          testsb.append(data[o] + ",");
        }
        if (m < 6) testsb.append("RedStapler");
        else testsb.append("BlackStapler");

        testsb.append("\n");
        testOut.write(testsb.toString());

        System.out.println("Img" + m + " " + svm.predict(testData));
      }
    } catch (IOException e) {
      e.printStackTrace();
      System.exit(-1);
    }
  }