c++ - Save what's inside of a circle detected by a Hough Circle on OpenCV -
i'm using hough circle transform opencv detect circles images passed command line parameter. need save information inside determined circle (shape , colors), defined specific color. actual code following:
// read image while(*argv){ images_read[i] = cv::imread(*argv, 1); if(!images_read[i].data){ std::cout << "image " << *argv << " couldn't read!\n"; exit(-1); } ++argv; ++i; } array_size = i; /// convert gray for(i = 0; < array_size; ++i){ cv::cvtcolor(images_read[i], images_gray[i], cv_bgr2gray); /// reduce noise 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 hough transform find circles cv::houghcircles(images_gray[i], circles[i], cv_hough_gradient, 1, images_gray[i].rows / 8, 200, 100, 0, 0); }
how can that? thanks.
i don't have complete grasp of code. however, can save image of bounding rectangle circle following. first find center of given circle.
// assuming variable circle output houghcircles cv::point center(cvround(circle[0]),cvround(circles[1]));
then find radius.
int radius = cvround(circle[2]);
given center , radius can create new image circles bounding rectangle
// assuming src 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 ) ));
and save following
cv::imwrite("/path/to/file", boundingrectangle);
so putting end following
#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 image for(size_t = 1; < argc; ++i) { cv::mat tmp = cv::imread(argv[i], 1); if(!tmp.data){ std::cout << "image " << *argv << " couldn't 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 gray for(size_t = 0; < images_read.size(); ++i){ cv::cvtcolor(images_read[i], images_gray[i], cv_bgr2gray); // reduce noise avoid false circle detection cv::gaussianblur(images_gray[i], images_gray[i], cv::size(9, 9), 2, 2); // apply hough transform find 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 of circles found , 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 image bounding // rectangle using center , 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 files you're reading 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; }
the key parts including finding radius, center, , creating image bounding rectangle of circle.
the image inside bounding rectangle saved file path number of j
after it.
Comments
Post a Comment