[英]How to change value of all pixels in cv::Mat
I have a simple function that attempts to loop through all the pixels in a single channel cv::Mat. 我有一个简单的函数,试图遍历单个通道cv :: Mat中的所有像素。 Its not functioning correctly.
它无法正常运行。 I'm running this in xcode on ios sim.
我在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;
}
For some reason it prints a striped image. 由于某种原因,它会打印条纹图像。
Here is what the output of the cv::Mat looks like before the function returns 这是函数返回之前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, ...
Ok, I changed your for
loops condition check, changed the definition of your Point2i
and also changed the template type of your at
method from int
to float
, to maintain type integrity. 好的,我更改了您的
for
循环条件检查,更改了Point2i
的定义,还将at
方法的模板类型从int
更改为float
,以保持类型完整性。
This should work now: 现在应该可以使用:
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 = cv:Point2i(x,y);
floated.at<float>(p) = currentColor;
//floated.at<float>(y,x) = currentColor; //You could also replace the two lines above with direct indexing (be careful with the order of the axis, as pointed by @Micka in the comments)
}
}
std::cout << floated.channels() << std::endl;
// prints 1
std::cout << floated << std::endl;
return floated;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.