Ask Your Question
0

Identify trees in photo

asked 2016-03-03 07:42:36 -0600

AlanJ gravatar image

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

edit retag flag offensive close merge delete

Comments

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?

Pedro Batista gravatar imagePedro Batista ( 2016-03-03 07:59:57 -0600 )edit

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

AlanJ gravatar imageAlanJ ( 2016-03-03 13:32:32 -0600 )edit

nice image, but, in the future, please post smaller pics ;)

berak gravatar imageberak ( 2016-03-03 14:53:35 -0600 )edit

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 ?

berak gravatar imageberak ( 2016-03-03 23:52:19 -0600 )edit

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.

AlanJ gravatar imageAlanJ ( 2016-03-04 05:49:33 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-03-03 22:53:43 -0600

Tetragramm gravatar image

Try using this filter, it's faster than bilateral, and is probably easier to control.

Second, take a look at this answer, and apply a version of it. No guarantees it will work, but it's worth trying.

Third suggestion: Use findContours, and erase any sections that aren't vertical. That should cut down on the leaf edges and make Hough Lines work better.

Trees are hard. More generally, Nature is hard.

edit flag offensive delete link more

Comments

I will get back to you in a couple of days when I have followed up your suggestions

AlanJ gravatar imageAlanJ ( 2016-03-04 06:53:13 -0600 )edit

I cannot find a python version of the filter you suggest. I have got the findContours to work but the results are disappointing and I need to get a better filtered result before using that stage.

AlanJ gravatar imageAlanJ ( 2016-03-08 10:04:28 -0600 )edit

Hmm, most of the edge aware smoothing is in that module. But it has wrap python in the CMake. You may have to build it yourself. Did you try cv2.ximgproc as your namespace, whatever python calls it?

Tetragramm gravatar imageTetragramm ( 2016-03-08 16:23:29 -0600 )edit

CV2 only seems to have one sub module called cv and it is not in there. I am not sure what version of openCV I am using I thought i down loaded 3.0 but the version is reported as being 4557. I understand that that is a known bug. I am also having problems like cv2 variables not being available and commands returning different results from the documentation. It seems that there has been some upheaval in OpenCV, probably going to ver 3 and the document ation has not kept up and possibly some functions are not yet available. The result is that I am going to start using SciPi.

AlanJ gravatar imageAlanJ ( 2016-03-09 06:30:31 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-03-03 07:42:36 -0600

Seen: 1,722 times

Last updated: Mar 03 '16