2015-07-21 387 views
1

我使用OpenCV的Hough Circle Transform檢測通過命令行參數傳遞的某些圖像的圓圈。 現在我需要從確定的圓形(形狀和所有顏色)保存信息,由特定顏色定義。 我的實際代碼如下:在OpenCV上保存由Hough Circle檢測到的圓的內容

// Read the image 
while(*argv){ 
    images_read[i] = cv::imread(*argv, 1); 

    if(!images_read[i].data){ 
     std::cout << "Image " << *argv << " couldn't be read!\n"; 
     exit(-1); 
    } 

    ++argv; 
    ++i; 
} 

array_size = i; 

/// Convert it to gray 
for(i = 0; i < array_size; ++i){ 
    cv::cvtColor(images_read[i], images_gray[i], CV_BGR2GRAY); 

    /// Reduce the noise so we avoid false circle detection 
    cv::GaussianBlur(images_gray[i], images_gray[i], cv::Size(9, 9), 2, 2); 
    //cvSmooth(images_gray[i], images_gray[i], CV_GAUSSIAN, 7, 7); 

    /// Apply the Hough Transform to find the circles 
    cv::HoughCircles(images_gray[i], circles[i], CV_HOUGH_GRADIENT, 1, images_gray[i].rows/8, 200, 100, 0, 0); 
} 

我怎麼能這樣做? 謝謝。

回答

2

我沒有完整掌握您的代碼。但是,您可以使用類似以下內容的方式將邊界矩形的圖像保存到圓形。首先找到給定圓的中心。

// Assuming the variable circle is your output from HoughCircles 
cv::Point center(cvRound(circle[0]),cvRound(circles[1])); 

然後找到半徑。

int radius = cvRound(circle[2]); 

鑑於圓心和半徑,您可以創建圈子的新圖像邊框

// Assuming src is your original image 
cv::Mat boundingRectangle(images_read[i], cv::Rect(
       cv::Point(
        center.x - radius, 
        center.y - radius 
       ), 
       cv::Point(
        center.x + radius, 
        center.y + radius 
       ) 
      )); 

然後用下面的

cv::imwrite("/path/to/file", boundingRectangle); 

保存它,以便把他們放在一起你可能會得到類似以下的東西

#include <vector> 
#include <sstream> 
#include <string> 

int main(int argc, char **argv) { 
    std::vector<cv::Mat> images_read; 
    std::vector<std::string> image_names; 

    // Read the image 
    for(size_t i = 1; i < argc; ++i) { 
     cv::Mat tmp = cv::imread(argv[i], 1); 

     if(!tmp.data){ 
      std::cout << "Image " << *argv << " couldn't be read!\n"; 
      exit(-1); 
     } 
     images_read.push_back(tmp); 
     image_names.push_back(argv[i]); 
    } 

    std::vector<cv::Mat> images_gray(images_read.size()); 

    // Convert it to gray 
    for(size_t i = 0; i < images_read.size(); ++i){ 
     cv::cvtColor(images_read[i], images_gray[i], CV_BGR2GRAY); 

     // Reduce the noise so we avoid false circle detection 
     cv::GaussianBlur(images_gray[i], images_gray[i], cv::Size(9, 9), 2, 2); 

     // Apply the Hough Transform to find the circles 
     std::vector<cv::Vec3f> circles; 
     cv::HoughCircles(images_gray[i], circles, CV_HOUGH_GRADIENT,1, 
      images_gray[i].rows/8, 200, 100, 0, 0); 

     // Loop through all of the circles found and write them 
     for(size_t j = 0; j < circles.size(); ++j) { 
      cv::Point center(
         cvRound(circles[j][0]), 
         cvRound(circles[j][1]) 
         ); 
      int radius = cvRound(circles[j][2]); 

      // Create a image from the bounding 
      // rectangle using the center and radius 
      cv::Mat boundingRectangle(images_read[i], cv::Rect(
         cv::Point(
           center.x - radius, 
           center.y - radius 
           ), 
         cv::Point(
           center.x + radius, 
           center.y + radius 
           ) 
           )); 
      std::string tmp = std::string(image_names[i]); 
      // Assuming the files you're reading are jpeg images 
      std::string output = std::string(tmp, 0, tmp.find(".jpg"));  

      std::ostringstream os; 
      os << "-" << j << ".jpg"; 

      output += os.str(); 

      cv::imwrite(output, boundingRectangle); 
     } 
    } 
    return 0; 
} 

關鍵部分包括查找半徑,中心,然後從圓的邊界矩形創建圖像。

邊界矩形內的圖像將被保存爲文件路徑,其後面的數字爲j