0

I am trying to use Python along with opencv, numpy and matplotlib to do some computer vision for a robot which will use a railing to navigate. I am currently extremely stuck have run out of places to look. My current code is:

import cv2
import numpy as np
import matplotlib.pyplot as plt

image = cv2.imread('railings.jpg')
railing_image = np.copy(image)

resized_image = cv2.resize(railing_image,(881,565))

gray = cv2.cvtColor(resized_image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5, 5), 0)
canny = cv2.Canny(blur, 85, 255)

cv2.imshow('test',canny)

image_array = np.array(canny)
ncols, nrows = image_array.shape

count = 0
scan = np.array
for x in range(0,image_array.shape[1]):
    for y in range(0,image_array.shape[0]):
        if image_array[y, x] == 0:
            count += 1
    scan = [scan, count]

print(scan)

plt.plot([0, count])
plt.axis([0, nrows, 0, ncols])
plt.show()

cv2.waitKey(0)

I am using a canny image which is stored in an array of 1's and 0's, the image I need represented is

Source image

The final result should look something like the following image.

terrible_drawing_of_image

I've tried using a histogram function but I've only managed to get that to output essentially a count of the number of times a 1 or 0 appears.

If anyone could help me or point me in the right direction that would produce a graph that represents the image pixels within a graph of height and width dimensions.

Thank you

Ed Smith
  • 12,716
  • 2
  • 43
  • 55
Gentem
  • 97
  • 1
  • 6
  • What exactly does "axis being height and width dimensions and the data being the pixels" mean? The drawing you provide doesn't have any axes drawn, let alone labeled... – Dan Mašek Feb 22 '19 at 18:15
  • Ah yeah, forgot to draw those in, I just mean that the y axis would be the image height and the x axis would be the image width and the pixels would be plotted depending on whether or not a pixel is black at a x-y coordinate. Therefore if all locations were plotted where there is a black pixel value in the array, then a graph that looks like the abomination that I drew, would be the result. – Gentem Feb 22 '19 at 19:08
  • Oh, so based on the accepted answer, what you're actually asking is a plot which shows for each column the lowest Y location with non-black pixel... Because what you describe above is nowhere close to that. – Dan Mašek Feb 22 '19 at 19:15
  • Sorry for the confusion! I sometimes end up over-describing and not getting my point across! – Gentem Feb 22 '19 at 19:24

1 Answers1

1

I'm not sure how general this is but you could just use numpy argmax to get location of the maximum (like this) in your case. You should avoid loops as this will be very slow, better to use numpy functions. I've imported your image and used the cutoff criterion that 200 or more in the yellow channel is railing,

import cv2
import numpy as np
import matplotlib.pyplot as plt

#This loads the canny image you uploaded
image = cv2.imread('uojHJ.jpg')

#Trim off the top taskbar
trimimage = image[100:, :,0]

#Use argmax with 200 cutoff colour in one channel
maxindex = np.argmax(trimimage[:,:]>200, axis=0)

#Plot graph
plt.plot(trimimage.shape[0] - maxindex)
plt.show()

Where this looks as follows:

enter image description here

Ed Smith
  • 12,716
  • 2
  • 43
  • 55