代码之家  ›  专栏  ›  技术社区  ›  Cristian

如何更改cv::mat中所有像素的值

  •  0
  • Cristian  · 技术社区  · 6 年前

    我有一个简单的函数,它试图循环遍历单个通道cv::mat中的所有像素。它不能正常工作。我正在iOS SIM卡上的xcode中运行此程序。

    cv::mat filledge(cv::mat floaded){
    浮点当前颜色=255.0;
    cv::size shape=floaded.size();
    int h=形状高度;
    int w=shape.width;
    
    int计数=1;
    
    对于(int y=0;y!= h;y++){
    对于(int x=0;x!{= W;x++){
    cv::点2i p(y,x);
    浮点型。at<int>(p)=currentcolor;
    }
    }
    std::cout<<fload.channels()<<std::endl;
    //印刷品1
    
    
    std::cout<<fload<<std::endl;
    回报浮动;
    }
    < /代码> 
    
    

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

    在函数返回之前,cv::mat的输出是什么样子的

    [255,0,0,0,255,0,0,0,255,0,0,0,0,255,0,0,0,255,
    0,0,0,255,0,0,0,255,0,0,0,255,0,0,0,0,0,255,0,
    0,0,255,0,0,0,255,0,0,0,255,0,0,0,0,255,0,0,
    0,255,0,0,0,255,0,0,0,255,0,0,0,255,0,0,…
    < /代码> 
    
    
    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,   ...
    
    2 回复  |  直到 6 年前
        1
  •  0
  •   Vinícius Queiroz Usman Kurd    6 年前

    好吧,我换了你的 for 循环条件检查,更改了 Point2i 还更改了模板类型 at 方法从 int float ,以保持类型完整性。

    现在应该可以工作了:

    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;
    }
    
        2
  •  1
  •   Miki    6 年前

    你应该使用 setTo :

    cv::Mat fillEdge(cv::Mat floated) {
        float currentColor = 255.0;
        floated.setTo(cv::Scalar(currentColor));
        return floated;
    }