Question

I need help with the edge detection code. Filter: Edge Detection Edge detection is a technique...

I need help with the edge detection code.

Filter: Edge Detection Edge detection is a technique that creates an image that looks like a pencil sketch, by changing the pixels' colours to black or white. A simple algorithm for performing edge detection is: for every pixel that has a pixel below it, check the contrast between the two pixels. If the contrast is high, change the top pixel's colour to black, but if the contrast is low, change the top pixel's colour to white. For the bottom row (which has no pixels below it), simply set the pixel to white. One way to calculate the contrast between two colours is to calculate the brightness of the top pixel (the average of the red, green and blue components, with all three components weighted equally), and subtract the brightness of the pixel below it. We then calculate the absolute value of this difference. If this absolute value is greater than a threshold value, the contrast between the two pixels is high, so we change the top pixel's colour to black; otherwise, the contrast between the two pixels is low, so we change the top pixel's colour to white. Develop a filter named detect_edges that returns a copy of an image, in which the copy has been modified using the edge detection technique described in the preceding paragraphs. This filter has two parameters: an image and a threshold, which is a positive number. Before developing your filter, sketch some cases where the pixel will be changed to black or to white (e.g. sketch a few pairs of squares representing pairs of pixels, with the R/G/B values shown in each; show the pixel values before and after applying the filter logic).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

About Edge Detection:

Edge detection is an image processing technique for finding the boundaries of objects within images. It works by detecting discontinuities in brightness. Edge detection is used for image segmentation and data extraction in areas such as image processing, computer vision, and machine vision.

Approach:
For edge detection, we take the help of convolution: Convolution = I * m where I is the image, m is the mask and * is convolutional operator. To perform convolution on an image following steps are required:

  1. Flip the mask horizontally and then vertically. This will result in 180-degree rotation of an image.
  2. Slide the mask onto the image such that every pixel in the image coincides with the center of the mask atleast once.
  3. Multiply the corresponding elements with the pixel values below it and then add them.
  4. Repeat this procedure until all pixel values of the image have been calculated for updation.

Now, we will take 3×3 mask for the same.

3×3 Mask for vertical edges : [1, 0, -1; 1, 0, -1; 1, 0, -1]
3×3 Mask for horizontal edges : [1, 0, -1; 1, 0, -1; 1, 0, -1]
3×3 Mask for principal diagonal edges : [1, 0, -1; 1, 0, -1; 1, 0, -1]
3×3 Mask for secondary diagonal edges : [1, 0, -1; 1, 0, -1; 1, 0, -1]

We’ll find these edges separately and finally combine them using max function or mean function, but max function is more accurate for this.

Matlab Code for vertical edges:filter_none

brightness_4

I=double((imread('image1.jpg')); %read image

In=I;      %copy image

mask=[1, 0, -1;1, 0, -1;1, 0, -1];

%Rotate image by 180 degree first flip up to down then left to right

mask=flipud(mask);

mask=fliplr(mask);

for i=2:size(I, 1)-1

    for j=2:size(I, 2)-1

        %multiplying mask value with the corresponding image pixel value

        neighbour_matrix=mask.*In(i-1:i+1, j-1:j+1);

        avg_value=sum(neighbour_matrix(:));

        I(i, j)=avg_value;

    end

end

figure, imshow(uint8(I));


Matlab code for horizontal edges:

filter_none

brightness_4

I=double((imread('image1.jpg'));

In=I;

mask=[1, 1, 1;0, 0, 0;-1, -1, -1];

mask=flipud(mask);

mask=fliplr(mask);

for i=2:size(I, 1)-1

    for j=2:size(I, 2)-1

        neighbour_matrix=mask.*In(i-1:i+1, j-1:j+1);

        avg_value=sum(neighbour_matrix(:));

        I(i, j)=avg_value;

    end

end

figure, imshow(uint8(I));

Matlab code for principal diagonal edges:

filter_none

brightness_4

I=double((imread('image1.jpg'));

In=I;

mask=[0, -1, -1;1, 0, -1;1, 1, 0];

mask=flipud(mask);

mask=fliplr(mask);

for i=2:size(I, 1)-1

    for j=2:size(I, 2)-1

        neighbour_matrix=mask.*In(i-1:i+1, j-1:j+1);

        avg_value=sum(neighbour_matrix(:));

        I(i, j)=avg_value;

    end

end

figure, imshow(uint8(I));

Matlab Code for secondary diagonal edges:

filter_none

brightness_4

I=double((imread('image1.jpg'));

In=I;

mask=[1, 1, 1;0, 0, 0;-1, -1, -1];

mask=flipud(mask);

mask=fliplr(mask);

for i=2:size(I, 1)-1

    for j=2:size(I, 2)-1

        neighbour_matrix=mask.*In(i-1:i+1, j-1:j+1);

        avg_value=sum(neighbour_matrix(:));

        I(i, j)=avg_value;

    end

end

figure, imshow(uint8(I));

Final Matlab code for edge detection:

filter_none

brightness_4

I=double(imread('image1.jpg'));

In=I;

mask1=[1, 0, -1;1, 0, -1;1, 0, -1];

mask2=[1, 1, 1;0, 0, 0;-1, -1, -1];

mask3=[0, -1, -1;1, 0, -1;1, 1, 0];

mask4=[1, 1, 0;1, 0, -1;0, -1, -1];

mask1=flipud(mask1);

mask1=fliplr(mask1);

mask2=flipud(mask2);

mask2=fliplr(mask2);

mask3=flipud(mask3);

mask3=fliplr(mask3);

mask4=flipud(mask4);

mask4=fliplr(mask4);

for i=2:size(I, 1)-1

    for j=2:size(I, 2)-1

        neighbour_matrix1=mask1.*In(i-1:i+1, j-1:j+1);

        avg_value1=sum(neighbour_matrix1(:));

        neighbour_matrix2=mask2.*In(i-1:i+1, j-1:j+1);

        avg_value2=sum(neighbour_matrix2(:));

        neighbour_matrix3=mask3.*In(i-1:i+1, j-1:j+1);

        avg_value3=sum(neighbour_matrix3(:));

        neighbour_matrix4=mask4.*In(i-1:i+1, j-1:j+1);

        avg_value4=sum(neighbour_matrix4(:));

        %using max function for detection of final edges

        I(i, j)=max([avg_value1, avg_value2, avg_value3, avg_value4]);

    end

end

figure, imshow(uint8(I));

Add a comment
Know the answer?
Add Answer to:
I need help with the edge detection code. Filter: Edge Detection Edge detection is a technique...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • WRITE A C++ PROGRAM TO ANSWER THE QUESTIONS BELOW. You are provided with an image in...

    WRITE A C++ PROGRAM TO ANSWER THE QUESTIONS BELOW. You are provided with an image in a new file format called JBC Here is an example of the format: 2 3 100 50 40 200 66 56 12 45 65 23 67 210 0 255 100 45 32 123 The first line means that this is a 2 (width) by 3 (height) image of pixels. Then, each line represents the color of a pixel. The numbers are from 0 to...

  • Image proccessing in PROGRAMING C NO MAIN FUNCTION NEEDED! Color-Filter an image: 1. All pixels in...

    Image proccessing in PROGRAMING C NO MAIN FUNCTION NEEDED! Color-Filter an image: 1. All pixels in the picture with color in the chosen range will be replaced with new color. The following shows the pseudo code for the color filter. if (R in the range of [target_r - threshold, target_r + threshold]) and (G in the range of [target_g - threshold, target_g + threshold]) and (B in the range of [target_b - threshold, target_b + threshold]) R = replace_r ;...

  • We’re sure you've seen old black-and-white (actually, grayscale) photos that, over time, have gained a yellowish...

    We’re sure you've seen old black-and-white (actually, grayscale) photos that, over time, have gained a yellowish tint. We can mimic this effect by creating sepia-tinted images. There are several different ways to sepia-tint an image. In one of the simplest techniques, the image is first converted to grayscale, and then each pixel's red and blue components are adjusted, leaving the green component unchanged. Here's the algorithm: · First, we convert the image to grayscale, because old photographic prints were grayscale....

  • from PIL import Image import random # NOTE: Feel free to add in any constant values...

    from PIL import Image import random # NOTE: Feel free to add in any constant values you find useful to use BLACK = (0, 0, 0) WHITE = (255, 255, 255) # NOTE: Feel free to add in any helper functions to organize your code but # do NOT rename any existing functions (or else, autograder # won't be able to find them!!) # NOTE: The following function is already completed for you as an example # You can use...

  • the picture above is the one you are supposed to use for the MATlab code please...

    the picture above is the one you are supposed to use for the MATlab code please help Problems. Grayscale images are composed of a 2D matrix of light intensities from O (Black) to 255 (White). In this lab you will be using grayscale images and do 2D-array operations along with loops and iflelse branches. 1. We can regard 2D grayscale images as 2D arrays. Now load the provided image of scientists at the Solvay Conference in 1927 using the imread)...

  • the following python code edits BMP image file to negative but I need help with modifying...

    the following python code edits BMP image file to negative but I need help with modifying this code so it turns it the same BMP image file into a black and white instead of a negative. heres python code I have so far [pasted below] ## # This program processes a digital image by creating a negative of a BMP image. # from io import SEEK_CUR from sys import exit def main() : filename = input("Please enter the file name:...

  • The ACME Manufacturing Company has hired you to help automate their production assembly line. Cameras have...

    The ACME Manufacturing Company has hired you to help automate their production assembly line. Cameras have been placed above a conveyer belt to enables parts on the belt to be photographed and analyzed. You are to augment the system that has been put in place by writing C code to detect the number of parts on the belt, and the positions of each object. The process by which you will do this is called Connected Component Labeling (CCL). These positions...

  • Hi. I require your wonderful help with figuring out this challenging code. import java.awt.Color; import java.awt.image.BufferedImage;...

    Hi. I require your wonderful help with figuring out this challenging code. import java.awt.Color; import java.awt.image.BufferedImage; import java.io.*; import javax.imageio.ImageIO; public class ImageLab { /* * This is the grayscale example. Use it for inspiration / help, but you dont * need to change it. * * Creates and returns a new BufferedImage of the same size as the input * image. The new image is the grayscale version of the input (red, green, and * blue components averaged together)....

  • 1. Fill out the following table by indicating which general technique (light microscopy (LM) or electron...

    1. Fill out the following table by indicating which general technique (light microscopy (LM) or electron microscopy (EM]) could be used to observe each structure or phenomenon. Put "no" in the box if the technique could not be used. If light microscopy can be used, name one technique (bright-field, phase-contrast, fluorescence, etc.) that you think would be effective. You will find some useful information in Appendix 1 of this manual and Chapter 18 of your textbook. Structure or phenomenon Could...

  • please help me make this into a contradiction or a direct proof please. i put the question, my answer, and the textbook i used. thank you also please write neatly proof 2.5 Prove har a Sim...

    please help me make this into a contradiction or a direct proof please. i put the question, my answer, and the textbook i used. thank you also please write neatly proof 2.5 Prove har a Simple sraph and 13 cdges cannot be bipartite CHint ercattne gr apn in to ertex Sets and Court tne忤of edges Claim Splitting the graph into two vertex, Sets ves you a 8 Ver ices So if we Change tne书 apn and an A bipartite graph...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT