Question

Write a function to "smooth" a black-and-white image by replacing each pixel by the average of...

Write a function to "smooth" a black-and-white image by replacing each pixel by the average of itself and its neighbors. In MATLAB a black-and-white image is just a matrix of 1s and 0s - 1 represents white, and 0 represents black.

To keep the algorithm simple, ignore the edges of the image - that is, do not change the first and last rows and columns.

function name = smooth_image()

input argument = input matrix

output argument = output matrix

The algorithm can be described as follows:

  • Given an NxM input matrix
  • make a copy of the matrix - this will be the output matrix
  • loop over rows 2 to N-1 of the input matrix
  • loop over columns 2 to M-1 of the input matrix
  • take the average of the 3x3 submatrix centered on the current row & column
  • set the corresponding element of the output matrix equal to this average
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here, we have to implement an average filter. Input is an NxM matrix. The approach is to iterate the input matrix using for loop and for every cell in the matrix, iterate the submatrix surrounding it. Calculate the mean of all the values in the submatrix and update the corresponding cell of the output matrix. Following is the MATLAB program :

function output_matrix = smooth_image(input_matrix)

% MatLab function average filter; input is NxM matrix;

% submatrix size is 3x3

[N,M] = size(input_matrix); % find the number of rows and columns of input matrix

output_matrix = input_matrix; % creates copy of the input matrix

for i = 2:N-1 % for loop to iterate input matrix; leaves the values at edges

for j = 2:M-1

sum = 0; % initialize sum to zero

for i1 = i-1:i+1 % for loop to iterate submatrix

for j1 = j-1:j+1

sum = sum + input_matrix(i1,j1); % sum of values in submatrix

end

end

output_matrix(i,j) = floor(sum/9); % update the values of output matrix with average values

% floor function is used to

% keep only the integer part of the average value

% sum value is divided by 9 because size of submatrix is 3x3

end

end

end

Note: This function will work for both whether the values in the input matrix are integers or just 1s and 0s.

Output for integer matrix:

Output for input matrix with 1s and 0s:

Add a comment
Know the answer?
Add Answer to:
Write a function to "smooth" a black-and-white image by replacing each pixel by the average of...
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
  • 4. Write a function extract(pixels, rmin, rmax, cmin, cmax) that takes the 2-D list pixels contai...

    Please design the function in version 3 of python 4. Write a function extract(pixels, rmin, rmax, cmin, cmax) that takes the 2-D list pixels containing pixels for an image, and that creates and returns a new 2-D list that represents the portion of the original image that is specified by the other four parameters. The extracted portion of the image should consist of the pixels that fall in the intersection of the rows of pixels that begin with row rmin...

  • O https//maryash.github.io/135/labs/lab 08 html Since the program should work for all input image...

    c++ solve each task please O https//maryash.github.io/135/labs/lab 08 html Since the program should work for all input images that fit into the array, don't Thard-code" the cat picture dimensions into the program, use variables w and h instead Task D. One-pixethick frame Program frame.cpp Same as the previous task, but it should be a frame exactly one pixel thick Example: Task E. Scale 200% Program scale.cpp Scale the original picture to 200% of its size. It can be done by...

  • 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....

  • 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 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...

  • For this project, each part will be in its oun matlab script. You will be uploading a total 3 m f...

    For this project, each part will be in its oun matlab script. You will be uploading a total 3 m files. Be sure to make your variable names descriptive, and add comments regularly to describe what your code is doing and hou your code aligns with the assignment 1 Iterative Methods: Conjugate Gradient In most software applications, row reduction is rarely used to solve a linear system Ar-b instead, an iterative algorithm like the one presented below is used. 1.1...

  • MATLAB ONLY gauss.jpg BELOW Instructions: The following problems can be done interactively or by writing the...

    MATLAB ONLY gauss.jpg BELOW Instructions: The following problems can be done interactively or by writing the commands iın an M-file (or by a combination of the two). In either case, record all MATLAB input commands and output in a text document and edit it according to the instructions of LAB 1 and LAB 2. For problem 2, include a picture of the rank-1 approximation. For problem 3, include a picture of the rank-10 approximation and for problem 4, include a...

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