【问题标题】:OpenCV : the difference between mask and ROIOpenCV:掩码和ROI的区别
【发布时间】:2012-01-31 16:55:32
【问题描述】:

我尝试在图像中设置带角度的 ROI。起初,我认为使用蒙版会得到与在 IplImage 结构中设置 ROI 相同的结果。然后,我会像使用 ROI 时一样使用 cvResize,但这次是使用蒙版。

但是,这显然不是那么容易,因为角度的关系。

有没有什么方法可以将任何矩形的内部以任何角度复制到一个新的 IplImage 中,该 IplImage 的大小与这个矩形一样大?

CvSeq* approximatedContour = cvApproxPoly(currentContour,
                                          sizeof(CvContour),
                                          0,
                                          CV_POLY_APPROX_DP,
                                          8);

// Circonscrire le polygone trouve dans un rectangle
etiquetteBox = cvMinAreaRect2(approximatedContour);

CvPoint2D32f boxPoints[4];
CvPoint2D32f* c1 = (&cvPoint2D32f(0,0),
                    &cvPoint2D32f(200,0),
                    &cvPoint2D32f(0,200),
                    &cvPoint2D32f(200,200));

CvMat* mmat = cvCreateMat(3,3,CV_32FC1);

cvBoxPoints(etiquetteBox, boxPoints);

IplImage* mask = cvCreateImage(cvSize(in->width,in->height), IPL_DEPTH_8U, 1);
IplImage* ROIimg = cvCreateImage(cvSize(in->width,in->height), IPL_DEPTH_8U, 1);
drawBox(mask,etiquetteBox,target_color[3]);

cvAnd(thresImg,mask,ROIimg,mask);
if(voirSeuillage)
    cvCvtColor(ROIimg,in,CV_GRAY2BGR); //ROIimg is OK here!

mmat = cvGetPerspectiveTransform(boxPoints,c1,mmat);
cvWarpPerspective(ROIimg,thresImgResized,mmat); // here I get a full black image!

这样做,正如 Banthar 所建议的那样,我得到一个全黑的图像,而不是 ROIimg 中由 boxPoints 分隔的图像,这段代码有什么问题?

应用答案后:

这是我现在要做的:

double angle = 0.;
// TODO adaptive angle compensation
if(abs(etiquetteBox.angle) > 30)
    angle = etiquetteBox.angle + 270.;
else
    angle = etiquetteBox.angle - 270.;

CvPoint2D32f boxPoints[4];
CvPoint2D32f c1[] = {cvPoint2D32f(0,0),
                     cvPoint2D32f(20,0),
                     cvPoint2D32f(20,20),
                     cvPoint2D32f(0,20)};

CvMat* mmat = cvCreateMat(3,3,CV_32FC1);
cvBoxPoints(etiquetteBox, boxPoints);
Point center = Point(10,10);

//warp the image to fit the polygon into the 20x20 image
mmat = cvGetPerspectiveTransform(boxPoints,c1,mmat);
cvWarpPerspective(thresImg,thresImgResized,mmat);
//rotate the image because the inconsistent angle of etiquetteBox
// from a frame to the next ...
//it would be very cool to find a way to fix this...
CvMat rot_mat = getRotationMatrix2D( center, angle,1.0);
cvWarpAffine(thresImgResized,rotatedIm,&rot_mat);
It is still not quite what I want, because the object is rotating into the 20x20 rotatedImg; in thresImgResized, after cvWarpPerspective, the object is well segmented, BUT it is reversed because the inconsistency of the angle of etiquetteBox (-0 degrees in a frame, -90 in the next, depending on how I hold the object to be detected), which I get this way:

cvFindContours(dilImage,
               contoursStorage,
               &contours,
               sizeof(CvContour),
               CV_RETR_LIST,
               CV_CHAIN_APPROX_TC89_KCOS);

// Trouver des polygones
CvSeq* currentContour = contours;
while(currentContour != 0 && !etiquette)
{
    CvSeq* approximatedContour = cvApproxPoly(currentContour,
                                              sizeof(CvContour),
                                              0,
                                              CV_POLY_APPROX_DP,
                                              9);

    // Circonscrire le polygone trouve dans un rectangle
    etiquetteBox = cvMinAreaRect2(approximatedContour);

我不知道如何解决这个问题,但至少比设置我的 IplImage ROI 更好,因为我在连续帧中将 etiquetteBox 的角度切换从 -0 度补偿到 -90 度。

【问题讨论】:

  • 您可以使用cvGetPerspectiveTransformcvWarpPerspective
  • 在 CvMat* cvGetPerspectiveTransform(const CvPoint2D32f* src, const CvPoint2D32f* dst, CvMat* mapMatrix) 中,src 将是我的 boxPoints 中的 4 个点,而 dst 是点 [0,0]、[0 ,destImg->width], [destImg->height,0], [destImg->height,destImg->width] ?
  • 是的。我不确定顶点的顺序,但你会看看是否需要交换它们。
  • 我google了一下,顶点的顺序好像不错;但是它的行为很奇怪,我得到了一张全白的图像...请参阅上面的编辑
  • 其实我得到一个全黑的图像...

标签: opencv roi


【解决方案1】:

您在c1 的定义中使用了错误的括号。试试这个:

    CvPoint2D32f c1[] = {
                        cvPoint2D32f(0,200),
                        cvPoint2D32f(0,0),
                        cvPoint2D32f(200,0),
                        cvPoint2D32f(200,200),
    };

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-12
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    • 1970-01-01
    • 2015-08-12
    • 2019-10-12
    • 1970-01-01
    相关资源
    最近更新 更多