van*_*exu 7 opencv roi mask image-processing
我有一个RotatedRect,我想在旋转区域做一些图像处理(比如提取颜色直方图).我怎样才能获得投资回报率?我的意思是获取区域(像素),以便我可以进行处理.
我找到了这个,但它通过使用getRotationMatrix2D和更改了区域warpAffine,因此它不适用于我的情况(我需要处理原始图像像素).
然后我发现这建议使用面具,这听起来很合理,但任何人都可以教我如何将面具作为下面的绿色RotatedRect.

超越面具,还有其他解决方案吗?
谢谢你的任何提示
这是我的解决方案,使用掩码:
这个想法是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;
}
Run Code Online (Sandbox Code Playgroud)
如何构建面具?
使用以下代码.
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
完成,
vancexu
小智 5
我发现以下帖子非常有用。 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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12857 次 |
| 最近记录: |