Esempio n. 1
0
  /**
   * Process the given Mat, finding, classifying and displaying all resistors present.
   *
   * @param originalImg The image to parse
   */
  private void parseImage(Mat originalImg) {
    Mat workingCopy = new Mat();
    originalImg.copyTo(workingCopy);

    // blur the original image to form resistor blobs. should remove white/silver/grey bands which
    // would otherwise
    // get picked up as part of the background, leads or shadows.
    Mat blurred = new Mat();
    // Imgproc.medianBlur(workingCopy, blurred, 29);
    Imgproc.blur(workingCopy, blurred, new Size(20, 20)); // faster

    // get a mask off the background
    Mat resistorMask = new Mat();
    generateResistorMask(blurred, Imgproc.THRESH_BINARY).copyTo(resistorMask);

    // find countours in mask
    List<MatOfPoint> contours = getContours(resistorMask, originalImg, true);

    // extract resistors in input image
    List<Resistor> resistors = extractResistorsFromContours(contours, originalImg, true);

    // segment and extract colour bands for each resistor
    extractColourBandsAndClassify(resistors, true);

    // calculate and display resistance value for each resistor based on classified colour bands
    calculateAndDisplayResistanceValues(resistors);
  }
  /**
   * @param image
   * @param size
   * @return Mat
   */
  public static Mat cannyDetection(Mat image, int size) {

    Mat grayImage = new Mat();
    Mat detectedEdges = new Mat();
    // convert to grayscale
    Imgproc.cvtColor(image, grayImage, Imgproc.COLOR_BGR2GRAY);
    // reduce noise with a 3x3 kernel
    Imgproc.blur(grayImage, detectedEdges, new Size(3, 3));
    // canny detector, with ratio of lower:upper threshold of 3:1
    Imgproc.Canny(detectedEdges, detectedEdges, size, size / 3, 7, false);
    return detectedEdges;
  }
Esempio n. 3
0
  // OpenCV code
  private void modifyImage(String fileName) {
    // Create a face detector from the cascade file
    CascadeClassifier faceDetector = new CascadeClassifier("haarcascade_frontalface_alt.xml");
    Mat image = Highgui.imread(fileName);

    // Detect faces in the image.
    // MatOfRect is a special container class for Rect.
    MatOfRect faceDetections = new MatOfRect();
    faceDetector.detectMultiScale(image, faceDetections);

    // Blur each face
    for (Rect rect : faceDetections.toArray()) {
      Mat faceArea = image.submat(rect);
      Imgproc.blur(faceArea, faceArea, new Size(30, 30));
    }
    // Save the modified image
    Highgui.imwrite("edited_" + fileName, image);
  }
  /**
   * @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;*/
  }