Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > Java программирование > Общие вопросы по Java, Java SE, Kotlin
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 17.05.2016, 17:04   #1
Sumy-ua
Пользователь
 
Регистрация: 12.05.2013
Сообщений: 31
По умолчанию OpenCV склеить изображения

Пытаюсь переделать программу на плюсах(https://ramsrigoutham.com/2012/11/22...ing-in-opencv/) под java но никак не выходит.

Код:
import org.opencv.calib3d.Calib3d;
import org.opencv.core.*;
import org.opencv.features2d.*;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;

import java.util.LinkedList;
import java.util.List;

public class Main {

    static Mat image1;
    static Mat image2;

    static FeatureDetector fd;
    static DescriptorExtractor fe;
    static DescriptorMatcher fm;

    public static void initialise() {
        fd = FeatureDetector.create(FeatureDetector.SURF);
        fe = DescriptorExtractor.create(DescriptorExtractor.SURF);
        fm = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);

        //images
        image1 = Highgui.imread("panorama_image1.jpg");
        image2 = Highgui.imread("panorama_image2.jpg");

        Mat imageGray1 = new Mat();
        Mat imageGray2 = new Mat();
        Imgproc.cvtColor(image1, imageGray1, Imgproc.COLOR_RGB2GRAY);
        Imgproc.cvtColor(image2, imageGray2, Imgproc.COLOR_RGB2GRAY);

        //structures for the keypoints from the 2 images
        MatOfKeyPoint keypoints1 = new MatOfKeyPoint();
        MatOfKeyPoint keypoints2 = new MatOfKeyPoint();

        //structures for the computed descriptors
        Mat descriptors1 = new Mat();
        Mat descriptors2 = new Mat();

        //structure for the matches
        MatOfDMatch matches = new MatOfDMatch();

        //getting the keypoints
        fd.detect(imageGray1, keypoints1);
        fd.detect(imageGray2, keypoints2);

        //getting the descriptors from the keypoints
        fe.compute(image1, keypoints1, descriptors1);
        fe.compute(image2, keypoints2, descriptors2);

        //getting the matches the 2 sets of descriptors
        fm.match(descriptors2, descriptors1, matches);

        //turn the matches to a list
        List<DMatch> matchesList = matches.toList();

        Double maxDist = 0.0; //keep track of max distance from the matches
        Double minDist = 100.0; //keep track of min distance from the matches

        //calculate max & min distances between keypoints
        for (int i = 0; i < descriptors1.rows(); i++) {
            Double dist = (double) matchesList.get(i).distance;
            if (dist < minDist) minDist = dist;
            if (dist > maxDist) maxDist = dist;
        }

        System.out.println("max dist: " + maxDist);
        System.out.println("min dist: " + minDist);

        //structure for the good matches
        LinkedList<DMatch> goodMatches = new LinkedList<>();

        //use only the good matches (i.e. whose distance is less than 3*min_dist)
        for (int i = 0; i < descriptors1.rows(); i++) {
            if (matchesList.get(i).distance < 3 * minDist) {
                goodMatches.addLast(matchesList.get(i));
            }
        }

        //structures to hold points of the good matches (coordinates)
        LinkedList<Point> objList = new LinkedList<Point>(); // image1
        LinkedList<Point> sceneList = new LinkedList<Point>(); //image 2

        List<KeyPoint> keypoints_objectList = keypoints1.toList();
        List<KeyPoint> keypoints_sceneList = keypoints2.toList();

        //putting the points of the good matches into above structures
        for (int i = 0; i < goodMatches.size(); i++) {
            objList.addLast(keypoints_objectList.get(goodMatches.get(i).queryIdx).pt);
            sceneList.addLast(keypoints_sceneList.get(goodMatches.get(i).trainIdx).pt);
        }

        System.out.println("\nNum. of good matches" + goodMatches.size());

        //converting the points into the appropriate data structure
        MatOfPoint2f obj = new MatOfPoint2f();
        obj.fromList(objList);

        MatOfPoint2f scene = new MatOfPoint2f();
        scene.fromList(sceneList);

        //finding the homography matrix
        Mat H = Calib3d.findHomography(obj, scene, Calib3d.RANSAC,3);

        //structure to hold the result of the homography matrix
        Mat result = new Mat();

        //size of the new image - i.e. image 1 + image 2
        Size s = new Size(image1.cols() + image2.cols(), image1.rows());

        //using the homography matrix to warp the two images
        Imgproc.warpPerspective(image1, result, H, s);
        int i = image1.cols();
        Mat half = new Mat(result, new Rect(0, 0, image2.cols(), image2.rows()));
        image2.copyTo(half);

        Highgui.imwrite("imageStitched.jpg", result);
    }


    public static void main(String args[]) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        initialise();
    }
}
Результат работы программы
imageStitched.jpg

Если поменять местами картинки то программа то выкидывает исключение Exception in thread "main" java.lang.ArrayIndexOutOfBoundsExce ption
Sumy-ua вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц

Опции темы Поиск в этой теме
Поиск в этой теме:

Расширенный поиск


Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Склеить два видео Pearl777 Общие вопросы Delphi 9 30.10.2014 23:59
Обработка изображения на С++ используя OpenCV _Лето_ Общие вопросы C/C++ 0 17.02.2014 00:38
Как склеить текст Protector81 Microsoft Office Excel 5 19.12.2011 20:23
Склеить VB6 и базу RecklessPhoenix Помощь студентам 0 26.05.2011 23:59
Как склеить документы? drooon Microsoft Office Word 1 04.01.2009 02:28