yard inside garden detection (cv2)

I want to seperate the gras thats inside my garden from the gras outside of it using the fence basement and the stones. The script should be able to draw the red line that you can see at the second image. It should be possible with cv2

Ive tried a code that i found at a driving lane detection system, but unfortunately its not working good for gras.

gray = cv2.cvtColor(gras_image, cv2.COLOR_RGB2GRAY) blur = cv2.GaussianBlur(gray, (5,5), 0) canny = cv2.Canny(blur, 50, 200) 

yard

enter image description here

Thanks for helping

Add Comment
1 Answer(s)

See the comments in my code below for how you can select just your yard in your image.

This won’t be a robust solution if the color of the border between your yard and not your yard changes. I found a suitable HSV range by opening your image in GIMP and selecting the brown border object on the right side with GIMP’s color picker tool. Miraculously this color also worked well for the gray blocks. You might be able to get better results by playing with the color ranges, or maybe even by creating separate masks for the brown blocks and the gray blocks.

import cv2 import numpy as np  #load the image image = cv2.imread("meadow.jpg")  #define the lower and upper bounds of colors to threshold for in the HSV color space. hsv_lower = (35//2, 0, 120) hsv_upper = (45//2, 255, 255)  #convert the image to HSV color space hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)  #find the areas of the image corresponding to the colors set above mask = cv2.inRange(hsv_image, hsv_lower, hsv_upper)  #set the detected areas to white and other areas to black new_image = np.where(mask, np.uint8(0), np.uint8(255))  #erode to fill in the gaps between the black pixels. new_image = cv2.erode(new_image, kernel=np.ones((7,7)))  #find connected components (the white areas) labels, stats = cv2.connectedComponentsWithStats(new_image)[1:3]  #create a mask for the area excluding the largest component not_my_yard = labels != np.argmax(stats[:,cv2.CC_STAT_AREA])  #set the color of the area excluding the largest component to black image[not_my_yard] = 0  #save the new image cv2.imwrite("my_yard.jpg", image) 

enter image description here

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.