# Draw a graph with cv::line based on RGB pixel data (no histogram)

**URL:** <https://forum.opencv.org/t/draw-a-graph-with-cv-line-based-on-rgb-pixel-data-no-histogram/6613>\
**Category:** C++\
**Tags:** imgproc\
**Created:** [December 8, 2021, 12:57pm UTC](https://forum.opencv.org/t/draw-a-graph-with-cv-line-based-on-rgb-pixel-data-no-histogram/6613 "2021-12-08T12:57:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![makoe91](https://avatars.discourse-cdn.com/v4/letter/m/f9ae1b/32.png) [@makoe91](https://forum.opencv.org/u/makoe91)\
**Post date:** [December 8, 2021, 12:57pm UTC](https://forum.opencv.org/t/draw-a-graph-with-cv-line-based-on-rgb-pixel-data-no-histogram/6613/1 "2021-12-08T12:57:30Z")

</div>

Hi,

last week I tried a draw a graph with the `cv::Plot2d` function. Unfortunately `Plot2d` does not meet my requirements. So I try drawing a graph for the red, green and blue channel with `cv::line`.

I checked the openCV [histogram tutorial](https://docs.opencv.org/4.5.3/d8/dbc/tutorial_histogram_calculation.html) and I think I had understand this. What I did not understand is how I can create the first and the last point.

In the histogram tutorial a line is created like this:  
`line(histImage, cv::Point(bin_w * (i - 1), hist_h - cvRound(b_hist.at<float>(i - 1))), cv::Point(bin_w * (i), hist_h - cvRound(b_hist.at<float>(i))), cv::Scalar(255, 0, 0), 1, 4, 0);`

In this example `b_hist` depends on the call of `calcHist()`.

So my question is how I can create a graph without `calcHist()`? I think there is a need of a “function” with do a first job and than this return value I can give over to `cv::Point()`.

Or is it better to use a library like [CvPlot](https://github.com/Profactor/cv-plot). With this drawing a graph based on the pixel values is quite easy.

---

<div class="post-metadata">

**Author:** ![kbarni](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/kbarni/32/26_2.png) [@kbarni](https://forum.opencv.org/u/kbarni)\
**Post date:** [December 8, 2021, 1:28pm UTC](https://forum.opencv.org/t/draw-a-graph-with-cv-line-based-on-rgb-pixel-data-no-histogram/6613/2 "2021-12-08T13:28:52Z")

</div>

Well, if you want to draw a histogram, first you need to compute the histogram. If you need separate histogram to each channel, then decompose the image to individual channels (using the split function), then run the code for the three channels.

If you just want to plot your custom data, then you need to draw lines between the successive values of the data.

```auto
stepx = img.cols/data.size();
stepy = img.rows/max_value; //this is the highest value of the data

```

The position of the _i_th data point on the graph would be:

```auto
Point2i pointposition(int pos,int val, int stepx, int stepy, int height)
{
    //tis function maps a value to the graph. Note that it has to be 
    //scaled to fill the image and turned upside down, hence the height-y
    return Point2i(pos*stepx,height-val*stepy);
}

```

Now, draw the lines between the successive data points using the previous function:

```auto
for(int i=0;i<data.size()-1;i++)
   line(image,pointposition(i,data[i],stepx,stepy,img.rows),pointposition(i+1,data[i+1],stepx,stepy,img.rows),Scalar(255,0,0));

```
