c++ - 如何更改 cv::Mat 中所有像素的值

标签 c++ ios xcode opencv matrix

我有一个简单的函数,它试图遍历单个 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;
}

出于某种原因,它打印出条纹图像。

enter image description here enter image description here

这是函数返回前 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/

相关文章:

ios - 在 Swift 中的 View Controller 之间传递数据

iphone - 在 setimage 上收到内存警告

ios - 升级到 Xcode8 后 React Native RCCTCustomScrollView 损坏

c++ - Cocos2dX deprecated classes 问题

c++ - 尽管构建成功,但 Eclipse Neon 构建错误

ios - swift 2 : AVAssetReader and NSInputStream Audio Graph

ios - 使用自动布局调整 super View 和所有 super View 的 sibling

objective-c - 返回到自身 View Controller

c++ - 以相反顺序打印表达式时得到不同的结果

c++ - 为什么 boost::checked_delete "intentionally complex"?