C++ 如何在OpenCV中裁剪圆(使用Hough变换找到)?

C++ 如何在OpenCV中裁剪圆(使用Hough变换找到)?,c++,image,opencv,crop,geometry,C++,Image,Opencv,Crop,Geometry,我使用的代码来自。它工作得很好,我得到了一个圆圈,我正在寻找。我所有的图像只有一个圆,我修改了HoughCircles的参数,这样代码只返回一个圆 如何裁剪原始图像,使新图像中只有圆圈和区域,并将新图像保存为JPEG或PNG文件 根据原始代码,圆心由 (cvRound(circles[1][0]), cvRound(circles[1][1])); cvRound(circles[1][2]); 半径由下式给出 (cvRound(circles[1][0]), cvRound(circles

我使用的代码来自。它工作得很好,我得到了一个圆圈,我正在寻找。我所有的图像只有一个圆,我修改了HoughCircles的参数,这样代码只返回一个圆

如何裁剪原始图像,使新图像中只有圆圈和区域,并将新图像保存为JPEG或PNG文件

根据原始代码,圆心由

(cvRound(circles[1][0]), cvRound(circles[1][1]));
cvRound(circles[1][2]);
半径由下式给出

(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);

您可以根据圆的坐标在图像上定义感兴趣的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);
您应该使用与遮罩一起检索圆内的图像部分,然后可以根据圆的边界框进行裁剪

您可以使用保存图像

这个小例子应该让你开始学习

#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;
}
您应该使用与遮罩一起检索圆内的图像部分,然后可以根据圆的边界框进行裁剪

您可以使用保存图像

这个小例子应该让你开始学习

#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

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)

一个接一个地修剪圆=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)

第一行会返回一个圆吗?它似乎是一个矩形区域:嗯……图像是矩形的。不能有圆形图像。如果你想的话,你可以把圆圈外的像素涂成黑色,或者更好地使用阿尔法通道使它们透明。这个主意不错,这对你来说很管用。请提供相关建议。是否可以提供至少一些示例代码或参考示例?我是新的C++,需要一些帮助它是一个伟大的学习锻炼!你为什么不先试试,用我的提示和一点尝试和错误,如果你真的被卡住了,你随时可以问一个问题!第一行会返回一个圆吗?它似乎是一个矩形区域:嗯……图像是矩形的。不能有圆形图像。如果你想的话,你可以把圆圈外的像素涂成黑色,或者更好地使用阿尔法通道使它们透明。这个主意不错,这对你来说很管用。请提供相关建议。是否可以提供至少一些示例代码或参考示例?我是新的C++,需要一些帮助它是一个伟大的学习锻炼!你为什么不先试试,用我的提示和一点尝试和错误,如果你真的被卡住了,你随时可以问一个问题!你从哪里得到的w0和h0?w0和h0是img或掩模的宽度/高度,即-w0=img.shape[0],h0=img.shape[1]。你从哪里得到的w0和h0?w0和h0是img或掩模的宽度/高度,即-w0=img.shape[0],h0=img.shape[1]。