簡體   English   中英

如何在OpenCV / C ++中為Mat對象創建圓形蒙版?

[英]How to create circular mask for Mat object in OpenCV / C++?

我的目標是在Mat對象上創建一個圓形蒙版,例如對於看起來像這樣的Mat

0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

...修改它,使得我在其中獲得1秒的“圓形” ,所以.eg

0 0 0 0 0 
0 0 1 0 0 
0 1 1 1 0
0 0 1 0 0
0 0 0 0 0

我目前正在使用以下代碼:

typedef struct {
    double radius;
    Point center;
} Circle;

...

for (Circle c : circles) {

    // get the circle's bounding rect
    Rect boundingRect(c.center.x-c.radius, c.center.y-c.radius, c.radius*2,c.radius*2);

    // obtain the image ROI:
    Mat circleROI(stainMask_, boundingRect);
    int radius = floor(radius);
    circle(circleROI, c.center, radius, Scalar::all(1), 0);
}

問題是,在我調用circlecircleROI 中最多只有一個字段設置為1 ...根據我的理解,此代碼應該有效,因為circle應該使用有關centerradius的信息修改circleROI ,使得圓圈區域內的所有點都應該設置為1 ...有沒有人對我有什么解釋我做錯了什么? 我是否采取了正確的方法解決問題,但實際問題可能在其他地方(這也是非常可能的,因為我是C ++和OpenCv的新手)?

請注意,我還嘗試將circle調用中的最后一個參數(即圓形輪廓粗細 )修改為1-1 ,沒有任何影響。

這是因為你正在用大墊子中的圓圈坐標填充你的circleROI。 circleROI中的圓坐標應相對於circleROI,在您的情況下:new_center =(c.radius,c.radius),new_radius = c.radius。

這是循環的剪輯代碼:

for (Circle c : circles) {

    // get the circle's bounding rect
    Rect boundingRect(c.center.x-c.radius, c.center.y-c.radius, c.radius*2+1,c.radius*2+1);

    // obtain the image ROI:
    Mat circleROI(stainMask_, boundingRect);

    //draw the circle
    circle(circleROI, Point(c.radius, c.radius), c.radius, Scalar::all(1), -1);

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM