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

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 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. With this drawing a graph based on the pixel values is quite easy.

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.

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

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

``````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:

``````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));
``````
1 Like