Search code examples
c++opencvvideo-capturematinteger-division

Integer division between an Image taken from a VideoCapture of OpenCV and a scalar


I'm using version OpenCV 3.2.

I open the default camera:

VideoCapture cap;
cap.open(0);

and grab the current frame:

Mat frame;
cap.read(frame);

Now I want to reduce the number of colours shown in the image. Thus, I do an integer division and multiply the image by the same scalar value, let it be 10 for example. This can easily done with the C++ operators' overload.

Mat processedFrame = (frame / 10) * 10

However, frame values are in float format. The division is then not performed as an integer division, and the reduction of colours is not achieved.

I guess it can be fixed by making a cast of frame to integer values before doing the integer division. How could I do this cast taking into account that a camera can grab images in 1 (gray scale) or 3 (BGR) colour channels?


Solution

  • The OpenCV provides the API for multiply and divide as well. You can use that API to get the desired results instead of using the overloaded operators as:

    // Assuming the image to be RGB 
    cv::divide(frame, cv::Scalar(10, 10, 10), frame, 1, CV_8UC3);
    cv::multiply(frame, cv::Scalar(10, 10, 10), frame, 1, CV_8UC3);