Identify trees in photo
I am trying to use a camera to help localize my robot in my garden. I want to be able to identify trees as they are the most prominent features.
I am having great difficulty extracting the trees from the background. they seem to have the same colur as the surroundings so I cannot use that so i have been trying to identify the edges.
I have looked at each of RGB and HSV and none seperatly seem to show the edges better than grayscale.
I have used a bilateral filter as it is supposed to blur but maintain the edges and I have used a sobel filter to highlight the edges as it seems to make a better job on this than Cany.
Here is a link to the original test photo. link text
I only take the middle third of the picture to reduce the size and because that part has the treetrunks.
Here is my code:
import cv2
import numpy as np
thresh = 0
sobel_8u = 0
gray = 0
img = 0
kernel = 0
def changeThreshhold(x):
global thresh,sobel_8u,gray,img
ret,thresh = cv2.threshold(sobel_8u,x,255,cv2.THRESH_BINARY )
img = np.vstack((gray,sobel_8u,thresh))
treeName = 'C:\Users\Alan\Desktop\Trees\tree4.jpg'
imgC = cv2.imread(treeName,cv2.CV_LOAD_IMAGE_COLOR)
h,w,c = np.shape(imgC)
partImgC = imgC[h/2:2*h/3,:,:]
gry = cv2.cvtColor(partImgC,cv2.COLOR_BGR2GRAY)
gray = cv2.equalizeHist(gry)
blur = cv2.bilateralFilter(gray,21,75,75)
sobelx64f = cv2.Sobel(blur,cv2.CV_64F,1,0,ksize=3)
abs_sobel64f = np.absolute(sobelx64f)
sobel_8u = np.uint8(abs_sobel64f)
changeThreshhold(65)
cv2.namedWindow('image',cv2.cv.CV_WINDOW_NORMAL)
cv2.createTrackbar('threshhold','image',65,255,changeThreshhold)
while(1):
cv2.imshow('image',img)
k = cv2.waitKey(1) & 0xFF
if k == 27:
break
cv2.destroyAllWindows()
<\code><\pre>
The results are here:link text
They do not look so bad but when I try to use HoughLines to extract the lines and hence the trees I get a huge amount of rubbish.
Can anybody show me a better way.
Thanks Alan
Wow! You have a nice garden!
What kind of colour segmentation have you tried? Did you try to remove green from image to eliminate grass?
I have tried removing the green but it makes it worse. Funnily removing the blue gives something perhaps slightly better than grayscale but not much. Here is a link to the lines drawn. https://dl.dropboxusercontent.com/u/3... You can see that the lines do not seem to follow the outline of the trees
nice image, but, in the future, please post smaller pics ;)
recognizing trees is a noble goal, but - nature is pretty hard. unbelievable hard.
what about sticking some ar markers to them, and call it a day ?
I did use markers for another project - purple colored ones where best, but they had to be huge to be more than a couple of pixels wide at say 40m. I am not giving up on the trees yet.