0% found this document useful (0 votes)
93 views

Drawing and Writing On Image With OpenCV Python

The document discusses drawing basic shapes on images using OpenCV Python functions. It describes how to draw lines, rectangles, circles, and ellipses using cv2.line(), cv2.rectangle(), cv2.circle(), and cv2.ellipse() respectively. For each shape, it provides the syntax, parameter descriptions and an example code snippet.

Uploaded by

Danang Erwanto
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views

Drawing and Writing On Image With OpenCV Python

The document discusses drawing basic shapes on images using OpenCV Python functions. It describes how to draw lines, rectangles, circles, and ellipses using cv2.line(), cv2.rectangle(), cv2.circle(), and cv2.ellipse() respectively. For each shape, it provides the syntax, parameter descriptions and an example code snippet.

Uploaded by

Danang Erwanto
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

grabbed, frame = camera.

read()
if not grabbed:
raise ValueError("Camera read failed!")
bg = utils.image_resize(frame, height=600).astype(np.float32)

while True:
grabbed, frame = camera.read()
if not grabbed:
print "Camera read failed"
break

frame = utils.image_resize(frame, height=600)


height, width, channels = frame.shape

if not calibrated:
# Sample hand color
utils.add_text(frame, "Press space after covering rectangle with hand. Hit SPACE when ready")
x, y, w, h = width / 4, height / 2, 50, 50

Drawing and Writing on Image


cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imshow("Calibration", frame)
if cv2.waitKey(2) & 0xFF == ord(' '):

with OpenCV Python


roi = frame[y:y + h, x:x + w, :]
roi_hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
min_value = np.amin(roi_hsv, (0, 1))
max_value = np.amax(roi_hsv, (0, 1))
cv2.destroyWindow("Calibration")
calibrated = True

else:
cv2.accumulateWeighted(frame, bg, 0.01) MK: Image Processing

Oleh: Danang Erwanto, ST., MT

010100010010101010000100010100101010010100010001001101010010010101010
010100010010101010000100010100101010010100010001001101010010010101010
010100010010101010000100010101001001101010100100100101010
010100010010101010000100010101001001101010100100100101010
grabbed, frame = camera.read()
if not grabbed:
raise ValueError("Camera read failed!")

Drawing line
bg = utils.image_resize(frame, height=600).astype(np.float32)

while True:
grabbed, frame = camera.read()
if not grabbed:
print "Camera read failed"
break

• Function  cv2.line()
• Contoh syntax

• Keterangan sintax:
1.Fungsi untuk menggambar garis
2.img : variable di mana ingin menggambar garis
3.Titik koordinat awal garis digambar (x,y)
4.Titik koordinat akhir garis digambar (x,y)
5.color : Warna dari bentuknya. untuk BGR, misal: (255,0,0) untuk warna biru.
6.thickness : Tebal garis. ketebalan default = 1
MK : Image Processing (MKKPC09603)
Program Studi : Teknik Elektro – Uniska Kediri
grabbed, frame = camera.read()
if not grabbed:
raise ValueError("Camera read failed!")

Drawing line (cont’d)


bg = utils.image_resize(frame, height=600).astype(np.float32)

while True:
grabbed, frame = camera.read()
if not grabbed:
print "Camera read failed"
break

Contoh Source Code

MK : Image Processing (MKKPC09603)


Program Studi : Teknik Elektro – Uniska Kediri
grabbed, frame = camera.read()
if not grabbed:
raise ValueError("Camera read failed!")

Drawing Rectangle
bg = utils.image_resize(frame, height=600).astype(np.float32)

while True:
grabbed, frame = camera.read()
if not grabbed:
print "Camera read failed"
break

• Function  cv2.rectangle()
• Contoh syntax

• Keterangan sintax:
1.Fungsi untuk menggambar persegi
2.image : variable di mana ingin menggambar persegi
3.Titik koordinat sudut kiri atas persegi (x,y)
4.Titik koordinat sudut kanan bawah persegi (x,y)
5.color : Warna dari bentuknya. untuk BGR, misal: (0,255,0) untuk warna hijau.
6.thickness : Tebal garis, ketebalan default = 1
MK : Image Processing (MKKPC09603)
Program Studi : Teknik Elektro – Uniska Kediri
grabbed, frame = camera.read()
if not grabbed:
raise ValueError("Camera read failed!")

Drawing Rectangle (cont’d)


bg = utils.image_resize(frame, height=600).astype(np.float32)

while True:
grabbed, frame = camera.read()
if not grabbed:
print "Camera read failed"
break

Contoh Source Code

MK : Image Processing (MKKPC09603)


Program Studi : Teknik Elektro – Uniska Kediri
grabbed, frame = camera.read()
if not grabbed:
raise ValueError("Camera read failed!")

Drawing Circle
bg = utils.image_resize(frame, height=600).astype(np.float32)

while True:
grabbed, frame = camera.read()
if not grabbed:
print "Camera read failed"
break

• Diperlukan pusat koordinat dan radius dari lingkaran


• Function  cv2.circle()
• Contoh syntax

• Keterangan sintax:
1.Fungsi untuk menggambar lingkaran
2.image : variable di mana ingin menggambar lingkaran
3.Titik koordinat pusat lingkaran
4.Radius
5.color : Warna dari bentuknya. untuk BGR, misal: (0,0,255) untuk warna merah.
6.thickness : Tebal garis lingkaran. Jika diisi -1 maka akan mengisi bentuknya. ketebalan default = 1
MK : Image Processing (MKKPC09603)
Program Studi : Teknik Elektro – Uniska Kediri
grabbed, frame = camera.read()
if not grabbed:
raise ValueError("Camera read failed!")

Drawing Circle (cont’d)


bg = utils.image_resize(frame, height=600).astype(np.float32)

while True:
grabbed, frame = camera.read()
if not grabbed:
print "Camera read failed"
break

Contoh Source Code

MK : Image Processing (MKKPC09603)


Program Studi : Teknik Elektro – Uniska Kediri
grabbed, frame = camera.read()
if not grabbed:
raise ValueError("Camera read failed!")

Drawing Ellipse
bg = utils.image_resize(frame, height=600).astype(np.float32)

while True:
grabbed, frame = camera.read()
if not grabbed:
print "Camera read failed"
break

• Perlu menulis beberapa argument antara lain :


• argumen lokasi pusat (x, y).
• Argumen sumbu panjang (panjang sumbu Mayor, minor
sumbu panjang).
• Angle adalah sudut dari rotasi Ellips. startAngle dan
endAngle menunjukkan awal dan akhir dari busur elips
yang diukur dengan searah jarum jam dari axis utama,
contohnya memberikan nilai 360 untuk elips penuh.
• Function  cv2.ellipse()
MK : Image Processing (MKKPC09603)
Program Studi : Teknik Elektro – Uniska Kediri
grabbed, frame = camera.read()
if not grabbed:
raise ValueError("Camera read failed!")

Drawing Ellipse (cont’d)


bg = utils.image_resize(frame, height=600).astype(np.float32)

while True:
grabbed, frame = camera.read()
if not grabbed:
print "Camera read failed"
break

Contoh Source Code

Keterangan sintax:
1. Fungsi untuk menggambar elips
2. image : variable di mana ingin menggambar elips
3. Titik koordinat pusat elips
4. Panjang Sumbu Mayor
5. Panjang Sumbu Minor
6. startAngle
7. endAngle
8. Sudut putaran elips (360 = penuh)
9. color : Warna dari bentuknya. untuk BGR, misal: (0, ,255,0) untuk warna hijau.
10. thickness : Tebal garis lingkaran. Jika diisi -1 maka akan mengisi bentuknya. ketebalan default = 1

MK : Image Processing (MKKPC09603)


Program Studi : Teknik Elektro – Uniska Kediri
grabbed, frame = camera.read()
if not grabbed:
raise ValueError("Camera read failed!")

Drawing Poligon
bg = utils.image_resize(frame, height=600).astype(np.float32)

while True:
grabbed, frame = camera.read()
if not grabbed:
print "Camera read failed"
break

• Untuk menggambar poligon, pertama memerlukan


koordinat simpul (Buat titik-titik menjadi array bentuk
ROWSx1x2 dimana ROWS adalah jumlah simpul dan harus
tipe int32).
• Function  cv2.polylines()

MK : Image Processing (MKKPC09603)


Program Studi : Teknik Elektro – Uniska Kediri
grabbed, frame = camera.read()
if not grabbed:
raise ValueError("Camera read failed!")
bg = utils.image_resize(frame, height=600).astype(np.float32)

while True:
grabbed, frame = camera.read()
if not grabbed:
print "Camera read failed"
break

True False
MK : Image Processing (MKKPC09603)
Program Studi : Teknik Elektro – Uniska Kediri
grabbed, frame = camera.read()
if not grabbed:
raise ValueError("Camera read failed!")

Drawing Poligon (cont’d)


bg = utils.image_resize(frame, height=600).astype(np.float32)

while True:
grabbed, frame = camera.read()
if not grabbed:
print "Camera read failed"
break

Contoh Source Code


pts = np.array([[20,5],[50,5],[70,30],[50,55],[20,55],[0,30]], np.int32)
pts = pts.reshape((-1,1,2))
img = cv2.polylines(image,[pts],True,(0,0,255),2)

1 2 3 4 5 6
Keterangan sintax:
1. Fungsi untuk menggambar poligon
2. image : variable di mana ingin menggambar poligon
3. Titik koordinat simpul
4. True/False
5. color : Warna dari bentuknya. untuk BGR, misal: (0, ,255,0) untuk warna
hijau.
6. thickness : Tebal garis lingkaran. Jika diisi -1 maka akan mengisi
bentuknya. ketebalan default = 1

MK : Image Processing (MKKPC09603)


Program Studi : Teknik Elektro – Uniska Kediri
grabbed, frame = camera.read()
if not grabbed:
raise ValueError("Camera read failed!")

Adding Text
bg = utils.image_resize(frame, height=600).astype(np.float32)

while True:
grabbed, frame = camera.read()
if not grabbed:
print "Camera read failed"
break

Untuk memasukkan teks, perlu menentukan hal-hal berikut:


• Teks yang ingin Kita tulis
• Posisi koordinat di mana Kita ingin meletakkannya.
• Jenis font
• Skala Font (menentukan ukuran font)
• Hal biasa seperti warna, ketebalan, lineType dll. Untuk
tampilan yang lebih baik, lineType = cv2.LINE_AA
direkomendasikan.
• Function  cv2.putText()
MK : Image Processing (MKKPC09603)
Program Studi : Teknik Elektro – Uniska Kediri
grabbed, frame = camera.read()
if not grabbed:
raise ValueError("Camera read failed!")
bg = utils.image_resize(frame, height=600).astype(np.float32)

while True:
grabbed, frame = camera.read()
if not grabbed:
print "Camera read failed"
break

MK : Image Processing (MKKPC09603)


Program Studi : Teknik Elektro – Uniska Kediri
grabbed, frame = camera.read()
if not grabbed:
raise ValueError("Camera read failed!")

Font Style
bg = utils.image_resize(frame, height=600).astype(np.float32)

while True:
grabbed, frame = camera.read()
if not grabbed:
print "Camera read failed"
break

MK : Image Processing (MKKPC09603)


Program Studi : Teknik Elektro – Uniska Kediri
grabbed, frame = camera.read()
if not grabbed:
raise ValueError("Camera read failed!")
bg = utils.image_resize(frame, height=600).astype(np.float32)

while True:
grabbed, frame = camera.read()
if not grabbed:
print "Camera read failed"
break

MK : Image Processing (MKKPC09603)


Program Studi : Teknik Elektro – Uniska Kediri

You might also like