0

I have a Mat object that is created with a loop:

cluster_centre = cv::Mat(num_clusters,num_dimensions,cv::DataType<double>::type) 
// <double> is essential

for (j = 0; j < num_clusters; j++) {
        for (k = 0; k < num_dimensions; k++) {
          ...
          cluster_centre.at<double>(j,k) = ...
          }
}

// rounding numbers 0...255
cluster_centre.convertTo(cluster_centre, CV_32S);

The output of cout << cluster_centre << endl is OK:

 [79, 99, 148;
 73, 29, 14;
 254, 254, 254;
 171, 70, 3;
 178, 189, 211]

And it seems that reshaping has apparently no effect (cols and rows stay the same):

cluster_centre.reshape(3,1); // storing as 1-D array of 3-channel vectors
cout << cluster_centre.cols //output 3;

And when I try to access my elements and paint BGR color further I get:

cout << Scalar(
    mycolors.at<uchar>(0,0), 
    mycolors.at<uchar>(0,1), 
    mycolors.at<uchar>(0,2))<<endl;

[79, 0, 0, 0] // ??

cout << Scalar(
    mycolors.at<uchar>(0,0), 
    mycolors.at<uchar>(1,0), 
    mycolors.at<uchar>(2,0))<<endl;

[79, 73, 254, 0] //vertical

EDIT: The matrix isContinuous, checked.

Slowpoke
  • 1,069
  • 1
  • 13
  • 37
  • 1
    It's not very clear... But take a look at the second snippet [here](http://stackoverflow.com/a/34734939/5008845). To me it seems that you're trying to do something very similar – Miki Oct 24 '16 at 10:48
  • @Miki The output of `cout << mycolors.at(0)< – Slowpoke Oct 24 '16 at 10:52
  • @Miki It seems that the floats were accessible before rounding `cluster_centre.convertTo(cluster_centre, CV_32S);` – Slowpoke Oct 24 '16 at 10:53
  • @Miki Yes! `cluster_centre.convertTo(cluster_centre, CV_8U);` worked! Thank you! – Slowpoke Oct 24 '16 at 10:57

1 Answers1

1

The function Mat::reshape has no effect on the mat object itself. It returns an cv::Mat object which is reshaped. The correct function call would be:

cluster_centre = cluster_centre.reshape(3,1);

Please note that the returned object data points to the data of the source object, i.e. only the header has changed.

Tobias Senst
  • 2,665
  • 17
  • 38
  • Thank you! Yes, looks like I had a mistake when calling the `reshape`, so now I can switch from the previous workaround to the correct solution with the array of `` color vectors. – Slowpoke Oct 25 '16 at 13:18