我有一个简单的函数,它试图遍历单个 channel cv::Mat 中的所有像素。它不能正常工作。我在 ios sim 上的 xcode 中运行它。
cv::Mat fillEdge(cv::Mat floated) {
float currentColor = 255.0;
cv::Size shape = floated.size();
int h = shape.height;
int w = shape.width;
int count = 1;
for(int y = 0; y!= h; y++) {
for(int x = 0; x!= w; x++) {
cv::Point2i p(y, x);
floated.at<int>(p) = currentColor;
}
}
std::cout << floated.channels() << std::endl;
// prints 1
std::cout << floated << std::endl;
return floated;
}
出于某种原因,它打印出条纹图像。
这是函数返回前 cv::Mat 的输出结果
[255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255,
0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0,
0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0,
0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, ...
最佳答案
你应该使用 setTo
:
cv::Mat fillEdge(cv::Mat floated) {
float currentColor = 255.0;
floated.setTo(cv::Scalar(currentColor));
return floated;
}
关于c++ - 如何更改 cv::Mat 中所有像素的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53329950/