首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RotatedRect ROI in OpenCV

RotatedRect ROI in OpenCV
EN

Stack Overflow用户
提问于 2012-10-26 03:56:35
回答 4查看 16K关注 0票数 8

我有一个RotatedRect,我想在旋转区域做一些图像处理(比如提取颜色直方图)。我怎么才能拿到投资回报?我的意思是得到区域(像素),以便我可以进行处理。

我找到了this,但是它通过使用getRotationMatrix2DwarpAffine来改变区域,所以它不适合我的情况(我需要处理原始的图像像素)。

然后我发现this建议使用掩码,这听起来很合理,但谁能教我如何获得下面的绿色RotatedRect。

除了面具,还有其他解决办法吗?

谢谢你的暗示

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-10-31 06:29:43

下面是我的解决方案,使用掩码

这个想法是通过给我的Mat mask分配255个来构造一个RotatedRect ROI

如何知道ROI中的哪一点(应该分配给255)?

我使用以下函数isInROI来解决这个问题。

代码语言:javascript
复制
/** decide whether point p is in the ROI.
*** The ROI is a rotated rectange whose 4 corners are stored in roi[] 
**/
bool isInROI(Point p, Point2f roi[])
{
    double pro[4];
    for(int i=0; i<4; ++i)
    {
        pro[i] = computeProduct(p, roi[i], roi[(i+1)%4]);
    }
    if(pro[0]*pro[2]<0 && pro[1]*pro[3]<0)
    {
        return true;
    }
    return false;
}

/** function pro = kx-y+j, take two points a and b,
*** compute the line argument k and j, then return the pro value
*** so that can be used to determine whether the point p is on the left or right
*** of the line ab
**/
double computeProduct(Point p, Point2f a, Point2f b)
{
    double k = (a.y-b.y) / (a.x-b.x);
    double j = a.y - k*a.x;
    return k*p.x - p.y + j;
}

如何构造掩码

使用以下代码。

代码语言:javascript
复制
Mat mask = Mat(image.size(), CV_8U, Scalar(0));
for(int i=0; i<image.rows; ++i)
{
    for(int j=0; j<image.cols; ++j)
    {
        Point p = Point(j,i);   // pay attention to the cordination
        if(isInROI(p,vertices))
        {
            mask.at<uchar>(i,j) = 255;
        }
    }
}

完成了,

万科

票数 7
EN

Stack Overflow用户

发布于 2014-04-25 20:54:40

我发现下面的帖子对做同样的事情非常有用。http://answers.opencv.org/question/497/extract-a-rotatedrect-area/

唯一的注意事项是:(a)这里的“角度”假定是围绕整个图像的中心(而不是包围框)的旋转,(b)在下面的最后一行(我认为) "rect.center“需要转换为旋转的图像(通过应用旋转矩阵)。

代码语言:javascript
复制
    // rect is the RotatedRect 
    RotatedRect rect;
    // matrices we'll use
    Mat M, rotated, cropped;
    // get angle and size from the bounding box
    float angle = rect.angle;
    Size rect_size = rect.size;
    // thanks to http://felix.abecassis.me/2011/10/opencv-rotation-deskewing/
    if (rect.angle < -45.) {
        angle += 90.0;
        swap(rect_size.width, rect_size.height);
    }
    // get the rotation matrix
    M = getRotationMatrix2D(rect.center, angle, 1.0);
    // perform the affine transformation
    warpAffine(src, rotated, M, src.size(), INTER_CUBIC);
    // crop the resulting image
    getRectSubPix(rotated, rect_size, rect.center, cropped);
票数 5
EN

Stack Overflow用户

发布于 2012-11-26 21:08:51

如果你需要一个超高速的解决方案,我建议:

  • 在RotatedRect rr内裁剪一段短文。
  • rotate+translate返回裁剪后的图像,使RotatedRect现在等效于Rect。(在旋转和平移3x3矩阵的乘积上使用warpAffine )
  • 保持旋转回像(roi=Rect(Point(0,0), rr.size()))的roi。

写起来有点费时,因为您需要计算组合仿射变换。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13080515

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档