Question

In: Computer Science

(Using Matlab) Write a function to "smooth" a black-and-white image by replacing each pixel by the...

(Using Matlab) 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

Solutions

Expert Solution

function f=smooth(arrIn)

%copy the matrix
y=arrIn;

side1 = size(y)(1,1); %determine the row of the matrix
side2 = size(y)(1,2); %determine the column of the matrix

t = ones(3)/9;   % create a smoothing filter .. it is matrix of ones divided by 9 to give average value

for m = 2:side1-1 %traverse the matrix from row 2 to previous to the end row
    for n=2:side2-1 % traverse the matrix from column 2 to previous to the end of column
    mat = arrIn(m-1:m+1,n-1:n+1); % get the 3x3 matrix to be processed
    y(m,n)=round(conv2(mat,t)(3,3)); % convolution is done between the matrices and centre value which represents the mean value is taken and put back into y matrix
   end

end

f=y; % y matrix returned

end

=====================================================

example matrix and its smoothed matrix

===================================================

N.B: convolution of two matrix is simply multiplying the given matrix by the other matrix while shifting the other matrix over the first matrix.

So for each location in the matrix, that location is aligned with the center of the other matrix and respective elements are multiplied together and summed.

Here in this case the other matrix is 3x3, so when 3x3 matrix convoluted with 3x3 matrix it will give 5x5 matrix. element (3,3) will be the one needed because it will be the one where each of the respective element will be multiplied with each other and summed up..


Related Solutions

Using MATLAB (a) Write a computer program capable of zooming and shrinking an image by pixel...
Using MATLAB (a) Write a computer program capable of zooming and shrinking an image by pixel replication. Assume that the desired zoom/shrink factors are integers.
detect skin region for the input image, using the following condition for each pixel: R >...
detect skin region for the input image, using the following condition for each pixel: R > 95 and G > 40 and B>20 and max{R,G,B} - min{R,G,B} >15 and |R-G| > 15 and R>G and R>B please solve the problem using matlab code
An image is partitioned into two regions, one white and the other black. A reading taken...
An image is partitioned into two regions, one white and the other black. A reading taken from a randomly chosen point in the white section will be normally distributed with μ=4 and σ2=4, whereas one taken from a randomly chosen point in the black region will have a normally distributed reading with parameters (6, 9). A point is randomly chosen on the image and has a reading between 4 and 5. What is the probability that the point is in...
write a Matlab function file to solve system Ax=b by using the output of the function...
write a Matlab function file to solve system Ax=b by using the output of the function lufac2a your function should have inputs f=matrix return from lufac2a, piv=array return by lufac2a and b=right hand side of your system.the only output for your system should be x guideline 1.use the column access for the matrix entries 2. do not create any other matrix in your function-get your data directly from the matrix passed into your function 3.do not use Matlab command designed...
write a matlab function for frequency analysis using DFT. the function should take as input a...
write a matlab function for frequency analysis using DFT. the function should take as input a signal, and as output the number of sinusoids and their frequencies.
Write a function using MATLAB that will accept a structure as an argument and return two...
Write a function using MATLAB that will accept a structure as an argument and return two cell arrays containing the names of the fields of that structure and the data types of each field. Be sure to check that the input argument is a structure, and generate an error message if it is not.
(Using Matlab) and "while" function 1.   Write a program that prompts the User for if they...
(Using Matlab) and "while" function 1.   Write a program that prompts the User for if they would like to enter a real number. If yes, prompt the User for the real number. Continue to do this until the User enters “no” to the first question. After the User enters “no”, display the average of all the numbers entered. (Using Matlab) and "while" function 2.   Write a program that prompts the User for if they would like to enter a real...
how to write a function that clears every pixel and sets it to the Red color....
how to write a function that clears every pixel and sets it to the Red color. similarly, we have to open the image including height and width and use while loop that accesses each pixel in Python
1- Write a Matlab program to perform image resizing, image rotation to 90 degrees, conversion to...
1- Write a Matlab program to perform image resizing, image rotation to 90 degrees, conversion to binary and gray scale, Image segmentation and extraction, draw and find the region of interest with a circle, Plot bounding box, Region extraction and histogram. 2- Write a MATLAB program to Design a GUI calculator which is as shown in below figure 1. Add tool bar options to the GUI calculator and Highlight specific codes written in program and signify the steps followed.
The language is MATLAB Write a function that will generate three random integers, each in the...
The language is MATLAB Write a function that will generate three random integers, each in the inclusive range from 10 to 80. It will then return a string consisting of the three integers joined together, and also a character vector consisting of the three integers joined together. For example, if the random integers are 11, 29, and 76, the string that is returned will be "112976" and the character vector that is returned will be '112976'. I'm really confused on...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT