如何在 OpenCV 中裁剪圆圈(通过 Hough 变换找到)?
How to crop circles (found with Hough Transform) in OpenCV?
我正在使用 page 中的代码。它工作得很好,我得到了一个我正在寻找的圆圈。我所有的图像只有 1 个圆圈,我修改了 HoughCircles()
的参数,使代码 returns 只有 1 个圆圈。
如何裁剪原始图像,使新图像只有圆圈(和圆圈内的区域)并将新图像另存为 JPEG 或 PNG 文件?
根据原代码,圆心为
(cvRound(circles[1][0]), cvRound(circles[1][1]));
半径由
给出
cvRound(circles[1][2]);
您可以根据圆的坐标在图像上定义感兴趣区域 (ROI),然后 imwrite
将只保存裁剪部分:
cv::Rect roi(x, y, w, h); // I let you do the math ;)
cv::Mat cropped = original(roi);
cv::imwrite("cropped.png", cropped);
你应该使用copyTo和蒙版来检索图像在圆圈内的部分,然后你可以根据圆圈的边界框进行裁剪。
您使用 imwrite 保存图像。
这个小例子应该可以帮助您入门。
#include "opencv2/opencv.hpp"
using namespace cv;
int main()
{
// Your initial image
Mat3b img = imread("path_to_image");
// Your Hough circle
Vec3f circ(100,50,30); // Some dummy values for now
// Draw the mask: white circle on black background
Mat1b mask(img.size(), uchar(0));
circle(mask, Point(circ[0], circ[1]), circ[2], Scalar(255), CV_FILLED);
// Compute the bounding box
Rect bbox(circ[0] - circ[2], circ[1] - circ[2], 2 * circ[2], 2 * circ[2]);
// Create a black image
Mat3b res(img.size(), Vec3b(0,0,0));
// Copy only the image under the white circle to black image
img.copyTo(res, mask);
// Crop according to the roi
res = res(bbox);
// Save the image
imwrite("filename.png", res);
// Show your result
imshow("Result", res);
waitKey();
return 0;
}
HoughCircles(src, circles, CV_HOUGH_GRADIENT, 1, parameter_1, parameter_2, parameter_3, parameter_4, parameter_5);
for (size_t i = 0; i < circles.size(); i++)
{
Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
int radius = cvRound(circles[i][2]);
circleroi = src(Rect(center.x - radius, coordinate
center.y - radius,
radius *2,
radius * 2));
}
在圈子之后裁剪圈子=HoughCircles(...)
if len(circles) == 1:
x, y, r = circles[0][0]
print x, y, r
mask = np.zeros((w0,h0),dtype=np.uint8)
cv2.circle(mask,(x,y),r,(255,255,255),-1,8,0)
#cv2.imwrite(argv[2],mask)
out = img*mask
white = 255-mask
cv2.imwrite(argv[2],out+white)
我正在使用 page 中的代码。它工作得很好,我得到了一个我正在寻找的圆圈。我所有的图像只有 1 个圆圈,我修改了 HoughCircles()
的参数,使代码 returns 只有 1 个圆圈。
如何裁剪原始图像,使新图像只有圆圈(和圆圈内的区域)并将新图像另存为 JPEG 或 PNG 文件?
根据原代码,圆心为
(cvRound(circles[1][0]), cvRound(circles[1][1]));
半径由
给出cvRound(circles[1][2]);
您可以根据圆的坐标在图像上定义感兴趣区域 (ROI),然后 imwrite
将只保存裁剪部分:
cv::Rect roi(x, y, w, h); // I let you do the math ;)
cv::Mat cropped = original(roi);
cv::imwrite("cropped.png", cropped);
你应该使用copyTo和蒙版来检索图像在圆圈内的部分,然后你可以根据圆圈的边界框进行裁剪。
您使用 imwrite 保存图像。
这个小例子应该可以帮助您入门。
#include "opencv2/opencv.hpp"
using namespace cv;
int main()
{
// Your initial image
Mat3b img = imread("path_to_image");
// Your Hough circle
Vec3f circ(100,50,30); // Some dummy values for now
// Draw the mask: white circle on black background
Mat1b mask(img.size(), uchar(0));
circle(mask, Point(circ[0], circ[1]), circ[2], Scalar(255), CV_FILLED);
// Compute the bounding box
Rect bbox(circ[0] - circ[2], circ[1] - circ[2], 2 * circ[2], 2 * circ[2]);
// Create a black image
Mat3b res(img.size(), Vec3b(0,0,0));
// Copy only the image under the white circle to black image
img.copyTo(res, mask);
// Crop according to the roi
res = res(bbox);
// Save the image
imwrite("filename.png", res);
// Show your result
imshow("Result", res);
waitKey();
return 0;
}
HoughCircles(src, circles, CV_HOUGH_GRADIENT, 1, parameter_1, parameter_2, parameter_3, parameter_4, parameter_5);
for (size_t i = 0; i < circles.size(); i++)
{
Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
int radius = cvRound(circles[i][2]);
circleroi = src(Rect(center.x - radius, coordinate
center.y - radius,
radius *2,
radius * 2));
}
在圈子之后裁剪圈子=HoughCircles(...)
if len(circles) == 1:
x, y, r = circles[0][0]
print x, y, r
mask = np.zeros((w0,h0),dtype=np.uint8)
cv2.circle(mask,(x,y),r,(255,255,255),-1,8,0)
#cv2.imwrite(argv[2],mask)
out = img*mask
white = 255-mask
cv2.imwrite(argv[2],out+white)