2

I'm trying to find peaks on the left and right halves of the image (basically this is a binary image of a road with left and right lanes). For some reason, the left argmax is giving a value to the right of midpoint, and right is giving beyond the size of the image.

Here's my code

import numpy as np
import cv2

binary_warped = cv2.imread('data\Sobel\warped-example.jpg')
histogram = np.sum(binary_warped[binary_warped.shape[0]//2:,:], axis=0)
plt.plot(histogram)
midpoint = np.int(histogram.shape[0]//2)
leftx_base = np.argmax(histogram[:midpoint])
rightx_base = np.argmax(histogram[midpoint:]) + midpoint
print('Shape {} midpoint {} left peak {} right peak {}'.format(histogram.shape, midpoint, leftx_base, rightx_base))

This is my input Binary Warped Image

Input with shapes on the axis Input with shapes

Ideally the left peak should be around 370 and right should be arnd 1000, but Here is my result

Shape (1280, 3) midpoint 640 left peak 981 right peak 1633

Result of above code

Where was the mistake?

Ravi
  • 561
  • 2
  • 7
  • 19
  • Where does that graph come from? Why do you think the peaks should be around 370 and 1000? Where is the image file you're running this on? – abarnert Apr 10 '18 at 00:33
  • Hi, I've added the image now, sorry, forgot to include earlier. – Ravi Apr 10 '18 at 00:47

1 Answers1

3

The clue is given when you look at the shape of your histogram. It is 2-dimensional as it has a shape of (1280, 3)

When you call np.argmax(histogram[:midpoint]), argmax is called on a 2-d array and will first be unraveled before finding the largest value/index

You can see an example of this in the numpy docs:

>>> a = np.arange(6).reshape(2,3)
>>> a
array([[0, 1, 2],
       [3, 4, 5]])
>>> np.argmax(a)
5
Simon
  • 9,762
  • 15
  • 62
  • 119