Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How can I correct this program?
I want to read 100 binary images, do some process on them, save the results as 40 length arrays, give a label to each picture and finally train them in SVM classification. This is my first experience to use SVM and I don't know how can I correct the problem?

C++
#include <cxcore.h>
#include <cv.h>
#include <iostream>
#include <highgui.h>
#include <ml.h>
#include <opencv2/opencv.hpp>
#include <vector>
#include <utility>
#include <math.h>
#include <bitset>
#include <cstdlib>
#include <string>
#include <sstream>


using namespace cv;
using namespace std;

int gnum[50][100][41], new_gnum[50][100][41] , child1[50][100][41], child2[50][100][41];
int g0=0, g1=0, g2=0;
int correct=0, total=0;
int number=0; float labels[100];
Mat img;
int response;



int convert(int input_int)
{
    int remainder, digits = 0, dividend = input_int;
    while(dividend != 0)
    {
        dividend = dividend / 2;
        digits++;
    }
    int array[digits];
    dividend = input_int;
    for(int i = digits - 1; i >= 0; i--)
    {
        remainder = dividend % 2;
        array[i] = remainder;
        dividend = dividend / 2;
    }
/*
    for(int j = 0; j < digits; j++)
    {
        cout << array[j];
    }
    if(input_int == 0)
    cout << 0;
    cout << endl;
    */
}



int Y_centroid(int  y[])
{
    int sum=0; int numb=0;
    for(int i=0;i<100 ; i++)
    {
        int temp=y[i];
        if(temp != 0)
        {

        sum=+y[i];
        numb=i;
        }

    }
    int Y_centroid= sum/numb;
    gnum[g0][g1][g2]=convert(Y_centroid);
    g2++;
}//j1


int X_centroid(int  x[])
{
    int sum=0; int numb=0;
    for(int i=0;i<100 ; i++)
    {
        int temp=x[i];
        if(temp != 0)
        {

        sum=+x[i];
        numb=i;
        }

    }
    int X_centroid= sum/numb;
    gnum[g0][g1][g2]=convert(X_centroid);
    g2++;
}

int shadowON_y(int y[])
{
    int max=0, min=33, Yside_shadow=0;
    for(int i=0; i<100; i++)
    {
        if(y[i]>max)
        max=y[i];
    }
    for(int j=0; j<100; j++)
    {
        if(y[j]<min)
        min=y[j];
    }
    Yside_shadow= max-min;
    gnum[g0][g1][g2]=convert(Yside_shadow);
    g2++;


}

int shadowON_x(int x[])
{
    int max=0, min=33, Xside_shadow=0;
    for(int i=0; i<100; i++)
    {
        if(x[i]>max)
        max=x[i];
    }
    for(int j=0; j<100; j++)
    {
        if(x[j]<min)
        min=x[j];
    }
    Xside_shadow= max-min;
    gnum[g0][g1][g2]=convert(Xside_shadow);
    g2++;
}


int Crossover()
{
    int i=0,j=0;
    for( i=0;  i<100 ; i+2)
    {
        for( j=0; j<20; j++)
        {

         child1[g0][i][j]=gnum[g0][i][j];
         child2[g0][i][j]=gnum[g0][i+1][j];

       }


    for(int j=20;j<41; j++ )
    {
        child1[g0][i][j]=gnum[g0][i][j];
        child2[g0][i][j]=gnum[g0][i+1][j];
    }
        gnum[g0][i][j]=child1[g0][i][j];
        gnum[g0][i+1][j]=child2[g0][i][j];


             }//i loop

}

int Mutation()
{
    srand(time(NULL));
    int Random1=(rand() % 100);
    int Random2=(rand() % 41);

        if(child1[g0][Random1][Random2]==0)
        child1[g0][Random1][Random2]=1;


}






int detection(Mat img)
{


    int i1=0,j1=0;
    int i2=0,j2=0;
    int i3=0,j3=0;
    int i4=0,j4=0;
    int i5=0,j5=0;
    int i6=0,j6=0;
    int i7=0,j7=0;
    int i8=0,j8=0;

    int iq1=0,jq1=0;
    int iq2=0,jq2=0;
    int iq3=0,jq3=0;
    int iq4=0,jq4=0;

 int num1_1=0, num2_1=0;
 int num1_2=0, num2_2=0;
 int num1_3=0, num2_3=0;
 int num1_4=0, num2_4=0;
 int num1_5=0, num2_5=0;
 int num1_6=0, num2_6=0;
 int num1_7=0, num2_7=0;
 int num1_8=0, num2_8=0;
 int num3_1=0, num4_1=0;
 int num3_2=0, num4_2=0;
 int num3_3=0, num4_3=0;
 int num3_4=0, num4_4=0;

 int y1[100],x1[100];
 int y2[100],x2[100];
 int y3[100],x3[100];
 int y4[100],x4[100];
 int y5[100],x5[100];
 int y6[100],x6[100];
 int y7[100],x7[100];
 int y8[100],x8[100];

 int yq1[100],xq1[100];
 int yq2[100],xq2[100];
 int yq3[100],xq3[100];
 int yq4[100],xq4[100];

for(int sh=0; sh<100; sh++){y1[sh]=0;}
for(int sh=0; sh<100; sh++){y2[sh]=0;}
for(int sh=0; sh<100; sh++){y3[sh]=0;}
for(int sh=0; sh<100; sh++){y4[sh]=0;}
for(int sh=0; sh<100; sh++){y5[sh]=0;}
for(int sh=0; sh<100; sh++){y6[sh]=0;}
for(int sh=0; sh<100; sh++){y7[sh]=0;}
for(int sh=0; sh<100; sh++){y8[sh]=0;}

for(int sh=0; sh<100; sh++){x1[sh]=0;}
for(int sh=0; sh<100; sh++){x2[sh]=0;}
for(int sh=0; sh<100; sh++){x3[sh]=0;}
for(int sh=0; sh<100; sh++){x4[sh]=0;}
for(int sh=0; sh<100; sh++){x5[sh]=0;}
for(int sh=0; sh<100; sh++){x6[sh]=0;}
for(int sh=0; sh<100; sh++){x7[sh]=0;}
for(int sh=0; sh<100; sh++){x8[sh]=0;}

for(int sh=0; sh<100; sh++){xq1[sh]=0;}
for(int sh=0; sh<100; sh++){xq2[sh]=0;}
for(int sh=0; sh<100; sh++){xq3[sh]=0;}
for(int sh=0; sh<100; sh++){xq4[sh]=0;}

for(int sh=0; sh<100; sh++){yq1[sh]=0;}
for(int sh=0; sh<100; sh++){yq2[sh]=0;}
for(int sh=0; sh<100; sh++){yq3[sh]=0;}
for(int sh=0; sh<100; sh++){yq4[sh]=0;}



vector<Vec3f> circles;

HoughCircles(img, circles, CV_HOUGH_GRADIENT,1,img.rows/10,200,100,0,0 );
//GaussianBlur( img,  img, Size(9, 9), 2, 2 );
/*
for(size_t i=0; i< circles.size(); i++)
    {
    Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
   int radius = cvRound(circles[i][2]);
   // circle center
   circle( img, center, 30, Scalar(0,255,0), -1, 8, 0 );
   // circle outline
   circle( img, center, radius, Scalar(0,0,255), 3, 8, 0 );
    }

namedWindow("win", WINDOW_AUTOSIZE);
imshow("win", img);
*/

if(circles.size()<20)
    {
        gnum[g0][g1][39]=0;
    }else{gnum[g0][g1][39]=1;}




//region num1
    for(i1=0; i1<15; i1++)
    {
        for(int j1=(i1+1);j1<16;j1++)
        {
        int pix_val=(int)img.at<uchar>(i1,j1);
            if(pix_val<15)
            {
            pix_val=0;
           y1[num1_1]=i1;
           x1[num2_1]=j1;
            num2_1++;
            }
        }num1_1++;
    }



         shadowON_y(y1);
         shadowON_x(x1);

         Y_centroid(y1);
         X_centroid(x1);




//region num2
    for(i2=1; i2<16; i2++)
    {
        for(j2=0; j2<(i2+1)-1; j2++)
        {
            int pix_val=(int)img.at<uchar>(i2,j2);
            if(pix_val<15)
            {
            pix_val=0;
           y2[num1_2]=i2;
           x2[num2_2]=j2;
            num2_2++;

            }num1_2++;
        }
    }

         shadowON_y(y2);
         shadowON_x(x2);

         Y_centroid(y2);
         X_centroid(x2);

//region num3
    for(i3=16; i3<31; i3++)
    {
        for(j3=0; j3<(31-i3); j3++)
        {
            int pix_val=(int)img.at<uchar>(i3,j3);
            if(pix_val<15)
            {
            pix_val=0;
           y3[num1_3]=i3;
           x3[num2_3]=j3;
            num2_3++;
            }num1_3++;
        }
    }

         shadowON_y(y3);
         shadowON_x(x3);

         Y_centroid(y3);
         X_centroid(x3);

//region num4
    for(i4=17; i4<32; i4++)
    {
        for(j4=(32-i4); j4<16 ;j4++)
        {
            int pix_val=(int)img.at<uchar>(i4,j4);
            if(pix_val<15)
            {
            pix_val=0;
           y4[num1_4]=i4;
           x4[num2_4]=j4;
            num2_4++;
            }num1_4++;
        }
    }
         shadowON_y(y4);
         shadowON_x(x4);

         Y_centroid(y4);
         X_centroid(x4);

//region num5
    for(i5=0; i5<15; i5++)
    {
        for(j5=16; j5<(31-i5); j5++)
        {
            int pix_val=(int)img.at<uchar>(i5,j5);
            if(pix_val<15)
            {
            pix_val=0;
           y5[num1_5]=i5;
           x5[num2_5]=j5;
           num2_5++;
            }num1_5++;
        }
    }
         shadowON_y(y5);
         shadowON_x(x5);

         Y_centroid(y5);
         X_centroid(x5);

//region num6
        for(i6=1; i6<16; i6++)
    {
        for(j6=(32-i6);j6<32;j6++)
        {
            int pix_val=(int)img.at<uchar>(i6,j6);
            if(pix_val<15)
            {
            pix_val=0;
           y6[num1_6]=i6;
           x6[num2_6]=j6;
            num2_6++;
            }num1_6++;
        }
    }
         shadowON_y(y6);
         shadowON_x(x6);

         Y_centroid(y6);
         X_centroid(x6);

//region num7
    for(i7=17; i7<32; i7++)
    {
        for(j7=16; j7<i7; j7++)
        {
            int pix_val=(int)img.at<uchar>(i7,j7);
            if(pix_val<15)
            {
            pix_val=0;
           y7[num1_7]=i7;
           x7[num2_7]=j7;
            num2_7++;
            }num1_7++;
        }
    }
         shadowON_y(y7);
         shadowON_x(x7);

         Y_centroid(y7);
         X_centroid(x7);

//region num8
    for(i8=16; i8<31; i8++)
    {
        for(j8=(33-i8); j8<32; j8++)
        {
            int pix_val=(int)img.at<uchar>(i8,j8);
            if(pix_val<15)
            {
            pix_val=0;
           y8[num1_8]=i8;
           x8[num2_8]=j8;
            num2_8++;
            }num1_8++;

        }

    }

         shadowON_y(y8);
         shadowON_x(x8);

         Y_centroid(y8);
         X_centroid(x8);


//quad region1

int number=0;
for(iq1=0; iq1<16; iq1++)
{
    for(jq1=0; jq1<16; jq1++)
    {
        int pix_val=(int)img.at<uchar>(iq1,jq1);
        if(pix_val<15)
        {
        pix_val=0;
        yq1[num3_1]=iq1;
        xq1[num4_1]=jq1;
        num4_1++;
        }num3_1++;
    }
}
        int y_dist1=0; int x_dist1=0; int max1_dist1=0; int max1_dist2=0;
        for(int sh=0; sh<100; sh++)
        {
            y_dist1=yq1[sh]; x_dist1=xq1[sh];
            int dist=cvRound(sqrt(((y_dist1)*(y_dist1))+((x_dist1)*(x_dist1))));
            if(x_dist1>max1_dist1){max1_dist1=y_dist1;}
            if(dist>max1_dist2)
            max1_dist2=dist;

        }
        gnum[g0][g1][g2]=max1_dist2;
        g2++;
        gnum[g0][g1][g2]=max1_dist1;
        g2++;



//quad region2
for(iq2=16; iq2<32; iq2++)
{
    for(jq2=0; jq2<16; jq2++)
    {
        int pix_val=(int)img.at<uchar>(iq2,jq2);
        if(pix_val<15)
        {
        pix_val=0;
        yq2[num3_1]=iq2;
        xq2[num4_1]=jq2;
        num4_2++;
        }
    }num3_2++;
}
        int y_dist2=0; int x_dist2=0; int max2_dist1=0; int max2_dist2=0;
        for(int sh=0; sh<100; sh++)
        {
            y_dist2=yq2[sh]; x_dist2=xq2[sh];
            int dist=sqrt(((y_dist2 - 31)*(y_dist2 - 31))+((x_dist2)*(x_dist2)));
            if(x_dist2>max2_dist2){max2_dist2=x_dist2;}
            if(dist>max2_dist2)
            max2_dist2=dist;

        }
        gnum[g0][g1][g2]=max2_dist2;
        g2++;
        gnum[g0][g1][g2]=max2_dist1;
        g2++;



//quad region3
for(iq3=0; iq3<16; iq3++)
{
    for(jq3=16; jq3<32; jq3++)
    {
        int pix_val=(int)img.at<uchar>(iq3,jq3);
        if(pix_val<15)
        {
        pix_val=0;
        yq3[num3_3]=iq3;
        xq3[num4_3]=jq3;
        num4_3++;
        }
    }num3_3++;
}

        int y_dist3=0; int x_dist3=31; int min3_dist1=0; int max3_dist2=0;
        for(int sh=0; sh<100; sh++)
        {
            y_dist3=yq2[sh]; x_dist3=xq2[sh];
            int dist=sqrt(((y_dist3)*(y_dist3))+((x_dist3 -31)*(x_dist3 - 31)));
            if(x_dist3<min3_dist1){min3_dist1=x_dist3;}
            if(dist>max3_dist2)
            max3_dist2=dist;

        }
        gnum[g0][g1][g2]=max3_dist2;
        g2++;
        gnum[g0][g1][g2]=min3_dist1;
        g2++;


//quad region4
for(iq4=16; iq4<32; iq4++)
{
    for(jq4=16; jq4<32; jq4++)
    {
        int pix_val=(int)img.at<uchar>(iq4,jq4);
        if(pix_val<15)
        {
        pix_val=0;
        yq4[num3_4]=iq4;
        xq4[num4_4]=jq4;
        num4_4++;
        }
    }num3_4++;
}

        int y_dist4=0; int x_dist4=0; int min4_dist1=0; int max4_dist2=0;
        for(int sh=0; sh<100; sh++)
        {
            y_dist4=yq2[sh]; x_dist4=xq2[sh];
            int dist=sqrt(((y_dist4)*(y_dist4))+((x_dist4)*(x_dist4)));
            if(x_dist4>max4_dist2){max4_dist2=x_dist4;}
            if(dist>max4_dist2)
            max4_dist2=dist;

        }
        gnum[g0][g1][g2]=max4_dist2;
        g2++;
        gnum[g0][g1][g2]=min4_dist1;
        g2++;


if(number==0)
{
    labels[g1]=0;

}
if(number==1)
{
    labels[g1]=1;

}
if(number==2)
{
    labels[g1]=2;

}
if(number==3)
{
    labels[g1]=3;

}
if(number==4)
{
    labels[g1]=4;

}
if(number==5)
{
    labels[g1]=5;
}
if(number==6)
{
    labels[g1]=6;
}
if(number==7)
{
    labels[g1]=7;

}
if(number==8)
{
    labels[g1]=8;

}
if(number==9)
{
    labels[g1]=9;

}

//}//second for loop
//}//first for loop

} //detection


//SVM classification (Our fitness function)

int svm()
{
        CvSVM SVM;


    for(g1=0; g1<100; g1++)
    {
    for(number=0; number<10; number++)
    {
        Mat img(32, 32, CV_32FC1);
        img=imread(format("/home/chris/bang/bn%04d%d.bmp",g1,number ), CV_LOAD_IMAGE_GRAYSCALE);
        detection(img);


    Mat labelsMat(100, 1, CV_32FC1, labels);


    CvSVMParams params;
    params.svm_type    = CvSVM::C_SVC;
    params.kernel_type = CvSVM::RBF;
    params.gamma=0.5;


    SVM.train(img, labelsMat, Mat(), Mat(), params);
    }

    srand(time(NULL));
    int random=(rand() % 6000);

    Mat image=imread(format("/home/chris/bang/bn%05d",random), CV_LOAD_IMAGE_GRAYSCALE);
    detection(image);
    response= SVM.predict(image);



return response;

    }
}


int selection()
{
    for(g0=0; g0<50; g0++)
    {
        svm();
        if(response>=0 && response<10)
        {
        Crossover();
        Mutation();
        new_gnum[g0][g1][g2]=gnum[g0][g1][g2];
        correct++;
        }
        total++;
        gnum[g0][g1][g2]=new_gnum[g0][g1][g2];
    }
}


int main()
{
    float percent=0;
    svm();
    percent=(correct*100)/total;
    cout<<"The percentage of GA algorithm recognition for bangala digits is: "<<percent;

    return 0;



}
Posted
Updated 27-Jun-14 19:51pm
v2

1 solution

First of all, why do you post hundreds lines of code in a quick question. Do your really expect people to read your entire program?

The error message obviously comes from the SVM.train call in your svm function. So that would have been all you should have posted.

Now, if you read the error message, it appears to me to be pretty clear. The CvSVM train function expects a matrix of floating point type in its first argument, and not an integer matrix. Your mistake appears to be in the two lines
C++
Mat img(32, 32, CV_32FC1);
img=imread(format("/home/chris/bang/bn%04d%d.bmp",g1,number ),
    CV_LOAD_IMAGE_GRAYSCALE);

The second line does not convert the image to the floating point format, as you might have expected, but discards your pre-allocated matrix and assigns the image to it, which is of course in integer format. Use one of the conversions to do that.
 
Share this answer
 
Comments
hor_313 28-Jun-14 8:17am    
Thanks for reply!
I changed the code by converting my input image to floating point matrix. but the new problem is that in past I read the pixel values of each pixel by this code " int pix_val=(int)img.at<uchar>(x,y);" to find which pixel is black. but now I dont know how can I do that?
nv3 1-Jul-14 2:13am    
Nothing has really changed, except that pixel values now are float instead of int. You can still compare them with another value.

In practice, "dark" pixels in a character recognition application just have "low" values and are rarely exactly zero. The same applies with you convert an image to float format. You should find a reasonable threshold value and compare with it, instead of 0.
Stefan_Lang 30-Jun-14 3:43am    
looks like you posted this as a comment to your solution rather than a reply. You might want to repost that in the correct location.

btw., have a 5 :-)
nv3 1-Jul-14 2:14am    
Oops, thanks Stefan!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900