Grow your YouTube views, likes and subscribers for free
Get Free YouTube Subscribers, Views and Likes

Draw a Forest of Random Tree Objects with Python OpenCV - Classes and OOP Practice for Beginners

Follow
Python Simplified

In this tutorial, we will practice the principles of ObjectOriented Programming and draw a forest of unique tree objects.
We will first create a Tree class and then we will replicate it in different locations, sizes and colours. We will also include a very large degree of randomness in our code, so that each time our program runs a brand new forest with brand new trees is created!

HAVE YOU WATCHED ✋
*******************************************
1. My OOP and Classes Tutorial:
   • Python Classes and Objects  OOP for ...  

2. My Draw Shapes with OpenCV Tutorial:
   • Draw with OpenCV  No more Photoshop!...  
*******************************************

⏰ TIMESTAMPS ⏰
00:00 intro
00:16 starter code overview
01:01 create a class
01:22 class attributes
02:07 class methods
03:40 call class
05:40 relative coordinates
06:22 draw object
09:04 scale object
13:35 fancy design tricks
14:40 draw step repeat objects
16:04 draw objects at random location
19:47 draw with random colors
24:14 coming soon and thank you!

STARTER CODE
************************************************
import numpy as np
import cv2 as cv
import random

general parameters
width = 900
height = 600
n_trees = 30
ground_level = height100

colours
green, light_green, brown = (40,185,40),(25,220,0),(30,65,155)

blank image
bg = np.zeros((height, width, 3), dtype=np.uint8)

draw background
cv.rectangle(bg,(width,0), (0, ground_level), (255,225,95), 1)
cv.rectangle(bg,(width, ground_level), (0, height), green, 1)

***************
YOUR CODE GOES HERE
***************

#display image
cv.imshow('forest of objects', bg)

cv.waitKey(0)
cv.destroyAllWindows()
************************************************
END OF STARTER CODE

Didn't feel like coding along?
Get the complete code from my Github: https://github.com/MariyaSha/OpenCV_D...

Thanks for watching!
Find me on:
LinkedIn   / mariyasha888  
Discord   / discord  

posted by sportivosc4b