Ask Your Question
0

aligment plate or RotatedRect and draw

asked 2015-08-31 08:06:33 -0600

msucv gravatar image

updated 2015-08-31 08:13:57 -0600

LBerger gravatar image

Hi, i find car plate with opencv and the plate is in the vector<rotatedrect> this is not alignment (0 degree), how can i rotate "rotatdrect" vector concurrent with original plate . image description

int cmin-100;
int cmax = 1000;
vector<vector<Point> >::iterator itc= contours.begin();
vector<RotatedRect> rects;

while (itc!=contours.end()) {

    RotatedRect mr= minAreaRect(Mat(*itc));

     if (itc->size()  , cmin II  ITC - . SIZE()  . cmax)
        itc= contours.erase(itc);
    else
        ++itc;
        rects.push_back(mr);
    }

NOW    HOW  CAN   I  ONLY     DRAW   "RECTS"  WITH CROP PICTURE  WITH ANGLE 0'.

Many Thanks.

edit retag flag offensive close merge delete

Comments

1

I think you want to know rectangle angle relative to horizontal.

Fill you rect with black

calculate moments. using moments you will have ellipse small and large axis abd angle relative to horizontal.

You can use contours too and after try to approximate contour with a polygon

LBerger gravatar imageLBerger ( 2015-08-31 08:20:45 -0600 )edit

i want to draw and rotate bundle rect with image . also , i want to rotate bundle and attach image to angle 0. Thanks,

msucv gravatar imagemsucv ( 2015-08-31 08:45:21 -0600 )edit

how can i extract a RotatedRect to a submatrix image and then rotate image & matrix to angle 0. thanks

msucv gravatar imagemsucv ( 2015-08-31 09:23:49 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-08-31 10:44:48 -0600

LBerger gravatar image

updated 2015-09-01 03:06:43 -0600

It's not finished but idea is here. it gives me

image description with this program

int main (int argc,char **argv)
{
Mat x = imread("C:/Users/Laurent.PC-LAURENT-VISI/Downloads/14410256402214986.jpg",CV_LOAD_IMAGE_GRAYSCALE);
imshow("original",x);
Mat y,yy;
x(Rect(0,0,290,138)).copyTo(yy);

threshold(yy,y,227,255,THRESH_BINARY);
Mat yc;
 vector<vector<Point> > contours;
findContours(y,contours,hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
imshow("seuil",y);
double lengthMax=0;
double theta;

vector<Moments> mu(contours.size() );
double pi = acos(-1);
int indMax=0;
for (int i = 1; i < contours.size(); i++)
{
    double l=arcLength(contours[i],true);
    mu[i] = moments( contours[i], false );
    if (lengthMax<l)
    {
        if (mu[i].mu20-mu[i].mu02!=0)
            theta=1/2.0*atan(2*mu[i].mu11/(mu[i].mu20-mu[i].mu02));
        else 
            theta =0;
        if (mu[i].mu20>mu[i].mu02)
            theta  += pi/2;
        if (theta<0)
            theta += pi;
        cout << theta << "\n";
        lengthMax=l;
        indMax=i;
    }


}
cout << theta << "\n";
theta=pi/2-theta;
Mat r(2,2,CV_32FC1);
r.at<float>(0, 0) = cos(theta);
r.at<float>(1, 0) = sin(theta);
r.at<float>(0, 1) = -sin(theta);
r.at<float>(1, 1) = cos(theta);
        cout << r << "\n" ;

Mat ra= getRotationMatrix2D(Point(mu[indMax].m10/mu[indMax].m00,mu[indMax].m01/mu[indMax].m00), -theta/pi*180, 1.0);
warpAffine(yy,y,ra,Size(500,500)); 
imshow("rotated",y);

for (int i = 0; i < contours.size(); i++)
{
    for (int j = 0; j<contours[i].size() && mu[i].m00>0;j++)
    {
        Mat m;
        Mat pt(2,1,CV_32FC1);
        pt.at<float>(0,0)=contours[i][j].x-mu[i].m10/mu[i].m00;
        pt.at<float>(1,0)=contours[i][j].y-mu[i].m01/mu[i].m00;
        gemm(r, pt, 1, Mat(), 0, m);
       //m=r*pt;
        contours[i][j].x = m.at<float>(0,0)+mu[i].m10/mu[i].m00;
        contours[i][j].y = m.at<float>(1,0)+mu[i].m01/mu[i].m00;

    }
}
Mat plate;
Rect rPlate=boundingRect(contours[indMax]);
y(rPlate).copyTo(plate);
imshow("rotated crop",plate);
Mat a=Mat::zeros(x.size(),CV_8UC3);
drawContours(a,contours,-1,Scalar(255,0,0));
imshow("Image",a);
cout<<theta<<" "<<lengthMax;
waitKey(0);
}
edit flag offensive delete link more

Comments

Hi, Many Thanks for response. please see this link: http://answers.opencv.org/question/49... If I have a lot of object that are each different angle. I have to rotate the whole picture for each object. That takes a lot of processing.

msucv gravatar imagemsucv ( 2015-08-31 11:04:23 -0600 )edit

So what is your problem? I do not understand. If you want to use your good link i have inserted some lines in my answer.

LBerger gravatar imageLBerger ( 2015-08-31 15:14:29 -0600 )edit
1

LBerger, - very nice so far, but i think, it needs to warpAffine() the roi with the plate into horizontal, not just the contourpoints.

berak gravatar imageberak ( 2015-09-01 02:28:04 -0600 )edit
1

@berak I have already a warpAffine ( after @msucv give a link ) but there is an error in my code ( classical copy and paste).

LBerger gravatar imageLBerger ( 2015-09-01 03:04:25 -0600 )edit

apologies for the noise then, i missed that.

berak gravatar imageberak ( 2015-09-01 03:11:39 -0600 )edit

MANY THANKS. i found the plates in the image . it is into "vector<rotatedrect> rect" . the plate is not horizontal . i have to rotate to angel 0(horizon); if i rotate "rect" the whole of the image will rotate too. i only want to rotate the plate , because i have up to 12 plate per image.

msucv gravatar imagemsucv ( 2015-09-01 04:05:40 -0600 )edit

I think for one plate my answer is good you have a crop plate in image plate or you have all point rotated in contours.

Now if you 've got many plates in one image you can divide your image if you know where are the plates or if you don't know may be you can use contours hierarchy to look for platee position.

LBerger gravatar imageLBerger ( 2015-09-01 05:11:02 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-08-31 08:06:33 -0600

Seen: 1,518 times

Last updated: Sep 01 '15