1. LAPORAN WORKSHOP KOMPUTER VISI
IMAGE FUNDAMENTAL
LUSIANA DIYAN NINGRUM
2210181051
3 D4 TEKNIK KOMPUTER B
PRODI SARJANA TERAPAN TEKNIK KOMPUTER
DEPARTEMEN TEKNIK INFORMATIKA DAN KOMPUTER
POLITEKNIK ELEKTRONIKA NEGERI SURABAYA
SURABAYA
3. TUGAS 1
1. Read and display image from file
#include <iostream>
#include <fstream>
#include <Windows.h>
#include "tchar.h"
//#include "stdafx.h"
#include <opencv2opencv.hpp>
#include <opencv2/core/core.hpp>
#include "opencv2/features2d.hpp"
#include "opencv2/calib3d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat image;
image = imread("ricee.png", CV_LOAD_IMAGE_COLOR);
namedWindow("Display window", WINDOW_AUTOSIZE);
imshow("Tugas 1.1", image);
int row = image.rows;
printf("row %dn", row);
int coloumn = image.cols;
printf("coloumn %dn", coloumn);
waitKey();
return EXIT_SUCCESS;
}
4. 2. Write and display image from file
#include <iostream>
#include <fstream>
#include <Windows.h>
#include "tchar.h"
//#include "stdafx.h"
#include <opencv2opencv.hpp>
#include <opencv2/core/core.hpp>
#include "opencv2/features2d.hpp"
#include "opencv2/calib3d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat image;
image = imread("ricee.png", CV_LOAD_IMAGE_COLOR);
int row = image.rows;
printf("row %dn", row);
int coloumn = image.cols;
printf("coloumn %dn", coloumn);
line(image, Point(0,0), Point(image.cols, image.rows), Scalar(0, 0, 100), 5,
8);
imshow("Tugas 1.2", image);
imwrite("result1_2.jpg", image);
waitKey();
return EXIT_SUCCESS;
}
5. 3. Gambar blok persegi putih dengan dimensi piksel (10 x 10)
#include <iostream>
#include <fstream>
#include <Windows.h>
#include "tchar.h"
//#include "stdafx.h"
#include <opencv2opencv.hpp>
#include <opencv2/core/core.hpp>
#include "opencv2/features2d.hpp"
#include "opencv2/calib3d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat image;
image = imread("ricee.png", CV_LOAD_IMAGE_COLOR);
int row = image.rows;
printf("row %dn", row);
int coloumn = image.cols;
printf("coloumn %dn", coloumn);
rectangle(image, Point(103, 123), Point(115, 135), Scalar(255, 255, 255),
FILLED);
imshow("Tugas 1.3", image);
imwrite("result1_3.jpg", image);
waitKey();
return EXIT_SUCCESS;
}
6. 4. Beda Sumber Folder
#include <iostream>
#include <fstream>
#include <Windows.h>
#include "tchar.h"
//#include "stdafx.h"
#include <opencv2opencv.hpp>
#include <opencv2/core/core.hpp>
#include "opencv2/features2d.hpp"
#include "opencv2/calib3d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat image;
image = imread("E:/ricee.png", CV_LOAD_IMAGE_COLOR);
int row = image.rows;
printf("row %dn", row);
int coloumn = image.cols;
printf("coloumn %dn", coloumn);
imshow("Tugas 1.4", image);
imwrite("E:/result1_4.jpg", image);
waitKey();
return EXIT_SUCCESS;
}
7. ANALISA TUGAS 1
Sintaks Mat pict digunakan untuk membuat matriks yang akan menyimpan gambar. Sintaks
yang digunakan untuk menggambar garis, yaitu line(src, Point(koord_x_awal,
koord_y_awal), Point(koord_x_akhir, koord_y_akhir), Scalar(R,G,B), ketebalan_garis).
PERCOBAAN 2
#include <iostream>
#include <fstream>
#include <Windows.h>
#include "tchar.h"
//#include "stdafx.h"
#include <opencv2opencv.hpp>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
VideoCapture cap("video1.mp4");
if (cap.isOpened()== false)
{
cout << "Can't open the video file" << endl;
cin.get();
return - 1;
}
cap.set(CAP_PROP_POS_MSEC, 15000);
double fps = cap.get(CAP_PROP_FPS);
cout << "Frames per seconds : " << fps << endl;
String window_name = "My First Video";
namedWindow(window_name, WINDOW_NORMAL);
while (true)
{
Mat frame;
bool bSuccess = cap.read(frame);
if (bSuccess == false)
{
cout << "Found the end of the video" << endl;
break;
}
imshow(window_name, frame);
if (waitKey(10) == 27)
{
cout << "Esc key is pressed by user. Stopping the video" << endl;
break;
}
}
return 0;
}
8. TUGAS 2
#include <iostream>
#include <fstream>
#include <Windows.h>
#include "tchar.h"
//#include "stdafx.h"
#include <opencv2opencv.hpp>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
VideoCapture cap("video1.mp4");
if (cap.isOpened()== false)
{
cout << "Can't open the video file" << endl;
cin.get();
return -1;
}
cap.set(CAP_PROP_POS_MSEC, 30000);
double fps = cap.get(CAP_PROP_FPS);
cout << "Frames per seconds : " << fps << endl;
String window_name = "My First Video";
namedWindow(window_name, WINDOW_NORMAL);
while (true)
{
Mat frame;
bool bSuccess = cap.read(frame);
if (bSuccess == false)
{
cout << "Found the end of the video" << endl;
9. break;
}
imshow(window_name, frame);
if (waitKey(10) == 27)
{
cout << "Esc key is pressed by user. Stopping the video" << endl;
break;
}
}
return 0;
}
ANALISA TUGAS 2
Untuk mengatur waktu awal video diputar yakni dengan mengganti pada
cap.set(CAP_PROP_POS_MSEC, 30000); untuk detik ke 30 sama dengan 30000 millisecon.
Sehingga ketika diputar pertama kali video akan memulai dari detik ke 30.
PERCOBAAN 3
#include <iostream>
#include <fstream>
10. #include <Windows.h>
#include "tchar.h"
//#include "stdafx.h"
#include <opencv2opencv.hpp>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
VideoCapture cap(0);
if (!cap.isOpened())
{
cout << "Error opening video stream" << endl;
return -1;
}
int frame_width = cap.get(CV_CAP_PROP_FRAME_WIDTH);
int frame_height = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
printf("row %dn", frame_width);
printf("coloumn %dn", frame_height);
VideoWriter video("outcpp.avi", CV_FOURCC('M', 'J', 'P', 'G'), 10,
Size(frame_width, frame_height));
while (1)
{
Mat frame;
cap >> frame;
if (frame.empty())
break;
video.write(frame);
imshow("Frame", frame);
char c = (char)waitKey(1);
if (c == 27)
{
break;
}
}
cap.release();
video.release();
destroyAllWindows();
return 0;
}
11. TUGAS 3
#include <iostream>
#include <fstream>
#include <Windows.h>
#include "tchar.h"
//#include "stdafx.h"
#include <opencv2opencv.hpp>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
VideoCapture cap(0);
if (!cap.isOpened())
{
cout << "Error opening video stream" << endl;
return -1;
}
int frame_width = cap.get(CV_CAP_PROP_FRAME_WIDTH);
int frame_height = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
printf("row %dn", frame_width);
printf("coloumn %dn", frame_height);
VideoWriter video("result 3_1.avi", CV_FOURCC('M', 'J', 'P', 'G'), 10,
Size(frame_width, frame_height));
while (1)
{
Mat frame;
cap >> frame;
if (frame.empty())
break;
video.write(frame);
imshow("Frame", frame);
12. char c = (char)waitKey(1);
if (c == 27)
{
break;
}
}
cap.release();
video.release();
destroyAllWindows();
return 0;
}
ANALISA TUGAS 3
VideoWriter video("result 3_1.avi", CV_FOURCC('M', 'J', 'P', 'G'), 10,
Size(frame_width, frame_height));
Kode yang saya blok kuning merupakan kode yang digunakan untuk menyesuaikan nama
dari video yang sudah di generate ketika program selesai dijalankan.
PERCOBAAN dan TUGAS 4
#include <iostream>
#include <fstream>
#include <Windows.h>
#include "tchar.h"
//#include "stdafx.h"
#include <opencv2opencv.hpp>
#include <windows.h>
using namespace cv;
using namespace std;
int ct = 0;
char tipka;
char filename[100];
int c = 1;
int main(int, char**)
{
13. Mat frame;
VideoCapture cap;
cap.open(0);
int deviceID = 0;
int apiID = cv::CAP_ANY;
cap.open(deviceID + apiID);
if (!cap.isOpened())
{
cout << "ERROR! Unable to open cameran";
return -1;
}
cout << "Start grabbing" << endl
<< "Press a to terminate" << endl;
for (;;)
{
cap.read(frame);
if (frame.empty())
{
cout << "ERROR! blank frame grabbedn";
break;
}
Sleep(1);
imshow("CAMERA 1", frame);
tipka = cv::waitKey(30);
if (tipka == 's')
{
sprintf_s(filename, "E:/trial/trial/result4/Frame_%d.jpg", c);
cv::waitKey(10);
imshow("CAMERA 1", frame);
imwrite(filename, frame);
cout << "Frame_" << c << endl;
c++;
}
if (tipka == 'q')
{
cout << "Terminating..." << endl;
Sleep(10);
break;
}
}
return 0;
}
14. ANALISA TUGAS 4
Pada tugas ini, kode yang digunakan untuk mengambil file capture gambar dan
menyimpannya menggunakan nama_file yang telah ditentukan ketika user melakukan trigger
dengan menekan tombol ‘s’ yakni :
sprintf_s(filename, "E:/trial/trial/result4/Frame_%d.jpg", c);
dimana filename merupakan array yang digunakan untuk menampung nama_file yang
tercapture yang kemudian dikirimkan ke lokasi penyimpanan di computer/laptop, dan ‘c’
merupakan variabel untuk menentukan urutan image yang tercapture. Sedangkan variabel
tipka merupakan variabel yang menampung perintah untuk mengcapture image yang akan
disimpan pada penyimpanan lokal.
TUGAS 5
1. Percobaan Time Frame
#include <iostream>
#include <fstream>
#include <Windows.h>
#include "tchar.h"
//#include "stdafx.h"
#include <stack>
#include <math.h>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/video.hpp"
#include "opencv2/imgcodecs.hpp"
#include <time.h>
//#include <opencv2opencv.hpp>
using namespace cv;
using namespace std;
15. char keyboard;
int main(int argc, char** argv)
{
VideoCapture cap(0);
if (!cap.isOpened())
{
cout << "Cannot open the web cam" << endl;
return -1;
}
while ((char)keyboard != 'q' && (char)keyboard != 27)
{
Mat imgOriginal;
Mat ROOI;
clock_t a = clock();
bool bSuccess = cap.read(imgOriginal);
if (!bSuccess)
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
printf("Capture Time : %fn", double(clock() - a) /
double(CLOCKS_PER_SEC));
imshow("Original", imgOriginal);
if (waitKey(1) == 27)
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}
Time frame merupakan waktu yang dibutuhkan untuk mengcapture 1 image. Kode ini
double(clock() - a) / double(CLOCKS_PER_SEC)) merupakan perintah untuk menghitung
waktu (dalam second) yang dibutuhkan program untuk membuat capture 1 image.
16. 2. Time Frame rata – rata selama 5 detik pertama
3. Hitung frame per second (fps)
#include <iostream>
#include <fstream>
#include <Windows.h>
#include "tchar.h"
//#include "stdafx.h"
#include <stack>
#include <math.h>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/video.hpp"
#include "opencv2/imgcodecs.hpp"
#include <time.h>
//#include <opencv2opencv.hpp>
using namespace cv;
using namespace std;
char keyboard;
int main(int argc, char** argv)
{
VideoCapture cap(0);
if (!cap.isOpened())
{
cout << "Cannot open the web cam" << endl;
return -1;
}
double sum = 0;
double avg = 0;
int countimg = 0;
clock_t a5s = clock();
double fps;
while ((char)keyboard != 'q' && (char)keyboard != 27)
{
Mat imgOriginal;
Mat ROOI;
clock_t a = clock();
bool bSuccess = cap.read(imgOriginal);
if (!bSuccess)
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
double timeFrame = double(clock() - a) / double(CLOCKS_PER_SEC);
double timeFrame_5s = double(clock() - a5s) /
double(CLOCKS_PER_SEC);
if (timeFrame_5s > 0 && timeFrame_5s <= 5)
{
17. printf("Capture Time : %fn", timeFrame);
sum = sum + timeFrame;
countimg++;
}
else if (timeFrame_5s > 5)
{
printf("Total Time : %fn", sum);
printf("Total Data : %dn", countimg);
avg = sum / countimg;
printf("Time Frame Average 5s : %fn", avg);
fps = 1 / avg;
printf("Frame per Second : %fn", fps);
}
imshow("Original", imgOriginal);
if (waitKey(1) == 27)
{
cout << "esc key is pressed by user" << endl;
break;
}
}
system("pause");
}
18. Pada tugas 5 ini, untuk menghitung rata – rata time frame selama 5 sekon pertama dibuat
deklarasi baru untuk clock dan timeframe5s, dan untuk menghitungnya menggunakan rumus
double timeFrame_5s = double(clock() - a5s) / double(CLOCKS_PER_SEC);
Kemudian dari program diatas hanya akan menghitung waktu capture sampai waktu <= 5
sekon dan setelah itu akan menampilkan total waktu di capture beserta jumlah data yang
tercapture. Yang nantinya akan digunakan untuk menghitung rata – rata timeframe 5 sekon
pertama dan FPS nya.
TUGAS 6
#include <iostream>
#include <fstream>
#include <Windows.h>
#include "tchar.h"
#include <opencv2opencv.hpp>
#include <windows.h>
using namespace cv;
using namespace std;
int ct = 0;
char tipka;
char filename[100];
int c = 1;
int main(int, char**)
{
Mat frame;
Mat image[15];
VideoCapture cap;
cap.open(0);
int deviceID = 0;
int apiID = cv::CAP_ANY;
cap.open(deviceID + apiID);
if (!cap.isOpened())
{
cout << "ERROR! Unable to open cameran";
return -1;
}
cout << "Start grabbing" << endl
<< "Press a to terminate" << endl;
for (;;)
{
cap.read(frame);
if (frame.empty())
{
cout << "ERROR! blank frame grabbedn";
break;
}
Sleep(1);
20. Folder image (tugas6) dan video(result6_3) hasil running program
Program ini akan mengambil capture gambar ketika cursor diarahkan pada window
CAMERA 1 dan mendapatkan trigger ‘s’ dari keyboard. Kemudian program akan men-
generate video ketika mendapatkan trigger ‘q’ dari keyboard yang sekaligus menampilkan
jendela WINDOW untuk video yang telah dibuat.
#include <iostream>
#include <fstream>
#include <Windows.h>
#include "tchar.h"
#include <opencv2opencv.hpp>
#include <windows.h>
using namespace cv;
using namespace std;
int ct = 0;
char tipka;
char filename[100];
int c = 1;
int main(int, char**)
{
Mat frame;
Mat image[15];
VideoCapture cap;
21. cap.open(0);
int deviceID = 0;
int apiID = cv::CAP_ANY;
cap.open(deviceID + apiID);
if (!cap.isOpened())
{
cout << "ERROR! Unable to open cameran";
return -1;
}
cout << "Start grabbing" << endl
<< "Press a to terminate" << endl;
//mengambil capture image
for (;;)
{
cap.read(frame);
if (frame.empty())
{
cout << "ERROR! blank frame grabbedn";
break;
}
Sleep(1);
imshow("CAMERA 1", frame);
tipka = cv::waitKey(30);
if (tipka == 's')
{
sprintf_s(filename, "E:/trial/trial/tugas6/Frame_%d.jpg",
c);
cv::waitKey(10);
imshow("CAMERA 1", frame);
imwrite(filename, frame);
cout << "Frame_" << c << endl;
c++;
}
if (tipka == 'q')
{
cout << "Terminating..." << endl;
Sleep(10);
break;
}
}
//membaca capture image dan membuat video result6_3 dari image capture
for (int i = 1; i <= 15; i++)
{
sprintf_s(filename, "E:/trial/trial/tugas6/Frame_%d.jpg", i);
image[i - 1] = imread(filename, CV_LOAD_IMAGE_COLOR);
imshow("Window", image[i - 1]);
waitKey(10);
}
int frame_width = image[0].cols;
22. int frame_height = image[0].rows;
VideoWriter video("result6_3.avi", CV_FOURCC('M', 'J', 'P', 'G'), 3,
Size(frame_width, frame_height));
for (int i = 0; i < 15; i++) {
video.write(image[i]);
}
video.release();
VideoCapture videoFps("E:/trial/trial/result6_3.avi");
cout << "result6_3 fps : " << videoFps.get(CAP_PROP_FPS) << endl;
//membuat video result6_4 yang telah diubah fps nya dari video result6_3
VideoWriter video2("result6_4.avi", CV_FOURCC('M', 'J', 'P', 'G'), 5,
Size(frame_width, frame_height));
for (int i = 0; i < 15; i++) video2.write(image[i]);
video2.release();
VideoCapture video2Fps("E:/trial/trial/result6_4.avi");
cout << "result6_4 fps : " << video2Fps.get(CAP_PROP_FPS) << endl;
system("pause");
}
Untuk merubah fps video result 6_3 dengan menambahkan kode yang di blok warna, dimana
di kode tersebut dibuat looping untuk mengenerate image yang selanjutnya video tersebut di
rilis dan dihitung fps nya menggunakan cara yang sama untuk menghitung fps video
result6_3.