OpenCV ⸦ ϴ پ Լ ǰ ֽϴ. Ư cv::Sobel
̳ cv::Scharr
ȭ ϴ ֽϴ. ̷ Լ ̹ ⸦ Ͽ x y 踦 Ÿϴ.
, Sobel Ͽ ⸦ ϴ Դϴ:
#include
using namespace cv;
int main() {
Mat image = imread("image.jpg", IMREAD_GRAYSCALE);
if (image.empty()) {
return -1;
}
Mat grad_x, grad_y;
Mat abs_grad_x, abs_grad_y;
// Sobel ̿Ͽ x y
Sobel(image, grad_x, CV_16S, 1, , 3);
Sobel(image, grad_y, CV_16S, , 1, 3);
// 밪 ȯ
convertScaleAbs(grad_x, abs_grad_x);
convertScaleAbs(grad_y, abs_grad_y);
// ũ
Mat grad;
addWeighted(abs_grad_x, .5, abs_grad_y, .5, , grad);
imshow("Gradient", grad);
waitKey();
return ;
}
Sobel
Լ ̿ ⸦ , 밪 ȯϰ, ⸦ ļ ⸦ ðȭϰ ֽϴ. ̷ ⸦ ֽϴ.
ڱҰ ϴ.
־ (pt) (radian) ̿ ش絵 (pt1) y ǥ Ϸ ﰢԼ ϸ ˴ϴ.
Ϲ ̿ ֽϴ:
[ y_1 = y_ + (x_1 - x_) \cdot \tan(\theta) ]
⼭ ( \tan(\theta) ) ش ⸦ Ÿ, ( y_ ) Ⱑ Ǵ y ǥ Դϴ.
־ pt ˰ , Ư x ǥ y ǥ ֽϴ.
Ʒ ־ (pt) ش絵 x ǥ ϴ y ǥ ϴ C++ ڵ Դϴ:
#include
#include // tan, M_PI
using namespace std;
int main() {
// ־ pt
double pt_x = 2.; // pt x ǥ
double pt_y = 3.; // pt y ǥ
// ־ (radian)
double angle_rad = M_PI / 4; // : 45 (radian)
// pt1 x ǥ
double pt1_x = 4.; // ϴ pt1 x ǥ
// pt1 y ǥ
double pt1_y = pt_y + (pt1_x - pt_x) * tan(angle_rad);
cout << "pt1 y ǥ: " << pt1_y << endl;
return ;
}
ڵ ܰ踦 մϴ:
45(/4 ) شϴ ⸦ Ͽ y ǥ ߽ϴ. Ͻô Ͽ Ͻø ˴ϴ.
˰ , ⸦ ϴ Լ ϸ մϴ. Ư ( \tan ) Լ Ͽ ⸦ ֽϴ.
( m ) ǵ˴ϴ:
[ m = \tan(\theta) ]
⼭ ( \theta ) ()Դϴ. C++ ڵ ⸦ ϴ ϴ:
#include
#include // tan, M_PI
using namespace std;
int main() {
// (: 45 -> /4)
double angle_deg = 45.; //
double angle_rad = angle_deg * (M_PI / 180.); // ȯ
//
double slope = tan(angle_rad);
cout << ": " << slope << endl;
return ;
}
ڵ忡:
45 1 ˴ϴ. ٸ ؼ ⸦ ֽϴ.
( (x_1, y_1) ) ( (x_2, y_2) ) ־ , ϴ ⸦ ϰ, ̸ ֽϴ. ⸦ ũźƮ Լ Ͽ ȯմϴ.
ֽϴ:
[ \text{slope} = m = \frac{y_2 - y_1}{x_2 - x_1} ]
( \theta ) մϴ:
[ \theta = \tan^{-1}(m) ]
C++ ڵ ϸ ϴ:
#include
#include // atan2, M_PI
using namespace std;
int main() {
//
double x1 = 2., y1 = 3.; // ù °
double x2 = 5., y2 = 7.; // °
//
double deltaY = y2 - y1;
double deltaX = x2 - x1;
double slope = deltaY / deltaX;
// (atan2 )
double angle_rad = atan2(deltaY, deltaX);
//
cout << ": " << slope << endl;
cout << "ȿ : " << angle_rad << endl;
return ;
}
ڵ忡:
atan2
Ͽ մϴ. atan2
Լ ( \Delta y ) ( \Delta x ) ڷ شϴ ֹǷ, и鿡 ùٸ ȯϴ ֽϴ.ֽϴ.
( \Delta x = x_2 - x_1) ̶, ̴ x ǥ ٴ ǹմϴ. Ⱑ Ѵ밡 Ǹ, ˴ϴ. ǵ Ƿ, ũźƮ Լ atan2
Ͽ Ư ó ʿմϴ.
ǵ˴ϴ:
, ڵ忡 ó ֽϴ:
#include
#include // atan2, M_PI
using namespace std;
int main() {
//
double x1 = 2., y1 = 3.; // ù °
double x2 = 2., y2 = 7.; // ° (x1 x2 ϴ)
//
double deltaY = y2 - y1;
double deltaX = x2 - x1;
double angle_rad;
if (deltaX == ) {
// x ǥ
if (deltaY > ) {
angle_rad = M_PI / 2; //
} else if (deltaY < ) {
angle_rad = -M_PI / 2; // Ʒ
} else {
angle_rad = ; // ()
}
} else {
// atan2 ̿Ͽ
angle_rad = atan2(deltaY, deltaX);
}
//
cout << "ȿ : " << angle_rad << endl;
return ;
}
ڵ忡:
atan2
Ͽ մϴ.̷ ó 쿡 ϰ ֽϴ.
־ ־ ,⸦ ϴ ϴ. źƮ Ͽ ֽϴ.
ùٸ ϸ ϴ:
[ \text{slope} = m = \tan(\theta) ]
⼭ ( \theta ) ־ ()Դϴ.
C++ ڵ ⸦ ϴ Ʒ ϴ:
#include
#include // tan
using namespace std;
int main() {
// ־ ()
double angle_rad = M_PI / 4; // : /4 (45)
//
double slope = tan(angle_rad);
//
cout << ": " << slope << endl;
return ;
}
ڵ忡:
tan
Լ Ͽ ⸦ մϴ.( \frac{\pi}{4} ) (45) Ⱑ 1 ˴ϴ. ٸ ؼ ⸦ ֽϴ.
˰ ⸦ ˰ ⸦ ٸ ־ ̿ ֽϴ. ̸ ڼ ϰڽϴ.
˰ :
˰ :
:
, ⸦ , ˰ ִ Ǵ ֱ , ٸ Ÿ ֽϴ.
⸦ ϴ ڵ ⸦ ϴ ڵ带 ϴ ϰڽϴ. ڵ ⸦ ϰ, մϴ.
#include
#include // tan, atan2
using namespace std;
int main() {
//
double x1 = 2., y1 = 3.; // ù °
double x2 = 5., y2 = 7.; // °
// κ
double deltaY = y2 - y1;
double deltaX = x2 - x1;
double slope_from_points;
// Ⱑ ǵ ʴ ó
if (deltaX != ) {
slope_from_points = deltaY / deltaX;
} else {
slope_from_points = INFINITY; //
}
//
double angle_rad = atan2(deltaY, deltaX); // κ
double slope_from_angle = tan(angle_rad); // κ
//
cout << " κ : " << slope_from_points << endl;
cout << "κ : " << slope_from_angle << endl;
return ;
}
INFINITY
մϴ.atan2
Լ Ͽ κ մϴ.ڵ ǵ 鿡 ⸦ ֵ մϴ. ٸ Ȯ ֽϴ.