Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<opencv2/imgproc/imgproc.hpp>
- #include<opencv2/highgui/highgui.hpp>
- using namespace std;
- using namespace cv;
- // To compute edge detection in an image, we are in need of gradients that include the Sobel kernel values. These can be created as functions taking
- // arguements of x and y values. Those values need to come from an image, therefore we need a Matrix image - Mat
- int xGradient(Mat image, int x, int y)
- {
- return
- image.at<uchar>(y - 1, x - 1) +
- 2 * image.at<uchar>(y, x - 1) +
- image.at<uchar>(y + 1, x - 1) -
- image.at<uchar>(y - 1, x + 1) -
- 2 * image.at<uchar>(y, x + 1) -
- image.at<uchar>(y + 1, x + 1);
- }
- int yGradient(Mat image, int x, int y)
- {
- return
- image.at<uchar>(y - 1, x - 1) +
- 2 * image.at<uchar>(y - 1, x) +
- image.at<uchar>(y - 1, x + 1) -
- image.at<uchar>(y + 1, x - 1) -
- 2 * image.at<uchar>(y + 1, x) -
- image.at<uchar>(y + 1, x + 1);
- }
- int main()
- {
- // In order to load images to the program, we need variables of the type Mat
- // For this code we create two Mat (Matrix) variables to load store the source image into, as well as a cloned image we'll use to
- // perform computations in the program.
- // Calculating the final image
- // To get the final image at the end, we need to add the absolute value of the two gradients together.
- // To do this, we create two variables which we can call the gradient functions we create at the top.
- // Since we're adding it together, we get a sum. Therefore we also need to store the sum in a variable, which will be of type Int
- Mat src, clone;
- int gradientXAxis, gradientYAxis;
- int sum = 0;
- // Load an image
- // Initialize the src variable to the source image we wish to work with, which takes parameters of the system path to the image
- // Second variable is which color spectrum the image should be loaded into. 0 = Greyscale image. 1 = Colour image.
- // We want a greyscale image for this project, therefore the int is set to 0. It could also be initialized to CV_LOAD_IMAGE_GRAYSCALE
- // Which does the same
- // Cloning image
- // If we wish to show the source image and the edge enhanced image, we clone the source image to a new Mat variable, which clones all the
- // information. The clone we can then perform the desired actions and show the edge enhanced image. That's why we initialize the clone image to
- // a clone of the source image
- src = imread("C:/Pictures/building.jpg", 0);
- clone = src.clone();
- imshow("initial", src);
- // Checks if data is present and it is not empty - Then execute the code.
- // Executes two for loops to access every pixel in the image
- if (src.data && !src.empty())
- {
- for (int y = 1; y < src.rows - 1; y++){
- for (int x = 1; x < src.cols - 1; x++){
- // Initialize gradientXAxis to the xGradient function values
- gradientXAxis = xGradient(src, x, y);
- // Initialize gradientYAxis to the yGradient function values
- gradientYAxis = yGradient(src, x, y);
- // Calculate absolute value of both gradient functions store it to the sum variable
- sum = abs(gradientXAxis) + abs(gradientYAxis);
- // If statement to threshold the image, which we'll need to make it a binary image with only black and white values,
- // where the edges are white and the rest is black
- // If the sum is equal to or above 100, set the pixel value to 255. Else, set pixel value to 0, or black
- if (sum >= 125) {
- clone.at<uchar>(y, x) = 255;
- }
- else {
- clone.at<uchar>(y, x) = 0;
- }
- }
- }
- }
- imshow("final", clone);
- waitKey(0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement