I work with TIF images containing signed integer data. After successfully inputting one and processing it, I need to output the image in the same format (input and output of both * .tif files).
For input, I know that OpenCV does not know whether the data is signed or not, so it accepts unsigned. Using this trick solves this problem (by manually switching the cv::Mat type).
However, when I output the image and load it again, I do not get the expected result. The file contains several segments (groups of pixels), and the format is as follows ( I have to use this format ):
- all pixels that do not belong to any segment have a value of
-9999 - all pixels belonging to the same segment have the same positive integer value
- (for example, all pixels of the 1st segment have the value
1 , the second 2 , etc.)
And here is a sample code:
void ImageProcessor::saveSegments(const std::string &filename){ cv::Mat segmentation = cv::Mat(workingImage.size().height, workingImage.size().width, CV_32S, cv::Scalar(-9999)); for (int i=0, szi = segmentsInput.size(); i < szi; ++i){ for (int j=0, szj = segmentsInput[i].size(); j < szj; ++j){ segmentation.at<int>(segmentsInput[i][j].Y, ssegmentsInput[i][j].X) = i+1; } } cv::imwrite(filename, segmentation); }
You can assume that all variables (e.g. workingImage , segmentsInput ) exist as global variables.
Using this code, when I enter an image and look at the values, most of the values ββare set to 0 , and those that are set take the full range of integer values ββ(in my example, I had 20 segments).
c ++ image opencv
penelope
source share