OpenCV 旋轉矩形設置爲Mask

利用OpenCV實現圖像中旋轉矩形區域設置爲Mask掩膜。實現函數如下所示:

void setPixelValueByContours(CvRect& bndRect, CvSeq*& contour, IplImage*& dst)
{
    int rectx = bndRect.x;
    int recty = bndRect.y;
    int rectw = rectx + bndRect.width;
    int recth = recty + bndRect.height;
    // std::cout << recty << "\t" << recth << "\t" << rectx << "\t" << rectw << std::endl;
    CvPoint2D32f pt;
    CvScalar sv;
    sv.val[0] = 255;
    sv.val[1] = 255;
    sv.val[2] = 255;
    for (int j = recty; j < recth; ++j)
    {
        for (int i = rectx; i < rectw; ++i)
        {
            pt.y = j;
            pt.x = i;
            double ptestv = cvPointPolygonTest(contour, pt, false);
            if ( ptestv >= 0.0) 
            {// in: >0  out: <0  on: ==0
                cvSet2D(dst, j, i, sv);
            }
        }
    }
}

參考

cvPointPolygonTest

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章