Reputation: 175
I would like to make a PCD file with X, Y, Z, RGBA and Label fields. Now I have a XYZRGBA, PCD file. It includes 640*480 points. In the other hand I have another file which includes 320*256 numbers that represent labels in a segmented image. I want to up-scale the label array and add it to my current PCD file for making a new PCD files with corresponding x,y,z, rgba and label information.This PCD file will be related to a segmented image. Here is my attempt. Label is the name of file which contains label information, first I converted it to an OpenCV matrix and now I want to up-scale it to 640*480 and then add it to the current xyzrgba, pcd file. After Up-scaling again I converted the resulted OpenCV matrix to a normal matrix with name: "array" for adding to my current PCD data.
cv::Mat LabelsMat=cv::Mat(320,256, CV_32SC1, label);
cv::Mat OriginalLabels=cv::Mat::zeros(320,256, CV_32SC1);
LabelsMat.copyTo(OriginalLabels);
cv::Mat UpScaledLabels=cv::Mat::zeros(640, 480, CV_32FC1);
cv::resize(OriginalLabels, UpScaledLabels, UpScaledLabels.size(), 0, 0,cv::INTER_NEAREST);
std::vector<int> array;
array.assign((int*)UpScaledLabels.datastart, (int*)UpScaledLabels.dataend);
But there is some problem. When I make this new PCD file and want to see only one segment of the image, e.g. 4, a wrong shape is appeared to me which is very different with segment 4 according to my basic image. I am sure the problem is because of this part and above code. Does any one could help me in finding the problem please? I appreciate your valuable help.
Upvotes: 1
Views: 545
Reputation: 20160
Ok, finally had the time...
It is always good to look at the Mat objects you produced, just use cv::imshow or cv::imwrite and scale the data accordingly.
Using this code (basically your own code with fixed array writing):
int label[320][256];
std::ifstream in("../inputData/UPSCALE_data.dat");
for (int i = 0; i < 320; i++)
for (int j = 0;j< 256; j++)
{
in >> label[i][j];
}
in.close();
// create Mat with label input:
cv::Mat LabelsMat=cv::Mat(320,256, CV_32SC1, label);
cv::Mat OriginalLabels = LabelsMat.clone(); // you could instead work on the non-copied data, if you liked to...
// upscale:
cv::Mat UpScaledLabels; // no need to allocate memory here during testing
cv::resize(OriginalLabels, UpScaledLabels, cv::Size(640, 480), 0, 0,cv::INTER_NEAREST);
std::vector<int> marray;
marray.reserve(UpScaledLabels.cols*UpScaledLabels.rows);
for(int j=0; j<UpScaledLabels.rows; ++j)
for(int i=0; i<UpScaledLabels.cols; ++i)
{
marray.push_back(UpScaledLabels.at<int>(j,i));
}
// now here marray has information about the upscaled image.
cv::Mat convertedCorrect;
UpScaledLabels.convertTo(convertedCorrect, CV_8UC1);
cv::imwrite("../outputData/UPSCALED_RESULT_ORIG.png", convertedCorrect*50);
I get this result:
That's because cv::Mat LabelsMat=cv::Mat(320,256, CV_32SC1, label);
produces an image with HEIGHT 320 and WIDTH 256 (I thought I already mentioned that in a comment but can't find it atm...)
So fixing that using this code:
int label[320][256];
std::ifstream in("../inputData/UPSCALE_data.dat");
for (int i = 0; i < 320; i++)
for (int j = 0;j< 256; j++)
{
in >> label[i][j];
}
in.close();
// create Mat with label input: HERE THE DIMENSIONS ARE SWAPPED
cv::Mat LabelsMat=cv::Mat(256,320, CV_32SC1, label);
cv::Mat OriginalLabels = LabelsMat.clone(); // you could instead work on the non-copied data, if you liked to...
// upscale:
cv::Mat UpScaledLabels; // no need to allocate memory here during testing
cv::resize(OriginalLabels, UpScaledLabels, cv::Size(640, 480), 0, 0,cv::INTER_NEAREST);
std::vector<int> marray;
marray.reserve(UpScaledLabels.cols*UpScaledLabels.rows);
for(int j=0; j<UpScaledLabels.rows; ++j)
for(int i=0; i<UpScaledLabels.cols; ++i)
{
marray.push_back(UpScaledLabels.at<int>(j,i));
}
// now here marray has information about the upscaled image.
cv::Mat convertedCorrect;
UpScaledLabels.convertTo(convertedCorrect, CV_8UC1);
cv::imwrite("../outputData/UPSCALED_RESULT_CORRECTED.png", convertedCorrect*50);
you get this Mat which looks much better:
But compared to your other image, that image is somehow rotated?!?
Upvotes: 1