Exemplo n.º 1
0
  // Initialize all the data
  private void initializeData() throws FileNotFoundException {
    nClusters = Integer.parseInt(cookies.getInstance().getString("nClusters"));
    map = pastCrime.burglaryCases("burglarywithprecinct.txt");
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    Mat sample = new Mat(map.size(), 2, CvType.CV_64F);
    int count = 0;

    // Put crimes into the Matrix for trainning
    for (crimeCase c : map.values()) {
      sample.put(count, 0, c.getLa());
      sample.put(count++, 1, c.getlo());
    }
    myModel = new gausianMixture(sample, nClusters, 1000, "burgulary");
    precincts = kmlParser.getPrecinct();
    rpaAggregate = new RpaAggregate(kmlParser.getRPAs());
  }
Exemplo n.º 2
0
  private Mat rotateImage(Mat src, double angle) {
    Point center = new Point(src.cols() / 2, src.rows() / 2);
    double scale = 1.0;

    Mat rotateMat = Imgproc.getRotationMatrix2D(center, angle, scale);
    Mat dst = new Mat();
    Size frameSize = new Size(src.rows(), src.cols());

    // adjust dst center point
    double m[] = new double[6];
    rotateMat.get(0, 0, m);
    m[2] += (frameSize.width - src.cols()) / 2;
    m[5] += (frameSize.height - src.rows()) / 2;
    rotateMat.put(0, 0, m);

    Imgproc.warpAffine(
        src,
        dst,
        rotateMat,
        frameSize,
        Imgproc.INTER_LINEAR,
        Imgproc.BORDER_CONSTANT,
        Scalar.all(0));

    return dst;
  }
Exemplo n.º 3
0
  protected static void rotateXAxis(Mat rotation) {
    // get the matrix corresponding to the rotation vector
    Mat R = new Mat(3, 3, CvType.CV_64FC1);
    Calib3d.Rodrigues(rotation, R);

    // create the matrix to rotate 90º around the X axis
    // 1, 0, 0
    // 0 cos -sin
    // 0 sin cos
    double[] rot = {
      1, 0, 0,
      0, 0, -1,
      0, 1, 0
    };
    // multiply both matrix
    Mat res = new Mat(3, 3, CvType.CV_64FC1);
    double[] prod = new double[9];
    double[] a = new double[9];
    R.get(0, 0, a);
    for (int i = 0; i < 3; i++)
      for (int j = 0; j < 3; j++) {
        prod[3 * i + j] = 0;
        for (int k = 0; k < 3; k++) {
          prod[3 * i + j] += a[3 * i + k] * rot[3 * k + j];
        }
      }
    // convert the matrix to a vector with rodrigues back
    res.put(0, 0, prod);
    Calib3d.Rodrigues(res, rotation);
  }
  public static void main(String[] args) {
    try {
      System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

      File input = new File("traffic_signal.jpg");
      BufferedImage image = ImageIO.read(input);

      byte[] data = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
      Mat mat = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC3);
      mat.put(0, 0, data);

      Mat mat1 = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC1);
      Imgproc.cvtColor(mat, mat1, Imgproc.COLOR_RGB2GRAY);

      byte[] data1 = new byte[mat1.rows() * mat1.cols() * (int) (mat1.elemSize())];
      mat1.get(0, 0, data1);
      BufferedImage image1 =
          new BufferedImage(mat1.cols(), mat1.rows(), BufferedImage.TYPE_BYTE_GRAY);
      image1.getRaster().setDataElements(0, 0, mat1.cols(), mat1.rows(), data1);

      File ouptut = new File("output\\grayscale_" + new Date().getTime() + ".jpg");
      ImageIO.write(image1, "jpg", ouptut);
    } catch (Exception e) {
      System.out.println("Error: " + e.getMessage());
    }
  }
  public void setHsvColor(Scalar hsvColor) {
    double minH =
        (hsvColor.val[0] >= mColorRadius.val[0]) ? hsvColor.val[0] - mColorRadius.val[0] : 0;
    double maxH =
        (hsvColor.val[0] + mColorRadius.val[0] <= 255)
            ? hsvColor.val[0] + mColorRadius.val[0]
            : 255;

    mLowerBound.val[0] = minH;
    mUpperBound.val[0] = maxH;

    mLowerBound.val[1] = hsvColor.val[1] - mColorRadius.val[1];
    mUpperBound.val[1] = hsvColor.val[1] + mColorRadius.val[1];

    mLowerBound.val[2] = hsvColor.val[2] - mColorRadius.val[2];
    mUpperBound.val[2] = hsvColor.val[2] + mColorRadius.val[2];

    mLowerBound.val[3] = 0;
    mUpperBound.val[3] = 255;

    Mat spectrumHsv = new Mat(1, (int) (maxH - minH), CvType.CV_8UC3);

    for (int j = 0; j < maxH - minH; j++) {
      byte[] tmp = {(byte) (minH + j), (byte) 255, (byte) 255};
      spectrumHsv.put(0, j, tmp);
    }

    Imgproc.cvtColor(spectrumHsv, mSpectrum, Imgproc.COLOR_HSV2RGB_FULL, 4);
  }
Exemplo n.º 6
0
  /**
   * @param inputImg
   * @return Mat
   */
  public static Mat watershed(Mat inputImg) {

    Mat target = new Mat(inputImg.rows(), inputImg.cols(), CvType.CV_8UC3);
    Imgproc.cvtColor(inputImg, target, Imgproc.COLOR_BGR2RGB);

    // Conversion to 8UC1 grayscale image
    Mat grayScale = new Mat(inputImg.rows(), inputImg.cols(), CvType.CV_32SC1);
    Imgproc.cvtColor(inputImg, grayScale, Imgproc.COLOR_BGR2GRAY);

    // constructing a 3x3 kernel for morphological opening
    Mat openingKernel = Mat.ones(9, 9, CvType.CV_8U);

    // яскравість
    // target.convertTo(target, -1, 10d * 12 / 100, 0);
    // Imgproc.dilate(target, target, new Mat(), new Point(-1, -1), 1);

    Size s = new Size(27, 27);
    Imgproc.GaussianBlur(target, target, s, 1.7);

    Imgproc.morphologyEx(target, target, Imgproc.MORPH_OPEN, openingKernel);

    // dilation operation for extracting the background
    // Imgproc.dilate(target, target, openingKernel);
    // Imgproc.erode(target, target, new Mat(), new Point(-1, -1), 1);

    Mat seeds = new Mat(target.rows(), target.cols(), CvType.CV_32SC1);

    for (int i = 0; i < 10; i++) {
      seeds.put(((int) Math.random()) % target.rows(), ((int) Math.random()) % target.cols(), i);
    }

    Imgproc.watershed(target, seeds);
    // Imgproc.threshold(target,target, 50, 155, Imgproc.THRESH_BINARY );
    return target;
  }
Exemplo n.º 7
0
 public SaliencyResult saliencyalgorithmInterface(ImageObj imgobj, String method) {
   // TODO Auto-generated method stub
   float min = Float.MAX_VALUE;
   float max = Float.MIN_VALUE;
   String imgpath = imgobj.getSourcePath();
   int k_num = imgobj.getK_num();
   SaliencyResult result = new SaliencyResult();
   Mat img = Highgui.imread(imgpath, Highgui.CV_LOAD_IMAGE_GRAYSCALE);
   Mat saliencyMap = new Mat();
   saliencyMap.create(img.rows(), img.cols(), CvType.CV_16U);
   int HistGram[] = new int[256];
   int Gray[] = new int[img.cols() * img.rows()];
   int Dist[] = new int[256];
   float DistMap[] = new float[img.rows() * img.cols()];
   for (int row = 0; row < img.rows(); row++) {
     int CurIndex = row * img.cols();
     for (int col = 0; col < img.cols(); col++) {
       HistGram[(int) (img.get(row, col)[0])]++;
       Gray[CurIndex] = (int) (img.get(row, col)[0]);
       CurIndex++;
     }
   }
   for (int Y = 0; Y < 256; Y++) {
     int Value = 0;
     for (int X = 0; X < 256; X++) Value += Math.abs(Y - X) * HistGram[X];
     Dist[Y] = Value;
   }
   for (int row = 0; row < img.rows(); row++) {
     int CurIndex = row * img.cols();
     for (int col = 0; col < img.cols(); col++) {
       DistMap[CurIndex] = Dist[Gray[CurIndex]];
       if (DistMap[CurIndex] < min) min = DistMap[CurIndex];
       if (DistMap[CurIndex] > max) max = DistMap[CurIndex];
       CurIndex++;
     }
   }
   for (int row = 0; row < img.rows(); row++) {
     int CurIndex = row * img.cols();
     for (int col = 0; col < img.cols(); col++) {
       saliencyMap.put(row, col, partTwo((DistMap[CurIndex] - min) / (max - min) * 255));
       CurIndex++;
     }
   }
   new findMarkUtil();
   int nums[] = null;
   if (method == "kmeans") {
     nums = findMarkUtil.findMarkUtil_kmeans(saliencyMap, k_num, 255, 0, 5);
   } else if (method == "random") {
     nums = findMarkUtil.findMarkUtil_random(saliencyMap, k_num, 255);
   }
   result.setK_num(k_num);
   result.setSource(imgpath);
   result.setResult(nums);
   result.setSaliency(saliencyMap);
   return result;
 }
Exemplo n.º 8
0
 @TargetApi(Build.VERSION_CODES.FROYO)
 public void onPreviewFrame(byte[] frame, Camera arg1) {
   Log.i(TAG, "Preview Frame received. Need to create MAT and deliver it to clients");
   Log.i(TAG, "Frame size  is " + frame.length);
   synchronized (this) {
     mBaseMat.put(0, 0, frame);
     this.notify();
   }
   if (mCamera != null) mCamera.addCallbackBuffer(mBuffer);
 }
Exemplo n.º 9
0
    @Override
    protected Void doInBackground(final Void... unused) {

      Wrapper wrapper = new Wrapper();

      try {
        serverAddress = InetAddress.getByName(Constants.SERVER_IP);
        serverSocket = new Socket();
        serverSocket.connect(
            new InetSocketAddress(Constants.SERVER_IP, Constants.SERVER_PORT), 5000);
      } catch (Exception e) {
        e.printStackTrace();
      }

      wrapper.type = 0;
      wrapper.status = serverSocket.isConnected();
      publishProgress(wrapper);

      try {
        Thread.sleep(500);

        dataInputStream = new DataInputStream(serverSocket.getInputStream());
        dataOutputStream = new DataOutputStream(serverSocket.getOutputStream());

        wrapper.type = 1;

        while (serverSocket.isConnected()) {
          bytes = 0;

          size = dataInputStream.readInt();
          data = new byte[size];

          for (int i = 0; i < size; i += bytes) {
            bytes = dataInputStream.read(data, i, size - i);
          }

          buff = new Mat(1, size, CvType.CV_8UC1);
          buff.put(0, 0, data);

          rev = Highgui.imdecode(buff, Highgui.CV_LOAD_IMAGE_UNCHANGED);

          Imgproc.cvtColor(rev, ret, Imgproc.COLOR_RGB2BGR);

          wrapper.img = ret;
          publishProgress(wrapper);
          Thread.sleep(75);
        }

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

      return null;
    }
Exemplo n.º 10
0
  @Override
  public void onPreviewFrame(byte[] data, Camera camera) {
    if (mDetecting == true) return;

    mDetecting = true;

    mFrameBuffer.put(0, 0, data);
    Message msg = Message.obtain();
    msg.obj = mFrameBuffer.submat(0, mFrameWidth, 0, mFrameHeight);
    mHandler.sendMessage(msg);
  }
  public void onCameraViewStarted(int width, int height) {
    mGray = new Mat();
    mRgba = new Mat();
    mIntermediateMat = new Mat();
    mSize0 = new Size();
    mHist = new Mat();
    mChannels = new MatOfInt[] {new MatOfInt(0), new MatOfInt(1), new MatOfInt(2)};
    mHistSizeNum = 25;
    mBuff = new float[mHistSizeNum];
    mHistSize = new MatOfInt(mHistSizeNum);
    mRanges = new MatOfFloat(0f, 256f);
    mMat0 = new Mat();
    mColorsRGB =
        new Scalar[] {
          new Scalar(200, 0, 0, 255), new Scalar(0, 200, 0, 255), new Scalar(0, 0, 200, 255)
        };
    mColorsHue =
        new Scalar[] {
          new Scalar(255, 0, 0, 255), new Scalar(255, 60, 0, 255), new Scalar(255, 120, 0, 255),
              new Scalar(255, 180, 0, 255), new Scalar(255, 240, 0, 255),
          new Scalar(215, 213, 0, 255), new Scalar(150, 255, 0, 255), new Scalar(85, 255, 0, 255),
              new Scalar(20, 255, 0, 255), new Scalar(0, 255, 30, 255),
          new Scalar(0, 255, 85, 255), new Scalar(0, 255, 150, 255), new Scalar(0, 255, 215, 255),
              new Scalar(0, 234, 255, 255), new Scalar(0, 170, 255, 255),
          new Scalar(0, 120, 255, 255), new Scalar(0, 60, 255, 255), new Scalar(0, 0, 255, 255),
              new Scalar(64, 0, 255, 255), new Scalar(120, 0, 255, 255),
          new Scalar(180, 0, 255, 255), new Scalar(255, 0, 255, 255), new Scalar(255, 0, 215, 255),
              new Scalar(255, 0, 85, 255), new Scalar(255, 0, 0, 255)
        };
    mWhilte = Scalar.all(255);
    mP1 = new Point();
    mP2 = new Point();

    // Fill sepia kernel
    mSepiaKernel = new Mat(4, 4, CvType.CV_32F);
    mSepiaKernel.put(0, 0, /* R */ 0.189f, 0.769f, 0.393f, 0f);
    mSepiaKernel.put(1, 0, /* G */ 0.168f, 0.686f, 0.349f, 0f);
    mSepiaKernel.put(2, 0, /* B */ 0.131f, 0.534f, 0.272f, 0f);
    mSepiaKernel.put(3, 0, /* A */ 0.000f, 0.000f, 0.000f, 1f);
  }
Exemplo n.º 12
0
  private void seamlessEdges(Mat image) {
    int imageHeight = image.height();
    int imageWidth = image.width();
    double smoothRange = smoothValue * (imageWidth / 2000f);
    double smoothRangeHalf = smoothRange / 2;
    double pixel1[];
    double pixel2[];
    double tempPixel[] = new double[4];
    tempPixel[3] = 255;

    int i, j;
    for (i = 0; i < smoothRangeHalf; i++) {
      for (j = 0; j < imageHeight; j++) {
        pixel1 = image.get(j, i);
        pixel2 = image.get(j, imageWidth - i - 1);
        tempPixel[0] =
            pixel1[0] * ((smoothRangeHalf + i) / smoothRange)
                + pixel2[0] * ((smoothRangeHalf - i) / smoothRange);
        tempPixel[1] =
            pixel1[1] * ((smoothRangeHalf + i) / smoothRange)
                + pixel2[1] * ((smoothRangeHalf - i) / smoothRange);
        tempPixel[2] =
            pixel1[2] * ((smoothRangeHalf + i) / smoothRange)
                + pixel2[2] * ((smoothRangeHalf - i) / smoothRange);
        pixel2[0] =
            pixel2[0] * ((smoothRangeHalf + i) / smoothRange)
                + pixel1[0] * ((smoothRangeHalf - i) / smoothRange);
        pixel2[1] =
            pixel2[1] * ((smoothRangeHalf + i) / smoothRange)
                + pixel1[1] * ((smoothRangeHalf - i) / smoothRange);
        pixel2[2] =
            pixel2[2] * ((smoothRangeHalf + i) / smoothRange)
                + pixel1[2] * ((smoothRangeHalf - i) / smoothRange);
        image.put(j, i, tempPixel);
        image.put(j, imageWidth - i - 1, pixel2);
      }
    }
  }
Exemplo n.º 13
0
  private static Mat quat2rpy(Mat quat) {
    double[] q = new double[4];
    quat.get(0, 0, q);

    double[] rpy = {
      Math.atan2(2 * (q[0] * q[1] + q[2] * q[3]), 1 - 2 * (q[1] * q[1] + q[2] * q[2])),
      Math.asin(2 * (q[0] * q[2] - q[3] * q[1])),
      Math.atan2(2 * (q[0] * q[3] + q[1] * q[2]), 1 - 2 * (q[2] * q[2] + q[3] * q[3]))
    };

    Mat rpym = new Mat(3, 1, CvType.CV_64F);
    rpym.put(0, 0, rpy);
    return rpym;
  }
  public void copyMat(Mat src, Mat dest) {
    int srcRows = src.rows();
    int srcCols = src.cols();
    int destRows = dest.rows();
    int destCols = dest.cols();

    for (int i = 0; i < srcRows; i++) {
      for (int j = 0; j < srcCols; j++) {
        double bit = src.get(i, j)[0];
        dest.put(i, j, bit);
        System.out.println(bit);
      }
    }
  }
Exemplo n.º 15
0
  private static Mat rodr2quat(Mat rodr) {
    double t = Core.norm(rodr);
    double[] r = new double[3];
    rodr.get(0, 0, r);

    double[] quat = {
      Math.cos(t / 2),
      Math.sin(t / 2) * r[0] / t,
      Math.sin(t / 2) * r[1] / t,
      Math.sin(t / 2) * r[2] / t
    };
    Mat quatm = new Mat(4, 1, CvType.CV_64F);
    quatm.put(0, 0, quat);
    return quatm;
  }
Exemplo n.º 16
0
      public synchronized Mat get() {

        if (!mValid) return null;
        while (full == false) {
          try {
            wait();
            if (!mValid) return null;
          } catch (InterruptedException e) {
            return null;
          }
        }
        mat.put(0, 0, bytes);
        full = false;
        notifyAll();
        return mat;
      }
  public static Mat getCCH(Mat image) {
    ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
    Mat hierarchy = new Mat();
    Imgproc.findContours(
        image, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE);

    Mat chainHistogram = Mat.zeros(1, 8, CvType.CV_32F);
    int n = 0;
    MatOfPoint2f approxCurve = new MatOfPoint2f();
    for (MatOfPoint contour : contours) {

      // get the freeman chain code from the contours
      int rows = contour.rows();
      // System.out.println("\nrows"+rows+"\n"+contour.dump());
      int direction = 7;
      Mat prevPoint = contours.get(0).row(0);
      n += rows - 1;
      for (int i = 1; i < rows; i++) {
        // get the current point
        double x1 = contour.get(i - 1, 0)[1];
        double y1 = contour.get(i - 1, 0)[0];

        // get the second point
        double x2 = contour.get(i, 0)[1];
        double y2 = contour.get(i, 0)[0];

        if (x2 == x1 && y2 == y1 + 1) direction = 0;
        else if (x2 == x1 - 1 && y2 == y1 + 1) direction = 1;
        else if (x2 == x1 - 1 && y2 == y1) direction = 2;
        else if (x2 == x1 - 1 && y2 == y1 - 1) direction = 3;
        else if (x2 == x1 && y2 == y1 - 1) direction = 4;
        else if (x2 == x1 + 1 && y2 == y1 - 1) direction = 5;
        else if (x2 == x1 + 1 && y2 == y1) direction = 6;
        else if (x2 == x1 + 1 && y2 == y1 + 1) direction = 7;
        else System.out.print("err");
        double counter = chainHistogram.get(0, direction)[0];
        chainHistogram.put(0, direction, ++counter);
        System.out.print(direction);
      }
    }
    System.out.println("\n" + chainHistogram.dump());
    Scalar alpha = new Scalar(n); // the factor
    Core.divide(chainHistogram, alpha, chainHistogram);
    System.out.println("\nrows=" + n + " " + chainHistogram.dump());
    return chainHistogram;
  }
Exemplo n.º 18
0
  public static Mat[] getMatArray(byte[] data, Camera camera) {
    Camera.Parameters parameters = camera.getParameters();
    int width = parameters.getPreviewSize().width;
    int height = parameters.getPreviewSize().height;

    Mat yuv = new Mat(height + height / 2, width, CvType.CV_8UC1);
    Mat rgba = new Mat(height, width, CvType.CV_8UC1);
    yuv.put(0, 0, data);
    Imgproc.cvtColor(yuv, rgba, Imgproc.COLOR_YUV2RGB_NV21);

    Mat rgbaResult = rotateMat(rgba);
    Mat grayResult = new Mat(rgbaResult.height(), rgbaResult.width(), CvType.CV_8UC1);
    Imgproc.cvtColor(rgbaResult, grayResult, Imgproc.COLOR_BGR2GRAY);

    yuv.release();
    rgba.release();
    Mat[] result = {grayResult, rgbaResult};
    return result;
  }
Exemplo n.º 19
0
  // Transform the json-type feature to mat-type
  public static Mat json2mat(String json) {

    JsonParser parser = new JsonParser();
    JsonElement parseTree = parser.parse(json);

    // Verify the input is JSON type
    if (!parseTree.isJsonObject()) {
      System.out.println("The input is not a JSON type...\nExiting...");
      System.exit(1);
    }
    JsonObject jobj = parser.parse(json).getAsJsonObject();

    if (jobj == null || !jobj.isJsonObject() || jobj.isJsonNull()) {
      return null;
    }

    // Detect broken/null features
    JsonElement r = jobj.get("rows");
    if (r == null) {
      return null;
    }

    int rows = jobj.get("rows").getAsInt();
    int cols = jobj.get("cols").getAsInt();
    int type = jobj.get("type").getAsInt();
    String data = jobj.get("data").getAsString();
    String[] pixs = data.split(",");

    Mat descriptor = new Mat(rows, cols, type);
    for (String pix : pixs) {
      String[] tmp = pix.split(" ");
      int r_pos = Integer.valueOf(tmp[0]);
      int c_pos = Integer.valueOf(tmp[1]);
      double rgb = Double.valueOf(tmp[2]);
      descriptor.put(r_pos, c_pos, rgb);
    }
    return descriptor;
  }
Exemplo n.º 20
0
 /**
  * Create a camera intrinsic matrix using input parameters
  *
  * <p>The camera intrinsic matrix will be like:
  *
  * <p>+- -+ | f 0 center.width | A = | 0 f center.height | | 0 0 1 | +- -+
  *
  * @return An approximated (not actually calibrated) camera matrix
  */
 private static Mat cameraMatrix(float f, Size center) {
   final double[] data = {f, 0, center.width, 0, f, center.height, 0, 0, 1f};
   Mat m = new Mat(3, 3, CvType.CV_64F);
   m.put(0, 0, data);
   return m;
 }
Exemplo n.º 21
0
 public static Mat bufferedImageToMat(BufferedImage bi) {
   Mat mat = new Mat(bi.getHeight(), bi.getWidth(), CvType.CV_8UC3);
   byte[] data = ((DataBufferByte) bi.getRaster().getDataBuffer()).getData();
   mat.put(0, 0, data);
   return mat;
 }
Exemplo n.º 22
0
 protected Mat HMatFromArray(double a[]) {
   Mat m = new Mat(3, 3, CvType.CV_64FC1);
   for (int i = 0; i < 3; ++i) for (int j = 0; j < 3; ++j) m.put(i, j, a[i * 3 + j]);
   return m;
 }
Exemplo n.º 23
0
  // Bhattacharyya coefficient for two distributions
  // a, b are original data samples
  // better if a, b are dimension-reduced Principal Components
  public static double Bhattacharyya(Mat a, Mat b) {

    // if not dimension-reduced
    //		int k = a.cols()/2;
    //		Mat temp1 = new Mat();
    //		Mat temp2 = new Mat();
    //		Mat pa = new Mat();
    //		Mat pb = new Mat();
    //		Core.PCACompute(a, temp1, temp2, k);
    //		Core.PCAProject(a, temp1, temp2, pa);
    //		Core.PCACompute(b, temp1, temp2, k);
    //		Core.PCAProject(b, temp1, temp2, pb);
    //

    Mat cova = new Mat();
    Mat covb = new Mat();
    Mat meana = new Mat();
    Mat meanb = new Mat();
    Core.calcCovarMatrix(a, cova, meana, Core.COVAR_NORMAL | Core.COVAR_ROWS);
    Core.calcCovarMatrix(b, covb, meanb, Core.COVAR_NORMAL | Core.COVAR_ROWS);

    // if not dimension-reduced
    //		Core.calcCovarMatrix(pa, cova, meana, Core.COVAR_NORMAL|Core.COVAR_ROWS);
    //		Core.calcCovarMatrix(pb, covb, meanb, Core.COVAR_NORMAL|Core.COVAR_ROWS);

    //		System.out.println("cov");
    //		System.out.println(cova.dump());
    //		System.out.println(covb.dump());

    double detcova = Core.determinant(cova);
    double detcovb = Core.determinant(covb);
    Mat cov = new Mat();
    Core.add(cova, covb, cov);
    //		cov = cov.mul(cov, 0.5);

    //		Core.scaleAdd(cov, 0.5, new Mat(), cov);
    for (int i = 0; i < cov.rows(); i++)
      for (int j = 0; j < cov.cols(); j++) {
        cov.put(i, j, 0.5 * cov.get(i, j)[0]);
      }

    double detcov = Core.determinant(cov);

    Mat inv = new Mat(); // inverse of cov
    Core.invert(cov, inv);

    Mat mabsub = new Mat();
    Core.subtract(meana, meanb, mabsub);

    Mat mabsub_tp = new Mat();
    Core.transpose(mabsub, mabsub_tp);

    Mat temp = new Mat();
    Core.gemm(mabsub, inv, 1, new Mat(), 0, temp);
    //		System.out.println(temp.dump());
    //		System.out.println(mabsub_tp.dump());

    Mat res = new Mat();
    Core.gemm(temp, mabsub_tp, 1, new Mat(), 0, res);
    //		System.out.println(res.dump());

    //		Mat temp = mabsub_tp.mul(inv);
    //		System.out.println(temp.dump());
    //
    //		Mat res = temp.mul(mabsub);
    //		System.out.println(res.dump());

    double s2 = Math.sqrt(detcov / (Math.sqrt(detcova * detcovb)));
    // s1 = 0.125 * (mabsub_tp*inv*mabsub)
    double s1 = 0.125 * res.get(0, 0)[0];

    double bdis = 1 / (Math.exp(s1) * s2);
    return bdis;
  }
Exemplo n.º 24
0
    public Mat heatmap(float[][] arr, ProgressDialog pg) {
      LinkedList<com.example.team436.thermalviz.Point> points =
          new LinkedList<com.example.team436.thermalviz.Point>();
      int rows = 480;
      int col = 640;
      float average = 0;
      int ave_cnt = 0;
      boolean[][] orig = new boolean[480][640];
      // float[][] buf1 = new float[480][640];
      int passes = 640;
      boolean has_max = false;
      boolean has_min = false;
      float min = arr[0][0];
      float max = arr[0][0];

      for (int i = 0; i < rows; i++) {
        for (int j = 0; j < col; j++) {
          if (arr[i][j] > -300) {
            orig[i][j] = true;
            average += arr[i][j];
            ave_cnt++;
            if (arr[i][j] > max || has_max == false) {
              max = arr[i][j];
              has_max = true;
            }
            if (arr[i][j] < min || has_min == false) min = arr[i][j];
            has_min = true;
          } else {
            orig[i][j] = false;
          }
        }
      }

      Log.v(TAG, "ARR 480: " + Float.toString(arr[479][479]));
      Log.v(TAG, "ARR 480: " + Float.toString(arr[479][480]));
      Log.v(TAG, "MIN: " + Float.toString(min));
      Log.v(TAG, "MAX: " + Float.toString(max));

      average /= (float) ave_cnt;

      for (int i = 0; i < rows; i++) {
        for (int j = 0; j < col; j++) {
          if (arr[i][j] <= -300) {
            arr[i][j] = average;
          }
        }
      }

      for (int k = 0; k < passes; k++) {
        // Log.v(TAG, "PASS: "******"PIX: " + Float.toString(cur));
          float val = (1 - ((cur - min) / (max - min))) * 125;
          if (val > 125) val = 125;
          if (val < 0) val = 0;
          pix[0] = (byte) val;
          pix[1] = (byte) 255;
          pix[2] = (byte) 255;
          hsvImg.put(j, i, pix);
        }
      }

      return hsvImg;
    }
Exemplo n.º 25
0
  /**
   * Extracts and classifies colour bands for each Resistor. Each ColourBand object is instantiated
   * and linked to their parent Resistor object.
   *
   * @param resistorList A list of Resistor objects from which to extract the colour bands
   * @param paintDebugInfo If ture, the extracted colour band ROIs are displayed on the GUI
   */
  private void extractColourBandsAndClassify(List<Resistor> resistorList, boolean paintDebugInfo) {
    if (resistorList.size() > 0) {
      for (int r = 0; r < resistorList.size(); r++) {
        Mat resImg = resistorList.get(r).resistorMat;

        Mat imgHSV = new Mat();
        Mat satImg = new Mat();
        Mat hueImg = new Mat();

        // convert to HSV
        Imgproc.cvtColor(resImg, imgHSV, Imgproc.COLOR_BGR2HSV);
        ArrayList<Mat> channels = new ArrayList<Mat>();
        Core.split(imgHSV, channels);
        // extract channels
        satImg = channels.get(1); // saturation
        hueImg = channels.get(0); // hue

        // threshold saturation channel
        Mat threshedROISatBands = new Mat(); // ~130 sat thresh val
        Imgproc.threshold(satImg, threshedROISatBands, SAT_BAND_THRESH, 255, Imgproc.THRESH_BINARY);

        // threshold hue channel
        Mat threshedROIHueBands = new Mat(); // ~50 hue thresh val
        Imgproc.threshold(hueImg, threshedROIHueBands, HUE_BAND_THRESH, 255, Imgproc.THRESH_BINARY);

        // combine the thresholded binary images
        Mat bandROI = new Mat();
        Core.bitwise_or(threshedROIHueBands, threshedROISatBands, bandROI);

        // find contours in binary ROI image
        ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
        Mat hierarchy = new Mat();
        Imgproc.findContours(
            bandROI,
            contours,
            hierarchy,
            Imgproc.RETR_EXTERNAL,
            Imgproc.CHAIN_APPROX_SIMPLE,
            new Point(0, 0));

        // remove any remaining noise by only keeping contours which area > threshold
        for (int i = 0; i < contours.size(); i++) {
          double area = Imgproc.contourArea(contours.get(i));
          if (area < MIN_BAND_AREA) {
            contours.remove(i);
            i--;
          }
        }

        // create a ColourBand object for each detected band
        // storing its center, the contour and the bandROI
        for (int i = 0; i < contours.size(); i++) {
          MatOfPoint contour = contours.get(i);

          // extract this colour band and store in a Mat
          Rect boundingRect = Imgproc.boundingRect(contour);
          Mat mask = Mat.zeros(bandROI.size(), CvType.CV_8U);
          Imgproc.drawContours(mask, contours, i, new Scalar(255), Core.FILLED);
          Mat imageROI = new Mat();
          resImg.copyTo(imageROI, mask);
          Mat colourBandROI = new Mat(imageROI, boundingRect);

          // instantiate new ColourBand object
          ColourBand cb = new ColourBand(findCenter(contour), contour, colourBandROI);

          // cluster the band colour
          cb.clusterBandColour(BAND_COLOUR_K_MEANS);

          // classify using the Lab colourspace as feature vector
          Mat sampleMat =
              new Mat(1, 3, CvType.CV_32FC1); // create a Mat contacting the clustered band colour
          sampleMat.put(0, 0, cb.clusteredColourLAB[0]);
          sampleMat.put(0, 1, cb.clusteredColourLAB[1]);
          sampleMat.put(0, 2, cb.clusteredColourLAB[2]);
          Mat classifiedValue = new Mat(1, 1, CvType.CV_32FC1);
          Mat neighborResponses = new Mat(); // dont actually use this
          Mat dists = new Mat(); // dont actually use this
          // classify
          knn.find_nearest(sampleMat, 3, classifiedValue, neighborResponses, dists);

          // cast classified value into Colour enum and store
          cb.classifiedColour = ColourEnumVals[(int) classifiedValue.get(0, 0)[0]];
          // add the band to the parent resistor
          resistorList.get(r).bands.add(cb);
        }

        // paint the extracted band ROIs
        if (paintDebugInfo) {
          Mat finalBandROIMask = Mat.zeros(bandROI.size(), CvType.CV_8U);
          for (int i = 0; i < contours.size(); i++) {
            Scalar color = new Scalar(255, 255, 255);
            Imgproc.drawContours(
                finalBandROIMask, contours, i, color, -1, 4, hierarchy, 0, new Point());
          }
          Mat colourROI = new Mat();
          resImg.copyTo(colourROI, finalBandROIMask);
          paintResistorSubRegion(colourROI, r);
        }
      }
    }
  }
Exemplo n.º 26
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);
    }
  }
Exemplo n.º 27
0
 protected Mat ImageBGRFromString(byte[] data) {
   Mat frame = new Mat(1, data.length, CvType.CV_8UC1);
   frame.put(0, 0, data);
   return Highgui.imdecode(frame, 1);
 }