Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ using std please In this lab you will desaturate an image. That is, you will change it from colorful to black and white. What

C++ using std please

In this lab you will desaturate an image. That is, you will change it from colorful to black and white.

What makes a digital image greyscale?

We see white on a digital display when Red, Green and Blue components of a pixel are all on as bright as possible: all set to 255.

We see black when Red, Green and Blue are all off: all set to 0.

In between white and black is grey. For any lightness of grey, the Red, Green and Blue channels are equal to each other, and can be anywhere in the range from 1 (very, very dark grey) to 254 (almost white).

You can desaturate a colored image by averaging the Red, Green and Blue pixel values at each pixel location in the image.

Implement the Desaturate function

We've provided code to get an image file from the user over the command line, load it, display it, and save it as output.bmp. All you need to do is fill in the body of the function Desaturate in desaturate.cc.

You will need to loop (hint: use for loops) over every pixel in the image and desaturate them one by one. Some helpful functions you can call on a graphics::Image are:

// Returns the width and height of the image. int GetWidth(); int GetHeight(); // Gets the red, green or blue pixel value at an (x, y) pixel location. int GetRed(int x, int y); int GetGreen(int x, int y); int GetBlue(int x, int y); // Sets the red, green or blue pixel value at an (x, y) pixel location. bool SetRed(int x, int y, int red); bool SetGreen(int x, int y, int green); bool SetBlue(int x, int y, int blue); 

Here's how these can be used:

// Gets the width of the image. int width = image.GetWidth(); // Gets the height of the image. int height = image.GetHeight(); // Gets the red channel of the center pixel. int red = image.GetRed(width / 2, width / 2); // Sets the blue channel of the center pixel to be equal to the red one. image.SetBlue(width / 2, width / 2, red); 

You can learn more about using graphics::Image in the Intro to Images walk-through.

Run the program to desaturate images

To manually test Desaturate you can compile and run this program with:

clang++ -std=c++17 main.cc desaturate.cc cpputils/graphics/image.cc -o main -lm -lX11 -lpthread ./main 

--------------------

desaturate.cc

#include "desaturate.h"

void Desaturate(graphics::Image& image) {

// Your code here to desaturate the image.

}

----------------------

desaturate.h

#include "cpputils/graphics/image.h"

void Desaturate(graphics::Image& image);

--------------------

main.cc

#include

#include

#include "desaturate.h"

int main() {

// You do not need to edit this file.

std::cout << "Enter the filename of an image to desaturate: ";

std::string filename;

std::cin >> filename;

graphics::Image image;

if (!image.Load(filename)) {

std::cout << "Could not load the image " << filename << std::endl;

return 1;

}

Desaturate(image);

image.ShowUntilClosed();

image.SaveImageBmp("output.bmp");

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions