Как изменить значение всех пикселей в cv :: Mat - PullRequest
0 голосов
/ 16 ноября 2018

У меня есть простая функция, которая пытается перебрать все пиксели в одном канале cv :: Mat. Это не работает правильно. Я запускаю это в xcode на ios sim.

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 ]

0 голосов
/ 16 ноября 2018

Вы должны использовать setTo:

cv::Mat fillEdge(cv::Mat floated) {
    float currentColor = 255.0;
    floated.setTo(cv::Scalar(currentColor));
    return floated;
}
0 голосов
/ 16 ноября 2018

Хорошо, я изменил вашу проверку состояния циклов 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;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...