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;
 }
Example #2
0
  private void prepareSpherify(Bitmap bitmap) {

    int insideCircleOutRadius;
    int topY, footY, withinHeight;

    if (!OpenCVLoader.initDebug()) {
      // Handle initialization error
      AppFunctions.showToast(activity.getApplicationContext(), "OpenGL initialization error!");
      activity.finish();
    }

    if (srcImage == null) {
      srcImage = new Mat();

      srcImage.create(bitmap.getHeight(), bitmap.getWidth(), CvType.CV_8UC3);
      Bitmap myBitmap32 = bitmap.copy(Bitmap.Config.ARGB_8888, true);
      Utils.bitmapToMat(myBitmap32, srcImage);

      Imgproc.cvtColor(srcImage, srcImage, Imgproc.COLOR_BGR2RGB, 4);
    }

    Utils.bitmapToMat(bitmap, srcImage);
    // cropImage();
    seamlessEdges(srcImage);
    mSrcWidth = srcImage.cols();
    mSrcHeight = srcImage.rows();

    halfGenImageSize = genImageSize / 2;
    spherifiedImage = new Mat();
    spherifiedImage.create(genImageSize, genImageSize, CvType.CV_8UC4);

    insideCircleOutRadius = (int) (genImageSize / 12);

    topY = (int) (mSrcHeight * (topMargin));
    footY = (int) (mSrcHeight * (footMargin));
    withinHeight = topY - footY;
    scale = withinHeight / ((double) (croppedImageSize / 2 - insideCircleOutRadius));
    offset = (int) (footY - scale * insideCircleOutRadius);
    numProcesses = Runtime.getRuntime().availableProcessors();
    if (numProcesses < 3) numProcesses = 1;
    else numProcesses = 3;
  }
Example #3
0
  public void setSpherifiedImage(Bitmap bitmap) {

    spherifiedImage = new Mat();

    spherifiedImage.create(bitmap.getHeight(), bitmap.getWidth(), CvType.CV_8UC3);

    Utils.bitmapToMat(bitmap, spherifiedImage);

    genImageSize = spherifiedImage.cols();
    croppedImageSize = (int) (genImageSize * Math.sin(Math.PI / 4.0));
    halfGenImageSize = genImageSize / 2;
  }
  /**
   * @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;*/
  }