Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Hi can any expert help me with Matlab? You can just use pikachu.png as filename. Write a function program called replaceColor (and remember, the filename
Hi can any expert help me with Matlab? You can just use pikachu.png as filename.
Write a function program called replaceColor (and remember, the filename should match the function name). This function should take an image and replace all the pixels of a given color with a new color. The function should take the following 4 inputs: im, colorold, colorNew, tolerance. It should output an RGB array for the modified image. - im (the image to be modified) can be an RGB array of numeric values (both uint8 and doubles should work), or a character vector giving the path/filename of an image file, or a string giving the path/filename of an image file. Make sure your function can handle all of those situations. - If im is neither a 3-dimensional numeric array, nor a character array, nor a string, then throw an error with the following message: First input must be a filename (as character vector or string) or an RGB array. - colorold is the color to be replaced. Your function should accept either an RGB triplet or (for a grayscale tone) a scalar. For instance, 0 should be interpreted as [00], meaning black. - If there is no second input, or if the user enters empty brackets [] for the second input, use a default color of white. - If colorold is not numeric, or doesn't have 1 or 3 elements, throw an error with the following message: Old color must be a numeric scalar (for grayscale) or a 3-value numeric vector. - colorNew is the color that will replace the old color. Your function should accept either an RGB triplet or (for a grayscale tone) a scalar. For instance, 0 should be interpreted as [000], meaning black. - If there is no third input, or if the user enters empty brackets [] for the third input, use a default color of black. - If colorNew is not numeric, or doesn't have 1 or 3 elements, throw an error with the following message: New color must be a numeric scalar (for grayscale) or a 3-value numeric vector. - tolerance is the "plus-or-minus" to allow for the RGB values in colorold. For instance, if colorold is [200 100150] and tolerance is [20 2 20], then the color should be replaced at every pixel with a red value in the range 180-220, a green value in the range 98-102, and a blue value in the range 130-170. Your function should also allow tolerance to be a scalar, in which case the same value is used for all three color channels (e.g., 20 should be interpreted as [20 2020] ). - If there is no fourth input, or if the user enters empty brackets [] for the fourth input, use a default tolerance of [00], meaning only pixels whose color exactly matches colorold will be replaced. - If tolerance is not numeric, or doesn't have 1 or 3 elements, throw an error with the following message: Tolerance must be a numeric scalar or a 3-value numeric vector. - The output should be a uint8 RGB array-unless the original inputted image was of class 'double', in which case the output should be of class 'double' also. HINT: After defining the replacement mask, it may be easiest to perform the replacement one page at a time. EXAMPLE: I uploaded a file called pikachu . pnq , which contains the image below: If that file is in your present working directory, you should be able to create and display the image below by typing the following: newImage = replaceColor ( 'pikachu.png', [2552550],[2550255],80); figure(1) ; imshow (newImage) Alternatively, you should also be able to import the file to an RGB array beforehand and then input that array directly to your function: pikachuImage =imread( 'pikachu.png' ); newImage = replaceColor(pikachuImage, [2552550],[2550255],80); figure(1) ; imshow(newImage) Either way, you should get an image that looks like this: Notice we've replaced yellow [255 255 0] with magenta [255 0255 ], and we've allowed a tolerance of \pm 80 for each color channel to make sure we're catching yellow pixels that aren't exactly [255 255 0]Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started