4

I have integrated tensorflow object detection API with OpenCv to track a person within the frame so that the python script can draw a graph showing where the person entered into and exit the frame also where it moved inside the frame.

To draw graph I am using matplotlib. I have the x y coordinates and the graph is shown very nicely. Now I need to put an image in background of that graph. For this I have done below:

image = mpimg.imread(file)
plt.imshow(image)
plt.plot(x, y)
plt.plot(x[0], y[0], 'og')
plt.plot(x[-1], y[-1], 'ob')
plt.show()

In the above code, I am reading the file which is an image and then showing it on the graph. Doing this make my final graph not good looking as the image moves upward and the line graph remains on the lower portion while I wanted the line graph to be on the image. I referred to this question which is what I am trying to achieve but in the question which I have mentioned, the solution explains to draw a line over the image. In my scenario, I have to draw a graph which will have multiple coordinates.

I tried the solution explained in the referred question and used extent while showing the image in matplotlib, like below:

image = mpimg.imread(file)
plt.imshow(image, extent=[x[0], x[-1], y[0], y[-1]])
plt.plot(x, y)
plt.plot(x[0], y[0], 'og')
plt.plot(x[-1], y[-1], 'ob')
plt.show()

In the above code, I am using extent=[x[0], x[-1], y[0], y[-1]]) where x[0], x[-1], y[0], y[-1] means the first and last points of x and y coordinates, so that the image can fit properly on the graph. Doing this, I get below results:

enter image description here

As you can see the image is properly fitted in the graph but as soon as I plot my x y coordinates, it looks like below:

enter image description here

which is not as good as the line should be on the image. I am confused here as what I am doing wrong. I have tried to follow the same referred answer and used extent but while plotting the image moves a bit forward. Below are my coordinates:

x = [612, 590, 646, 712, 466, 475]
y = [623, 562, 557, 567, 530, 536]

Green dot resembles start and blue dot resembles end. Below is the image of the graph if I don't use a background image. One thing to note here is that x,y coordinate does not match with the above images but only matches with the below graph (without background image):

enter image description here

Can anyone please tell me what I am doing wrong here. Please help. Thanks

Antu
  • 2,197
  • 3
  • 25
  • 40
S Andrew
  • 5,592
  • 27
  • 115
  • 237
  • I don't think the problem is particulartly clear. All coordinates mentionned are correctly plotted in the graph. Whether or not it is reasonable to assume that the image needs to extend exactly between the first and last coordinate is hard to judge from the outside. However if the decision to do this is justified then the images are all correct. – ImportanceOfBeingErnest Dec 01 '18 at 11:09

1 Answers1

1

If the extent only uses the start and stop positions, the corners of the image will match to those positions. This will put any intermediate x and y values that are higher or lower outside the image.

I'm not sure what the x and y coordinates all include, but if they have points across the entire desired range, you could use the min and max values to get the extent.

image = mpimg.imread(file)
plt.imshow(image, extent=[min(x), max(x), min(y), max(y)])
plt.plot(x, y)
plt.plot(x[0], y[0], 'og')
plt.plot(x[-1], y[-1], 'ob')
plt.show()

enter image description here

However, I don't think this will be the case given these are tracked positions of the bounding box, in which case the extent shouldn't be based on those values at all. Instead, use the original image size, which I'm guessing is 1280x720.

plt.imshow(image, extent=[0, 1280, 0, 720])

enter image description here

It does appear to line up if you're tracking the mid-top of the bounding box.

A Kruger
  • 2,289
  • 12
  • 17
  • Thanks for the idea of using the `max` `min` values in `extent`. I will try it. One question how did you remove the zoom in/out, home and other buttons from the graph which are usually present at the bottom of the image. Thanks – S Andrew Dec 01 '18 at 16:41
  • I used a jupyter notebook to plot the image. I then just right-click, save, and that's the image I get. Also, what are `x` and `y` specifically? – A Kruger Dec 01 '18 at 16:45
  • About `x`, `y`, I am getting the coordinates of bounding boxes of object from tensorflow. I am then passing these coordinates into centroid tracking algorithm of OpenCV which returns `(x, y)` corrdinates of the tracked object centroid. Opencv algorithm keeps giving the these centroid coordinates untill an object is present in the frame. These coordinates are the `x` and `y` values. – S Andrew Dec 01 '18 at 16:48
  • I don't think the min/max will work. If it doesn't work, what's the original image dimensions? Also, tensorflow gives position relative to the top left. Here the positions are being plotted relative to the bottom left. – A Kruger Dec 01 '18 at 16:55