Example usage for org.opencv.core MatOfKeyPoint toArray

List of usage examples for org.opencv.core MatOfKeyPoint toArray

Introduction

In this page you can find the example usage for org.opencv.core MatOfKeyPoint toArray.

Prototype

public KeyPoint[] toArray() 

Source Link

Usage

From source file:cn.xiongyihui.webcam.JpegFactory.java

License:Open Source License

public void onPreviewFrame(byte[] data, Camera camera) {
    YuvImage yuvImage = new YuvImage(data, ImageFormat.NV21, mWidth, mHeight, null);

    mJpegOutputStream.reset();/*  w  w w .jav a2 s  . co  m*/

    try {
        //Log.e(TAG, "Beginning to read values!");
        double distanceTemplateFeatures = this.globalClass.getDistanceTemplateFeatures();
        double xTemplateCentroid = this.globalClass.getXtemplateCentroid();
        double yTemplateCentroid = this.globalClass.getYtemplateCentroid();
        int x0template = this.globalClass.getX0display();
        int y0template = this.globalClass.getY0display();
        int x1template = this.globalClass.getX1display();
        int y1template = this.globalClass.getY1display();
        Mat templateDescriptor = this.globalClass.getTemplateDescriptor();
        MatOfKeyPoint templateKeyPoints = this.globalClass.getKeyPoints();
        KeyPoint[] templateKeyPointsArray = templateKeyPoints.toArray();
        int numberOfTemplateFeatures = this.globalClass.getNumberOfTemplateFeatures();
        int numberOfPositiveTemplateFeatures = this.globalClass.getNumberOfPositiveTemplateFeatures();
        KeyPoint[] normalisedTemplateKeyPoints = this.globalClass.getNormalisedTemplateKeyPoints();
        double normalisedXcentroid = this.globalClass.getNormalisedXcentroid();
        double normalisedYcentroid = this.globalClass.getNormalisedYcentroid();
        int templateCapturedBitmapWidth = this.globalClass.getTemplateCapturedBitmapWidth();
        int templateCapturedBitmapHeight = this.globalClass.getTemplateCapturedBitmapHeight();
        //Log.e(TAG, "Ended reading values!");
        globalClass.setJpegFactoryDimensions(mWidth, mHeight);
        double scalingRatio, scalingRatioHeight, scalingRatioWidth;

        scalingRatioHeight = (double) mHeight / (double) templateCapturedBitmapHeight;
        scalingRatioWidth = (double) mWidth / (double) templateCapturedBitmapWidth;
        scalingRatio = (scalingRatioHeight + scalingRatioWidth) / 2; //Just to account for any minor variations.
        //Log.e(TAG, "Scaling ratio:" + String.valueOf(scalingRatio));
        //Log.e("Test", "Captured Bitmap's dimensions: (" + templateCapturedBitmapHeight + "," + templateCapturedBitmapWidth + ")");

        //Scale the actual features of the image
        int flag = this.globalClass.getFlag();
        if (flag == 0) {
            int iterate = 0;
            int iterationMax = numberOfTemplateFeatures;

            for (iterate = 0; iterate < (iterationMax); iterate++) {
                Log.e(TAG, "Point detected " + iterate + ":(" + templateKeyPointsArray[iterate].pt.x + ","
                        + templateKeyPointsArray[iterate].pt.y + ")");

                if (flag == 0) {
                    templateKeyPointsArray[iterate].pt.x = scalingRatio
                            * (templateKeyPointsArray[iterate].pt.x + (double) x0template);
                    templateKeyPointsArray[iterate].pt.y = scalingRatio
                            * (templateKeyPointsArray[iterate].pt.y + (double) y0template);
                }
                Log.e(TAG, "Scaled points:(" + templateKeyPointsArray[iterate].pt.x + ","
                        + templateKeyPointsArray[iterate].pt.y + ")");
            }

            this.globalClass.setFlag(1);
        }

        templateKeyPoints.fromArray(templateKeyPointsArray);
        //Log.e(TAG, "Template-features have been scaled successfully!");

        long timeBegin = (int) System.currentTimeMillis();
        Mat mYuv = new Mat(mHeight + mHeight / 2, mWidth, CvType.CV_8UC1);
        mYuv.put(0, 0, data);
        Mat mRgb = new Mat();
        Imgproc.cvtColor(mYuv, mRgb, Imgproc.COLOR_YUV420sp2RGB);

        Mat result = new Mat();
        Imgproc.cvtColor(mRgb, result, Imgproc.COLOR_RGB2GRAY);
        int detectorType = FeatureDetector.ORB;
        FeatureDetector featureDetector = FeatureDetector.create(detectorType);
        MatOfKeyPoint keypointsImage = new MatOfKeyPoint();
        featureDetector.detect(result, keypointsImage);
        KeyPoint[] imageKeypoints = keypointsImage.toArray();

        Scalar color = new Scalar(0, 0, 0);

        DescriptorExtractor descriptorExtractor = DescriptorExtractor.create(DescriptorExtractor.ORB);

        Mat imageDescriptor = new Mat();
        descriptorExtractor.compute(result, keypointsImage, imageDescriptor);

        //BRUTEFORCE_HAMMING apparently finds even the suspicious feature-points! So, inliers and outliers can turn out to be a problem

        DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING);
        MatOfDMatch matches = new MatOfDMatch();
        matcher.match(imageDescriptor, templateDescriptor, matches);

        //Log.e("Prasad", String.valueOf(mWidth) + "," + String.valueOf(mHeight));

        DMatch[] matchesArray = matches.toArray();

        double minimumMatchDistance = globalClass.getHammingDistance();

        int iDescriptorMax = matchesArray.length;
        int iterateDescriptor;

        double xMatchedPoint, yMatchedPoint;
        int flagDraw = Features2d.NOT_DRAW_SINGLE_POINTS;

        Point point;

        double rHigh = this.globalClass.getRhigh();
        double rLow = this.globalClass.getRlow();
        double gHigh = this.globalClass.getGhigh();
        double gLow = this.globalClass.getGlow();
        double bHigh = this.globalClass.getBhigh();
        double bLow = this.globalClass.getBlow();

        double[] colorValue;
        double red, green, blue;
        int[] featureCount;
        double xKernelSize = 9, yKernelSize = 9;
        globalClass.setKernelSize(xKernelSize, yKernelSize);
        double xImageKernelScaling, yImageKernelScaling;

        xImageKernelScaling = xKernelSize / mWidth;
        yImageKernelScaling = yKernelSize / mHeight;
        int[][] kernel = new int[(int) xKernelSize][(int) yKernelSize];
        double[][] kernelCounter = new double[(int) xKernelSize][(int) yKernelSize];
        int numberKernelMax = 10;
        globalClass.setNumberKernelMax(numberKernelMax);
        int[][][] kernelArray = new int[(int) xKernelSize][(int) yKernelSize][numberKernelMax];
        double featureImageResponse;
        double xImageCentroid, yImageCentroid;
        double xSum = 0, ySum = 0;
        double totalImageResponse = 0;

        for (iterateDescriptor = 0; iterateDescriptor < iDescriptorMax; iterateDescriptor++) {
            if (matchesArray[iterateDescriptor].distance < minimumMatchDistance) {
                //MatchedPoint: Awesome match without color feedback
                xMatchedPoint = imageKeypoints[matchesArray[iterateDescriptor].queryIdx].pt.x;
                yMatchedPoint = imageKeypoints[matchesArray[iterateDescriptor].queryIdx].pt.y;

                colorValue = mRgb.get((int) yMatchedPoint, (int) xMatchedPoint);

                red = colorValue[0];
                green = colorValue[1];
                blue = colorValue[2];

                int xKernelFeature, yKernelFeature;
                //Color feedback
                if ((rLow < red) & (red < rHigh) & (gLow < green) & (green < gHigh) & (bLow < blue)
                        & (blue < bHigh)) {
                    try {
                        featureImageResponse = imageKeypoints[matchesArray[iterateDescriptor].queryIdx].response;
                        if (featureImageResponse > 0) {
                            xSum = xSum + featureImageResponse * xMatchedPoint;
                            ySum = ySum + featureImageResponse * yMatchedPoint;
                            totalImageResponse = totalImageResponse + featureImageResponse;
                            point = imageKeypoints[matchesArray[iterateDescriptor].queryIdx].pt;

                            xKernelFeature = (int) (xMatchedPoint * xImageKernelScaling);
                            yKernelFeature = (int) (yMatchedPoint * yImageKernelScaling);
                            kernelCounter[xKernelFeature][yKernelFeature]++;
                            //Core.circle(result, point, 3, color);
                        }
                    } catch (Exception e) {
                    }
                }
                //Log.e(TAG, iterateDescriptor + ": (" + xMatchedPoint + "," + yMatchedPoint + ")");
            }
        }

        int iKernel = 0, jKernel = 0;
        for (iKernel = 0; iKernel < xKernelSize; iKernel++) {
            for (jKernel = 0; jKernel < yKernelSize; jKernel++) {
                if (kernelCounter[iKernel][jKernel] > 0) {
                    kernel[iKernel][jKernel] = 1;
                } else {
                    kernel[iKernel][jKernel] = 0;
                }
            }
        }

        xImageCentroid = xSum / totalImageResponse;
        yImageCentroid = ySum / totalImageResponse;

        if ((Double.isNaN(xImageCentroid)) | (Double.isNaN(yImageCentroid))) {
            //Log.e(TAG, "Centroid is not getting detected! Increasing hamming distance (error-tolerance)!");
            globalClass.setHammingDistance((int) (minimumMatchDistance + 2));
        } else {
            //Log.e(TAG, "Centroid is getting detected! Decreasing and optimising hamming (error-tolerance)!");
            globalClass.setHammingDistance((int) (minimumMatchDistance - 1));
            int jpegCount = globalClass.getJpegFactoryCallCount();
            jpegCount++;
            globalClass.setJpegFactoryCallCount(jpegCount);
            int initialisationFlag = globalClass.getInitialisationFlag();
            int numberOfDistances = 10;
            globalClass.setNumberOfDistances(numberOfDistances);

            if ((jpegCount > globalClass.getNumberKernelMax()) & (jpegCount > numberOfDistances)) {
                globalClass.setInitialisationFlag(1);
            }

            int[][] kernelSum = new int[(int) xKernelSize][(int) yKernelSize],
                    mask = new int[(int) xKernelSize][(int) yKernelSize];
            int iJpeg, jJpeg;
            kernelSum = globalClass.computeKernelSum(kernel);

            Log.e(TAG, Arrays.deepToString(kernelSum));

            for (iJpeg = 0; iJpeg < xKernelSize; iJpeg++) {
                for (jJpeg = 0; jJpeg < yKernelSize; jJpeg++) {
                    if (kernelSum[iJpeg][jJpeg] > (numberKernelMax / 4)) {//Meant for normalised kernel
                        mask[iJpeg][jJpeg]++;
                    }
                }
            }

            Log.e(TAG, Arrays.deepToString(mask));

            int maskedFeatureCount = 1, xMaskFeatureSum = 0, yMaskFeatureSum = 0;

            for (iJpeg = 0; iJpeg < xKernelSize; iJpeg++) {
                for (jJpeg = 0; jJpeg < yKernelSize; jJpeg++) {
                    if (mask[iJpeg][jJpeg] == 1) {
                        xMaskFeatureSum = xMaskFeatureSum + iJpeg;
                        yMaskFeatureSum = yMaskFeatureSum + jJpeg;
                        maskedFeatureCount++;
                    }
                }
            }

            double xMaskMean = xMaskFeatureSum / maskedFeatureCount;
            double yMaskMean = yMaskFeatureSum / maskedFeatureCount;

            double xSquaredSum = 0, ySquaredSum = 0;
            for (iJpeg = 0; iJpeg < xKernelSize; iJpeg++) {
                for (jJpeg = 0; jJpeg < yKernelSize; jJpeg++) {
                    if (mask[iJpeg][jJpeg] == 1) {
                        xSquaredSum = xSquaredSum + (iJpeg - xMaskMean) * (iJpeg - xMaskMean);
                        ySquaredSum = ySquaredSum + (jJpeg - yMaskMean) * (jJpeg - yMaskMean);
                    }
                }
            }

            double xRMSscaled = Math.sqrt(xSquaredSum);
            double yRMSscaled = Math.sqrt(ySquaredSum);
            double RMSimage = ((xRMSscaled / xImageKernelScaling) + (yRMSscaled / yImageKernelScaling)) / 2;
            Log.e(TAG, "RMS radius of the image: " + RMSimage);

            /*//Command the quadcopter and send PWM values to Arduino
            double throttlePWM = 1500, yawPWM = 1500, pitchPWM = 1500;
            double deltaThrottle = 1, deltaYaw = 1, deltaPitch = 1;
                    
            throttlePWM = globalClass.getThrottlePWM();
            pitchPWM = globalClass.getPitchPWM();
            yawPWM = globalClass.getYawPWM();
                    
            deltaThrottle = globalClass.getThrottleDelta();
            deltaPitch = globalClass.getPitchDelta();
            deltaYaw = globalClass.getYawDelta();
                    
            if(yImageCentroid>yTemplateCentroid) {
            throttlePWM = throttlePWM + deltaThrottle;
            }else{
            throttlePWM = throttlePWM - deltaThrottle;
            }
                    
            if(RMSimage>distanceTemplateFeatures) {
            pitchPWM = pitchPWM + deltaPitch;
            }else{
            pitchPWM = pitchPWM - deltaPitch;
            }
                    
            if(xImageCentroid>xTemplateCentroid) {
            yawPWM = yawPWM + deltaYaw;
            }else{
            yawPWM = yawPWM - deltaYaw;
            }
                    
            if(1000>throttlePWM){   throttlePWM = 1000; }
                    
            if(2000<throttlePWM){   throttlePWM = 2000; }
                    
            if(1000>pitchPWM){  pitchPWM = 1000;    }
                    
            if(2000<pitchPWM){  pitchPWM = 2000;    }
                    
            if(1000>yawPWM){    yawPWM = 1000;  }
                    
            if(2000<yawPWM){    yawPWM = 2000;  }
                    
            globalClass.setPitchPWM(pitchPWM);
            globalClass.setYawPWM(yawPWM);
            globalClass.setThrottlePWM(throttlePWM);*/

            //Display bounding circle
            int originalWidthBox = x1template - x0template;
            int originalHeightBox = y1template - y0template;

            double scaledBoundingWidth = (originalWidthBox * RMSimage / distanceTemplateFeatures);
            double scaledBoundingHeight = (originalHeightBox * RMSimage / distanceTemplateFeatures);

            double displayRadius = (scaledBoundingWidth + scaledBoundingHeight) / 2;
            displayRadius = displayRadius * 1.4826;
            displayRadius = displayRadius / numberKernelMax;
            double distanceAverage = 0;
            if (Double.isNaN(displayRadius)) {
                //Log.e(TAG, "displayRadius is NaN!");
            } else {
                distanceAverage = globalClass.imageDistanceAverage(displayRadius);
                //Log.e(TAG, "Average distance: " + distanceAverage);
            }

            if ((Double.isNaN(xImageCentroid)) | Double.isNaN(yImageCentroid)) {
                //Log.e(TAG, "Centroid is NaN!");
            } else {
                globalClass.centroidAverage(xImageCentroid, yImageCentroid);
            }

            if (initialisationFlag == 1) {
                //int displayRadius = 50;

                Point pointDisplay = new Point();
                //pointDisplay.x = xImageCentroid;
                //pointDisplay.y = yImageCentroid;
                pointDisplay.x = globalClass.getXcentroidAverageGlobal();
                pointDisplay.y = globalClass.getYcentroidAverageGlobal();
                globalClass.centroidAverage(xImageCentroid, yImageCentroid);
                int distanceAverageInt = (int) distanceAverage;
                Core.circle(result, pointDisplay, distanceAverageInt, color);
            }

        }

        Log.e(TAG, "Centroid in the streamed image: (" + xImageCentroid + "," + yImageCentroid + ")");
        /*try {
        //Features2d.drawKeypoints(result, keypointsImage, result, color, flagDraw);
        Features2d.drawKeypoints(result, templateKeyPoints, result, color, flagDraw);
        }catch(Exception e){}*/

        //Log.e(TAG, "High (R,G,B): (" + rHigh + "," + gHigh + "," + bHigh + ")");
        //Log.e(TAG, "Low (R,G,B): (" + rLow + "," + gLow + "," + bLow + ")");

        //Log.e(TAG, Arrays.toString(matchesArray));

        try {
            Bitmap bmp = Bitmap.createBitmap(result.cols(), result.rows(), Bitmap.Config.ARGB_8888);
            Utils.matToBitmap(result, bmp);
            //Utils.matToBitmap(mRgb, bmp);
            bmp.compress(Bitmap.CompressFormat.JPEG, mQuality, mJpegOutputStream);
        } catch (Exception e) {
            Log.e(TAG, "JPEG not working!");
        }

        long timeEnd = (int) System.currentTimeMillis();
        Log.e(TAG, "Time consumed is " + String.valueOf(timeEnd - timeBegin) + "milli-seconds!");

        mJpegData = mJpegOutputStream.toByteArray();

        synchronized (mJpegOutputStream) {
            mJpegOutputStream.notifyAll();
        }
    } catch (Exception e) {
        Log.e(TAG, "JPEG-factory is not working!");
    }

}

From source file:cn.xiongyihui.webcam.setup.java

License:Open Source License

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_setup);

    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    final Button cameraButton = (Button) findViewById(R.id.cameraButton);
    final Button selectButton = (Button) findViewById(R.id.selectButton);
    final Button templateButton = (Button) findViewById(R.id.templateButton);
    final Button instructionButton = (Button) findViewById(R.id.instructionButton);
    final ImageView imageView = (ImageView) findViewById(R.id.imageView);

    try {//from  www .j  a va2 s  . co m
        int NUMBER_OF_CORES = Runtime.getRuntime().availableProcessors();

        Toast.makeText(this, NUMBER_OF_CORES, Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
        Log.e(TAG, "Processor-cores are not getting detected!");
    }

    try {
        final Toast toast = Toast.makeText(this,
                "Please capture image; \n" + "select image; \n"
                        + "Drag-and-drop, swipe on the desired region and confirm template!",
                Toast.LENGTH_LONG);
        final TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
        instructionButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (v != null)
                    v.setGravity(Gravity.CENTER);
                toast.show();
            }
        });
    } catch (Exception e) {
        Log.e(TAG, "Instructions are not getting displayed!");
    }

    try {
        cameraButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                startActivityForResult(intent, requestCode);
            }
        });
    } catch (Exception e) {
        Log.e(TAG, "Camera is not working!");
    }

    try {
        selectButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent i = new Intent(Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                startActivityForResult(i, requestCode);

                bitmap = BitmapFactory.decodeFile(filePath);
                imageView.setImageBitmap(bitmap);
            }
        });
    } catch (Exception e) {
        Log.e(TAG, "Selection is not working!");
    }

    try {
        templateButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (imageView.getDrawable() == null) {
                    Log.e(TAG, "Null ImageView!");
                }
                Log.e(TAG, "Button is working.");
                try {
                    bitmap = BitmapFactory.decodeFile(filePath);
                    bitmap = Bitmap.createScaledBitmap(bitmap, bitmapWidth, bitmapHeight, true);
                    Mat frame = new Mat(bitmap.getWidth(), bitmap.getHeight(), CvType.CV_8UC4);
                    Utils.bitmapToMat(bitmap, frame);

                    GlobalClass globalVariable = (GlobalClass) getApplicationContext();
                    globalVariable.setTemplateCapturedBitmapHeight(bitmapHeight);
                    globalVariable.setTemplateCapturedBitmapWidth(bitmapWidth);
                    Log.e(TAG, "Bitmap has been set successfully; Template is being generated!");

                    rect = new Rect(x0final, y0final, x1final - x0final, y1final - y0final);
                    Utils.matToBitmap(frame, bitmap);

                    if (x0final < x1final) {
                        x0display = x0final;
                        x1display = x1final;
                    }
                    if (x0final > x1final) {
                        x1display = x0final;
                        x0display = x1final;
                    }
                    if (y0final < y1final) {
                        y0display = y0final;
                        y1display = y1final;
                    }
                    if (y0final > y1final) {
                        y1display = y0final;
                        y0display = y1final;
                    }

                    long timeBegin = (int) System.currentTimeMillis();

                    bitmap = Bitmap.createBitmap(bitmap, x0display, y0display, x1display - x0display,
                            y1display - y0display);

                    /*String path = Environment.getExternalStorageDirectory().toString();
                            
                    Log.e(TAG, "File is about to be written!");
                            
                    //File file = new File(path, "TraQuad");
                    //bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOutputStream);
                            
                    //Log.e(TAG, "Stored image successfully!");
                    //fOutputStream.flush();
                    //fOutputStream.close();
                            
                    //MediaStore.Images.Media.insertImage(getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());*/

                    /*Prominent colors code; This is not working in Android; OpenCV assertion error
                    Log.e(TAG, "Retrieved image successfully!");
                            
                    Imgproc.medianBlur(frame, frame, 3);
                    Log.e(TAG, "Filtered image successfully!");
                            
                    try {
                    Mat mask = new Mat(bitmap.getWidth(), bitmap.getHeight(), CvType.CV_8UC1);
                    MatOfFloat range = new MatOfFloat(0f, 255f);
                    Mat hist = new Mat();
                    MatOfInt mHistSize = new MatOfInt(256);
                    List<Mat> lHsv = new ArrayList<Mat>(3);
                    Mat hsv = new Mat();
                    Imgproc.cvtColor(frame, hsv, Imgproc.COLOR_RGB2HSV);
                    Core.split(frame, lHsv);
                    Mat mH = lHsv.get(0);
                    Mat mS = lHsv.get(1);
                    Mat mV = lHsv.get(2);
                    ArrayList<Mat> ListMat = new ArrayList<Mat>();
                    ListMat.add(mH);
                    Log.e(TAG, String.valueOf(ListMat));
                    MatOfInt channels = new MatOfInt(0, 1);
                    Imgproc.calcHist(Arrays.asList(mH), channels, mask, hist, mHistSize, range);
                    ListMat.clear();
                    }catch (Exception e){
                    Log.e(TAG, "Prominent colors are not getting detected!");
                    }*/

                    Mat colorFrame = frame;
                    colorFrame = frame.clone();

                    Utils.bitmapToMat(bitmap, frame);
                    Imgproc.cvtColor(frame, frame, Imgproc.COLOR_RGB2GRAY);

                    Log.e(TAG, "Converted color successfully!");

                    int detectorType = FeatureDetector.ORB;
                    //int detectorType = FeatureDetector.SIFT; //SIFT and SURF are not working!
                    //int detectorType = FeatureDetector.SURF;
                    FeatureDetector featureDetector = FeatureDetector.create(detectorType);

                    Log.e(TAG, "Feature detection has begun!");

                    MatOfKeyPoint keypoints = new MatOfKeyPoint();

                    featureDetector.detect(frame, keypoints);

                    Log.e(TAG, "Feature detection has ended successfully!");

                    /*if (!featureDetector.empty()) {
                    //Draw the detected keypoints
                    int flagDraw = Features2d.NOT_DRAW_SINGLE_POINTS;
                    Features2d.drawKeypoints(frame, keypoints, frame, color, flagDraw);
                    Utils.matToBitmap(frame, bitmap);
                    }*/

                    imageView.setImageBitmap(bitmap);

                    Log.e(TAG, "Final bitmap has been loaded!");

                    KeyPoint[] referenceKeypoints = keypoints.toArray();

                    Log.e(TAG, "Number of keypoints detected is " + String.valueOf(referenceKeypoints.length));

                    int iterationMax = referenceKeypoints.length;
                    int iterate = 0;
                    double xFeaturePoint, yFeaturePoint;
                    double xSum = 0, ySum = 0;
                    double totalResponse = 0;
                    double keyPointResponse = 0;
                    double xTemplateCentroid = 0, yTemplateCentroid = 0;

                    DescriptorExtractor descriptorExtractor = DescriptorExtractor
                            .create(DescriptorExtractor.ORB);

                    Mat templateDescriptor = new Mat();

                    descriptorExtractor.compute(frame, keypoints, templateDescriptor);

                    for (iterate = 0; iterate < iterationMax; iterate++) {
                        xFeaturePoint = referenceKeypoints[iterate].pt.x;
                        yFeaturePoint = referenceKeypoints[iterate].pt.y;
                        keyPointResponse = referenceKeypoints[iterate].response;

                        if (keyPointResponse > 0) {
                            xSum = xSum + keyPointResponse * xFeaturePoint;
                            ySum = ySum + keyPointResponse * yFeaturePoint;
                            totalResponse = totalResponse + keyPointResponse;

                            //Log.e(TAG, "Feature " + String.valueOf(iterate) + ":" + String.valueOf(referenceKeypoints[iterate]));
                        }
                    }

                    xTemplateCentroid = xSum / totalResponse;
                    yTemplateCentroid = ySum / totalResponse;
                    Log.e(TAG, "Finished conversion of features to points!");
                    Log.e(TAG, "Centroid location is: (" + xTemplateCentroid + "," + yTemplateCentroid + ")");

                    double xSquareDistance = 0, ySquareDistance = 0;
                    double distanceTemplateFeatures = 0;
                    int numberOfPositiveResponses = 0;

                    double[] colorValue;
                    double rSum = 0, gSum = 0, bSum = 0;
                    double rCentral, gCentral, bCentral;

                    for (iterate = 0; iterate < iterationMax; iterate++) {
                        xFeaturePoint = referenceKeypoints[iterate].pt.x;
                        yFeaturePoint = referenceKeypoints[iterate].pt.y;
                        keyPointResponse = referenceKeypoints[iterate].response;

                        colorValue = colorFrame.get((int) yFeaturePoint, (int) xFeaturePoint);
                        rSum = rSum + colorValue[0];
                        gSum = gSum + colorValue[1];
                        bSum = bSum + colorValue[2];

                        if (keyPointResponse > 0) {
                            xSquareDistance = xSquareDistance
                                    + (xFeaturePoint - xTemplateCentroid) * (xFeaturePoint - xTemplateCentroid);
                            ySquareDistance = ySquareDistance
                                    + (yFeaturePoint - yTemplateCentroid) * (yFeaturePoint - yTemplateCentroid);
                            numberOfPositiveResponses++;
                        }
                    }

                    rCentral = rSum / iterationMax;
                    gCentral = gSum / iterationMax;
                    bCentral = bSum / iterationMax;

                    double deltaColor = 21;

                    double rLow = rCentral - deltaColor;
                    double rHigh = rCentral + deltaColor;
                    double gLow = rCentral - deltaColor;
                    double gHigh = rCentral + deltaColor;
                    double bLow = rCentral - deltaColor;
                    double bHigh = rCentral + deltaColor;

                    Log.e(TAG, "Prominent color (R,G,B): (" + rCentral + "," + gCentral + "," + bCentral + ")");

                    distanceTemplateFeatures = Math
                            .sqrt((xSquareDistance + ySquareDistance) / numberOfPositiveResponses);

                    KeyPoint[] offsetCompensatedKeyPoints = keypoints.toArray();

                    double xMaxNormalisation, yMaxNormalisation;

                    xMaxNormalisation = x1display - x0display;
                    yMaxNormalisation = y1display - y0display;

                    for (iterate = 0; iterate < iterationMax; iterate++) {
                        offsetCompensatedKeyPoints[iterate].pt.x = offsetCompensatedKeyPoints[iterate].pt.x
                                / xMaxNormalisation;
                        offsetCompensatedKeyPoints[iterate].pt.y = offsetCompensatedKeyPoints[iterate].pt.y
                                / yMaxNormalisation;

                        //Log.e(TAG, "Compensated: (" + String.valueOf(offsetCompensatedKeyPoints[iterate].pt.x) + "," + String.valueOf(offsetCompensatedKeyPoints[iterate].pt.y) + ")");
                    }

                    double xCentroidNormalised, yCentroidNormalised;

                    xCentroidNormalised = (xTemplateCentroid - x0display) / xMaxNormalisation;
                    yCentroidNormalised = (yTemplateCentroid - y0display) / yMaxNormalisation;

                    Log.e(TAG, "Normalised Centroid: (" + String.valueOf(xCentroidNormalised) + ","
                            + String.valueOf(yCentroidNormalised));

                    long timeEnd = (int) System.currentTimeMillis();
                    Log.e(TAG, "Time consumed is " + String.valueOf(timeEnd - timeBegin) + " milli-seconds!");

                    Log.e(TAG, "RMS distance is: " + distanceTemplateFeatures);

                    globalVariable.setDistanceTemplateFeatures(distanceTemplateFeatures);
                    globalVariable.setX0display(x0display);
                    globalVariable.setY0display(y0display);
                    globalVariable.setX1display(x1display);
                    globalVariable.setY1display(y1display);
                    globalVariable.setKeypoints(keypoints);
                    globalVariable.setXtemplateCentroid(xTemplateCentroid);
                    globalVariable.setYtemplateCentroid(yTemplateCentroid);
                    globalVariable.setTemplateDescriptor(templateDescriptor);
                    globalVariable.setNumberOfTemplateFeatures(iterationMax);
                    globalVariable.setNumberOfPositiveTemplateFeatures(numberOfPositiveResponses);
                    globalVariable.setRhigh(rHigh);
                    globalVariable.setRlow(rLow);
                    globalVariable.setGhigh(gHigh);
                    globalVariable.setGlow(gLow);
                    globalVariable.setBhigh(bHigh);
                    globalVariable.setBlow(bLow);
                    globalVariable.setXnormalisedCentroid(xCentroidNormalised);
                    globalVariable.setYnormalisedCentroid(yCentroidNormalised);
                    globalVariable.setNormalisedTemplateKeyPoints(offsetCompensatedKeyPoints);

                    Log.e(TAG, "Finished setting the global variables!");

                } catch (Exception e) {
                    Log.e(TAG, "Please follow instructions!");
                }
            }
        });
    } catch (Exception e) {
        Log.e(TAG, "Template is not working!");
    }

}

From source file:eu.fpetersen.robobrain.behavior.followobject.OrbObjectDetector.java

License:Open Source License

public void process(Mat image) {
    Mat tempImage = new Mat();
    Imgproc.cvtColor(image, tempImage, Imgproc.COLOR_RGBA2RGB);
    MatOfKeyPoint keypoints = detectInImage(tempImage);
    Mat descriptors = extractDescriptors(keypoints, tempImage);
    MatOfDMatch matches = new MatOfDMatch();
    matcher.match(descriptors, originalDescriptors, matches);

    KeyPoint[] keypointArray = keypoints.toArray();
    KeyPoint[] originalKeypointArray = originalKeypoints.toArray();

    float min = 40.0f;
    float max = 1000.0f;
    for (DMatch match : matches.toList()) {
        if (match.distance < min) {
            min = match.distance;/*from  w  w w  .j a v a 2s. c om*/
        } else if (match.distance > max) {
            max = match.distance;
        }
    }

    float threshold = 1.5f * min;
    List<KeyPoint> matchedKeyPoints = new ArrayList<KeyPoint>();
    List<Point> matchedPoints = new ArrayList<Point>();
    List<Point> matchedOriginalPoints = new ArrayList<Point>();
    for (DMatch match : matches.toList()) {
        if (match.distance < threshold) {
            KeyPoint matchedKeypoint = keypointArray[match.queryIdx];
            matchedKeyPoints.add(matchedKeypoint);
            matchedPoints.add(matchedKeypoint.pt);

            KeyPoint matchedOriginalKeypoint = originalKeypointArray[match.trainIdx];
            matchedOriginalPoints.add(matchedOriginalKeypoint.pt);
        }
    }

    if (matchedKeyPoints.size() > 10) {

        Mat H = Calib3d.findHomography(
                new MatOfPoint2f(matchedOriginalPoints.toArray(new Point[matchedOriginalPoints.size()])),
                new MatOfPoint2f(matchedPoints.toArray(new Point[matchedPoints.size()])), Calib3d.RANSAC, 10);

        List<Point> originalCorners = new ArrayList<Point>();
        originalCorners.add(new Point(0, 0));
        originalCorners.add(new Point(originalImage.cols(), 0));
        originalCorners.add(new Point(originalImage.cols(), originalImage.rows()));
        originalCorners.add(new Point(0, originalImage.rows()));

        List<Point> corners = new ArrayList<Point>();
        for (int i = 0; i < 4; i++) {
            corners.add(new Point(0, 0));
        }
        Mat objectCorners = Converters.vector_Point2f_to_Mat(corners);

        Core.perspectiveTransform(Converters.vector_Point2f_to_Mat(originalCorners), objectCorners, H);
        corners.clear();
        Converters.Mat_to_vector_Point2f(objectCorners, corners);

        Core.line(tempImage, corners.get(0), corners.get(1), new Scalar(0, 255, 0), 4);
        Core.line(tempImage, corners.get(1), corners.get(2), new Scalar(0, 255, 0), 4);
        Core.line(tempImage, corners.get(2), corners.get(3), new Scalar(0, 255, 0), 4);
        Core.line(tempImage, corners.get(3), corners.get(0), new Scalar(0, 255, 0), 4);
    }

    Features2d.drawKeypoints(tempImage,
            new MatOfKeyPoint(matchedKeyPoints.toArray(new KeyPoint[matchedKeyPoints.size()])), tempImage);
    Imgproc.cvtColor(tempImage, image, Imgproc.COLOR_RGB2RGBA);

}

From source file:overwatchteampicker.OverwatchTeamPicker.java

public static ReturnValues findImage(String template, String source, int flag) {
    File lib = null;/*from w w w .  jav  a2s  .  c  o  m*/
    BufferedImage image = null;
    try {
        image = ImageIO.read(new File(source));
    } catch (Exception e) {
        e.printStackTrace();
    }

    String os = System.getProperty("os.name");
    String bitness = System.getProperty("sun.arch.data.model");

    if (os.toUpperCase().contains("WINDOWS")) {
        if (bitness.endsWith("64")) {
            lib = new File("C:\\Users\\POWERUSER\\Downloads\\opencv\\build\\java\\x64\\"
                    + System.mapLibraryName("opencv_java2413"));
        } else {
            lib = new File("libs//x86//" + System.mapLibraryName("opencv_java2413"));
        }
    }
    System.load(lib.getAbsolutePath());
    String tempObject = "images\\hero_templates\\" + template + ".png";
    String source_pic = source;
    Mat objectImage = Highgui.imread(tempObject, Highgui.CV_LOAD_IMAGE_GRAYSCALE);
    Mat sceneImage = Highgui.imread(source_pic, Highgui.CV_LOAD_IMAGE_GRAYSCALE);

    MatOfKeyPoint objectKeyPoints = new MatOfKeyPoint();
    FeatureDetector featureDetector = FeatureDetector.create(FeatureDetector.SURF);
    featureDetector.detect(objectImage, objectKeyPoints);
    KeyPoint[] keypoints = objectKeyPoints.toArray();
    MatOfKeyPoint objectDescriptors = new MatOfKeyPoint();
    DescriptorExtractor descriptorExtractor = DescriptorExtractor.create(DescriptorExtractor.SURF);
    descriptorExtractor.compute(objectImage, objectKeyPoints, objectDescriptors);

    // Create the matrix for output image.
    Mat outputImage = new Mat(objectImage.rows(), objectImage.cols(), Highgui.CV_LOAD_IMAGE_COLOR);
    Scalar newKeypointColor = new Scalar(255, 0, 0);
    Features2d.drawKeypoints(objectImage, objectKeyPoints, outputImage, newKeypointColor, 0);

    // Match object image with the scene image
    MatOfKeyPoint sceneKeyPoints = new MatOfKeyPoint();
    MatOfKeyPoint sceneDescriptors = new MatOfKeyPoint();
    featureDetector.detect(sceneImage, sceneKeyPoints);
    descriptorExtractor.compute(sceneImage, sceneKeyPoints, sceneDescriptors);

    Mat matchoutput = new Mat(sceneImage.rows() * 2, sceneImage.cols() * 2, Highgui.CV_LOAD_IMAGE_COLOR);
    Scalar matchestColor = new Scalar(0, 255, 25);

    List<MatOfDMatch> matches = new LinkedList<MatOfDMatch>();
    DescriptorMatcher descriptorMatcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);
    descriptorMatcher.knnMatch(objectDescriptors, sceneDescriptors, matches, 2);

    LinkedList<DMatch> goodMatchesList = new LinkedList<DMatch>();

    float nndrRatio = .78f;

    for (int i = 0; i < matches.size(); i++) {
        MatOfDMatch matofDMatch = matches.get(i);
        DMatch[] dmatcharray = matofDMatch.toArray();
        DMatch m1 = dmatcharray[0];
        DMatch m2 = dmatcharray[1];

        if (m1.distance <= m2.distance * nndrRatio) {
            goodMatchesList.addLast(m1);

        }
    }

    if (goodMatchesList.size() >= 4) {

        List<KeyPoint> objKeypointlist = objectKeyPoints.toList();
        List<KeyPoint> scnKeypointlist = sceneKeyPoints.toList();

        LinkedList<Point> objectPoints = new LinkedList<>();
        LinkedList<Point> scenePoints = new LinkedList<>();

        for (int i = 0; i < goodMatchesList.size(); i++) {
            objectPoints.addLast(objKeypointlist.get(goodMatchesList.get(i).queryIdx).pt);
            scenePoints.addLast(scnKeypointlist.get(goodMatchesList.get(i).trainIdx).pt);
        }

        MatOfPoint2f objMatOfPoint2f = new MatOfPoint2f();
        objMatOfPoint2f.fromList(objectPoints);
        MatOfPoint2f scnMatOfPoint2f = new MatOfPoint2f();
        scnMatOfPoint2f.fromList(scenePoints);

        Mat homography = Calib3d.findHomography(objMatOfPoint2f, scnMatOfPoint2f, Calib3d.RANSAC, 3);

        Mat obj_corners = new Mat(4, 1, CvType.CV_32FC2);
        Mat scene_corners = new Mat(4, 1, CvType.CV_32FC2);

        obj_corners.put(0, 0, new double[] { 0, 0 });
        obj_corners.put(1, 0, new double[] { objectImage.cols(), 0 });
        obj_corners.put(2, 0, new double[] { objectImage.cols(), objectImage.rows() });
        obj_corners.put(3, 0, new double[] { 0, objectImage.rows() });

        Core.perspectiveTransform(obj_corners, scene_corners, homography);

        Mat img = Highgui.imread(source_pic, Highgui.CV_LOAD_IMAGE_COLOR);

        Core.line(img, new Point(scene_corners.get(0, 0)), new Point(scene_corners.get(1, 0)),
                new Scalar(0, 255, 255), 4);
        Core.line(img, new Point(scene_corners.get(1, 0)), new Point(scene_corners.get(2, 0)),
                new Scalar(255, 255, 0), 4);
        Core.line(img, new Point(scene_corners.get(2, 0)), new Point(scene_corners.get(3, 0)),
                new Scalar(0, 255, 0), 4);
        Core.line(img, new Point(scene_corners.get(3, 0)), new Point(scene_corners.get(0, 0)),
                new Scalar(0, 255, 0), 4);

        MatOfDMatch goodMatches = new MatOfDMatch();
        goodMatches.fromList(goodMatchesList);

        Features2d.drawMatches(objectImage, objectKeyPoints, sceneImage, sceneKeyPoints, goodMatches,
                matchoutput, matchestColor, newKeypointColor, new MatOfByte(), 2);
        if (new Point(scene_corners.get(0, 0)).x < new Point(scene_corners.get(1, 0)).x
                && new Point(scene_corners.get(0, 0)).y < new Point(scene_corners.get(2, 0)).y) {
            System.out.println("found " + template);
            Highgui.imwrite("points.jpg", outputImage);
            Highgui.imwrite("matches.jpg", matchoutput);
            Highgui.imwrite("final.jpg", img);

            if (flag == 0) {
                ReturnValues retVal = null;
                int y = (int) new Point(scene_corners.get(3, 0)).y;
                int yHeight = (int) new Point(scene_corners.get(3, 0)).y
                        - (int) new Point(scene_corners.get(2, 0)).y;
                if (y < image.getHeight() * .6) { //if found hero is in upper half of image then return point 3,0
                    retVal = new ReturnValues(y + (int) (image.getHeight() * .01), yHeight);
                } else { //if found hero is in lower half of image then return point 2,0
                    y = (int) new Point(scene_corners.get(2, 0)).y;
                    retVal = new ReturnValues(y + (int) (image.getHeight() * .3), yHeight);
                }
                return retVal;
            } else if (flag == 1) {
                int[] xPoints = new int[4];
                int[] yPoints = new int[4];

                xPoints[0] = (int) (new Point(scene_corners.get(0, 0)).x);
                xPoints[1] = (int) (new Point(scene_corners.get(1, 0)).x);
                xPoints[2] = (int) (new Point(scene_corners.get(2, 0)).x);
                xPoints[3] = (int) (new Point(scene_corners.get(3, 0)).x);

                yPoints[0] = (int) (new Point(scene_corners.get(0, 0)).y);
                yPoints[1] = (int) (new Point(scene_corners.get(1, 0)).y);
                yPoints[2] = (int) (new Point(scene_corners.get(2, 0)).y);
                yPoints[3] = (int) (new Point(scene_corners.get(3, 0)).y);

                ReturnValues retVal = new ReturnValues(xPoints, yPoints);
                return retVal;

            }
        }
    }
    return null;

}

From source file:samples.GripVision.java

License:Open Source License

private Rect getTargetRect(GripPipeline pipeline, Mat image) {
    Rect targetRect = null;/*w  w w. ja va  2  s  .c o m*/
    MatOfKeyPoint detectedTargets;

    pipeline.process(image);
    detectedTargets = pipeline.findBlobsOutput();
    if (detectedTargets != null) {
        KeyPoint[] targets = detectedTargets.toArray();
        if (targets.length > 1) {
            HalDashboard.getInstance().displayPrintf(15, "%s: %s", pipeline, targets[0]);
            double radius = targets[0].size / 2;
            targetRect = new Rect((int) (targets[0].pt.x - radius), (int) (targets[0].pt.y - radius),
                    (int) targets[0].size, (int) targets[0].size);
        }
        detectedTargets.release();
    }

    return targetRect;
}

From source file:View.Signature.java

public static int sift(String routeVal, String route, String n_img1, String n_img2, String extension) {

    String bookObject = routeVal + n_img2 + extension;
    String bookScene = route + n_img1 + extension;

    //System.out.println("Iniciando SIFT");
    //java.lang.System.out.print("Abriendo imagenes | ");
    Mat objectImage = Highgui.imread(bookObject, Highgui.CV_LOAD_IMAGE_COLOR);
    Mat sceneImage = Highgui.imread(bookScene, Highgui.CV_LOAD_IMAGE_COLOR);

    MatOfKeyPoint objectKeyPoints = new MatOfKeyPoint();
    FeatureDetector featureDetector = FeatureDetector.create(FeatureDetector.SIFT);
    //java.lang.System.out.print("Encontrar keypoints con SIFT | ");  
    featureDetector.detect(objectImage, objectKeyPoints);
    KeyPoint[] keypoints = objectKeyPoints.toArray();

    MatOfKeyPoint objectDescriptors = new MatOfKeyPoint();
    DescriptorExtractor descriptorExtractor = DescriptorExtractor.create(DescriptorExtractor.SIFT);
    //java.lang.System.out.print("Computando descriptores | ");  
    descriptorExtractor.compute(objectImage, objectKeyPoints, objectDescriptors);

    // Create the matrix for output image.   
    Mat outputImage = new Mat(objectImage.rows(), objectImage.cols(), Highgui.CV_LOAD_IMAGE_COLOR);
    Scalar newKeypointColor = new Scalar(255, 0, 0);

    //java.lang.System.out.print("Dibujando keypoints en imagen base | ");  
    Features2d.drawKeypoints(objectImage, objectKeyPoints, outputImage, newKeypointColor, 0);

    // Match object image with the scene image  
    MatOfKeyPoint sceneKeyPoints = new MatOfKeyPoint();
    MatOfKeyPoint sceneDescriptors = new MatOfKeyPoint();
    //java.lang.System.out.print("Detectando keypoints en imagen base | ");
    featureDetector.detect(sceneImage, sceneKeyPoints);
    //java.lang.System.out.print("Computando descriptores en imagen base | ");
    descriptorExtractor.compute(sceneImage, sceneKeyPoints, sceneDescriptors);

    Mat matchoutput = new Mat(sceneImage.rows() * 2, sceneImage.cols() * 2, Highgui.CV_LOAD_IMAGE_COLOR);
    Scalar matchestColor = new Scalar(0, 255, 0);

    List<MatOfDMatch> matches = new LinkedList<MatOfDMatch>();
    DescriptorMatcher descriptorMatcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);
    //java.lang.System.out.print("Encontrando matches entre imagenes | ");  
    descriptorMatcher.knnMatch(objectDescriptors, sceneDescriptors, matches, 2);

    //java.lang.System.out.println("Calculando buenos matches");
    LinkedList<DMatch> goodMatchesList = new LinkedList<DMatch>();

    float nndrRatio = 0.7f;
    java.lang.System.out.println(matches.size());
    for (int i = 0; i < matches.size(); i++) {
        MatOfDMatch matofDMatch = matches.get(i);
        DMatch[] dmatcharray = matofDMatch.toArray();
        DMatch m1 = dmatcharray[0];/*from  w  ww. jav  a  2 s . co  m*/
        DMatch m2 = dmatcharray[1];

        if (m1.distance <= m2.distance * nndrRatio) {
            goodMatchesList.addLast(m1);

        }
    }

    if (goodMatchesList.size() >= 7) {
        //java.lang.System.out.println("Match enontrado!!! Matches: "+goodMatchesList.size());
        //if(goodMatchesList.size()>max){

        //cambio = 1;
        //}    

        List<KeyPoint> objKeypointlist = objectKeyPoints.toList();
        List<KeyPoint> scnKeypointlist = sceneKeyPoints.toList();

        LinkedList<Point> objectPoints = new LinkedList<>();
        LinkedList<Point> scenePoints = new LinkedList<>();

        for (int i = 0; i < goodMatchesList.size(); i++) {
            objectPoints.addLast(objKeypointlist.get(goodMatchesList.get(i).queryIdx).pt);
            scenePoints.addLast(scnKeypointlist.get(goodMatchesList.get(i).trainIdx).pt);
        }

        MatOfPoint2f objMatOfPoint2f = new MatOfPoint2f();
        objMatOfPoint2f.fromList(objectPoints);
        MatOfPoint2f scnMatOfPoint2f = new MatOfPoint2f();
        scnMatOfPoint2f.fromList(scenePoints);

        Mat homography = Calib3d.findHomography(objMatOfPoint2f, scnMatOfPoint2f, Calib3d.RANSAC, 3);

        Mat obj_corners = new Mat(4, 1, CvType.CV_32FC2);
        Mat scene_corners = new Mat(4, 1, CvType.CV_32FC2);

        obj_corners.put(0, 0, new double[] { 0, 0 });
        obj_corners.put(1, 0, new double[] { objectImage.cols(), 0 });
        obj_corners.put(2, 0, new double[] { objectImage.cols(), objectImage.rows() });
        obj_corners.put(3, 0, new double[] { 0, objectImage.rows() });

        //System.out.println("Transforming object corners to scene corners...");  
        Core.perspectiveTransform(obj_corners, scene_corners, homography);

        Mat img = Highgui.imread(bookScene, Highgui.CV_LOAD_IMAGE_COLOR);

        Core.line(img, new Point(scene_corners.get(0, 0)), new Point(scene_corners.get(1, 0)),
                new Scalar(0, 255, 0), 4);
        Core.line(img, new Point(scene_corners.get(1, 0)), new Point(scene_corners.get(2, 0)),
                new Scalar(0, 255, 0), 4);
        Core.line(img, new Point(scene_corners.get(2, 0)), new Point(scene_corners.get(3, 0)),
                new Scalar(0, 255, 0), 4);
        Core.line(img, new Point(scene_corners.get(3, 0)), new Point(scene_corners.get(0, 0)),
                new Scalar(0, 255, 0), 4);

        //java.lang.System.out.println("Dibujando imagen de coincidencias");
        MatOfDMatch goodMatches = new MatOfDMatch();
        goodMatches.fromList(goodMatchesList);

        Features2d.drawMatches(objectImage, objectKeyPoints, sceneImage, sceneKeyPoints, goodMatches,
                matchoutput, matchestColor, newKeypointColor, new MatOfByte(), 2);
        String n_outputImage = route + "results\\" + n_img2 + "_outputImage_sift" + extension;
        String n_matchoutput = route + "results\\" + n_img2 + "_matchoutput_sift" + extension;
        String n_img = route + "results\\" + n_img2 + "_sift" + extension;
        Highgui.imwrite(n_outputImage, outputImage);
        Highgui.imwrite(n_matchoutput, matchoutput);
        //Highgui.imwrite(n_img, img);  
        java.lang.System.out.println(goodMatches.size().height);
        double result = goodMatches.size().height * 100 / matches.size();

        java.lang.System.out.println((int) result);
        //double result =goodMatches.size().height;
        if (result > 100) {
            return 100;
        } else if (result <= 100 && result > 85) {
            return 85;
        } else if (result <= 85 && result > 50) {
            return 50;
        } else if (result <= 50 && result > 25) {
            return 25;
        } else {
            return 0;
        }
    } else {
        //java.lang.System.out.println("Firma no encontrada");  
    }
    return 0;
    //System.out.println("Terminando SIFT");  
}

From source file:View.SignatureLib.java

public static int sift(String routeRNV, String routeAdherent) {

    String bookObject = routeAdherent;
    String bookScene = routeRNV;/*from  w w  w.  jav a 2 s .c om*/

    //System.out.println("Iniciando SIFT");
    //java.lang.System.out.print("Abriendo imagenes | ");
    Mat objectImage = Highgui.imread(bookObject, Highgui.CV_LOAD_IMAGE_COLOR);
    Mat sceneImage = Highgui.imread(bookScene, Highgui.CV_LOAD_IMAGE_COLOR);

    MatOfKeyPoint objectKeyPoints = new MatOfKeyPoint();
    FeatureDetector featureDetector = FeatureDetector.create(FeatureDetector.SIFT);
    //java.lang.System.out.print("Encontrar keypoints con SIFT | ");  
    featureDetector.detect(objectImage, objectKeyPoints);
    KeyPoint[] keypoints = objectKeyPoints.toArray();

    MatOfKeyPoint objectDescriptors = new MatOfKeyPoint();
    DescriptorExtractor descriptorExtractor = DescriptorExtractor.create(DescriptorExtractor.SIFT);
    //java.lang.System.out.print("Computando descriptores | ");  
    descriptorExtractor.compute(objectImage, objectKeyPoints, objectDescriptors);

    // Create the matrix for output image.   
    Mat outputImage = new Mat(objectImage.rows(), objectImage.cols(), Highgui.CV_LOAD_IMAGE_COLOR);
    Scalar newKeypointColor = new Scalar(255, 0, 0);

    //java.lang.System.out.print("Dibujando keypoints en imagen base | ");  
    Features2d.drawKeypoints(objectImage, objectKeyPoints, outputImage, newKeypointColor, 0);

    // Match object image with the scene image  
    MatOfKeyPoint sceneKeyPoints = new MatOfKeyPoint();
    MatOfKeyPoint sceneDescriptors = new MatOfKeyPoint();
    //java.lang.System.out.print("Detectando keypoints en imagen base | ");
    featureDetector.detect(sceneImage, sceneKeyPoints);
    //java.lang.System.out.print("Computando descriptores en imagen base | ");
    descriptorExtractor.compute(sceneImage, sceneKeyPoints, sceneDescriptors);

    Mat matchoutput = new Mat(sceneImage.rows() * 2, sceneImage.cols() * 2, Highgui.CV_LOAD_IMAGE_COLOR);
    Scalar matchestColor = new Scalar(0, 255, 0);

    List<MatOfDMatch> matches = new LinkedList<MatOfDMatch>();
    DescriptorMatcher descriptorMatcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);
    //java.lang.System.out.println(sceneDescriptors);  

    if (sceneDescriptors.empty()) {
        java.lang.System.out.println("Objeto no encontrado");
        return 0;
    }

    descriptorMatcher.knnMatch(objectDescriptors, sceneDescriptors, matches, 2);

    //java.lang.System.out.println("Calculando buenos matches");
    LinkedList<DMatch> goodMatchesList = new LinkedList<DMatch>();

    float nndrRatio = 0.7f;

    for (int i = 0; i < matches.size(); i++) {
        MatOfDMatch matofDMatch = matches.get(i);
        DMatch[] dmatcharray = matofDMatch.toArray();
        DMatch m1 = dmatcharray[0];
        DMatch m2 = dmatcharray[1];

        if (m1.distance <= m2.distance * nndrRatio) {
            goodMatchesList.addLast(m1);

        }
    }

    if (goodMatchesList.size() >= 7) {
        max = goodMatchesList.size();

        List<KeyPoint> objKeypointlist = objectKeyPoints.toList();
        List<KeyPoint> scnKeypointlist = sceneKeyPoints.toList();

        LinkedList<Point> objectPoints = new LinkedList<>();
        LinkedList<Point> scenePoints = new LinkedList<>();

        for (int i = 0; i < goodMatchesList.size(); i++) {
            objectPoints.addLast(objKeypointlist.get(goodMatchesList.get(i).queryIdx).pt);
            scenePoints.addLast(scnKeypointlist.get(goodMatchesList.get(i).trainIdx).pt);
        }

        MatOfPoint2f objMatOfPoint2f = new MatOfPoint2f();
        objMatOfPoint2f.fromList(objectPoints);
        MatOfPoint2f scnMatOfPoint2f = new MatOfPoint2f();
        scnMatOfPoint2f.fromList(scenePoints);

        Mat homography = Calib3d.findHomography(objMatOfPoint2f, scnMatOfPoint2f, Calib3d.RANSAC, 3);

        Mat obj_corners = new Mat(4, 1, CvType.CV_32FC2);
        Mat scene_corners = new Mat(4, 1, CvType.CV_32FC2);

        obj_corners.put(0, 0, new double[] { 0, 0 });
        obj_corners.put(1, 0, new double[] { objectImage.cols(), 0 });
        obj_corners.put(2, 0, new double[] { objectImage.cols(), objectImage.rows() });
        obj_corners.put(3, 0, new double[] { 0, objectImage.rows() });

        //System.out.println("Transforming object corners to scene corners...");  
        Core.perspectiveTransform(obj_corners, scene_corners, homography);

        Mat img = Highgui.imread(bookScene, Highgui.CV_LOAD_IMAGE_COLOR);

        Core.line(img, new Point(scene_corners.get(0, 0)), new Point(scene_corners.get(1, 0)),
                new Scalar(0, 255, 0), 4);
        Core.line(img, new Point(scene_corners.get(1, 0)), new Point(scene_corners.get(2, 0)),
                new Scalar(0, 255, 0), 4);
        Core.line(img, new Point(scene_corners.get(2, 0)), new Point(scene_corners.get(3, 0)),
                new Scalar(0, 255, 0), 4);
        Core.line(img, new Point(scene_corners.get(3, 0)), new Point(scene_corners.get(0, 0)),
                new Scalar(0, 255, 0), 4);

        //java.lang.System.out.println("Dibujando imagen de coincidencias");
        MatOfDMatch goodMatches = new MatOfDMatch();
        goodMatches.fromList(goodMatchesList);

        Features2d.drawMatches(objectImage, objectKeyPoints, sceneImage, sceneKeyPoints, goodMatches,
                matchoutput, matchestColor, newKeypointColor, new MatOfByte(), 2);

        String n_outputImage = "../pre/outputImage_sift.jpg";
        String n_matchoutput = "../pre/matchoutput_sift.jpg";
        String n_img = "../pre/sift.jpg";
        Highgui.imwrite(n_outputImage, outputImage);
        Highgui.imwrite(n_matchoutput, matchoutput);
        Highgui.imwrite(n_img, img);
        java.lang.System.out.println(goodMatches.size().height);
        double result = goodMatches.size().height;//*100/matches.size();
        int score = 0;
        if (result > 26) {
            score = 100;
        } else if (result <= 26 && result > 22) {
            score = 85;
        } else if (result <= 22 && result > 17) {
            score = 50;
        } else if (result <= 17 && result > 11) {
            score = 25;
        } else {
            score = 0;
        }
        java.lang.System.out.println("Score: " + score);
        return score;
    } else {
        java.lang.System.out.println("Objeto no encontrado");
        return 0;
    }
    //System.out.println("Terminando SIFT");  
}

From source file:vinylsleevedetection.Analyze.java

public void Check() {
    count = 1;//from w w  w .j  a  va 2  s. c  o m
    //load openCV library
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    //for loop to compare source images to user image
    for (int j = 1; j < 4; j++) {
        //source image location (record sleeve)
        String Object = "E:\\Users\\Jamie\\Documents\\NetBeansProjects\\VinylSleeveDetection\\Source\\" + j
                + ".jpg";
        //user image location
        String Scene = "E:\\Users\\Jamie\\Documents\\NetBeansProjects\\VinylSleeveDetection\\Output\\camera.jpg";
        //load images
        Mat objectImage = Imgcodecs.imread(Object, Imgcodecs.CV_LOAD_IMAGE_COLOR);
        Mat sceneImage = Imgcodecs.imread(Scene, Imgcodecs.CV_LOAD_IMAGE_COLOR);
        //use BRISK feature detection
        MatOfKeyPoint objectKeyPoints = new MatOfKeyPoint();
        FeatureDetector featureDetector = FeatureDetector.create(FeatureDetector.BRISK);
        //perform feature detection on source image
        featureDetector.detect(objectImage, objectKeyPoints);
        KeyPoint[] keypoints = objectKeyPoints.toArray();
        //use descriptor extractor
        MatOfKeyPoint objectDescriptors = new MatOfKeyPoint();
        DescriptorExtractor descriptorExtractor = DescriptorExtractor.create(DescriptorExtractor.BRISK);
        descriptorExtractor.compute(objectImage, objectKeyPoints, objectDescriptors);

        Mat outputImage = new Mat(objectImage.rows(), objectImage.cols(), Imgcodecs.CV_LOAD_IMAGE_COLOR);
        Scalar newKeypointColor = new Scalar(255, 0, 0);

        Features2d.drawKeypoints(objectImage, objectKeyPoints, outputImage, newKeypointColor, 0);

        MatOfKeyPoint sceneKeyPoints = new MatOfKeyPoint();
        MatOfKeyPoint sceneDescriptors = new MatOfKeyPoint();
        featureDetector.detect(sceneImage, sceneKeyPoints);
        descriptorExtractor.compute(sceneImage, sceneKeyPoints, sceneDescriptors);

        Mat matchoutput = new Mat(sceneImage.rows() * 2, sceneImage.cols() * 2, Imgcodecs.CV_LOAD_IMAGE_COLOR);
        Scalar matchestColor = new Scalar(0, 255, 0);

        List<MatOfDMatch> matches = new LinkedList<>();
        DescriptorMatcher descriptorMatcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE);
        descriptorMatcher.knnMatch(objectDescriptors, sceneDescriptors, matches, 2);

        LinkedList<DMatch> goodMatchesList = new LinkedList<DMatch>();

        float nndrRatio = 0.7f;

        for (int i = 0; i < matches.size(); i++) {
            MatOfDMatch matofDMatch = matches.get(i);
            DMatch[] dmatcharray = matofDMatch.toArray();
            DMatch m1 = dmatcharray[0];
            DMatch m2 = dmatcharray[1];

            if (m1.distance <= m2.distance * nndrRatio) {
                goodMatchesList.addLast(m1);

            }
        }
        //if the number of good mathces is more than 150 a match is found
        if (goodMatchesList.size() > 150) {
            System.out.println("Object Found");

            List<KeyPoint> objKeypointlist = objectKeyPoints.toList();
            List<KeyPoint> scnKeypointlist = sceneKeyPoints.toList();

            LinkedList<Point> objectPoints = new LinkedList<>();
            LinkedList<Point> scenePoints = new LinkedList<>();

            for (int i = 0; i < goodMatchesList.size(); i++) {
                objectPoints.addLast(objKeypointlist.get(goodMatchesList.get(i).queryIdx).pt);
                scenePoints.addLast(scnKeypointlist.get(goodMatchesList.get(i).trainIdx).pt);
            }

            MatOfPoint2f objMatOfPoint2f = new MatOfPoint2f();
            objMatOfPoint2f.fromList(objectPoints);
            MatOfPoint2f scnMatOfPoint2f = new MatOfPoint2f();
            scnMatOfPoint2f.fromList(scenePoints);

            Mat homography = Calib3d.findHomography(objMatOfPoint2f, scnMatOfPoint2f, Calib3d.RANSAC, 3);

            Mat obj_corners = new Mat(4, 1, CvType.CV_32FC2);
            Mat scene_corners = new Mat(4, 1, CvType.CV_32FC2);

            obj_corners.put(0, 0, new double[] { 0, 0 });
            obj_corners.put(1, 0, new double[] { objectImage.cols(), 0 });
            obj_corners.put(2, 0, new double[] { objectImage.cols(), objectImage.rows() });
            obj_corners.put(3, 0, new double[] { 0, objectImage.rows() });

            Core.perspectiveTransform(obj_corners, scene_corners, homography);

            Mat img = Imgcodecs.imread(Scene, Imgcodecs.CV_LOAD_IMAGE_COLOR);
            //draw a green square around the matched object
            Imgproc.line(img, new Point(scene_corners.get(0, 0)), new Point(scene_corners.get(1, 0)),
                    new Scalar(0, 255, 0), 10);
            Imgproc.line(img, new Point(scene_corners.get(1, 0)), new Point(scene_corners.get(2, 0)),
                    new Scalar(0, 255, 0), 10);
            Imgproc.line(img, new Point(scene_corners.get(2, 0)), new Point(scene_corners.get(3, 0)),
                    new Scalar(0, 255, 0), 10);
            Imgproc.line(img, new Point(scene_corners.get(3, 0)), new Point(scene_corners.get(0, 0)),
                    new Scalar(0, 255, 0), 10);

            MatOfDMatch goodMatches = new MatOfDMatch();
            goodMatches.fromList(goodMatchesList);

            Features2d.drawMatches(objectImage, objectKeyPoints, sceneImage, sceneKeyPoints, goodMatches,
                    matchoutput, matchestColor, newKeypointColor, new MatOfByte(), 2);
            //output image with match, image of the match locations and keypoints image
            String folder = "E:\\Users\\Jamie\\Documents\\NetBeansProjects\\VinylSleeveDetection\\Output\\";
            Imgcodecs.imwrite(folder + "outputImage.jpg", outputImage);
            Imgcodecs.imwrite(folder + "matchoutput.jpg", matchoutput);
            Imgcodecs.imwrite(folder + "found.jpg", img);
            count = j;
            break;
        } else {
            System.out.println("Object Not Found");
            count = 0;
        }

    }

}