예제 #1
0
  public final double distance(F g1, F g2, LocalFeaturesMatches matches) {

    distCount++;

    if (matches == null || matches.size() == 0) return 1.0;

    if (matches.size() / (double) g1.size() < minPercMatches) {
      updateStats(0, matches.size(), 0);
      return 1.0;
    }

    if (nHoughMatches) {
      Hashtable<Long, LocalFeaturesMatches> ht = this.getLoweHoughTransforms_HT(matches);
      if (ht == null || ht.size() == 0) {
        updateStats(0, matches.size(), 0);
        return 1.0;
      }
      LocalFeaturesMatches[] ordered = LoweHoughTransform.orderHT(ht);
      updateStats(0, matches.size(), 0);
      return 1.0 - this.getPercentage(ordered[0].size(), g1.size(), g2.size());
    } else if (onlyNMatches) {
      updateStats(0, matches.size(), 0);
      return 1.0 - this.getPercentage(matches.size(), g1.size(), g2.size());
    }

    double res = 0;

    if (matches.size() / (double) g1.size() < minPercMatches) {
      updateStats(0, matches.size(), 0);
      return 1.0;
    }

    ArrayList<TransformationHypothesis> trArr = getTrHypothesis(matches, g1.size());

    if (trArr == null || trArr.size() == 0) {
      res = 0.0;
      updateStats(0, 0, matches.size());
    } else {

      res = this.getPercentage(trArr.get(0).getMatches().size(), g1.size(), g2.size());

      updateStats(matches.size(), trArr.get(0).getMatches().size(), 0);
    }
    if (res > 1.0) res = 1.0;

    return 1.0 - res;
  }
 /** {@inheritDoc} */
 @Override
 public int size() {
   return F.size(iterator());
 }
  /** {@inheritDoc} */
  @Override
  public void onCollision(
      Collection<GridCollisionJobContext> waitJobs,
      Collection<GridCollisionJobContext> activeJobs) {
    assert waitJobs != null;
    assert activeJobs != null;

    int activeSize = F.size(activeJobs, RUNNING_JOBS);

    waitingCnt.set(waitJobs.size());
    runningCnt.set(activeSize);
    heldCnt.set(activeJobs.size() - activeSize);

    int waitSize = waitJobs.size();

    int activateCnt = parallelJobsNum - activeSize;

    if (activateCnt > 0 && !waitJobs.isEmpty()) {
      if (waitJobs.size() <= activateCnt) {
        for (GridCollisionJobContext waitJob : waitJobs) {
          waitJob.activate();

          waitSize--;
        }
      } else {
        List<GridCollisionJobContext> passiveList =
            new ArrayList<GridCollisionJobContext>(waitJobs);

        Collections.sort(
            passiveList,
            new Comparator<GridCollisionJobContext>() {
              /** {@inheritDoc} */
              @Override
              public int compare(GridCollisionJobContext o1, GridCollisionJobContext o2) {
                int p1 = getJobPriority(o1);
                int p2 = getJobPriority(o2);

                return p1 < p2 ? 1 : p1 == p2 ? 0 : -1;
              }
            });

        if (preventStarvation) bumpPriority(waitJobs, passiveList);

        for (int i = 0; i < activateCnt; i++) {
          passiveList.get(i).activate();

          waitSize--;
        }
      }
    }

    if (waitSize > waitJobsNum) {
      List<GridCollisionJobContext> waitList = new ArrayList<GridCollisionJobContext>(waitJobs);

      // Put jobs with highest priority first.
      Collections.sort(
          waitList,
          new Comparator<GridCollisionJobContext>() {
            /** {@inheritDoc} */
            @Override
            public int compare(GridCollisionJobContext o1, GridCollisionJobContext o2) {
              int p1 = getJobPriority(o1);
              int p2 = getJobPriority(o2);

              return p1 < p2 ? 1 : p1 == p2 ? 0 : -1;
            }
          });

      int skip = waitJobs.size() - waitSize;

      int i = 0;

      for (GridCollisionJobContext waitCtx : waitList) {
        if (++i >= skip) {
          waitCtx.cancel();

          if (--waitSize <= waitJobsNum) break;
        }
      }
    }
  }
  @Override
  public boolean process() {
    final long startTime = System.currentTimeMillis();
    /*
    if ( container.numDimensions() == 3 && Array.class.isInstance( container ) && FloatType.class.isInstance( container.createVariable() ))
    {
      		//System.out.println( "GaussianConvolution: Input is instance of Image<Float> using an Array3D, fast forward algorithm");
      		computeGaussFloatArray3D();

      		processingTime = System.currentTimeMillis() - startTime;

      		return true;
    }
      	*/
    final Img<T> temp = outputFactory.create(input, input.firstElement().createVariable());
    final long containerSize = input.size();

    //
    // Folding loop
    //
    for (int dim = 0; dim < numDimensions; dim++) {
      final int currentDim = dim;

      final AtomicInteger ai = new AtomicInteger(0);
      final Thread[] threads = SimpleMultiThreading.newThreads(numThreads);

      final long threadChunkSize = containerSize / threads.length;
      final long threadChunkMod = containerSize % threads.length;

      for (int ithread = 0; ithread < threads.length; ++ithread)
        threads[ithread] =
            new Thread(
                new Runnable() {
                  public void run() {
                    // Thread ID
                    final int myNumber = ai.getAndIncrement();

                    // System.out.println("Thread " + myNumber + " folds in dimension " +
                    // currentDim);

                    final RandomAccess<T> inputIterator;
                    final Cursor<T> outputIterator;

                    if (numDimensions % 2 == 0) // even number of dimensions ( 2d, 4d, 6d, ... )
                    {
                      if (currentDim == 0) // first dimension convolve to the temporary container
                      {
                        inputIterator = input.randomAccess(outOfBoundsFactory1);
                        outputIterator = temp.localizingCursor();
                      } else if (currentDim % 2
                          == 1) // for odd dimension ids we convolve to the output container,
                                // because that might be the last convolution
                      {
                        inputIterator = temp.randomAccess(outOfBoundsFactory2);
                        outputIterator = convolved.localizingCursor();
                      } else // if ( currentDim % 2 == 0 ) // for even dimension ids we convolve to
                             // the temp container, it is not the last convolution for sure
                      {
                        inputIterator = convolved.randomAccess(outOfBoundsFactory2);
                        outputIterator = temp.localizingCursor();
                      }
                    } else // ( numDimensions % 2 != 0 ) // even number of dimensions ( 1d, 3d, 5d,
                           // ... )
                    {
                      if (currentDim
                          == 0) // first dimension convolve to the output container, in the 1d case
                                // we are done then already
                      {
                        inputIterator = input.randomAccess(outOfBoundsFactory1);
                        outputIterator = convolved.localizingCursor();
                      } else if (currentDim % 2
                          == 1) // for odd dimension ids we convolve to the output container,
                                // because that might be the last convolution
                      {
                        inputIterator = convolved.randomAccess(outOfBoundsFactory2);
                        outputIterator = temp.localizingCursor();
                      } else // if ( currentDim % 2 == 0 ) // for even dimension ids we convolve to
                             // the temp container, it is not the last convolution for sure
                      {
                        inputIterator = temp.randomAccess(outOfBoundsFactory2);
                        outputIterator = convolved.localizingCursor();
                      }
                    }

                    // move to the starting position of the current thread
                    final long startPosition = myNumber * threadChunkSize;

                    // the last thread may has to run longer if the number of pixels cannot be
                    // divided by the number of threads
                    final long loopSize;
                    if (myNumber == numThreads - 1) loopSize = threadChunkSize + threadChunkMod;
                    else loopSize = threadChunkSize;

                    // convolve the container in the current dimension using the given cursors
                    float[] kernelF = new float[kernel[currentDim].length];

                    for (int i = 0; i < kernelF.length; ++i)
                      kernelF[i] = (float) kernel[currentDim][i];

                    convolve(
                        inputIterator,
                        outputIterator,
                        currentDim,
                        kernelF,
                        startPosition,
                        loopSize);
                  }
                });
      SimpleMultiThreading.startAndJoin(threads);
    }

    processingTime = System.currentTimeMillis() - startTime;

    return true;
  }