user3388706
user3388706

Reputation: 11

Segmentation Fault in creation of 3-dim array of type MAT in openCV/image Processing

I have an image of size 576x720 and I am dividing it into small parts, starting from 1.40625x1.125 to 288x360 by using different roi_width and roi_height in every iteration (from k = 9 to k= 1). I want to create an array of type Mat dst_n(3 dimensions, k, x_dim (number of windows along X) and y_dim(Num. of windows along Y) but its giving segmentation fault in creation? How can I allocate it correctly?

    int a,b;

    a = log_base_n(image1.cols,2);
    b = log_base_n(image1.rows,2);
    double roi_width = (image2.cols)/pow(2.0,a);
    cout << roi_width << endl;
    double roi_height = (image2.rows)/pow(2.0,a);
    cout << roi_height << endl;

    double roi_left = roi_width/2.0;
    cout << roi_left << endl;
    double roi_top  = roi_height/2.0;
    cout << roi_top << endl;

    for(int k=9 ;k>0;k--){

        int x_dim = 2*pow(2.0,k)-1;
        int y_dim = 2*pow(2.0,k)-1;
        cout << "working"<< endl;
        **Mat dst_n[k][x_dim][y_dim];**       // This line is giving segmentation fault. I think, its due to some dynamic allocation, because when I assigned Mat dst[3][3], it was working fine.


        **vector<KeyPoint> vec_keypoints_n[k][x_dim][y_dim];
        Mat descriptors_n[k][x_dim][y_dim];
        Mat bowDescriptor_n[k][x_dim][y_dim];
        double base_test_n[k][x_dim][y_dim];**
        for (int j=0;j<y_dim;j++){
            cout << "working"<< endl;
            for (int i=0;i<x_dim;i++){
                cout << "working"<< endl;
                image2(Rect(roi_left*i,roi_top*j,roi_width,roi_height)).copyTo(dst_n[k][j][i]);
                detector.detect(dst_n[k][j][i],vec_keypoints_n[k][j][i]);
                cout << vec_keypoints_n[k][j][i].size() << endl;
                sift_extractor1->compute(dst_n[k][j][i],vec_keypoints_n[k][j][i],descriptors_n[k][j][i]);
                bowDE.compute(dst_n[k][j][i],vec_keypoints_n[k][j][i], bowDescriptor_n[k][j][i]);
                base_test_n[k][j][i] = compareHist(bowDescriptor1,bowDescriptor_n[k][j][i],0);

                                 cout << "base_test_n = " << base_test_n[k][j][i] << endl;
            }
            cout << "working"<< endl;
        }
        cout << "working"<< endl;
        roi_width = roi_width*2;
        roi_height = roi_height*2;
        roi_left = roi_left*2;
        roi_top = roi_top*2;
    }

I am extracting a region from Image and I want to store it in array. Therefore, I wanted to create an array of type Mat, where array elements can store the matrix (part of image).

Earlier, I have successfully done this by using code mentioned below: Here dst is a 2-d Array which stores matrix using the command image2(Rect(roiLeft*i,roiTop*j,roiWidth,roiHeight)).copyTo(dst[j][i]);

Similarly, I was trying to include a variable k for variable window size and created a 3-D array of type MAT but it didn't work. I dont know whats the problem or how can I use your method?

Mat dst[3][3];
vector<KeyPoint> vec_keypoints[3][3];
Mat descriptors[3][3];
for (int j=0;j<3;j++){
    for (int i=0;i<3;i++){
        image2(Rect(roiLeft*i,roiTop*j,roiWidth,roiHeight)).copyTo(dst[j][i]);
        detector.detect(dst[j][i],vec_keypoints[j][i]);
        cout << vec_keypoints[j][i].size() << endl;
        sift_extractor1->compute(dst[j][i],vec_keypoints[j][i],descriptors[j][i]);

    }
}




Mat dst_n[k][x_dim][y_dim];  // you cannot create a Mat like this

To create a 3D matrix in OpenCV, you should use

int sizes[] = { k, x_dim, y_dim};
Mat matrix = Mat(3, sizes, CV_64FC1);

Upvotes: 0

Views: 247

Answers (1)

herohuyongtao
herohuyongtao

Reputation: 50667

Mat dst_n[k][x_dim][y_dim];  // you cannot create a Mat like this

To create a 3D matrix in OpenCV, you should use

int sizes[] = { k, x_dim, y_dim};
Mat matrix = Mat(3, sizes, CV_64FC1);

Upvotes: 0

Related Questions