Plot r,b,g value along a line from a frame in video

I am using opencv c++ to plot pixel value along a line in an frame in video. First, I use LineIterator to specify the considered line and then iterate through that line. Based on the LineIterator tutorial, I can print out the value of pixel value (pixelValues). Anyone know how to plot these pixel value when opening the video. Thank you very much.

This is my code’

void main() {
string video = "testVideo.MP4";
VideoCapture cap(video);
Mat frame, matrix, frameWarp;
float w = 700, h = 200;
while (1)
    cap >> frame;
    if (!
    imshow("Video", frame);
    Point2f src[4] = { {460,490},{858,499},{460,670},{858,670} };
    Point2f dst[4] = { {0.0f,0.0f},{w,0.0f},{0.0f,h},{w,h} };
    matrix = getPerspectiveTransform(src, dst);
    warpPerspective(frame, frameWarp, matrix, Point(w, h));

    LineIterator it(frameWarp, {0,123}, {700,123}, 8, true);
    vector<Vec3b> pixelValues;
    for (int i = 0; i < it.count; i++) {

    line(frameWarp, { 0,123 }, { 700,123 }, Scalar(0, 255, 255), 2, LINE_4);
    imshow("Image", frameWarp);
    cout << Mat(pixelValues) << endl;
    if (waitKey(300) >= 0)

so, you sampled pxels from a horzontal line and now you want to draw the very same thing ? in the same place ? or do you want to draw numbers ?
please explain

Hi Berak,

Thank you very much for your reply. I cropped a small region from every frame in video and named it as Mat frameWarp has the size of 700,200 in x,y. Then I use LineIterator to iterate through every point in a line in frameWarp. My final purpose is to plot the graph with x axis is along to the iterated line and y axis is color value (r, g,b).

My graph with be x : 0-700 ; y:0-255 with three value of r,g,b.
So sorry for my bad English and explanation. please let me know if there is something unclear. Again, thank you very much for your time to support me.

ok, how do you want to put 3 values into a single curve ?
maybe 3 curves, similar to the hist plot here ?

1 Like

Yes, It should be 3 curves and could be observe during the video opening.
I copy the output value from cout << Mat(pixelValues) << endl;
and use excel to plot it, it should be look like this figure.

Thank you very much.

you can do this straight from your iterator loop:

float sy = float(h) / 256;
for (int i = 0; i < it.count; i++) {
        Vec3b p(*it);
        int by = p[0] * sy; // scale range to window
        by = h - by;     // y points down, so invert      <Vec3b>(by, i) = Vec3b(255,0,0);<Vec3b>((h - p[1] * sy), i) = Vec3b(0,255,0);<Vec3b>((h - p[2] * sy), i) = Vec3b(0,0,255);
        pixelValues.push_back(p); // still needed ?
1 Like

Thank you very much. It works.

1 Like

when asking others for help, make it easy. your problem has nothing to do with video, so leave that out of the problem description.