6

我目前正在尝试将圆圈外的所有内容涂黑。我正在使用以下代码行绘制圆圈:

cv::Point center(cvRound(circles[i][0]), cvRound(circles[i][1])); // CVRound converts floating numbers to integer
int radius = cvRound(circles[i][2]);                              // Radius is the third parameter [i][0] = x [i][1]= y [i][2] = radius
circle( image, center, 3, cv::Scalar(0,255,0), -1, 8, 0 );        // Drawing little circle to Image Center , next Line of Code draws the real circle
circle( image, center, radius, cv::Scalar(0,0,255), 3, 8, 0 );    // Circle(img, center, radius, color, thickness=1, lineType=8, shift=0)

如果我有一个半径和我的圆的中心,那么将所有圆形黑色绘制的最佳方法是什么?OpenCV 是否提供了一种简单的机制来执行此操作,或者我应该遍历图像的所有像素并根据位置将它们着色为黑色吗?

4

4 回答 4

7

感谢 Abid 的提示,我最终采用了这种方法。一切正常:

cv::Mat src = someMethodThatReturnsSrcImage(); // src Image 
cv::Mat maskedImage; // stores masked Image
std::vector<cv::Vec3f> circles = someMethodThatReturnsCircles(src);    
cv::Mat mask(srcImageForDimensions.size(),srcImageForDimensions.type());  // create an Mat that has same Dimensons as src
mask.setTo(cv::Scalar(0,0,0));                                            // creates black-Image
    // Add all found circles to mask
for( size_t i = 0; i < circles.size(); i++ )                          // iterate through all detected Circles
       {
         cv::Point center(cvRound(circles[i][0]), cvRound(circles[i][1])); // CVRound converts floating numbers to integer
         int radius = cvRound(circles[i][2]);                              // Radius is the third parameter [i][0] = x [i][1]= y [i][2] = radius
         cv::circle( mask, center, radius, cv::Scalar(255,255,255),-1, 8, 0 );    // Circle(img, center, radius, color, thickness=1, lineType=8, shift=0)
       }

src.copyTo(maskedImage,mask); // creates masked Image and copies it to maskedImage
于 2013-08-27T09:31:29.260 回答
3

你可以让背景变成你想要的颜色

image=cv::Scalar(red_value, green_value, blue_value);

然后画你的圈子

于 2013-08-27T09:07:16.660 回答
1

我认为您问题下的评论是最好的解决方案。我为鱼眼相机的 5M 图像制作了代码的修改版本。此图像还需要使圆圈外的所有点都变黑。

#include <Windows.h>
#include <Vfw.h>
#include <string>
#include <iostream>    
#include "opencv2\core\core.hpp"
#include "opencv2\imgproc\imgproc.hpp"
#include "opencv2\imgcodecs\imgcodecs.hpp"
#include "opencv2\highgui\highgui.hpp"    
using namespace std;
using namespace cv;    
int _tmain(int argc, _TCHAR* argv[])
{
    cv::Mat im_source_non_square = cv::imread("D:/FishLib/sample_02.bmp", CV_LOAD_IMAGE_COLOR);
    cv::namedWindow("Image",CV_WINDOW_FREERATIO);
    cv::imshow("Image", im_source_non_square);
    Mat im_source_square;
    int m_nCenterX=1280;
    int m_nCenterY=960;
    int m_nRadius=916;
    Mat im_mask=im_source_non_square.clone();
    im_mask.setTo(cv::Scalar(0,0,0));
    circle( im_mask, cv::Point(m_nCenterX,m_nCenterY), m_nRadius, cv::Scalar(255,255,255), -3, 8, 0 );
    cv::namedWindow("Mask image",CV_WINDOW_FREERATIO);
    cv::imshow("Mask image", im_mask);
    Mat im_source_circle;
    cv::bitwise_and(im_source_non_square,im_mask,im_source_circle);
    cv::namedWindow("Combined image",CV_WINDOW_FREERATIO);
    cv::imshow("Combined image", im_source_circle);
    cv::waitKey(0);
    return 0;
}
于 2018-04-25T15:04:08.107 回答
0

刚刚尝试了您的代码片段,它可以工作。此外,如果您想更改背景颜色而不是黑色,根据此处copyTo的 opencv 文档,如果需要,在初始化目标垫之前,只需添加以下代码:

cv::Mat maskedImage(srcImageForDimensions.size(), srcImageForDimensions.type()); // stores masked Image 
maskedImage.setTo(cv::Scalar(0,0,255)); // set background color to red
于 2017-07-04T10:30:09.357 回答