Skip to content

Instantly share code, notes, and snippets.

@emctoo
Created May 7, 2012 12:51
Show Gist options
  • Save emctoo/2627595 to your computer and use it in GitHub Desktop.
Save emctoo/2627595 to your computer and use it in GitHub Desktop.
[OpenCV]operation
// create random Mat
int rows = 10, cols = 10;
cv::Mat m = cv::Mat::zeros(rows, cols, CV_32F);
cv::RNG(cv::getTickCount().fill(m, cv::RNG::UNIFORM, cv::Scalar(0), cv::Scalar(1));
// or you can use randu and randn. I prefer RNG, because I can set the seed.
randu(m, cv::Scalar(0), cv::Scalr(1));
// change one line
cv::mat row = m.row(0);
row = cv::Mat::ones(1, m.cols, m.type());
// if want to replace one line with a line in t
cv::Mat t = cv::Mat::zeros(rows, cols, CV_32F);
cv::RNG(cv::getTickCount().fill(t, cv::RNG::UNIFORM, cv::Scalar(0), cv::Scalar(1));
t.row(2).copyTo(row);
// cv::Mat normalize to 1
void normalize_mat(cv::Mat &m)
{
assert(m.rows == 1);
float sum = cv::sum(m)[0];
// cv::multiply(m, cv::Mat::ones(cv::Size(m.cols, m.rows), CV_32F), m, 1./sum);
m = m*(1./sum);
for(int i=0; i<m.cols; ++i) {
m.at<float>(0, i) += m.at<float>(0, i-1);
}
m.at<float>(0, m.cols) = 1; // resolve precision problem
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment