Reputation: 601
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))
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
Where was the mistake?
Upvotes: 2
Views: 1379
Reputation: 10150
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
Upvotes: 3