1

I need to pass a video buffer through a bandpass filter for a project I'm working on. The bandpass filter involves both an FFT and an IFFT, as well as a flattening of the frequencies outside of the pass band. In order to accomplish this, I need to have more room to store values than is available in the standard uchar type, so I am temporarily converting my video frames to CV_64F, with the plan to convert them back to CV_8UC3. Here is my question:

What is the behavior of OpenCV when converting cv::Mat data from a large data type to a smaller one? More specifically, if I have values in a cv::Mat object with CV_64F data that are larger than the maximum possible value for a single data point in CV_8U, does OpenCV set the target data to the maximum possible value for that data type, or do we have an overflow or truncating problem?

1 Answer 1

2

Just wrote a simple program to figure out how this works:

#include <iostream>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
int main() {
    cv::Mat img1(cv::Size(2,2), CV_64F);
    img1 = cv::Scalar(5000000);
    cv::Mat img2;
    img1.convertTo(img2, CV_8U);
    for(int i = 0; i < img2.rows; i++) {
        for(int j = 0; j < img2.cols; j++) {
            unsigned char tmp = img2.at<uchar>(i, j);
            cout << (int)tmp << endl;
        }
    }
    return 0;
}

Compiled it with pkg-config --cflags --libs opencv and ran it. The output was 255 on 4 lines.

Conclusion: OpenCV, when converting from larger data types to smaller ones, will set the smaller data type value to the max possible if the source data is a larger value. In other words, the value 500,000, which is larger than the max value of an unsigned char, will be set to 255 when converting to CV_8U.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.