2013-12-19 83 views
1

創建圖像我要創建一個空的圖像,這意味着其中一個選擇從另一個圖像的一些圖像的白色背景圖像和我想將這些圖像添加到所創建的空單豪我可以應用使用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); 
    } 

選擇圖像通過此代碼爲循環我得到的圖像列表我的我的代碼部分我想將這些圖像添加到一個新的空圖像

回答

0

從我所指的OD,可能是你想創建一個使用

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. 
+0

爲什麼加入黑色圖像更正確 – oooss

+0

cvAdd需要4參數而不是3 void cvAdd(const CvArr * src1,const CvArr * src2,CvArr * dst,const CvArr * mask = NULL)其中src.channel()必須是= dst.channel()可以是添加的src和目標hav e相同的尺寸,我想添加隨機大小的隨機圖像 – oooss

+0

對於cvAdd,最後一個參數是可選的。你可能不會。而對於大小,您可以相應地設置ROI並添加。 對於前: 尺寸IMG1的是100×100 –