Treetrunk detection


i got assigned a project where i have to programm a tree detection software. Now i have already written a programm where i draw the contours of an example picture. But it seems i cannot “extract” the trees from the background that only the trunks are getting colored. I tried to change the values of the threshold but that doesn’t make much difference. And i also tried the canny algorithm but this one seems to work even worse. Can someone help me with this?
Also i am pretty new to Python and OpenCV, so please explain functions when u recommend them to me.
I’ll link the result.

Here’s the code

import cv2

img = cv2.imread('trees.JPG')

imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(imgGray, 50, 255, cv2.THRESH_BINARY)
contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
image_copy = img.copy()
cv2.drawContours(image_copy, contours, -1, (0, 255, 0), 2, cv2.LINE_AA)

while (1):

    cv2.imshow('image', image_copy)
    k = cv2.waitKey(1) & 0xFF
    if k == 27:

Thank you in advance!!

for now, discard that entire approach. you can see it doesn’t work and you aren’t clear on what information you’re supposed to extract.

“tree detection software”. with what goal? what is the specific information you need to determine? why is this required? what will this information be used for? the one giving you the task ought to answer that question.

I am working for a tree-harvesting company and the goal is to see on a display where tree’s are. They should be highlighted or a rectangle put around them. They will put this informaton in further use so that the worker on the machine can just select the tree and the machine will work without any other input by the worker. But that’s not what i should do right now. My only assignment is the tree detection only. I found a similiar problem in the old forum: Identify trees in photo - OpenCV Q&A Forum
But i don’t understand the answer to this or simply put i don’t know how to implement the answers in my program.

what you found on the old forum does not apply to your situation.

this can’t be solved with low level image processing, not even in principle. you’ll get junk results.

you need deep learning for any usable results.

a good solution is gonna need a cylinder model for that tree trunk, so that’s 3D localization. you won’t be able to do that with a single camera. a single camera can’t estimate distance.

or, for monocular vision, you need at least something like a “centerline” on the tree (or more), so whatever robotic manipulator can blindly approach the trunk and sense (can it?) when it’s made contact.

instance segmentation could give you a mask for each tree trunk.

Okay thank you, I will look over these answer on the weekend. If i have any further more question I’ll ask them in this thread again.

Thank you again!