Ask Your Question

Revision history [back]

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" ;
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 a=Mat::zeros(x.size(),CV_8UC3);
drawContours(a,contours,-1,Scalar(255,0,0));
imshow("Image",a);
cout<<theta<<" "<<lengthMax;
waitKey(0);
}

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].m10/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 a=Mat::zeros(x.size(),CV_8UC3);
drawContours(a,contours,-1,Scalar(255,0,0));
imshow("Image",a);
cout<<theta<<" "<<lengthMax;
waitKey(0);
}

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].m10/mu[indMax].m00),
getRotationMatrix2D(Point(mu[indMax].m10/mu[indMax].m00,mu[indMax].m01/mu[indMax].m00), -theta/pi*180, 1.0); 
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);
}