-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsobel.cpp
More file actions
42 lines (40 loc) · 1.09 KB
/
sobel.cpp
File metadata and controls
42 lines (40 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <math.h>
using namespace std;
using namespace cv;
int main(int argc, char const *argv[])
{
VideoCapture cap(0);
if (!cap.isOpened())
{
return -1;
}
namedWindow("image",WINDOW_NORMAL);
int th=0;
createTrackbar("Threshold","image",&th,255);
while (1)
{
Mat a;
cap>>a;
cvtColor(a,a,COLOR_RGB2GRAY);
Mat img=a.clone();
for (int i = 1; i < img.rows-1; i++)
{
for (int j = 1; j < img.cols-1; j++)
{
int sy=((a.at<uchar>(i-1,j-1)*(-1)) + (a.at<uchar>(i-1,j)*(-2)) + (a.at<uchar>(i-1,j+1)*(-1)) + (a.at<uchar>(i+1,j-1)*(1)) + (a.at<uchar>(i+1,j)*(2)) + (a.at<uchar>(i+1,j+1)*(1)));
int sx=((a.at<uchar>(i-1,j-1)*(-1)) + (a.at<uchar>(i,j-1)*(-2)) + (a.at<uchar>(i+1,j-1)*(-1)) + (a.at<uchar>(i-1,j+1)*(1)) + (a.at<uchar>(i,j+1)*(2)) + (a.at<uchar>(i+1,j+1)*(1)));
float val= pow((sx*sx)+(sy*sy),0.5);
if (val>=th)
img.at<uchar>(i,j)=0;
else
img.at<uchar>(i,j)=255;
}
}
imshow("image",img);
waitKey(1);
}
}