i have a kind of problem. i've seen some more similar question but i can't find a solution.
my problem is that i have a CV_32FC3 cv::Mat, where values are stored in range between 0 and 255, let's call it S.
I have to create a matrix, called P, where store BGR values of all pixel.
So P should have as rows number the total element of S pixel, and as cols number the number of channels (3).
this is what i've tried to do:
int n_pixels = S.cols * S.rows;
p = Mat::zeros(n_pixels, 3, CV_32FC1);
for(int i=0; i<n_pixels; i++) {
Scalar pixel = S.at<Scalar>(i); // i've tried also Vec3f, Point3_, etc..
p.at<float>(i,0) = pixel[0];
p.at<float>(i,1) = pixel[1];
p.at<float>(i,2) = pixel[2];
}
i've also tried low level c-api data access like this:
for(int i=0; i<selection.rows; i++) {
p.at<float>(i,0) = S.ptr<float>(i)[0];
p.at<float>(i,1) = S.ptr<float>(i)[1];
p.at<float>(i,2) = S.ptr<float>(i)[2];
}
and also splitting channels (but splitted channels have type CV_8U so i think it is wrong):
vector<Mat> bgr;
cv::split(S, bgr);
for(int i=0; i<n_pixels; i++) {
p.at<float>(i,0) = bgr[0].data[i];
p.at<float>(i,1) = bgr[1].data[i];
p.at<float>(i,2) = bgr[2].data[i];
}
but every time i get really weird result if coutting the pixels, like:
172.2042 0.0000 0.0000
2771.1414 0.0000 0.0000
2939505920.0000 3.3468 0.0000
3079446986752.0000 0.0129 0.0000
192669347217408.0000 0.8367 0.0000
51956177323392237568.0000 16301891256320.0000 0.0509
58314208864123224064.0000 3.3945 0.0029
180449.1406 0.0000 0.0000
0.6531 0.0000 0.0000
0.0100 0.0000 0.0000
2.7373 0.0000 0.0000
10957.3184 0.0000 0.0000
739729604608.0000 3.3778 0.0000
0.0000 0.0000 0.0000
0.0000 0.0000 0.0000
0.0000 0.0000 0.0000
0.0000 0.0000 0.0000
and, in some cases also segfault..
any delucidations? how to resolve it? why my trials are wrong?
EDIT: i think the problems are data types conversion