打印cv::Mat opencv
我正在尝试打印包含我的图像的cv::Mat。然而,每当我使用cout打印Mat时,一个2D数组就会打印到我的文本文件中。我只想在一行中打印一个像素。如何从cv::Mat打印行像素。对于每个循环,您可以使用它打印数据打印cv::Mat opencv,opencv,image-processing,Opencv,Image Processing,我正在尝试打印包含我的图像的cv::Mat。然而,每当我使用cout打印Mat时,一个2D数组就会打印到我的文本文件中。我只想在一行中打印一个像素。如何从cv::Mat打印行像素。对于每个循环,您可以使用它打印数据 /** *@brief implement details of for_each_channel, user should not use this function */ template<typename T, typename UnaryFunc> Unary
/**
*@brief implement details of for_each_channel, user should not use this function
*/
template<typename T, typename UnaryFunc>
UnaryFunc for_each_channel_impl(cv::Mat &input, int channel, UnaryFunc func)
{
int const rows = input.rows;
int const cols = input.cols;
int const channels = input.channels();
for(int row = 0; row != rows; ++row){
auto *input_ptr = input.ptr<T>(row) + channel;
for(int col = 0; col != cols; ++col){
func(*input_ptr);
input_ptr += channels;
}
}
return func;
}
/**
*@对于每个信道,用户不应使用此功能
*/
模板
每个通道的UnaryFunc(cv::Mat&input,int通道,UnaryFunc func)
{
int const rows=input.rows;
int const cols=input.cols;
int const channels=input.channels();
对于(int行=0;行!=行;++行){
auto*input_ptr=input.ptr(行)+通道;
for(int col=0;col!=cols;++col){
func(*输入\ ptr);
输入_ptr+=通道;
}
}
返回函数;
}
像这样使用它
for_each_channel_impl<uchar>(input, 0, [](uchar a){ std::cout<<(size_t)a<<", "; });
对于每个通道的impl(输入,0,[](uchar a){std::cout
/**
*@brief apply stl like for_each algorithm on a channel
*
* @param
* T : the type of the channel(ex, uchar, float, double and so on)
* @param
* channel : the channel need to apply for_each algorithm
* @param
* func : Unary function that accepts an element in the range as argument
*
*@return :
* return func
*/
template<typename T, typename UnaryFunc>
inline UnaryFunc for_each_channel(cv::Mat &input, int channel, UnaryFunc func)
{
if(input.channels() == 1 && input.isContinuous()){
return for_each_continuous_channels<T>(input, func);
}else{
return for_each_channel_impl<T>(input, channel, func);
}
}