Professional Documents
Culture Documents
SPATIAL Bai1
SPATIAL Bai1
SPATIAL Bai1
BÀI 1:
Kernel
1 2 3 4
8 3 10 11 1 2 3
2 1 2 18 4 5 6
22 23 24 2 7 8 9
a)
Phương pháp: Thêm một hàng 0 cột 0 vào hình
0 0 0 0 0 0
0 1 2 3 4 0
0 8 3 10 11 0
0 2 1 2 18 0
0 22 23 24 2 0
0 0 0 0 0 0
a[0;1] = 2+2.3+5.8+6.3+8.2+9 = 91
a[1;1] = 1+ 2.2+3.3+8.4+3.5+6.10+2.7+8+3.9 = 170
a[2;1] = 2.1+3.2+4.3+3.4+5.10+6.11+7+8.3+18.9 = 341
a[3;1] = 3+4.2+10.4+11.5+3.7+18.8 = 271
b)
Kết quả giống câu a)
c)
Kernel
1 2 3 4
8 3 10 11 1 2 3
2 1 2 18 4 5 6
22 23 24 2 7 8 9
9 8 7
6 5 4
3 2 1
a[0;0] = 5+4.2+2.8+3 = 32
a[1;0] = 6+5.2+4.3+3.8+2.3+10 = 68
a[2;0] = 6.2+5.3+4.4+3.3+2.10+11 = 83
a[3;0] = 6.3+5.4+10.3+2.11 = 90
a[0;1] = 8+7.2+5.8+4.3+2.2+1= 79
a[1;1] = 9+2.8+3.7+8.6+3.5+10.4+2.3+2+3 = 160
a[2;1] = 2.9+3.8+4.7+3.6+10.5+11.4+3+3.2+18 = 209
a[3;1] = 3.9+4.8+6.10+11.5+3.3+18.2 = 219
32 68 83 90
79 160 209 219
166 331 396 362
225 390 425 325
d)
Kết quả giống câu c)
Code chương trình
#include <iostream>
#include <stdio.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgcodecs/imgcodecs.hpp>
#include <opencv2/opencv.hpp>
float src_data[4][4] = {
{1 , 2 , 3 , 4},
{8 , 3 , 10 , 11},
{2 , 1 , 3 , 18},
{22 , 23 , 24 , 2}
};
Mat src = Mat(4, 4, CV_32F, src_data);
float Kernel_data[3][3] = {
{1 , 2 , 3 },
{4 , 5 , 6 },
{7 , 8 , 9}
};
Mat Kernel = Mat(3, 3, CV_32F, Kernel_data);
int main()
{
//convolution
for (int i = 0; i < src.rows; i++)
{
for (int j = 0; j < src.cols; j++)
{
float value = 0;
for (int m = 0; m < Kernel.rows; m++)
{
for (int n = 0; n < Kernel.cols; n++)
{
int xposition = i - m + (Kernel.rows - 1) /
2;
int yposition = j - n + (Kernel.cols - 1) /
2;
if (xposition >= 0 && xposition < src.rows &&
yposition >= 0 && yposition < src.cols)
{
value += src.at<float>(xposition,
yposition)*Kernel.at<float>(m, n);
cout << " src = " <<
src.at<float>(xposition, yposition) << endl;
cout << " Kernel = " <<
Kernel.at<float>(m, n) << endl;
waitKey(0);
}
}
}
output3.at<float>(i, j) = value;
value = 0;
}
}
return 0;
}