发布于 2012-10-31 06:29:43
下面是我的解决方案,使用掩码
这个想法是通过给我的Mat mask分配255个来构造一个RotatedRect ROI。
如何知道ROI中的哪一点(应该分配给255)?
我使用以下函数isInROI来解决这个问题。
/** 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;
}如何构造掩码
使用以下代码。
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;
}
}
}完成了,
万科
发布于 2014-04-25 20:54:40
我发现下面的帖子对做同样的事情非常有用。http://answers.opencv.org/question/497/extract-a-rotatedrect-area/
唯一的注意事项是:(a)这里的“角度”假定是围绕整个图像的中心(而不是包围框)的旋转,(b)在下面的最后一行(我认为) "rect.center“需要转换为旋转的图像(通过应用旋转矩阵)。
// 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);发布于 2012-11-26 21:08:51
如果你需要一个超高速的解决方案,我建议:
rr内裁剪一段短文。roi=Rect(Point(0,0), rr.size()))的roi。写起来有点费时,因为您需要计算组合仿射变换。
https://stackoverflow.com/questions/13080515
复制相似问题