Converting RGB values to contour values

I am using matplotlib to generate a filled contour plot, please consider the below example as a sample contour plot. I want to read off the contour values from such a filled contour plot using opencv's mouse interaction modules. For example, if the uses hovers the mouse over this contour image, it should dynamically display the contour values as the mouse moves over the image. I have the opencv part figured out, but I am struggling to link the RGB values to the actual numeric values.

I think that in the background, matplotlib maps the colormap (jet, viridis, inferno etc) to the min and max values that will be shown in the contour plot, and segments it in accordance to the number of contours chosen to show the contour data. How do I then access these contours and their RGB values so that I can pass this mapping to opencv?

Topic opencv matplotlib

Category Data Science


I do not know if this is the answer, but I think I am not wrong on that.

I have implemented contour lines and stuff for my own project and I think it is the same situation in all projects. In order to create graphics like that you do not explicitly create the curves, it would be too hard and I think almost impossible. Any algorithms does not try to create the curves itself, but split the space in small squares, and in each small square they test the values and draws very simple shapes. One well known algorithm is marching squares. Check the drawings from that page and you will soon understand how it works.

The algorithm approximates the curves that you finally see, but because the squares are small you do not see that they are only approximations. The trouble with such structure is that it contains a lot of information and it would be a great effort to keep that information somewhere and from that data to resemble the curves that you want. The usual approach is simply to not keep the whole stuff but run the algorithms and draw directly on the raster image. It is much easier and does not waste memory for the eventuality that somebody would want to do something with that.

I am not saying that the library you use does not provide the data structure from which you can resemble the iso curves, since I do not know, I say only that I would not offer some feature since anyway it is rarely used and requires a lot of resources.

Going further, I think that what you need is not the iso curve information, but a function which given a color would return you a value. A better place to find that transformation would be the scale object, not the graph itself. As far as I know matplotlib offers a scale object.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.