Beispiel #1
0
  public boolean hasChanges(Mat current) {
    int PIXEL_DIFF_THRESHOLD = 5;
    int IMAGE_DIFF_THRESHOLD = 5;
    Mat bg = new Mat();
    Mat cg = new Mat();
    Mat diff = new Mat();
    Mat tdiff = new Mat();

    Imgproc.cvtColor(base, bg, Imgproc.COLOR_BGR2GRAY);
    Imgproc.cvtColor(current, cg, Imgproc.COLOR_BGR2GRAY);
    Core.absdiff(bg, cg, diff);
    Imgproc.threshold(diff, tdiff, PIXEL_DIFF_THRESHOLD, 0.0, Imgproc.THRESH_TOZERO);
    if (Core.countNonZero(tdiff) <= IMAGE_DIFF_THRESHOLD) {
      return false;
    }

    Imgproc.threshold(diff, diff, PIXEL_DIFF_THRESHOLD, 255, Imgproc.THRESH_BINARY);
    Imgproc.dilate(diff, diff, new Mat());
    Mat se = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(5, 5));
    Imgproc.morphologyEx(diff, diff, Imgproc.MORPH_CLOSE, se);

    List<MatOfPoint> points = new ArrayList<MatOfPoint>();
    Mat contours = new Mat();
    Imgproc.findContours(diff, points, contours, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
    int n = 0;
    for (Mat pm : points) {
      log(lvl, "(%d) %s", n++, pm);
      printMatI(pm);
    }
    log(lvl, "contours: %s", contours);
    printMatI(contours);
    return true;
  }
  public Mat convertImageToBlackWhite(Mat imageMat, boolean applyGaussBlur) {
    Mat imageCloneMat = imageMat.clone();

    if (applyGaussBlur) {
      Imgproc.GaussianBlur(imageCloneMat, imageCloneMat, new Size(3, 3), 0, 0);
    }

    double thresh =
        Imgproc.threshold(
            imageCloneMat, imageCloneMat, 0, 255, Imgproc.THRESH_BINARY | Imgproc.THRESH_OTSU);

    Imgproc.threshold(imageCloneMat, imageCloneMat, thresh, 255, Imgproc.THRESH_BINARY_INV);

    return (imageCloneMat);
  }
Beispiel #3
0
  public static Mat warp(Mat inputMat, Mat startM, int factor) {

    int resultWidth = 400 * factor;
    int resultHeight = 240 * factor;

    Mat outputMat = new Mat(resultWidth, resultHeight, CvType.CV_8UC4);

    Point ocvPOut1 = new Point(0, 0);
    Point ocvPOut2 = new Point(0, resultHeight);
    Point ocvPOut3 = new Point(resultWidth, resultHeight);
    Point ocvPOut4 = new Point(resultWidth, 0);
    List<Point> dest = new ArrayList<Point>();
    dest.add(ocvPOut1);
    dest.add(ocvPOut2);
    dest.add(ocvPOut3);
    dest.add(ocvPOut4);
    Mat endM = Converters.vector_Point2f_to_Mat(dest);

    Mat perspectiveTransform = Imgproc.getPerspectiveTransform(startM, endM);

    Imgproc.warpPerspective(
        inputMat,
        outputMat,
        perspectiveTransform,
        new Size(resultWidth, resultHeight),
        Imgproc.INTER_AREA);
    Imgproc.GaussianBlur(outputMat, outputMat, new org.opencv.core.Size(5, 5), 5);
    Imgproc.resize(outputMat, outputMat, new Size(resultWidth / factor, resultHeight / factor));

    Imgproc.threshold(outputMat, outputMat, 127, 255, Imgproc.THRESH_TOZERO);
    return outputMat;
  }
  /**
   * Generates a mask of all resistors present in the given Mat. Also displays this mask in the
   * Bottom Left frame of the GUI.
   *
   * @param imgCap The Mat image to generate the mask for
   * @param type The threshold operation type
   * @return The mask as a Mat
   */
  private Mat generateResistorMask(Mat imgCap, int type) {
    Mat imgHSV = new Mat();
    Mat satImg = new Mat();

    // convert the input image from BGR to HSV
    Imgproc.cvtColor(imgCap, imgHSV, Imgproc.COLOR_BGR2HSV);

    ArrayList<Mat> channels = new ArrayList<Mat>();
    Core.split(imgHSV, channels);
    // extract the saturation channel
    satImg = channels.get(1);

    // remove the background and the resistor leads (combined with previous blurring)
    // thresh ~86
    Imgproc.threshold(satImg, satImg, RESISTOR_MASK_THRESHOLD, 255, type);

    paintBL(satImg);

    return satImg;
  }
  /**
   * @param inputImg
   * @param minValue
   * @param maxValue
   * @return Mat
   */
  public static Mat thresholding(Mat inputImg, Integer minValue, Integer maxValue) {

    Mat frame = inputImg;
    // яскравість
    // frame.convertTo(frame , -1, 10d * 33 / 100, 0);
    // Imgproc.medianBlur(frame,frame, 17);

    // Core.bitwise_not(frame,frame );

    // Mat frame = new Mat(image.rows(), image.cols(), image.type());

    // frame.convertTo(frame, -1, 10d * 20 / 100, 0);

    Mat hsvImg = new Mat();
    List<Mat> hsvPlanes = new ArrayList<>();
    Mat thresholdImg = new Mat();

    int thresh_type = Imgproc.THRESH_BINARY_INV;

    // if (this.inverse.isSelected())
    // thresh_type = Imgproc.THRESH_BINARY;

    // threshold the image with the average hue value
    // System.out.println("size " +frame.size());
    hsvImg.create(frame.size(), CvType.CV_8U);
    // Imgproc.cvtColor(frame, hsvImg, Imgproc.COLOR_BGR2HSV);
    Core.split(hsvImg, hsvPlanes);

    // get the average hue value of the image
    // double threshValue = PreProcessingOperation.getHistAverage(hsvImg, hsvPlanes.get(0));
    // System.out.println(threshValue);
    /*
    if(threshValue > 40){
        maxValue = 160;
    }else{
        maxValue = 40;
    }*/

    //        Imgproc.threshold(hsvPlanes.get(1), thresholdImg, minValue , maxValue , thresh_type);

    Imgproc.blur(thresholdImg, thresholdImg, new Size(27, 27));

    // dilate to fill gaps, erode to smooth edges
    Imgproc.dilate(thresholdImg, thresholdImg, new Mat(), new Point(-1, -1), 1);
    Imgproc.erode(thresholdImg, thresholdImg, new Mat(), new Point(-1, -1), 1);

    Imgproc.threshold(thresholdImg, thresholdImg, minValue, maxValue, Imgproc.THRESH_BINARY);

    // create the new image
    Mat foreground = new Mat(frame.size(), CvType.CV_8UC3, new Scalar(255, 255, 255));
    Core.bitwise_not(thresholdImg, foreground);

    frame.copyTo(foreground, thresholdImg);

    ///////////////////////////////////////////////////////////////////////////////////////
    ///
    ////

    return foreground;
    /*Mat hsvImg = new Mat();
    List<Mat> hsvPlanes = new ArrayList<>();
    Mat thresholdImg = new Mat();
    int thresh_type = Imgproc.THRESH_BINARY_INV;
    // threshold the image with the average hue value
    hsvImg.create(inputImg.size(), CvType.CV_8U);
    Imgproc.cvtColor(inputImg, hsvImg, Imgproc.COLOR_BGR2HSV);
    Core.split(hsvImg, hsvPlanes);
    // get the average hue value of the image
    double threshValue = PreProcessingOperation.getHistAverage(hsvImg, hsvPlanes.get(0));
    Imgproc.threshold(hsvPlanes.get(0), thresholdImg, minValue,
            maxValue, thresh_type);
    Imgproc.blur(thresholdImg, thresholdImg, new Size(3, 3));
    // dilate to fill gaps, erode to smooth edges
    Imgproc.dilate(thresholdImg, thresholdImg, new Mat(), new Point(-1, -1), 3);
    Imgproc.erode(thresholdImg, thresholdImg, new Mat(), new Point(-1, -1), 1);
    Imgproc.threshold(thresholdImg, thresholdImg, minValue,
            maxValue, Imgproc.THRESH_BINARY);
    // create the new image
    Mat foreground = new Mat(inputImg.size(), CvType.CV_8UC3, new Scalar(255, 255, 255));
    inputImg.copyTo(foreground, thresholdImg);
    Core.bitwise_not(foreground,foreground);
    return foreground;*/
  }
  /**
   * @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;
  }
  /**
   * 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);
        }
      }
    }
  }
    @Override
    public void run() {
      // Initialisation
      cptRect = 0;
      initialiseRectangle();

      if (video.isOpened()) {
        while (begin == true) {
          // On récupère l'image de la CaptureVideo
          video.retrieve(frameaux);
          // On modifie les dimensions de la frame
          Imgproc.resize(frameaux, frame, frame.size());
          // On copie
          frame.copyTo(currentFrame);

          if (jCheckBoxMotionDetection.isSelected()) {
            if (firstFrame) {
              frame.copyTo(lastFrame);
              firstFrame = false;
              continue;
            }

            // Soustraction de currentFrame par rapport à la dernière
            Core.subtract(currentFrame, lastFrame, processedFrame);

            // Filtre en niveau de gris
            Imgproc.cvtColor(processedFrame, processedFrame, Imgproc.COLOR_RGB2GRAY);

            // Filtre threshold + récupération du Jslider
            int threshold = jSliderThreshold.getValue();
            Imgproc.threshold(
                processedFrame, processedFrame, threshold, 255, Imgproc.THRESH_BINARY);

            // Detecte les contours et check dans les
            detection_contours(currentFrame, processedFrame);
          }
          // Dessine les rectangles d'authentifications
          drawRectangle();

          currentFrame.copyTo(processedFrame);

          // Encodage de la frame en MatOfByte
          Highgui.imencode(".jpg", processedFrame, matOfByte);
          byte[] byteArray = matOfByte.toArray();

          // Affichage de l'image
          try {
            in = new ByteArrayInputStream(byteArray);
            bufImage = ImageIO.read(in);
            image.updateImage(bufImage);
          } catch (Exception ex) {
            ex.printStackTrace();
          }

          try {
            Thread.sleep(50);
          } catch (Exception ex) {
            ex.printStackTrace();
          }
        }
      }
    }
  /** new algo */
  public static String findTips(Mat image) {
    Mat binary = new Mat();

    // Convert to B&W image
    Imgproc.threshold(image, binary, 0, 255, Imgproc.THRESH_BINARY);

    // Convert to binary image, 1 channel
    Imgproc.cvtColor(binary, binary, Imgproc.COLOR_RGB2GRAY);

    boolean flag_bg = false;
    boolean handInQuarterOfImg = false;
    int count_finger = 0;

    int start = -1;
    int end = -1;

    int tester = 0; // test variable CAN BE REMOVED

    double startPercent = 0, endPercent = 0;

    int leftHeight = -1;

    int firstHandPixel = -1;
    int lastHandPixel = -1;

    int KV_midFinger = -1;
    int KV_indxFinger = -1;

    double pixel[];
    for (int i = 0; i < binary.cols(); i++) {
      pixel = binary.get((int) (binary.rows() * 0.23), i);
      if (pixel[0] != 0.0) {
        if (firstHandPixel == -1) firstHandPixel = i;
        if (i < image.cols() / 4) handInQuarterOfImg = true;
        lastHandPixel = i;
        leftHeight = i;
      }

      if (pixel[0] != 0.0) {
        if (flag_bg == false) { // flag_bg is false
          count_finger++;
          flag_bg = true;

          start = i;
        }
        end = i;
      } else {
        if (count_finger == 1) KV_midFinger = end;
        else if (count_finger == 2) KV_indxFinger = start;
        flag_bg = false;
        if (tester != count_finger) { // remove this line -- just for
          // testing
          startPercent = ((double) start / (double) binary.cols()) * 100;
          endPercent = ((double) end / (double) binary.cols()) * 100;
        }
        tester = count_finger; // remove this line -- just for testing
      }
    }

    if (count_finger == 3) {
      if (endPercent > 80.0) {
        return "W";
      } else {
        return "F";
      }
    } else if (count_finger == 4) {
      return "B";
    } else if (count_finger == 1) {
      if (endPercent > 70.0)
        if (handInQuarterOfImg) return "B";
        else {
          int result = identify_R_U(binary);
          switch (result) {
            case 1:
              return "R";
            case 2:
              return "U";
            default:
              return " ";
              // return "RU";
          }
        }
      else {
        if ((float) (lastHandPixel - firstHandPixel) / image.cols() > 0.35) return "F";
        else {
          image = Filter.furtherClean(image);
          Mat binary2 = new Mat();
          // Convert to B&W image
          Imgproc.threshold(image, binary2, 0, 255, Imgproc.THRESH_BINARY);

          // Convert to binary image, 1 channel
          Imgproc.cvtColor(binary2, binary2, Imgproc.COLOR_RGB2GRAY);

          flag_bg = false;
          int countHandArea = 0;
          for (int i = 0; i < binary2.rows(); i++) {

            pixel = binary2.get(i, (int) (binary2.cols() * 0.70));

            if (pixel[0] != 0.0) {
              if (!flag_bg) {
                flag_bg = true;
                countHandArea++;
              }
            } else flag_bg = false;
          }
          if (countHandArea == 1) {
            if (checkAngleL(binary)) return "L";
            else {
              int result = identify_R_U(binary);
              switch (result) {
                case 1:
                  return "R";
                case 2:
                  return "U";
                default:
                  return " ";
              }
            }
          } else if (countHandArea == 2) return "D";
          else return " ";
        }
      }
    } else if (count_finger == 2) {
      // int result = identify_K_V(rgb, binary, new Point(KV_midFinger,
      // (int) (binary.rows() * 0.23)), new Point(KV_indxFinger,
      // (int) (binary.rows() * 0.23)));
      int startX = (int) KV_midFinger;
      int startY = (int) (binary.rows() * 0.23);
      int lastDir = 1; // 0 up; 1 down
      int currDir = 1;
      int ctr = 1;
      Point KV_PtsArray[] = new Point[3];
      KV_PtsArray[0] = new Point(startX, startY);
      // Core.circle(rgb,
      // new Point(UL.x + startX, UL.y + startY), 5,
      // new Scalar(0, 0, 255));
      // double pixel[];
      Log.i("START", "" + startX);
      Log.i("END", "" + startY);

      for (int i = startX; i < KV_indxFinger; i++) {

        for (int k = (int) (binary.rows() * 0.23); k < binary.rows(); k++) {

          pixel = binary.get(k, i + 1);
          Log.i("CHECK1", "" + binary.rows());
          Log.i("CHECK2", "" + binary.rows() * 0.23);
          Log.i("CHECK3", "" + (int) (binary.rows() * 0.23));
          Log.i("CHECK4", "" + pixel[0]);

          if (pixel[0] != 0.0) {
            // Plots the edges from hullPoint[0] to hullPoint[1]
            // Core.circle(rgb, new Point(UL.x + startX + 1,
            // UL.y
            // + k), 5, new Scalar(0, 255, 0));
            if (k > startY) currDir = 1;
            else if (k < startY) currDir = 0;
            if (currDir != lastDir && ctr < 3) {
              // Plots 3 significant pts in K and V
              // Core.circle(rgb,
              // new Point(UL.x + i + 1, UL.y + k), 5,
              // new Scalar(0, 0, 255));
              KV_PtsArray[ctr] = new Point(i + 1, k);
              ctr++;
            }
            lastDir = currDir;
            // startX = startX + 1;
            startY = k;
            break;
          }
        }

        if (ctr < 3 && i + 1 == (int) KV_indxFinger) {
          // Core.circle(rgb, new Point(UL.x + startX, UL.y + startY),
          // 5,
          // new Scalar(0, 0, 255));
          KV_PtsArray[ctr] = new Point(i, startY);
          ctr++;
        }

        if (ctr == 3) break;
      }

      if (ctr == 3) {
        if (KV_PtsArray[0].y == KV_PtsArray[2].y) return "V"; // V
        else return "K"; // K
      } else return " ";
    }
    return Integer.toString(count_finger);
  }
  public static void main(String[] args) {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

    //      Mat mat = Mat.eye( 3, 3, CvType.CV_8UC1 );
    //      System.out.println( "mat = " + mat.dump() );

    Sample n = new Sample();
    //   n.templateMatching();

    // put text in image
    //      Mat data= Highgui.imread("images/erosion.jpg");

    //      Core.putText(data, "Sample", new Point(50,80), Core.FONT_HERSHEY_SIMPLEX, 1, new
    // Scalar(0,0,0),2);
    //
    //      Highgui.imwrite("images/erosion2.jpg", data);

    // getting dct of an image
    String path = "images/croppedfeature/go (20).jpg";
    path = "images/wordseg/img1.png";
    Mat image = Highgui.imread(path, Highgui.IMREAD_GRAYSCALE);
    ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();

    Imgproc.threshold(image, image, 0, 255, Imgproc.THRESH_OTSU);
    Imgproc.threshold(image, image, 220, 128, Imgproc.THRESH_BINARY_INV);
    Mat newImg = new Mat(45, 100, image.type());

    newImg.setTo(new Scalar(0));
    n.copyMat(image, newImg);

    int vgap = 25;
    int hgap = 45 / 3;

    Moments m = Imgproc.moments(image, false);
    Mat hu = new Mat();
    Imgproc.HuMoments(m, hu);
    System.out.println(hu.dump());

    //      //divide the mat into 12 parts then get the features of each part
    //      int count=1;
    //      for(int j=0; j<45; j+=hgap){
    //    	  for(int i=0;i<100;i+=vgap){
    //    		  Mat result = newImg.submat(j, j+hgap, i, i+vgap);
    //
    //
    //    		  Moments m= Imgproc.moments(result, false);
    //    		  double m01= m.get_m01();
    //    		  double m00= m.get_m00();
    //    		  double m10 = m.get_m10();
    //    		  int x= m00!=0? (int)(m10/m00):0;
    //    		  int y= m00!=0? (int)(m01/m00):0;
    //    		  Mat hu= new Mat();
    //    		  Imgproc.HuMoments(m, hu);
    //    		  System.out.println(hu.dump());
    //    		  System.out.println(count+" :"+x+" and "+y);
    //    		  Imgproc.threshold(result, result, 0,254, Imgproc.THRESH_BINARY_INV);
    //    		  Highgui.imwrite("images/submat/"+count+".jpg", result);
    //    		  count++;
    //
    //    	  }
    //      }
    //
    //    for(int i=vgap;i<100;i+=vgap){
    //	  Point pt1= new Point(i, 0);
    //      Point pt2= new Point(i, 99);
    //      Core.line(newImg, pt1, pt2, new Scalar(0,0,0));
    //  }
    //  for(int i=hgap;i<45;i+=hgap){
    //	  Point pt1= new Point(0, i);
    //      Point pt2= new Point(99, i);
    //      Core.line(newImg, pt1, pt2, new Scalar(0,0,0));
    //  }
    //      Highgui.imwrite("images/submat/copyto.jpg", newImg);
  }
  public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
    mRgba = inputFrame.rgba();
    // convert to Gray scale
    Imgproc.cvtColor(mRgba, img_gray, Imgproc.COLOR_BGR2GRAY);
    // make it binary with threshold=100
    Imgproc.threshold(img_gray, erd, 100, 255, Imgproc.THRESH_OTSU);
    // remove pixel noise by "erode" with structuring element of 9X9
    Mat erode = Imgproc.getStructuringElement(Imgproc.MORPH_ERODE, new Size(9, 9));
    Imgproc.erode(erd, tgt, erode);
    // apply "dilation" to enlarge object
    Mat dilate = Imgproc.getStructuringElement(Imgproc.MORPH_DILATE, new Size(9, 9));
    Imgproc.dilate(tgt, erd, dilate);
    // take a window
    Size s = erd.size();
    int W = (int) s.width;
    int H = (int) s.height;
    Rect r = new Rect(0, H / 2 - 100, W, 200);
    Mat mask = new Mat(s, CvType.CV_8UC1, new Scalar(0, 0, 0));
    rectangle(mask, r.tl(), r.br(), new Scalar(255, 255, 255), -1);
    erd.copyTo(window, mask);

    // find the contours
    Imgproc.findContours(window, contours, dest, 0, 2);
    // find largest contour
    int maxContour = -1;
    double area = 0;
    for (int i = 0; i < contours.size(); i++) {
      double contArea = Imgproc.contourArea(contours.get(i));
      if (contArea > area) {
        area = contArea;
        maxContour = i;
      }
    }
    // form bounding rectangle for largest contour
    Rect rect = null;
    if (maxContour > -1) rect = Imgproc.boundingRect(contours.get(maxContour));
    // position to center
    while (!train) {
      if (rect != null) {
        Imgproc.rectangle(mRgba, rect.tl(), rect.br(), new Scalar(255, 255, 255), 5);
        if ((rect.x + rect.width / 2) > W / 2 - 20 && (rect.x + rect.width / 2) < W / 2 + 20) {
          runOnUiThread(
              new Runnable() {
                @Override
                public void run() {
                  Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_SHORT).show();
                }
              });
          train = true;
        }
      }
      if (contours != null) contours.clear();
      return mRgba;
    }
    if (train) {
      if (rect != null) {
        Imgproc.rectangle(mRgba, rect.tl(), rect.br(), new Scalar(255, 255, 255), 5);
        // direction of movement
        int thr = 100;
        if ((rect.x + rect.width / 2) < (W / 2 - thr)) {
          // move to the RIGHT
          uHandler.obtainMessage(MainActivity.LEFT).sendToTarget();
        } else {
          if ((rect.x + rect.width / 2) > (W / 2 + thr)) {
            uHandler.obtainMessage(MainActivity.RIGHT).sendToTarget();
          } else {
            uHandler.obtainMessage(MainActivity.FORWARD).sendToTarget();
          }
        }
      } else {
        // stop moving
        uHandler.obtainMessage(MainActivity.STOP).sendToTarget();
      }
    }
    if (contours != null) contours.clear();
    return mRgba;
  }
Beispiel #12
0
  public void run() {
    ArrayList<Geometry.Quad> squares;

    Mat image = new Mat();
    Utils.bitmapToMat(source, image);

    Mat bwimage = new Mat();
    cvtColor(image, bwimage, COLOR_RGB2GRAY);

    Mat blurred = new Mat();
    medianBlur(image, blurred, 9);

    int width = blurred.width();
    int height = blurred.height();
    int depth = blurred.depth();

    Mat gray0 = new Mat(width, height, depth);
    blurred.copyTo(gray0);

    squares = new ArrayList<Geometry.Quad>();

    // find squares in every color plane of the image
    for (int c = 0; c < 3; c++) {
      Core.mixChannels(
          Arrays.asList(blurred), Arrays.asList(new Mat[] {gray0}), new MatOfInt(c, 0));

      // try several threshold levels
      int thresholdLevel = 8;
      for (int l = 0; l < thresholdLevel; l++) {
        // use canny instead of 0 threshold level
        // canny helps catch squares with gradient shading
        Mat gray = new Mat();

        if (l == 0) {
          Canny(gray0, gray, 10.0, 20.0, 3, false);
          Mat kernel = new Mat(11, 11, CvType.CV_8UC1, new Scalar(1));
          dilate(gray, gray, kernel);
        } else {
          Mat thresh = new Mat(gray0.rows(), gray0.cols(), gray0.type());
          threshold(gray0, thresh, ((double) l) / thresholdLevel * 255, 128, THRESH_BINARY_INV);
          cvtColor(thresh, gray, COLOR_BGR2GRAY);
        }

        // find contours and store them in a list
        List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
        findContours(gray, contours, new Mat(), RETR_LIST, CHAIN_APPROX_SIMPLE);

        // test contours
        for (MatOfPoint contour : contours) {
          // approximate contour with accuracy proportional to the contour perimeter
          MatOfPoint2f thisContour = new MatOfPoint2f(contour.toArray());
          double arclength = 0.02 * arcLength(thisContour, true);
          MatOfPoint2f approx = new MatOfPoint2f();
          approxPolyDP(thisContour, approx, arclength, true);

          double area = contourArea(approx);
          boolean isConvex = isContourConvex(new MatOfPoint(approx.toArray()));

          if (approx.rows() == 4 && Math.abs(area) > SQUARE_SIZE && isConvex) {
            double maxCosine = 0;

            Point[] approxArray = approx.toArray();
            for (int j = 2; j < 5; j++) {
              double cosine =
                  Math.abs(angle(approxArray[j % 4], approxArray[j - 2], approxArray[j - 1]));
              maxCosine = Math.max(maxCosine, cosine);
            }

            if (maxCosine > THRESHOLD_COS) {
              squares.add(new Geometry.Quad(approxArray));
              Log.d(TAG, "area = " + area);
            }
          }
        }
      }
    }

    result = new Bundle();
    result.putParcelableArrayList("squares", squares);
    Log.d(TAG, "result created");

    finish();
  }
Beispiel #13
0
  public MatOfPoint getContourOfBestMatch() {

    if (matches == null) return null;

    Mat threshold = new Mat(imgGray.size(), imgGray.type());
    Imgproc.threshold(imgGray, threshold, 70, 255, Imgproc.THRESH_TOZERO);

    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
    Imgproc.findContours(
        threshold, contours, new Mat(), Imgproc.RETR_CCOMP, Imgproc.CHAIN_APPROX_NONE);
    // HashMap<Integer,MatOfPoint> coordinates = computeCoord(contours,)

    if (contours.size() == 0) return null;
    List<DMatch> matchList = matches.toList();
    List<KeyPoint> keyPointList = keyPointImg.toList();

    HashMap<Integer, Double> contourDensityMap = new HashMap<Integer, Double>();

    Log.i("getContourBestMatch::", "contour size::" + contours.size());

    for (int idx = 0; idx < contours.size(); idx++) {
      MatOfPoint2f ctr2f = new MatOfPoint2f(contours.get(idx).toArray());
      // double contourarea = Imgproc.contourArea(contours.get(idx));

      double contourarea = contours.get(idx).rows();
      if (contourarea < 50) continue;

      Rect r = Imgproc.boundingRect(contours.get(idx));

      double count = 0;
      // Log.i("contour area","contour area is::"+contourarea);
      for (DMatch match : matchList) {

        Point q = keyPointList.get(match.queryIdx).pt;
        if (q.x >= r.x && q.x <= (r.x + r.width) && q.y >= r.y && q.y <= (r.y + r.height)) count++;

        //
        // if(Imgproc.pointPolygonTest(ctr2f,keyPointList.get(match.queryIdx).pt,true)>0){
        //                    if(null ==contourDensityMap.get(idx))
        //                        contourDensityMap.put(idx,1.0);
        //
        //                    else{
        //                        contourDensityMap.put(idx,((Double)contourDensityMap.get(idx))+1);
        //                    }
        //
        //                }

      }
      //            if(contourDensityMap.containsKey(idx)) {
      //
      // Log.i("contourPoint","idx::"+idx+"count::"+contourDensityMap.get(idx)+"contour
      // area::"+contourarea);
      //                contourDensityMap.put(idx, contourDensityMap.get(idx) / contourarea);
      //            }
      if (count != 0) {
        contourDensityMap.put(idx, count / contourarea);
      }
    }

    Log.i("MarkerTracker", "contour density size::" + contourDensityMap.size());

    Map.Entry<Integer, Double> maxEntry = null;

    for (Map.Entry<Integer, Double> entry : contourDensityMap.entrySet()) {
      Log.i("contourDensityMap", "Entry value::" + entry.getValue());
      if (maxEntry == null || entry.getValue().compareTo(maxEntry.getValue()) > 0) {
        maxEntry = entry;
      }
    }
    Log.i("maxEntry::", "" + (maxEntry == null ? null : maxEntry.getKey()));
    // return contours;
    return contours.get(maxEntry != null ? maxEntry.getKey() : 0);
  }