Создание образа с помощью javacv

Я хочу создать пустое изображение, что означает белое фоновое изображение, в котором выбраны некоторые изображения из другого изображения, и я хочу добавить эти изображения в созданный пустой, как я могу применить это с помощью javacv

это моя часть моего кода для выбора изображений из загруженного

IplImage originalImage = cvLoadImage("test/test_11.jpg");

        // We need a grayscale image in order to do the recognition, so we
        // create a new image of the same size as the original one.
        IplImage resultImage = IplImage.create(originalImage.width(),
                originalImage.height(), IPL_DEPTH_8U, 1);

        /* convert RGB image we loaded into an grey image */
        cvCvtColor(originalImage, resultImage , CV_BGR2GRAY);
        cvSaveImage("test/test_12.jpg", cvtColorImage);

        /* smooth(blur) the above image, using gaussian blur method */
        cvSmooth(resultImage , resultImage , CV_GAUSSIAN, 7);

        /*
         * Then we can threshold the blurred image. Instead of normal
         * thresholding i've used adaptive thresholding for better results. In
         * adaptive thresholding it calculates threshold values for each pixel
         * on image by looking at pixels surrounding that pixel.
         */
        cvAdaptiveThreshold(resultImage , resultImage , 255,
                CV_ADAPTIVE_THRESH_GAUSSIAN_C, CV_THRESH_BINARY_INV, 11, 5);


        /*
         * Then comes the most important part. It is finding contours in
         * thresholded image. Code given below will find all the contours in the
         * above image and store them to dynamic data structure CvSeq contours.
         * Function cvFindContours() will do the job for us and it will return
         * the number of contours detected in the image.
         */
        CvMemStorage storage = CvMemStorage.create();
        CvSeq contours = new CvContour(null);
        int noOfContors = cvFindContours(resultImage , storage, contours,
                Loader.sizeof(CvContour.class), CV_RETR_CCOMP,
                CV_CHAIN_APPROX_NONE, new CvPoint(0, 0));

        /*
         * If we use the above image, function will detect x contours in above
         * image. Then we have to iterate through the data structure and filter
         * out objects we need. So i used a little trick here. I ignored
         * contours with bounding box with area greater than 1200 and less than
         * 3000. This will leave 10 contours out of x.
         */
        CvSeq ptr = new CvSeq();

        int count = 1;
        Random rand = new Random();
        CvPoint p1 = new CvPoint(0, 0), p2 = new CvPoint(0, 0);

        for (ptr = contours; ptr != null; ptr = ptr.h_next()) {

            Color randomColor = new Color(rand.nextFloat(), rand.nextFloat(),
                    rand.nextFloat());
            CvScalar color = CV_RGB(randomColor.getRed(),
                    randomColor.getGreen(), randomColor.getBlue());
            CvRect sq = cvBoundingRect(ptr, 0);
            double prop = (double) (sq.width()) / sq.height();

            if (sq.width() > 15)
                if (prop > 0 && prop < 3) {
                    System.out.println(sq.width() + " " + sq.height() + " "
                            + prop);

                    p1.x(sq.x());
                    p2.x(sq.x() + sq.width());
                    p1.y(sq.y());
                    p2.y(sq.y() + sq.height());
                    cvRectangle(resultImage , p1, p2, CV_RGB(255, 0, 0), 2, 8,
                            0);
//                  cvDrawContours(resultImage , ptr, color, CV_RGB(0, 0, 0),
//                          -1, CV_FILLED, 8, cvPoint(0, 0));
                    count++;
                }

        }
        System.out.println("Count =" + (count - 1));
        cvSaveImage("test/test_16.jpg", resultImage );
    } 

с помощью этого кода в цикле for я получаю список изображений, которые я хочу добавить в новое пустое изображение


person oooss    schedule 19.12.2013    source источник


Ответы (1)


Насколько я понял, возможно, вы хотели создать изображение, используя

IplImage sumImage = IplImage.create(originalImage.width(),
            originalImage.height(), IPL_DEPTH_8U, 1);

и установить содержимое на ноль

cvSetZero(sumImage); //Adding to black Image may be correct than white image. 

А затем используйте

    cvAdd(sumImage, resultImage, sumImage) in your for loop.
person Pavan Kumar Perali    schedule 19.12.2013
comment
cvAdd принимает 4 параметра, а не 3 void cvAdd(const CvArr* src1, const CvArr* src2, CvArr* dst, const CvArr* mask=NULL), где src.channel() должен быть = dst.channel() и то же самое для размера так как может быть, что добавленный src и пункт назначения имеют одинаковый размер, и я хочу добавить случайные изображения со случайными размерами - person oooss; 20.12.2013
comment
Для cvAdd последний параметр является необязательным. вам это может не понравиться. а по размеру можно соответственно задать ROI и доп. Например: размер img1 100 X 100 - person Pavan Kumar Perali; 20.12.2013
comment
Например: размер img1 составляет 100 X 100 размер img2 составляет 50 X 50, вы хотите добавить img2 к img1 в середине. CvRect R1(25,25,50,50); cvSetImageROI (img1, R1); cvAdd(img1,img2,img1): должно работать. потому что эффективный размер img1 и img2 одинаков. - person Pavan Kumar Perali; 20.12.2013
comment
хорошо, я понял, что вы имеете в виду, но я хочу добавить img2 за img1, а не на него, где мне нужно добавить последовательность изображений, все они находятся друг за другом, так как я могу применить это - person oooss; 20.12.2013
comment
Я полагаю, вы хотите добавить новое изображение в последовательность, если суммарное изображение уже не заполнено в этом пикселе. Вы можете добиться этого, используя сам sumimage в качестве маски (4-й параметр в cvAdd). Если я вас не правильно понял, приведите пример ввода и результатов. - person Pavan Kumar Perali; 20.12.2013
comment
Я не знаю, какие примеры написать, но я имею в виду, что у меня есть 2 изображения img1 и img2. что я сделал из cvAdd, так это то, что он добавляет img1 на img2, где он просто копирует img2 на img1, где он не добавляется, потому что img1 вообще исчезает. Итак, я хочу добавить img1 и img2 к другому изображению, где img1 находится сразу за img2, а не на нем. - person oooss; 22.12.2013