1. LAPORAN WORKSHOP KOMPUTER VISI
HISTOGRAM dan SEGMENTASI
LUSIANA DIYAN NINGRUM
2210181051
3 D4 TEKNIK KOMPUTER B
PRODI SARJANA TERAPAN TEKNIK KOMPUTER
DEPARTEMEN TEKNIK INFORMATIKA DAN KOMPUTER
POLITEKNIK ELEKTRONIKA NEGERI SURABAYA
SURABAYA
2. Histogram Gray Scale Image dari Sebuah File
#include <opencv2opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
Mat frame, gray_frame;
frame = imread("histo3.JPG", CV_LOAD_IMAGE_COLOR); // Read the file
"image.jpg".
cvtColor(frame, gray_frame, COLOR_BGR2GRAY);
imshow("Gray Scale Image", gray_frame);
/// Establish the number of bins
int histSize = 256;
/// Set the ranges
float range[] = { 0, 256 };
const float* histRange = { range };
bool uniform = true; bool accumulate = false;
Mat b_hist;
/// Compute the histograms:
calcHist(&gray_frame, 1, 0, Mat(), b_hist, 1, &histSize, &histRange,
uniform, accumulate);
// Draw the histograms
int hist_w = 512; int hist_h = 400;
int bin_w = cvRound((double)hist_w / histSize);
Mat histImage(hist_h, hist_w, CV_8UC3, Scalar(0, 0, 0));
/// Normalize the result to [ 0, histImage.rows ]
normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
/// Draw Histogram
for (int i = 1; i < histSize; i++)
{
line(histImage, Point(bin_w*(i - 1), hist_h -
cvRound(b_hist.at<float>(i - 1))),
Point(bin_w*(i), hist_h -
cvRound(b_hist.at<float>(i))),
Scalar(255, 0, 0), 2, 8, 0);
}
/// Display
imshow("Histogram of Gray Scale Image", histImage);
waitKey();
return EXIT_SUCCESS;
}
3. Image1
Image2
Image 3
Analisa
Berdasarkan hasil grafik image dapat diketahui bahwa semakin banyak bagian terang pada
suatu gambar, grafik akan cenderung tinggi di sebelah kanan, dan semakin banyak elemen
gelap dalam suatu gambar maka grafik akan cederung tinggi di sisi kiri. Hal ini dikarena
grafik di sebelah kiri merupakan warna hitam yang semakin ke kanan akan condong ke warna
putih, sehingga ketika suatu gambar memiliki banyak bagian yang cerah maka gambar
tersebut banyak memiliki elemen berwarna putih, sebaliknya gambar yang condong berwarna
gelap memiliki banyak elemen berwarna hitam.
4. Histogram Gray Scale Image dari Livecam
#include <opencv2opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
VideoCapture cap(0);
if (cap.isOpened() == false)
{
cout << "Cannot open the video file" << endl;
cin.get();
return -1;
}
while (1)
{
Mat frame, gray_frame;
cap >> frame;
cap.read(frame);
cvtColor(frame, gray_frame, COLOR_BGR2GRAY);
char c = (char)waitKey(1);
if (c == 27)
break;
/// Establish the number of bins
int histSize = 256;
/// Set the ranges
float range[] = { 0, 256 };
const float* histRange = { range };
bool uniform = true; bool accumulate = false;
Mat b_hist;
/// Compute the histograms:
calcHist(&gray_frame, 1, 0, Mat(), b_hist, 1, &histSize, &histRange,
uniform, accumulate);
// Draw the histograms
int hist_w = 512; int hist_h = 400;
int bin_w = cvRound((double)hist_w / histSize);
Mat histImage(hist_h, hist_w, CV_8UC3, Scalar(0, 0, 0));
/// Normalize the result to [ 0, histImage.rows ]
normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
/// Draw Histogram
for (int i = 1; i < histSize; i++)
{
line(histImage, Point(bin_w*(i - 1), hist_h -
cvRound(b_hist.at<float>(i - 1))),
Point(bin_w*(i), hist_h -
cvRound(b_hist.at<float>(i))),
Scalar(255, 0, 0), 2, 8, 0);
}
/// Display
namedWindow("calcHist Demo", CV_WINDOW_AUTOSIZE);
imshow("calcHist Demo", histImage);
imshow("CAMERA", frame);
}
waitKey();
return EXIT_SUCCESS;
}
5. Analisa
Ketika kamera menangkap suatu gambar yang memiliki intensitas yang rendah (0,0,0) maka grafik
akan mengalami kenaikan dari sisi kiri dan semakin terang gambar tersebut maka angka kenaikan
grafik akan semakin ke kanan. Berdasarkan hasil percobaan ini, dapat diketahui bahwa intensitas
cahaya pada gambar cenderung terang tetapi gambar juga menangkap objek yang berwarna gelap
sehingga grafik menunjukkan kenaikan di sisi kiri dan kenaikan grafik yang signifikan berada di
sebelah kanan karena gambar memiliki banyak bagian yang cenderung terang sehingga data yang
terbaca oleh grafik mendekati kea rah intensitas yang tinggi sekitar 255.
Histogram RGB Image dari Livecam
#include <opencv2opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
VideoCapture cap(0);
if (cap.isOpened() == false)
{
cout << "Cannot open the video file" << endl;
cin.get();
return -1;
}
while (1)
{
Mat frame;
cap >> frame;
char c = (char)waitKey(1);
if (c == 27)
break;
/// Separate the image in 3 places ( B, G and R )
6. vector<Mat> bgr_planes;
split(frame, bgr_planes);
/// Establish the number of bins
int histSize = 256;
/// Set the ranges ( for B,G,R) )
float range[] = { 0, 256 };
const float* histRange = { range };
bool uniform = true; bool accumulate = false;
Mat b_hist, g_hist, r_hist;
/// Compute the histograms:
calcHist(&bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRange,
uniform, accumulate);
calcHist(&bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange,
uniform, accumulate);
calcHist(&bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRange,
uniform, accumulate);
// Draw the histograms for B, G and R
int hist_w = 512; int hist_h = 400;
int bin_w = cvRound((double)hist_w / histSize);
Mat histImage(hist_h, hist_w, CV_8UC3, Scalar(0, 0, 0));
/// Normalize the result to [ 0, histImage.rows ]
normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
/// Draw for each channel
for (int i = 1; i < histSize; i++)
{
line(histImage, Point(bin_w*(i - 1), hist_h -
cvRound(b_hist.at<float>(i - 1))),
Point(bin_w*(i), hist_h -
cvRound(b_hist.at<float>(i))),
Scalar(255, 0, 0), 2, 8, 0);
line(histImage, Point(bin_w*(i - 1), hist_h -
cvRound(g_hist.at<float>(i - 1))),
Point(bin_w*(i), hist_h -
cvRound(g_hist.at<float>(i))),
Scalar(0, 255, 0), 2, 8, 0);
line(histImage, Point(bin_w*(i - 1), hist_h -
cvRound(r_hist.at<float>(i - 1))),
Point(bin_w*(i), hist_h -
cvRound(r_hist.at<float>(i))),
Scalar(0, 0, 255), 2, 8, 0);
}
/// Display
namedWindow("calcHist Demo", CV_WINDOW_AUTOSIZE);
imshow("calcHist Demo", histImage);
imshow("CAMERA", frame);
}
waitKey();
return EXIT_SUCCESS;
}
7. Analisa
Komposisi warna pada gambar tersebut menunjukkan jika grafik mengalami kenaikan pada
intensitas yang rendah (0,0,0) karena pada gambar yang terbaca oleh kamera menangkap objek
yang berwarna gelap, sehingga grafik RGB mengalami kenaikan yang cukup tinggi yang berarti
bahwa pada intensitas tersebut mengalami percampuran ketiga warna yang cukup tinggi untuk
menghasilkan objek berwarna gelap. Sama halnya dengan sisi lain dari gambar tersebut yang
memiliki intensitas yang tinggi yakni mendekati nilai 255 karena camera menangkap objek yang
berwarna terang dengan environment lighting yang cukup bagus sehingga grafik mulai mengalami
kenaikan pada nilai tengah yang berada di sekitar intensitas 128 yang berarti bahwa percampuran
warna dominan putih atau warna cerah cukup tinggi untuk menghasilkan sisi bagian yang berwarna
terang pada gambar. Sehingga, ketika kamera menangkap objek kertas yang berwarna putih, grafik
akan mengalami kenaikan yang tinggi pada intensitas nilai 255 sedangkan pada intensitas yang
rendah (0,0,0) mengalami kelandaian grafik.
Histogram RGB Image dari Image
#include <opencv2opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
Mat frame, gray_frame;
frame = imread("histo3.JPG", CV_LOAD_IMAGE_COLOR); // Read the file "image.jpg".
imshow("RGB Image", frame);
/// Separate the image in 3 places ( B, G and R )
vector<Mat> bgr_planes;
split(frame, bgr_planes);
/// Establish the number of bins
int histSize = 256;
/// Set the ranges
9. Image2
Image3
Analisa
Grafik pada image1 menunjukkan kenaikan yang tinggi pada sisi kiri karena gambar yang memiliki
warna gelap sehingga percampuran warna RGB pada intensitas rendah cukup tinggi untuk
menghasilkan warna gelap tersebut. Seperti halnya pada hasil pembacaan image2 yang memiliki
sisi gelap tetapi ada sedikit dari gambar yang memiliki warna terang sehingga yang tergambar di
grafik cukup merata yakni pada nilai intensitas 0 – 128, hal ini juga terlihat pada grafik hasil
pembacaan image3 dimana gambar tersebut memilik warna yang terang sehingga grafik
menunjukkan kenaikan pada intensitas 128 – 255 yang berarti bahwa perpaduan warna RGB
dengan intensitas 128 – 255 cukup tinggi untuk menghasilkan gambar yang berwarna terang
tersebut. Hal ini terbukti pada hasil pembacaan grafik biru yang mengalami kenaikan paling tinggi
pada intensitas 255 karena gambar memiliki warna terang yang menunjukkan objek langit dengan
warna biru.
Histogram Equalization
#include <opencv2opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
10. using namespace std;
using namespace cv;
using std::cout;
int main(int argc, char** argv)
{
//memulai capture video
VideoCapture cap(0);
if (!cap.isOpened())
return -1;
//mendapatkan nilai dimensi video, disimpan dalam variabel, ditampilkan
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);
for (;;)
{
Mat frame; Mat hsv;
cap >> frame;
imshow("Frame", frame);
if (waitKey(30) >= 0)
break;
cap.read(frame);
cvtColor(frame, frame, COLOR_BGR2GRAY);
Mat dst;
equalizeHist(frame, dst);
imshow("Source image", frame);
imshow("Equalized Image", dst);
if (frame.empty())
{
cout << "ERROR blank framen";
break;
}
/// Establish the number of bins
int histSize = 256;
/// Set the ranges
float range[] = { 0, 256 };
const float* histRange = { range };
bool uniform = true; bool accumulate = false;
Mat hist;
/// Compute the histograms:
calcHist(&frame, 1, 0, Mat(), hist, 1, &histSize, &histRange, uniform,
accumulate
);
// Draw the histograms
int hist_w = 512; int hist_h = 400;
int bin_w = cvRound((double)hist_w / histSize);
Mat histImage(hist_h, hist_w, CV_8UC3, Scalar(0, 0, 0));
/// Normalize the result to [ 0, histImage.rows ]
normalize(hist, hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
/// Draw for channel
for (int i = 1; i < histSize; i++)
{
line(histImage, Point(bin_w*(i - 1), hist_h - cvRound(hist.at<float>(i
- 1))),
Point(bin_w*(i), hist_h - cvRound(hist.at<float>(i))),
Scalar(255, 255, 255), 2, 8, 0);
}
/// Display
namedWindow("calcHist Demo", CV_WINDOW_AUTOSIZE);
11. imshow("calcHist Demo", histImage);
}
return 0;
}
Analisa
Pada gambar histogram equalization jarak graylevel memiliki rentang antaranilai warna satu dengan
lainnya terdapat jarak yang cukup lebar yaitu artinya semakinsering/ intensitas greylevel yang
muncul itu tinggi. Histogram Equalization bertujuan mengubah pemetaan greylevel agar
sebarannya lebih menyebar pada kisaran 0 - 255, sehingga setiap derajat keabuan memiliki jumlah
pixel yang relatif sama.
Histogram Equalization Greyscale Mode Livecamera
#include <opencv2opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/imgproc/imgproc.hpp"
12. #include <iostream>
using namespace std;
using namespace cv;
using std::cout;
int sourceHist(Mat frame) {
/// Establish the number of bins
int histSize = 256;
/// Set the ranges
float range[] = { 0, 256 };
const float* histRange = { range };
bool uniform = true; bool accumulate = false;
Mat histsrc;
/// Compute the histograms:
calcHist(&frame, 1, 0, Mat(), histsrc, 1, &histSize, &histRange,
uniform, accumulate);
// Draw the histograms
int hist_w = 512; int hist_h = 400;
int bin_w = cvRound((double)hist_w / histSize);
Mat histSrc(hist_h, hist_w, CV_8UC3, Scalar(0, 0, 0));
/// Normalize the result to [ 0, histSrc.rows ]
normalize(histsrc, histsrc, 0, histSrc.rows, NORM_MINMAX, -1, Mat());
/// Draw Histogram
for (int i = 1; i < histSize; i++)
{
line(histSrc, Point(bin_w * (i - 1), hist_h -
cvRound(histsrc.at<float>(i - 1))),
Point(bin_w * (i), hist_h -
cvRound(histsrc.at<float>(i))),
Scalar(255, 255, 255), 2, 8, 0);
}
namedWindow("Source histogram", CV_WINDOW_AUTOSIZE);
imshow("Source histogram", histSrc);
return 0;
}
int main(int argc, char** argv)
{
//memulai capture video
VideoCapture cap(0);
if (!cap.isOpened())
return -1;
//mendapatkan nilai dimensi video, disimpan dalam variabel, ditampilkan
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);
for (;;)
{
Mat frame; Mat hsv;
cap >> frame;
//imshow("Frame", frame); //gambar asli
if (waitKey(30) >= 0)
break;
cap.read(frame);
cvtColor(frame, frame, COLOR_BGR2GRAY);
Mat dst;
equalizeHist(frame, dst);
imshow("Source image", frame); //gray scale
13. imshow("Equalized Image", dst); //equalized
if (frame.empty())
{
cout << "ERROR blank framen";
break;
}
/// Establish the number of bins
int histSize = 256;
/// Set the ranges
float range[] = { 0, 256 };
const float* histRange = { range };
bool uniform = true; bool accumulate = false;
Mat hist; //srchist;
/// Compute the histograms:
calcHist(&dst, 1, 0, Mat(), hist, 1, &histSize, &histRange, uniform,
accumulate);
//calcHist(&frame, 1, 0, Mat(), srchist, 1, &histSize, &histRange, uniform,
accumulate);
// Draw the histograms
int hist_w = 512; int hist_h = 400;
int bin_w = cvRound((double)hist_w / histSize);
Mat histImage(hist_h, hist_w, CV_8UC3, Scalar(0, 0, 0));
//Mat histSrc(hist_h, hist_w, CV_8UC3, Scalar(0, 0, 0));
/// Normalize the result to [ 0, histImage.rows ]
normalize(hist, hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
//normalize(srchist, srchist, 0, histSrc.rows, NORM_MINMAX, -1, Mat());
/// Draw for channel
for (int i = 1; i < histSize; i++)
{
line(histImage, Point(bin_w * (i - 1), hist_h -
cvRound(hist.at<float>(i - 1))),
Point(bin_w * (i), hist_h - cvRound(hist.at<float>(i))),
Scalar(255, 255, 255), 2, 8, 0);
}
/// Display
namedWindow("calcHist Demo", CV_WINDOW_AUTOSIZE);
imshow("calcHist Demo", histImage);
sourceHist(frame);
}
return 0;
}
14. Analisa
Histogram equalization merupakan metode perbaikan kualitas citra yang bertujuan untuk meratakan
persebaran nilai intensitas piksel suatu citra. Hal ini terlihat pada grafik dari gambar asli yang
belum mengalamu ekualisasi, persebaran nilai piksel nya masih terfokus pada rentang nilai 0 – 128
karena objek yang tertangkap kamera memiliki intensitas cahaya yang cukup serta adanya obje
yang berwarna gelap. Tetapi ketika sudah mengalami ekualifikasi pada gambar, data pembacaan
grafik menunjukkan persebarang nilai intensitas piksel yang merata dari rentang 0 – 255 yang
berarti bahwa gambar yang telah mengalami ekualifikasi mendapatkan perbaikan kualitas dari citra
image sehingga persebaran nilai intensitas piksel pada image tersebut lebih merata.
Histogram Equalization RGB dari Original livecam
#include <opencv2opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int RGB_grafik(Mat frame) {
Mat b_hist, g_hist, r_hist;
int histSize = 256;
/// Set the ranges
float range[] = { 0, 256 };
const float* histRange = { range };
bool uniform = true; bool accumulate = false;
/// Separate the image in 3 places ( B, G and R )
vector<Mat> bgr_planes;
split(frame, bgr_planes);
calcHist(&bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform,
accumulate);
calcHist(&bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform,
accumulate);
calcHist(&bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform,
accumulate);
15. // Draw the histograms for B, G and R
int hist_w = 512; int hist_h = 400;
int bin_w = cvRound((double)hist_w / histSize);
Mat histImage(hist_h, hist_w, CV_8UC3, Scalar(0, 0, 0));
/// Normalize the result to [ 0, histImage.rows ]
normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
/// Draw for each channel
for (int i = 1; i < histSize; i++)
{
line(histImage, Point(bin_w*(i - 1), hist_h -
cvRound(b_hist.at<float>(i - 1))),
Point(bin_w*(i), hist_h -
cvRound(b_hist.at<float>(i))),
Scalar(255, 0, 0), 2, 8, 0);
line(histImage, Point(bin_w*(i - 1), hist_h -
cvRound(g_hist.at<float>(i - 1))),
Point(bin_w*(i), hist_h -
cvRound(g_hist.at<float>(i))),
Scalar(0, 255, 0), 2, 8, 0);
line(histImage, Point(bin_w*(i - 1), hist_h -
cvRound(r_hist.at<float>(i - 1))),
Point(bin_w*(i), hist_h -
cvRound(r_hist.at<float>(i))),
Scalar(0, 0, 255), 2, 8, 0);
}
namedWindow("RGB histogram", CV_WINDOW_AUTOSIZE);
imshow("RGB histogram", histImage);
return 0;
}
int main(int argc, char* argv[])
{
//int RGB_grafik(frame);
//Open the video file for reading
VideoCapture cap(0);
// if not success, exit the program
if (cap.isOpened() == false)
{
cout << "Cannot open the video file" << endl;
cin.get(); //wait for any key press
return -1;
}
/// Establish the number of bins
int histSize = 256;
/// Set the ranges
float range[] = { 0, 256 };
const float* histRange = { range };
bool uniform = true; bool accumulate = false;
Mat hist;
//Define the names of windows
String windowNameOfOriginalImage = "Original Video";
String windowNameOfHistogramEqualized = "Histogram Equalized Video";
// Create windows with the above names
namedWindow(windowNameOfOriginalImage, WINDOW_NORMAL);
namedWindow(windowNameOfHistogramEqualized, WINDOW_NORMAL);
while (true)
{
16. Mat frame;
bool bSuccess = cap.read(frame); // Read a new frame from the video file
//Breaking the while loop at the end of the video
if (bSuccess == false)
{
cout << "Found the end of the video" << endl;
break;
}
//Convert the frame from BGR to YCrCb color space
Mat hist_equalized_image;
cvtColor(frame, hist_equalized_image, COLOR_BGR2YCrCb);
//Split the image into 3 channels; Y, Cr and Cb channels respectively and
store it in a std::vector
vector<Mat> vec_channels;
split(hist_equalized_image, vec_channels);
//Equalize the histogram of the Y channel
equalizeHist(vec_channels[0], vec_channels[0]);
//Merge 3 channels in the std::vector to form the color image in YCrCB color
space.
merge(vec_channels, hist_equalized_image);
//Convert the histogram equalized image from YCrCb to BGR color space again
cvtColor(hist_equalized_image, hist_equalized_image, COLOR_YCrCb2BGR);
Mat b_hist, g_hist, r_hist;
calcHist(&vec_channels[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRange,
uniform, accumulate);
calcHist(&vec_channels[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange,
uniform, accumulate);
calcHist(&vec_channels[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRange,
uniform, accumulate);
// Draw the histograms
int hist_w = 512; int hist_h = 400;
int bin_w = cvRound((double)hist_w / histSize);
Mat histImage(hist_h, hist_w, CV_8UC3, Scalar(0, 0, 0));
/// Normalize the result to [ 0, histImage.rows ]
normalize(hist, hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
for (int i = 1; i < histSize; i++)
{
line(histImage, Point(bin_w*(i - 1), hist_h -
cvRound(b_hist.at<float>(i - 1))),
Point(bin_w*(i), hist_h -
cvRound(b_hist.at<float>(i))),
Scalar(255, 0, 0), 2, 8, 0);
line(histImage, Point(bin_w*(i - 1), hist_h -
cvRound(g_hist.at<float>(i - 1))),
Point(bin_w*(i), hist_h -
cvRound(g_hist.at<float>(i))),
Scalar(0, 255, 0), 2, 8, 0);
line(histImage, Point(bin_w*(i - 1), hist_h -
cvRound(r_hist.at<float>(i - 1))),
Point(bin_w*(i), hist_h -
cvRound(r_hist.at<float>(i))),
Scalar(0, 0, 255), 2, 8, 0);
}
17. //show frames in the created windows
imshow(windowNameOfOriginalImage, frame);
imshow(windowNameOfHistogramEqualized, hist_equalized_image);
imshow("calcHist Demo", histImage);
RGB_grafik(frame);
if (waitKey(5) == 27)
{
cout << "Esc key is pressed by the user. Stopping the video" << endl;
break;
}
}
destroyAllWindows(); //Destroy all opened windows
return 0;
}
Analisa
Melihat tujuan dari histogram equalization yang merupakan metode perbaikan kualitas citra untuk
meratakan persebaran nilai intensitas piksel suatu citra. Hal ini terlihat pada grafik dari gambar asli
dari livecam yang belum mengalami ekualisasi, persebaran nilai piksel RGB nya masih terfokus
pada rentang nilai 0 – 128 karena objek yang tertangkap kamera memiliki intensitas cahaya yang
cukup serta adanya objek yang berwarna gelap sehingga perpaduan warna RGB nya juga berada
pada rentang nilai 0 – 128. Tetapi ketika sudah mengalami ekualifikasi pada gambar, data
pembacaan grafik menunjukkan persebarang nilai intensitas piksel yang merata dari rentang 0 – 255
yang berarti bahwa gambar yang telah mengalami ekualifikasi mendapatkan perbaikan kualitas dari
citra image sehingga persebaran nilai intensitas piksel pada image tersebut lebih merata. Terutama
pada warna biru yang memiliki persebaran merata dari rentang nilai 0 – 255 dibandingkan
persebaran warna merah dan hijau yang masih berada pada rentang nilai 128.
18. SEGMENTASI
1. Histogram RGB Image dari Image File
#include <opencv2opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
Mat frame, gray_frame;
frame = imread("book2.JPG", CV_LOAD_IMAGE_COLOR); // Read the file "image.jpg".
//imshow("RGB Image", frame);
/// Separate the image in 3 places ( B, G and R )
vector<Mat> bgr_planes;
split(frame, bgr_planes);
/// Establish the number of bins
int histSize = 256;
/// Set the ranges
float range[] = { 0, 256 };
const float* histRange = { range };
bool uniform = true; bool accumulate = false;
Mat b_hist, g_hist, r_hist;
/// Compute the histograms:
calcHist(&bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRange,
uniform, accumulate);
calcHist(&bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange,
uniform, accumulate);
calcHist(&bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRange,
uniform, accumulate);
// Draw the histograms for B, G and R
int hist_w = 512; int hist_h = 400;
int bin_w = cvRound((double)hist_w / histSize);
Mat histImage(hist_h, hist_w, CV_8UC3, Scalar(0, 0, 0));
/// Normalize the result to [ 0, histImage.rows ]
normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
/// Draw Histogram
for (int i = 1; i < histSize; i++)
{
line(histImage, Point(bin_w*(i - 1), hist_h -
cvRound(b_hist.at<float>(i - 1))),
Point(bin_w*(i), hist_h -
cvRound(b_hist.at<float>(i))),
Scalar(255, 0, 0), 2, 8, 0);
line(histImage, Point(bin_w*(i - 1), hist_h -
cvRound(g_hist.at<float>(i - 1))),
Point(bin_w*(i), hist_h -
19. cvRound(g_hist.at<float>(i))),
Scalar(0, 255, 0), 2, 8, 0);
line(histImage, Point(bin_w*(i - 1), hist_h -
cvRound(r_hist.at<float>(i - 1))),
Point(bin_w*(i), hist_h -
cvRound(r_hist.at<float>(i))),
Scalar(0, 0, 255), 2, 8, 0);
}
/// Display
imshow("RGB Histogram Image", histImage);
imshow("Original Image", frame);
waitKey();
return EXIT_SUCCESS;
}
2. Hasil gambar intensitas R > 125
#include <opencv2opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
Mat frame, gray_frame;
frame = imread("book2.JPG", CV_LOAD_IMAGE_COLOR); // Read the file "image.jpg".
/// Separate the image in 3 places ( B, G and R )
vector<Mat> bgr_planes;
split(frame, bgr_planes);
/// Establish the number of bins
int histSize = 256;
/// Set the ranges
float range[] = { 125, 256 };
const float* histRange = { range };
bool uniform = true; bool accumulate = false;
20. Mat b_hist, g_hist, r_hist;
/// Compute the histograms:
calcHist(&bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRange,
uniform, accumulate);
// Draw the histograms for B, G and R
int hist_w = 512; int hist_h = 400;
int bin_w = cvRound((double)hist_w / histSize);
Mat histImage(hist_h, hist_w, CV_8UC3, Scalar(0, 0, 0));
/// Normalize the result to [ 0, histImage.rows ]
normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());
/// Draw Histogram
for (int i = 1; i < histSize; i++)
{
line(histImage, Point(bin_w*(i - 1), hist_h -
cvRound(r_hist.at<float>(i - 1))),
Point(bin_w*(i), hist_h -
cvRound(r_hist.at<float>(i))),
Scalar(0, 0, 255), 2, 8, 0);
}
/// Display
imshow("RGB Histogram Image", histImage);
imshow("Original Image", frame);
waitKey();
return EXIT_SUCCESS;
}
Analisa
Segmentasi merupakan proses pemisahan latar depan – latar belakang suatu image. Untuk case
pada soal ini dimana kami diminta untuk menampilkan grafik dari warna merah yang memiliki nilai
intensitas R lebih dari 125, sehingga pada variabel array yang digunakan untuk menampung nilai
dari warna diubah dengan nilai pada rentang 126 – 255 yang pada grafik menghasilkan klaster
warna merah yang memiliki intensitas pada rentang 126 – 255 dengan kerapatan yang cukup stabil.
Hal ini dikarenakan pada pemetaan warna citra dari image masukan dikelompokkan sesuai dengan
kesamaan warna yang dimiliki oleh objek pada gambar. Setiap piksel citra dikonversi dalam suatu
garis vector RGB untuk selanjutnya ditampilkan berdasarkan rata – rata dari kelompok warna yang
dihasilkan.
21. Menampilkan Warna Merah Saja dari Image
#include <opencv2opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
Mat frame;
frame = imread("book2.JPG", CV_LOAD_IMAGE_COLOR);
/// Separate the image in 3 places ( B, G and R )
vector<Mat> bgr_planes;
split(frame, bgr_planes);
/*
Mat redOnly, mask;
Mat hsv_image;
cvtColor(frame, hsv_image, COLOR_BGR2HSV);
inRange(hsv_image, Scalar(0, 120, 70), Scalar(10, 255, 255), mask);
*/
Mat g, output;
g = Mat::zeros(Size(frame.cols, frame.rows), CV_8UC1);
// Showing Red Channel
// G and B channels are kept as zero matrix for visual perception
vector<Mat> channels;
channels.push_back(g);
channels.push_back(g);
channels.push_back(bgr_planes[2] > 125);
/// Merge the three channels
merge(channels, output);
namedWindow("Red", 1);
///Converting image from BGR to HSV color space.
Mat hsv_image;
cvtColor(frame, hsv_image, COLOR_BGR2HSV);
/// Creating masks to detect the upper and lower red color.
Mat mask1, mask2, red_output;
inRange(hsv_image, Scalar(0, 40, 30), Scalar(10, 255, 255), mask1);
//inRange(hsv_image, Scalar(130, 120, 120), Scalar(180, 255, 255), mask2);
// Generating the final mask
//mask1 = mask1 + mask2;
bitwise_and(frame, frame, red_output, mask1);
/// Establish the number of bins
int histSize = 256;
/// Set the ranges ( for B,G,R) )
float range[] = { 0, 256 };
const float* histRange = { range };
bool uniform = true; bool accumulate = false;
23. Segmentasi pada suatu citra ( image segmentation) merupakan langkah awal pada proses analisa
citra yang bertujuan untuk mengambil informasi yang terdapat di dalam suatu citra. Segmentasi
citra membagi suatu citra ke dalam bagian-bagian atau objek-objek tertentu. Sampai sejauh
mana pembagian tersebut dilakukan tergantung pada masalah yang dihadapi. Idealnya, langkah
segmentasi tersebut dihentikan pada saat objek yang diinginkan sudah berhasil dipisahkan. Pada
kasus diatas, dari sampul buku terlebih dahulu dipisahkan dengan trasholding untuk mengetahui
dan memisahkan warna merah. Setelah warna merah sudah dapat diketahui selanjutnya bagian yang
selain warna merah dihitankan atau dihapus.