Professional Documents
Culture Documents
Binary Processing
Binary Processing
Binary Processing
• Threshold
• Histogram
• Pixel manipulation
Nếu như một ảnh số được mã hóa bằng 24bit, nghĩa là 8bit cho kênh R, 8bit cho kênh
G, 8bit cho kênh B, thì mỗi kênh màu này sẽ nhận giá trị từ 0-255. Với mỗi giá trị khác
nhau của các kênh màu kết hợp với nhau ta sẽ được một màu khác nhau, như vậy ta
sẽ có tổng cộng 255x255x255 = 1.66 triệu màu sắc.
Mat C = (Mat_<double>(3,3) << 0, -1, 0, -1, 5, -1, 0, -1, 0); cout << "C = " << endl << " " << C << endl << endl;
if(dist>threshold)
{
foregroundMask.at<unsigned char>(j,i) = 255;
}
}
Robotic & Vision Lab RoVis
Ví dụ 2.2: a) Đọc giá trị RGB tại điểm nhấn chuột phải (vùng mắt) của hình sau
b) Lưu lại 4 giá trị nhấn chuột đó
0 1 2 3
0 2 0 0
Như vậy: cường độ 0 có 4 pixel
cường độ 1 có 2 pixel 2 4 5 3
cường độ 2 có 2 pixel
1 5 5 4
cường độ 3 có 2 pixel
cường độ 4 có 2 pixel
cường độ 5 có 3 pixel
+ void calcHist(const Mat* images, int nimages, const int* channels, InputArray mask, OutputArray hist, int dims, const int*
histSize, const float** ranges, bool uniform=true, bool accumulate=false )
+ void normalize(InputArray src, OutputArray dst, double alpha=1, double beta=0, int norm_type=NORM_L2, int dtype=-1,
InputArray mask=noArray() )
return 0;
return 0;
}
#include "opencv2/highgui.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argv, char** argc)
{
//Original file
Mat original = imread("threshold.png", CV_LOAD_IMAGE_GRAYSCALE);
//Modified file
Mat modify = imread("threshold.png", CV_LOAD_IMAGE_GRAYSCALE);
//Accessing row
for (int i = 0; i < modify.rows; i++)
{
//Accessing column
for (int k = 0; k < modify.cols; k++)
{
//Pixel with value from 10 to 40
if (modify.at<uint8_t>(i, k) < 210
&& modify.at<uint8_t>(i, k) > 190)
modify.at<uint8_t>(i, k) = 0; //Will be white
else
modify.at<uint8_t>(i, k) = 255; //Other will be black
}
}
imshow("Original", original);
imshow("Theshold", modify);
waitKey(0);
1 1 2 2 3 3 4 4 5 5 1 1 2 2 3 3 4 4 5 5
1 1 2 2 8 3 4 4 5 5 1 1 2 2 8 3 4 4 5 5
1 1 2 7 8 9 4 4 5 5 1 1 2 7 8 9 4 4 5 5
1 1 6 7 8 9 10 4 5 5 1 1 6 7 8 9 10 4 5 5
1 5 6 7 8 9 10 11 5 5 Split 1 5 6 7 8 9 10 11 5 5
1 5 6 7 8 9 10 11 5 5
1 5 6 7 8 9 10 11 5 5
1 5 6 7 3 9 10 11 5 5
1 5 6 7 3 9 10 11 5 5
1 5 6 2 3 3 10 11 5 5
1 5 6 2 3 3 10 11 5 5
1 5 2 2 3 3 4 11 5 5
1 5 2 2 3 3 4 11 5 5
1 1 2 2 3 3 4 4 5 5
1 1 2 2 3 3 4 4 5 5
1 1 2 2 8 3 4 4 5 5
1 1 2 7 8 9 4 4 5 5
1 1 6 7 8 9 10 4 5 5
1 5 6 7 8 9 10 11 5 5
1 5 6 7 8 9 10 11 5 5
1 5 6 7 3 9 10 11 5 5
1 5 6 2 3 3 10 11 5 5
1 5 2 2 3 3 4 11 5 5
0 1 1 1 1 1 1 2 2 3 3 4 4 5 5 1 1 1 0 0
0 1 1 1 0 1 1 1 0 0
0 1 1 0 0 Thresholding Thresholding 0 1 1 0 0
0 1 0 0 0 T=4 T=7 0 0 1 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 1 1 1 0 0 0 0
0 0 1 1 1 1 1 0 0 0
0 1 1 1 1 1 1 1 0 0
0 1 1 1 1 1 1 1 0 0
0 1 1 1 0 1 1 1 0 0
0 1 1 0 0 0 1 1 0 0
0 1 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 0 0
0 1 1 1 0 1 1 1 0 0
merge merge
0 1 1 0 0 0 1 1 0 0
0 1 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 0 0
out.at<uchar>(cv::Point(x-4,y-4)) = 255;
}else{
out.at<uchar>(cv::Point(x-4,y-4));
}
}
}
}
t=87 t=100
int iLowH = 0;
int iHighH = 179;
int iLowS = 0;
int iHighS = 255;
int iLowV = 0;
int iHighV = 255;
while (true)
{
Mat imgOriginal;
Mat imgHSV;
cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV); //Convert the captured frame from BGR to HSV
Mat imgThresholded;
if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed,
break loop
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}
Robotic & Vision Lab RoVis
Tìm 2 màu đỏ và xanh lá cây trong hình cùng một lúc
if(dist>threshold)
{
foregroundMask.at<unsigned char>(j,i) = 255;
}
}
Robotic & Vision Lab RoVis
Ví dụ 10.3: lọc RGB hình người và chèn vào hình nền
cvSetZero(extractedBackgroundImage);
cvCopy(backgroundImage, extractedBackgroundImage, maskImage);
cvNot(maskImage, inverseMaskImage);
cvSetZero(extractedObjectImage);
cvCopy(objectImage, extractedObjectImage, inverseMaskImage);
cvAdd(extractedBackgroundImage, extractedObjectImage, destinationImage, NULL);
cvShowImage(windowNameExtract, extractedObjectImage);
cvShowImage(windowresultadd, destinationImage);