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

Getting Started With Opencv Python: MK: Image Processing

The document describes code for calibrating a camera for color detection. It resizes frames from the camera to 600 pixels height, reads frames in a loop, and handles errors if frames are not grabbed. If not calibrated, it draws a rectangle on the frame for the user to cover with their hand. It samples the color within the rectangle and waits for the user to press a key. After calibrating, it accumulates weighted frames to the background image.

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)
38 views

Getting Started With Opencv Python: MK: Image Processing

The document describes code for calibrating a camera for color detection. It resizes frames from the camera to 600 pixels height, reads frames in a loop, and handles errors if frames are not grabbed. If not calibrated, it draws a rectangle on the frame for the user to cover with their hand. It samples the color within the rectangle and waits for the user to press a key. After calibrating, it accumulates weighted frames to the background image.

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/ 6

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")

Getting Started with OpenCV


x, y, w, h = width / 4, height / 2, 50, 50

cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)


cv2.imshow("Calibration", frame)
if cv2.waitKey(2) & 0xFF == ord(' '):

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!")

Load & Save Image


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

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

• image = cv2.imread(“apel.jpg”) 
membaca (load) file “apel.jpg”. Jika gambar ada di
folder lain (misalnya di folder pictures) maka bisa
dipanggi dengan syntax:
“C:\Users\Public\Pictures\SamplePictures\apel.jpg”
• cv2.imwrite(“apel_merah.png”,
image)  menyimpan citra yang tersimpan di
variabel image, citra akan disimpan di drive komputer
dengan nama apel_merah.png. File ini tersimpan di
folder yang sama dengan folder apel.jpg.

MK : Image Processing (MKKPC09603)


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

Load & Show Image


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

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

• cv2.imshow(‘Tampil Image’, image) 


menampilkan citra yang tersimpan di variabel image dan
diletakkan di window dengan nama Tampil Image.
• cv2.waitKey(0)  window ditahan (tidak menutup
sendiri) hingga user menekan salah satu tombol di keyboard
(misal tombol ESC atau spacy). Jika user menekan tombol
(ESC misalnya) maka window baru menutup.
• cv2.destroyAllWindows()  semua window yang
terbuka (jika membuka lebih dari satu window) maka
semuanya akan tertutup saat menekan tombol di keyboard.
cv2.waitKey(0). cv2.destroyAllWindows() harus disertakan setiap
kali menggunakan cv2.imshow(). Jika tidak maka window tidak bisa
ditutup.
MK : Image Processing (MKKPC09603)
Program Studi : Teknik Elektro – Uniska Kediri
grabbed, frame = camera.read()
if not grabbed:
raise ValueError("Camera read failed!")

Capture & Save Image


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

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

• cap = cv2.VideoCapture(0) 
mengaktifkan kamera Webcam dan melakukan
capture (menagkap image) objek.
• Angka nol (0)  webcam utama di laptop.
• Jika webcam ada dua, maka untuk mengaktifkan webcam
kedua, menggunakan angka satu (1).
• ret,frame=cap.read()  sintak wajib yang
menyertai VideoCapture(). Tanpanya
VideoCapture() tidak bisa jalan.
Frame  image. Image disebut frame karena diibaratkan
sebagai frame (bingkai). Image yang tersimpan di variabel
cap, diload dan disimpan di variabel frame agar bisa diolah.
MK : Image Processing (MKKPC09603)
Program Studi : Teknik Elektro – Uniska Kediri
grabbed, frame = camera.read()
if not grabbed:
raise ValueError("Camera read failed!")

Capture berulang
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