Merge pull request #14107 from adityak2920:aditya
Adding python codes to samples/python * Python version of samples: - laplace.py - drawing.py * Update drawing.py * Update drawing.py * Update drawing.py * Update laplace.py * Update laplace.py * Update drawing.py * Update drawing.py * Update laplace.py * samples * drawing * drawing * drawing.py
This commit is contained in:
parent
4d14307f5d
commit
e007d93d93
191
samples/python/drawing.py
Normal file
191
samples/python/drawing.py
Normal file
@ -0,0 +1,191 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
'''
|
||||||
|
This program demonstrates OpenCV drawing and text output functions by drawing different shapes and text strings
|
||||||
|
Usage :
|
||||||
|
python3 drawing.py
|
||||||
|
Press any button to exit
|
||||||
|
'''
|
||||||
|
|
||||||
|
# Python 2/3 compatibility
|
||||||
|
from __future__ import print_function
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import cv2 as cv
|
||||||
|
|
||||||
|
# Drawing Lines
|
||||||
|
def lines():
|
||||||
|
for i in range(NUMBER*2):
|
||||||
|
pt1, pt2 = [], []
|
||||||
|
pt1.append(np.random.randint(x1, x2))
|
||||||
|
pt1.append(np.random.randint(y1, y2))
|
||||||
|
pt2.append(np.random.randint(x1, x2))
|
||||||
|
pt2.append(np.random.randint(y1, y2))
|
||||||
|
color = "%06x" % np.random.randint(0, 0xFFFFFF)
|
||||||
|
color = tuple(int(color[i:i+2], 16) for i in (0, 2 ,4))
|
||||||
|
arrowed = np.random.randint(0, 6)
|
||||||
|
if (arrowed<3):
|
||||||
|
cv.line(image, tuple(pt1), tuple(pt2), color, np.random.randint(1, 10), lineType)
|
||||||
|
else:
|
||||||
|
cv.arrowedLine(image, tuple(pt1), tuple(pt2), color, np.random.randint(1, 10), lineType)
|
||||||
|
cv.imshow(wndname, image)
|
||||||
|
if cv.waitKey(DELAY)>=0:
|
||||||
|
return
|
||||||
|
|
||||||
|
# Drawing Rectangle
|
||||||
|
def rectangle():
|
||||||
|
for i in range(NUMBER*2):
|
||||||
|
pt1, pt2 = [], []
|
||||||
|
pt1.append(np.random.randint(x1, x2))
|
||||||
|
pt1.append(np.random.randint(y1, y2))
|
||||||
|
pt2.append(np.random.randint(x1, x2))
|
||||||
|
pt2.append(np.random.randint(y1, y2))
|
||||||
|
color = "%06x" % np.random.randint(0, 0xFFFFFF)
|
||||||
|
color = tuple(int(color[i:i+2], 16) for i in (0, 2 ,4))
|
||||||
|
thickness = np.random.randint(-3, 10)
|
||||||
|
marker = np.random.randint(0, 10)
|
||||||
|
marker_size = np.random.randint(30, 80)
|
||||||
|
|
||||||
|
if (marker > 5):
|
||||||
|
cv.rectangle(image, tuple(pt1), tuple(pt2), color, max(thickness, -1), lineType)
|
||||||
|
else:
|
||||||
|
cv.drawMarker(image, tuple(pt1), color, marker, marker_size)
|
||||||
|
cv.imshow(wndname, image)
|
||||||
|
if cv.waitKey(DELAY)>=0:
|
||||||
|
return
|
||||||
|
|
||||||
|
# Drawing ellipse
|
||||||
|
def ellipse():
|
||||||
|
for i in range(NUMBER*2):
|
||||||
|
center = []
|
||||||
|
center.append(np.random.randint(x1, x2))
|
||||||
|
center.append(np.random.randint(x1, x2))
|
||||||
|
axes = []
|
||||||
|
axes.append(np.random.randint(0, 200))
|
||||||
|
axes.append(np.random.randint(0, 200))
|
||||||
|
angle = np.random.randint(0, 180)
|
||||||
|
color = "%06x" % np.random.randint(0, 0xFFFFFF)
|
||||||
|
color = tuple(int(color[i:i+2], 16) for i in (0, 2 ,4))
|
||||||
|
thickness = np.random.randint(-1, 9)
|
||||||
|
cv.ellipse(image, tuple(center), tuple(axes), angle, angle-100, angle + 200, color, thickness, lineType)
|
||||||
|
cv.imshow(wndname, image)
|
||||||
|
if cv.waitKey(DELAY)>=0:
|
||||||
|
return
|
||||||
|
|
||||||
|
# Drawing Polygonal Curves
|
||||||
|
def polygonal():
|
||||||
|
for i in range(NUMBER):
|
||||||
|
pt = [(0, 0)]*6
|
||||||
|
pt = np.resize(pt, (2, 3, 2))
|
||||||
|
pt[0][0][0] = np.random.randint(x1, x2)
|
||||||
|
pt[0][0][1] = np.random.randint(y1, y2)
|
||||||
|
pt[0][1][0] = np.random.randint(x1, x2)
|
||||||
|
pt[0][1][1] = np.random.randint(y1, y2)
|
||||||
|
pt[0][2][0] = np.random.randint(x1, x2)
|
||||||
|
pt[0][2][1] = np.random.randint(y1, y2)
|
||||||
|
pt[1][0][0] = np.random.randint(x1, x2)
|
||||||
|
pt[1][0][1] = np.random.randint(y1, y2)
|
||||||
|
pt[1][1][0] = np.random.randint(x1, x2)
|
||||||
|
pt[1][1][1] = np.random.randint(y1, y2)
|
||||||
|
pt[1][2][0] = np.random.randint(x1, x2)
|
||||||
|
pt[1][2][1] = np.random.randint(y1, y2)
|
||||||
|
color = "%06x" % np.random.randint(0, 0xFFFFFF)
|
||||||
|
color = tuple(int(color[i:i+2], 16) for i in (0, 2 ,4))
|
||||||
|
alist = []
|
||||||
|
for k in pt[0]:
|
||||||
|
alist.append(k)
|
||||||
|
for k in pt[1]:
|
||||||
|
alist.append(k)
|
||||||
|
ppt = np.array(alist)
|
||||||
|
cv.polylines(image, [ppt], True, color, thickness = np.random.randint(1, 10), lineType = lineType)
|
||||||
|
cv.imshow(wndname, image)
|
||||||
|
if cv.waitKey(DELAY) >= 0:
|
||||||
|
return
|
||||||
|
|
||||||
|
# fills an area bounded by several polygonal contours
|
||||||
|
def fill():
|
||||||
|
for i in range(NUMBER):
|
||||||
|
pt = [(0, 0)]*6
|
||||||
|
pt = np.resize(pt, (2, 3, 2))
|
||||||
|
pt[0][0][0] = np.random.randint(x1, x2)
|
||||||
|
pt[0][0][1] = np.random.randint(y1, y2)
|
||||||
|
pt[0][1][0] = np.random.randint(x1, x2)
|
||||||
|
pt[0][1][1] = np.random.randint(y1, y2)
|
||||||
|
pt[0][2][0] = np.random.randint(x1, x2)
|
||||||
|
pt[0][2][1] = np.random.randint(y1, y2)
|
||||||
|
pt[1][0][0] = np.random.randint(x1, x2)
|
||||||
|
pt[1][0][1] = np.random.randint(y1, y2)
|
||||||
|
pt[1][1][0] = np.random.randint(x1, x2)
|
||||||
|
pt[1][1][1] = np.random.randint(y1, y2)
|
||||||
|
pt[1][2][0] = np.random.randint(x1, x2)
|
||||||
|
pt[1][2][1] = np.random.randint(y1, y2)
|
||||||
|
color = "%06x" % np.random.randint(0, 0xFFFFFF)
|
||||||
|
color = tuple(int(color[i:i+2], 16) for i in (0, 2 ,4))
|
||||||
|
alist = []
|
||||||
|
for k in pt[0]:
|
||||||
|
alist.append(k)
|
||||||
|
for k in pt[1]:
|
||||||
|
alist.append(k)
|
||||||
|
ppt = np.array(alist)
|
||||||
|
cv.fillPoly(image, [ppt], color, lineType)
|
||||||
|
cv.imshow(wndname, image)
|
||||||
|
if cv.waitKey(DELAY) >= 0:
|
||||||
|
return
|
||||||
|
|
||||||
|
# Drawing Circles
|
||||||
|
def circles():
|
||||||
|
for i in range(NUMBER):
|
||||||
|
center = []
|
||||||
|
center.append(np.random.randint(x1, x2))
|
||||||
|
center.append(np.random.randint(x1, x2))
|
||||||
|
color = "%06x" % np.random.randint(0, 0xFFFFFF)
|
||||||
|
color = tuple(int(color[i:i+2], 16) for i in (0, 2 ,4))
|
||||||
|
cv.circle(image, tuple(center), np.random.randint(0, 300), color, np.random.randint(-1, 9), lineType)
|
||||||
|
cv.imshow(wndname, image)
|
||||||
|
if cv.waitKey(DELAY) >= 0:
|
||||||
|
return
|
||||||
|
|
||||||
|
# Draws a text string
|
||||||
|
def string():
|
||||||
|
for i in range(NUMBER):
|
||||||
|
org = []
|
||||||
|
org.append(np.random.randint(x1, x2))
|
||||||
|
org.append(np.random.randint(x1, x2))
|
||||||
|
color = "%06x" % np.random.randint(0, 0xFFFFFF)
|
||||||
|
color = tuple(int(color[i:i+2], 16) for i in (0, 2 ,4))
|
||||||
|
cv.putText(image, "Testing text rendering", tuple(org), np.random.randint(0, 8), np.random.randint(0, 100)*0.05+0.1, color, np.random.randint(1, 10), lineType)
|
||||||
|
cv.imshow(wndname, image)
|
||||||
|
if cv.waitKey(DELAY) >= 0:
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
def string1():
|
||||||
|
textsize = cv.getTextSize("OpenCV forever!", cv.FONT_HERSHEY_COMPLEX, 3, 5)
|
||||||
|
org = (int((width - textsize[0][0])/2), int((height - textsize[0][1])/2))
|
||||||
|
for i in range(0, 255, 2):
|
||||||
|
image2 = np.array(image) - i
|
||||||
|
cv.putText(image2, "OpenCV forever!", org, cv.FONT_HERSHEY_COMPLEX, 3, (i, i, 255), 5, lineType)
|
||||||
|
cv.imshow(wndname, image2)
|
||||||
|
if cv.waitKey(DELAY) >= 0:
|
||||||
|
return
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
print(__doc__)
|
||||||
|
wndname = "Drawing Demo"
|
||||||
|
NUMBER = 100
|
||||||
|
DELAY = 5
|
||||||
|
width, height = 1000, 700
|
||||||
|
lineType = cv.LINE_AA # change it to LINE_8 to see non-antialiased graphics
|
||||||
|
x1, x2, y1, y2 = -width/2, width*3/2, -height/2, height*3/2
|
||||||
|
image = np.zeros((height, width, 3), dtype = np.uint8)
|
||||||
|
cv.imshow(wndname, image)
|
||||||
|
cv.waitKey(DELAY)
|
||||||
|
lines()
|
||||||
|
rectangle()
|
||||||
|
ellipse()
|
||||||
|
polygonal()
|
||||||
|
fill()
|
||||||
|
circles()
|
||||||
|
string()
|
||||||
|
string1()
|
||||||
|
cv.waitKey(0)
|
||||||
|
cv.destroyAllWindows()
|
||||||
69
samples/python/laplace.py
Normal file
69
samples/python/laplace.py
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
'''
|
||||||
|
This program demonstrates Laplace point/edge detection using
|
||||||
|
OpenCV function Laplacian()
|
||||||
|
It captures from the camera of your choice: 0, 1, ... default 0
|
||||||
|
Usage:
|
||||||
|
python laplace.py <ddepth> <smoothType> <sigma>
|
||||||
|
If no arguments given default arguments will be used.
|
||||||
|
|
||||||
|
Keyboard Shortcuts:
|
||||||
|
Press space bar to exit the program.
|
||||||
|
'''
|
||||||
|
|
||||||
|
# Python 2/3 compatibility
|
||||||
|
from __future__ import print_function
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import cv2 as cv
|
||||||
|
import sys
|
||||||
|
|
||||||
|
def main():
|
||||||
|
# Declare the variables we are going to use
|
||||||
|
ddepth = cv.CV_16S
|
||||||
|
smoothType = "MedianBlur"
|
||||||
|
sigma = 3
|
||||||
|
if len(sys.argv)==4:
|
||||||
|
ddepth = sys.argv[1]
|
||||||
|
smoothType = sys.argv[2]
|
||||||
|
sigma = sys.argv[3]
|
||||||
|
# Taking input from the camera
|
||||||
|
cap=cv.VideoCapture(0)
|
||||||
|
# Create Window and Trackbar
|
||||||
|
cv.namedWindow("Laplace of Image", cv.WINDOW_AUTOSIZE)
|
||||||
|
cv.createTrackbar("Kernel Size Bar", "Laplace of Image", sigma, 15, lambda x:x)
|
||||||
|
# Printing frame width, height and FPS
|
||||||
|
print("=="*40)
|
||||||
|
print("Frame Width: ", cap.get(cv.CAP_PROP_FRAME_WIDTH), "Frame Height: ", cap.get(cv.CAP_PROP_FRAME_HEIGHT), "FPS: ", cap.get(cv.CAP_PROP_FPS))
|
||||||
|
while True:
|
||||||
|
# Reading input from the camera
|
||||||
|
ret, frame = cap.read()
|
||||||
|
if ret == False:
|
||||||
|
print("Can't open camera/video stream")
|
||||||
|
break
|
||||||
|
# Taking input/position from the trackbar
|
||||||
|
sigma = cv.getTrackbarPos("Kernel Size Bar", "Laplace of Image")
|
||||||
|
# Setting kernel size
|
||||||
|
ksize = (sigma*5)|1
|
||||||
|
# Removing noise by blurring with a filter
|
||||||
|
if smoothType == "GAUSSIAN":
|
||||||
|
smoothed = cv.GaussianBlur(frame, (ksize, ksize), sigma, sigma)
|
||||||
|
if smoothType == "BLUR":
|
||||||
|
smoothed = cv.blur(frame, (ksize, ksize))
|
||||||
|
if smoothType == "MedianBlur":
|
||||||
|
smoothed = cv.medianBlur(frame, ksize)
|
||||||
|
|
||||||
|
# Apply Laplace function
|
||||||
|
laplace = cv.Laplacian(smoothed, ddepth, 5)
|
||||||
|
# Converting back to uint8
|
||||||
|
result = cv.convertScaleAbs(laplace, (sigma+1)*0.25)
|
||||||
|
# Display Output
|
||||||
|
cv.imshow("Laplace of Image", result)
|
||||||
|
k = cv.waitKey(30)
|
||||||
|
if k == 27:
|
||||||
|
return
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print(__doc__)
|
||||||
|
main()
|
||||||
|
cv.destroyAllWindows()
|
||||||
Loading…
Reference in New Issue
Block a user