Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I am not familiar with the iterator of openCV provided, but I could offer you a solution to iterate through the column(or row?) of cv::Mat.

template<typename T>
void reverse_row_data(cv::Mat const &input, int row)
{
    auto input_ptr = input.ptr<T>(row) + input.cols - 1;
    for(int col = 0; col != input.cols; ++col){
        std::cout<<*input_ptr<<std::endl;
        --input_ptr;
    }

}

template<typename T>
void reverse_col_data(cv::Mat const &input, int col)
{
    for(int row = input.rows - 1; row != -1; --row){
        auto input_ptr = input.ptr<T>(row) + col;
        std::cout<<*input_ptr<<std::endl;
    }

}

int main()
{    
    cv::Mat m = (cv::Mat_<int>(3,3) << 1, 2, 3, 4, 5, 6, 7, 8, 9);
    std::cout << m << std::endl;

    reverse_row_data<int>(m, 0);
    reverse_row_data<int>(m, 1);
    reverse_row_data<int>(m, 2);

    reverse_col_data<int>(m, 0);
    reverse_col_data<int>(m, 1);
    reverse_col_data<int>(m, 2);

    return 0;
}

If you want it become more versatile

template<typename T, typename UnaryFunc>
void reverse_row_data(cv::Mat const &input, int row, UnaryFunc func)
{
    auto input_ptr = input.ptr<T>(row) + input.cols - 1;    
    for(int col = 0; col != input.cols; ++col){
        func(*input_ptr);
        --input_ptr;
    }
}

template<typename T, typename UnaryFunc>
void reverse_col_data(cv::Mat const &input, int col, UnaryFunc func)
{
    for(int row = input.rows - 1; row != -1; --row){
        auto input_ptr = input.ptr<T>(row) + col;
        func(*input_ptr);
    }

}

There are rooms to improve the performance, you could give it a try if you like.