保存OpenCV上Hough Circle检测到的圆圈内部的内容

时间:2015-07-21 21:58:35

标签: c++ opencv hough-transform

我使用OpenCV的Hough Circle Transform来检测命令行参数传递的一些图像中的圆圈。 现在我需要将信息保存在由特定颜色定义的确定圆(形状和所有颜色)中。 我的实际代码如下:

// 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);
}

我该怎么做? 感谢。

1 个答案:

答案 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个数。