简体   繁体   English

向量的格式说明符 <Vec3b> 什么时候使用printf

[英]Format specifier for vector<Vec3b> when using printf

I want to fetch, let us, say second pixel in buf and print using printf: 我想获取,让我们,说出buf中的第二个像素并使用printf进行打印:

vector<Vec3b> buf;
/* code that fills buf , I used push_back */
printf("%WHAT", buf[1])

I can print these values using cout something like this: 我可以使用像这样的cout打印这些值:

    LineIterator it(img, Point(1,1), Point(20,20), 8);

    vector<Vec3b> buf;   

    for(int i=0; i<it.count; i++)
    {
    buf.push_back( Vec3b(*it) ); /* code that fills buf , I used push_back */
    it++;
    }
    cout << Mat(buf[1]) << endl;

But I just want to do this using printf. 但我只是想用printf来做这件事。

The cout statement gives me three values, each corresponding to R, G and B. cout语句给出了三个值,每个值对应于R,G和B.

What should I place for WHAT in my printf? 我应该在我的printf中放置什么? ie what is the format specifier here? 即这里的格式说明符是什么?

there's no straightforward way to do so. 没有直接的方法可以做到这一点。 there are overloaded << operators in opencv, but no c solution (why do you even want this?) 在opencv中有重载的<<运算符,但没有c解决方案(为什么你甚至想要这个?)

you will have to do it all manually: 你必须手动完成所有操作:

for(size_t i=0; i<buf.size(); i++)
{
   Vec3b v = buf[i];
   printf("[%d %d %d] ", v[0], v[1], v[2] );
}
printf("\n");

You wont be able to just use it with printf like that because its not of a standard type. 你不能像print那样只使用它,因为它不是标准类型。 I think you should write a function to print this type for you, for example something like this: 我认为你应该编写一个函数来为你打印这种类型,例如:

void printForMe(vector<Vec3b> buf)
{
    for (auto &vec : buf)
    {
        cout << vec[0] << " " << vec[1] << " " vec[2] << std::endl;
    }
}

You can then also edit how your format will appear. 然后,您还可以编辑格式的显示方式。

This would be even easier if OpenCv's MatX class had an overload for operator<< but it doesn't so you will need to make this yourself. 如果OpenCv的MatX类对于operator<<具有重载,那么这将更容易。但事实并非如此,您将需要自己制作它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM