使用javacv创建图像

时间:2013-12-19 18:40:37

标签: java opencv image-processing 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循环中我得到一个图像列表我想将这些图像添加到一个新的空图像

1 个答案:

答案 0 :(得分:0)

根据我的理解,您可能希望使用

创建图像
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.