opencv error : sizes of input arguments do not match

asked 2016-02-01 03:15:40 -0600

sumit patel gravatar image

opencv error : sizes of input arguments do not match (The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array') in arithm_op, c:\opencv\modules\core\src\arithm.cpp line 1987

I can get output upto cout << "error" << ; then program threw error. in test_data i use 3 columns. Regarding it, I need to make changes in further part. Please suggest me , where have I need to changes and how ? code:

//test_data=78644 x 3 depth=5 channel= 1

int N=test_data.rows; //number of data points
    int K=5;  //number of labels

    RNG rng((unsigned)time(NULL)); 
    RNG rng_center(20);
    //Mat testDataMat(N, 2, CV_32FC1);
    int clusterCount=K;
    int sampleCount = N;
    Mat centers(5,2,CV_32FC1);
    readCenters(centers,"centers.txt");

    Mat labelsMat(N,1,CV_32SC1);
    Mat labelsMatKMeans;

    cout << " value of defined labelsMatKmeans : \n " << labelsMatKMeans << endl;
    cout<< "  " << endl;

    Mat img_kmeans(512, 512, CV_8UC3);

    Point center;
    center.x=0;//centers.at<float>(k,0);
    center.y=0;//:centers.at<float>(k,1);


    // Set up training data
    /* generate random labeld sample from multigaussian distribution */ 
    for( int k = 0; k < clusterCount; ++k )
    {
        Mat pointChunk = test_data.rowRange(k*sampleCount/clusterCount,
                                            k == clusterCount - 1 ? sampleCount :
                                            (k+1)*sampleCount/clusterCount);

        cout<< " error " << endl;
        rng.fill(pointChunk, CV_RAND_NORMAL, Scalar(center.x, center.y,0,0), Scalar(width*0.10, height*0.10));
        Mat repeat_center;
        repeat(centers.row(k),pointChunk.rows,1,repeat_center);
        pointChunk=pointChunk+repeat_center; 



        Mat labelChunk = labelsMat.rowRange(k*sampleCount/clusterCount,
                                            k == clusterCount - 1 ? sampleCount :
                                            (k+1)*sampleCount/clusterCount);

        labelChunk=k;

    }

    test_data=abs(test_data);
    for(int i=0;i<N;++i)
    {

        test_data.at<float>(i,0)= cvCeil(test_data.at<float>(i,0));
        test_data.at<float>(i,1)= cvCeil(test_data.at<float>(i,1));

    }

     std::vector<Vec3b> colors_region;
    std::vector<Vec3b> colors_data;
    //generate random colors for each label
    for(int i=0;i<K;++i)
    {
        int icolor = (unsigned) rng;
        colors_region.push_back( Vec3b( icolor&255, (icolor>>8)&255, (icolor>>16)&255 ));
        icolor=(unsigned)rng;
        colors_data.push_back( Vec3b( icolor&255, (icolor>>8)&255, (icolor>>16)&255 ));

    }

  //draw k-means output
          kmeans(test_data, clusterCount, labelsMatKMeans,
               TermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0),
               3, KMEANS_PP_CENTERS, centers);

          cout<< "  " << endl;


        img_kmeans = Scalar::all(0);

        for( int i = 0; i < sampleCount; i++ )
        {
            int clusterIdx = labelsMatKMeans.at<int>(i);

            //cout << "C_ID : " << labelsMatKMeans.at<float>(i);
            //cout << "C_ID : " << clusterIdx<< endl;
            Point ipt ;
            ipt.x= (int)test_data.at<float>(i,0);
            ipt.y=(int)test_data.at<float>(i,1);
            circle( img_kmeans, ipt, 5, Scalar(colors_data[clusterIdx]), -1, 8 );
        }

        imshow("K-Means clustering", img_kmeans);
        imwrite("k_means.png",img_kmeans);
}
edit retag flag offensive close merge delete