[Solved] The safest way to add integer into array value without having to call it?


This simplest way is to use cv::add, which overloads the + operator for the Mat class:

// Create a Mat of all 0's
cv::Mat dots = cv::Mat(5, 4, CV_8UC3, cv::Scalar(0,0,0));
std::cout << "dots:\n" << dots << std::endl;

// Add 0 to the B channel, 3 to the G channel, and 5 to R
cv::Mat newdots = dots + cv::Scalar(0, 3, 5);
std::cout << "newdots:\n" << newdots << std::endl;

Result:

dots:
[  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0]
newdots:
[  0,   3,   5,   0,   3,   5,   0,   3,   5,   0,   3,   5;
   0,   3,   5,   0,   3,   5,   0,   3,   5,   0,   3,   5;
   0,   3,   5,   0,   3,   5,   0,   3,   5,   0,   3,   5;
   0,   3,   5,   0,   3,   5,   0,   3,   5,   0,   3,   5;
   0,   3,   5,   0,   3,   5,   0,   3,   5,   0,   3,   5]

Note that dots += Scalar(0,3,5) also works if you just want to modify the original Mat.

0

solved The safest way to add integer into array value without having to call it?