SPATIAL Bai1

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

STT: 2

BÀI TẬP SPATIAL OPERATOR

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;0] = 5+2.6+8.8+9.3 = 108


a[1;0] = 4+5.2+6.3+7.8+3.8+9.10 = 202
a[2;0] = 4.2+3.5+4.6+7.3+8.10+9.11=247
a[3;0] = 3.4+4.5+10.7+11.8 = 190

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

a[0;2] = 2.8+3.3+5.2+6+8.22+9.23 = 424


a[1;2] = 8+2.3+10.3+4.2+5+6.3+22.7+23.8+24.9 = 629
a[2;2] = 3+10.2+11.3+4+5.3+6.18+7.23+8.24+9.2 = 554
a[3;2] = 10+2.11+3.4+18.5+24.7+2.8=318
a[0;3] = 2.2+3+22.5+23.6=255
a[1;3] = 2+2+3.3+22.5+23.6 = 255
a[2;3] = 1+2.3+18.3+23.4+24.5+2.6= 285
a[3;3] = 3+18.2+24.4+2.5= 145
Kết quả:

108 202 247 190


91 170 341 271
424 629 554 318
255 360 285 145

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

Kernel sau khi filpped x,y

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

a[0;2] = 8.8+3.7+5.2+4+22.2+23 = 166


a[1;2] = 8.9+3.8+10.7+2.6+5+3.4+22.3+23.2+24 = 331
a[2;2] = 3.9+10.8+11.7+6+3.5+18.4+23.3+24.2+2 = 396
a[3;2] = 10.9+11.8+6.3+18.5+24.3+2.2 = 362

a[0;3] = 2.8+7+22.5+23.4 = 225


a[1;3] = 2.9+8+3.7+22.6+23.5+24.4 = 390
a[2;3] = 9+3.8+18.7+23.6+24.5+2.4 = 425
a[3;3] = 3.9+18.8+24.6+2.5 = 325
Kết quả:

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>

using namespace cv;


using namespace std;

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()
{

Mat output1, output2, output3, output4, output5, output6;

output1 = cv::Mat::zeros(4, 4, CV_32F);


output2 = cv::Mat::zeros(4, 4, CV_32F);
output3 = cv::Mat::zeros(4, 4, CV_32F);
output4 = cv::Mat::zeros(4, 4, CV_32F);
output5 = cv::Mat::zeros(4, 4, CV_32F);
output6 = cv::Mat::zeros(4, 4, CV_32F);
//cross-correlation
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);
}
}
}
output1.at<float>(i, j) = value;
value = 0;
}
}

//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;
}
}

cout << "Cross_Correlation bang tay : " << endl;


cout << output1 << endl;
cout << "Cross-Corelation voi Fillter 2D result: " << endl;
filter2D(src, output2, src.depth(), Kernel, Point(-1, -1), 0, 0);
cout << output2 << endl;
cout << "Convolution bang tay : " << endl;
cout << output3 << endl;
cout << "Ma tran sau flipped theo hang: " << endl;
flip(Kernel, output4, 0);
cout << output4 << endl;
cout << "Ma tran sau flipped theo chieu cot: " << endl;
flip(output4, output5,1);
cout << output5 << endl;
filter2D(src, output6, src.depth(), output5, Point(-1, -1), 0, 0);
cout << "Convolution " << endl << output6 << endl;

return 0;
}

You might also like