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



我使用霍夫圆变换从OpenCV检测一些图像通过命令行参数传递的圆。现在我需要从一个确定的圆圈中保存信息(形状和所有颜色),由一个特定的颜色定义。我的实际代码如下:

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

我该怎么做呢?谢谢。

我没有完全理解您的代码。但是,您可以像下面这样将边框的图像保存到圆圈中。首先找到一个给定圆的圆心。

// 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

相关内容

  • 没有找到相关文章

最新更新