
Số hiệu: BM1/QT-PĐBCL-RĐTV Trang 1/5
TRƯỜNG ĐẠI HỌC SƯ PHẠM KỸ THUẬT
THÀNH PHỐ HỒ CHÍ MINH
KHOA CƠ KHÍ CHẾ TẠO MÁY
BỘ MÔN CƠ ĐIỆN TỬ
-------------------------
ĐÁP ÁN CUỐI KỲ HK I NĂM HỌC 2018-2019
Môn: XỬ LÝ ẢNH CÔNG NGHIỆP
Mã môn học: IIPR422529
-------------------------------
Câu 1: (3đ)
Xây dựng và cài đặt thuật toán cân bằng histogram tự động. (2đ)
Tại sao cân bằng histogram lại làm cho ảnh đẹp hơn? (1đ)
Đáp án:
Mục đích của cân bằng histogram là làm cho histogram đồng đều. Khi đó ta làm tăng được độ
tương phản của ảnh.
Cân bằng histogram được cho bằng phương trình:
s=T(r)=(L-1)
r
rdwwp
0
)(
với pr(w) : Xác suất xảy ra mức xám w
Trong xác suất, tích phân của hàm mật độ là hàm phân phối. Công thức trên có w là biến liên tục,
ta không thể lập trình nó. Ta phải dùng công thức rời rạc:
sk=T(rk)=(L-1)
k
j
jr rp
0
)(
với k= 0,1,2,…,L-1
void HistogramEqualization(Mat imgin, Mat imgout)
{
int M = imgin.size().height;
int N = imgin.size().width;
int x, y;
int r, h[L];
for (r=0; r<L; r++)
h[r] = 0;
for (x=0; x<M; x++)
for (y=0; y<N; y++) {
r = imgin.at<uchar>(x,y);
h[r]++;
}
double p[L];
for (r=0; r<L; r++)
p[r] = (double)h[r]/(M*N);
double s[L];
int j, k;
for (k=0; k<L; k++) {
s[k] = 0;
for (j=0; j<=k; j++)
s[k] += p[j];

Số hiệu: BM1/QT-PĐBCL-RĐTV Trang 2/5
s[k] *= L-1;
}
for (x=0; x<M; x++)
for (y=0; y<N; y++) {
r = imgin.at<uchar>(x,y);
imgout.at<uchar>(x,y) = (uchar)s[r];
}
return;
}
Câu 2: (4đ)
Xây dựng và cài đặt thuật toán loại bỏ các hạt gạo nhỏ hơn 90% hạt gạo lớn nhất, tức là
trong ảnh chỉ còn lại những hạt gạo lớn.
Đáp án:
void RemoveSmallRice(Mat imgin, Mat imgout)
{
// B1
Mat w1 = getStructuringElement(MORPH_ELLIPSE, Size(81, 81));
morphologyEx(imgin, imgout, MORPH_TOPHAT, w1);
// B2
threshold(imgout, imgout, 50, 255, THRESH_BINARY | THRESH_OTSU);
medianBlur(imgout, imgout, 3);
// B3
int M = imgout.size().height;
int N = imgout.size().width;
int x, y, dem = 0;
int r;
int color = 150;
for (x = 0; x < M; x++)
for (y = 0; y < N; y++)
{
r = imgout.at<uchar>(x, y);
if (r == L - 1)
{
floodFill(imgout, Point(y, x), CV_RGB(color, color, color));
dem++;
color++;
}
}

Số hiệu: BM1/QT-PĐBCL-RĐTV Trang 3/5
// B4
int a[L];
for (r = 0; r < L; r++)
a[r] = 0;
for (x = 0; x < M; x++)
for (y = 0; y < N; y++)
{
r = imgout.at<uchar>(x, y);
if (r > 0)
a[r]++;
}
int max = 0;
for (r=0; r<L; r++)
if (a[r] > max)
max = a[r];
max = (int)(max*0.9);
// B5
for (r = 0; r<L; r++)
if (a[r] < max)
a[r] = 0;
dem = 0;
for (r = 0; r < L; r++)
if (a[r] > 0)
dem++;
for (x = 0; x < M; x++)
for (y = 0; y < N; y++)
{
r = imgout.at<uchar>(x, y);
if (a[r] == 0)
imgout.at<uchar>(x, y) = 0;
else
imgout.at<uchar>(x, y) = L - 1;
}
char s[5];
sprintf(s, "%d", dem);
putText(imgout, s, Point(0, 30), CV_FONT_HERSHEY_SIMPLEX, 0.8, CV_RGB(255, 255,
255));
return;
}
Câu 3: (1.5đ)
Given the deep learning convolutional neural networks shown in the following figure:

Số hiệu: BM1/QT-PĐBCL-RĐTV Trang 4/5
a. Determine the number of parameters of filters (do not include the bias node) in the
convolutional layers. (0.75 points)
b. Determine the number of weights of the fully connected layer. (0.75 points)
Answer:
a.
Convolution 1: 5x5x32 = 800
Convolution 2: 5x5x32x64 = 51.200
Sum: 800 + 51.200 = 52.000 parameters
b.
7x7x64 = 3.132
3.132x1024 = 3.211.264
1.024x10 = 10.240
Sum: 3.211.264 + 10.240 = 3.221.504 weights
Câu 4: (1.5đ)
Given the input image and the filter shown in the following figure:

Số hiệu: BM1/QT-PĐBCL-RĐTV Trang 5/5
a. Determine the size of the output image if zero-padding is 0 and stride is 2. (0.5
points)
b. Compute the output image as the convolution of the input image and the filter. (1.0
points)
Answer:
a. Wout = (W-F+2P)/S + 1 = (7-3+2*0)/2 + 1 = 3, therefore the size of output image
is 3x3
b.
108
126
144
288
306
324
468
486
504
-----------------------HẾT-------------------
Ngày 14 tháng 1 năm 2019
Giáo viên
Trần Tiến Đức

