Reputation: 5019
codes 1
template<typename T>
inline T* get_pointer(cv::Mat &src, int row)
{
return src.ptr<T>(row);
}
template<typename T>
inline T* get_pointer(cv::Mat &src, int row, int col)
{
return get_pointer<T>(src, row) + col * src.channels();
}
template<typename T>
inline T* get_pointer(cv::Mat &src, int row, int col, int channel)
{
return get_pointer<T>(src, row, col) + channel;
}
codes 2
cv::Mat input = //....
auto *input_ptr = get_pointer<float>(input, row, col);
//back to previous row
input_ptr = reinterpret_cast<float*>(reinterpret_cast<uchar*>(input_ptr) - input.steps);
Are they safe?
Upvotes: 1
Views: 1687
Reputation: 2525
Why don't you use this (shorter and safer) code instead?
T *ptr_to_elem = &src.at<T>(row,col)[channel];
This works for non-contiguous arrays too. No potentially dangerous reinterpret_cast<>
needed. The only condition for safety here is that you know the datatype T. A more safe version is when you use cv::Mat_<T>
instead of cv::Mat
, so that mistakes are caught at compile time instead of crashing your program at run time.
T *ptr_to_elem = &src(row,col)[channel];
Upvotes: 1