如何裁剪圆形(找到与霍夫变换)在OpenCV? [英] How to crop circles (found with Hough Transform) in OpenCV?

查看:565
本文介绍了如何裁剪圆形(找到与霍夫变换)在OpenCV?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用页。它工作得很好,我得到一个我想要的圈子。所有我的图像只有一个圆,我修改了 HoughCircles()的参数,使得代码只返回一个圆。

I'm using the code from the page. It works very well and I get a circle that I am looking for. All my image have only 1 circle and I have modified parameters of HoughCircles() such that the code returns only 1 circle.

如何裁剪原始图片,使新图片只有圆形(和其中的区域),并将新图片另存为JPEG或PNG文件?

How can I crop my original image so that the new image has only the circle (and area within it) and save the new image as a JPEG or PNG file?

根据原始代码,圈子中心由

Based upon original code, the center of the circle is given by

(cvRound(circles[1][0]), cvRound(circles[1][1]));

,半径由

cvRound(circles[1][2]);


推荐答案

您应该使用 copyTo 使用遮罩来检索圆圈内的图像部分,然后您可以

You should use copyTo with a mask to retrieve the part of image inside the circle, and then you can crop according to the bounding box of the circle.

您可以使用 imwrite

这个小例子可以让你开始。

This small example should get you started.

#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;
}

这篇关于如何裁剪圆形(找到与霍夫变换)在OpenCV?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆