OpenCV:掩模和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 度连续帧。
I tried to set a ROI with angle in an image. At first, I thought that using a mask would give me the same result as setting the ROI in my IplImage structure. Then, I would just use cvResize the same way I did when I used ROI, but this time for a mask.
However, this is obviously not this easy, because of the angle.
Is there any way to copy the inside of any rectangle, at any angle, into a new IplImage, which would be of the size of this very rectangle?
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!
Doing this, as kindly suggested by Banthar, I get a full black image instead of what is delimited by boxPoints in ROIimg, what's wrong with this code?
After applying answer:
Here is what I do now :
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);
I don't know how to fix this, but, at least, it is better than setting my IplImage ROI, because I compensate the etiquetteBox's angle switch from -0 to -90 degrees in consecutive frames.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在
c1
的定义中使用了错误的括号。试试这个:You are using the wrong brackets in definition of
c1
. Try this: