1. (10 pts) Smoothing is a very useful technique for removing noise or small det
ID: 3591977 • Letter: 1
Question
1. (10 pts) Smoothing is a very useful technique for removing noise or small details from digital images. Smoothing works by replacing the value of each pixel in an image by the average of the values of the 8 neighboring pixels and itself. (a) Note: when finding the average of pixel values, you would find the average of the red, green and blue color intensities separately. See below for an example. If we want to smooth the small image below, the highlighted pixel value will become (2,3,2) in the new image, which is the average of the 9 pixels' corresponding values. (2,3,2) (2,2,2) (9,8,3) (3,4,3) (b) Since we need to use 8 surrounding pixels to smooth a pixel, you will not calculate an average for pixels on the edges of the image (i.e., the first and last row, and the first and last column). Instead, copy the values directly into the new image. Think carefully about the starting and stopping criteria for processing the remaining pixels! (c) In smoothimage.py write a function called smoothImage that takes a FileImage object as a parameter, smooths the image as described above, and returns a new image object that is original image smoothed. Note: you should use loops to calculate the averages, not explicitly write out all nineExplanation / Answer
// I have wrote the code in cpp .....
// simple logic just read the image file
// in the main function i am createing the oject of image class
// and passing the parameter as a name of imange.
// make sure that image should present in the same directory where you have your source code.
// other wise you wont get any output.
// if image is present in the same directory where you source code is present
// you will get output.bmp in the same directory as an output.
// make sure that your image name is input.bmp.
// you can use any name ot file format just make sure that use proper name, height and width in the code.
#include <fstream> // for file I/O
#include <cmath>
#include <stdio.h>
#include <string.h>
using namespace std;
typedef unsigned char unchar; // Easier to understand & code.
// set heigth and width based on image size
#define WIDTH 128
#define HEIGHT 128
#define HEADERDATA 1078
class Image {
private:
ifstream* pInputStream;
ofstream* pOutputStream;
unchar imageHeaderData[HEADERDATA]; //.bmp header data with offset 1078.
unchar** imageData;
unchar** filteredData;
public:
Image(const char* fileName); //Constructor
~Image(); //Deconstructor
void write(const char* fileName);
void smoothImage(); //smoothing filer.
};
//Constructor
Image::Image(const char* fileName){
imageData = new unchar* [HEIGHT]; // create new array size: height of image.
filteredData = new unchar* [HEIGHT];// create new array size: height of image.
for (int i = 0; i < HEIGHT; i++) {
imageData[i] = new unchar [WIDTH]; //create matrix.
filteredData[i] = new unchar [WIDTH]; //create matrix.
}
//image I/O
pInputStream = new ifstream;
pInputStream->open(fileName, ios::in | ios::binary); // open fileName and read as binary.
pInputStream->seekg(0, ios::beg); //pos filter at beginning of image file.
pInputStream->read(reinterpret_cast<char*>(imageHeaderData),HEADERDATA); //read bmp header data into array.
for(int i=0; i<HEIGHT; i++) {
pInputStream->read(reinterpret_cast<char*>(imageData[i]),WIDTH);//read row into each array entry.
}
pInputStream->close(); //close stream.
}
Image::~Image(){
delete pInputStream;
delete pOutputStream;
for(int i=0; i<HEIGHT; i++){
delete[] imageData[i];
delete[] filteredData[i];
}
delete[] imageData;
delete[] filteredData;
}
void Image::write(const char* fileImage) {
smoothImage();
pOutputStream = new ofstream;
pOutputStream->open(fileImage, ios::out | ios::trunc | ios::binary);
pOutputStream->write(reinterpret_cast<char*>(imageHeaderData), HEADERDATA); //write header data onto output
for(int i = 0; i < HEIGHT; i++){
pOutputStream->write(reinterpret_cast<char*>(filteredData[i]),WIDTH); // write new image data.
}
pOutputStream->close(); //close stream
}
void Image::smoothImage(){
for(int i= 0; i<HEIGHT; i++) {
strncpy (reinterpret_cast<char*>(filteredData[i]),
reinterpret_cast<char*>(imageData[i]),
sizeof(reinterpret_cast<char*>(filteredData[i])));
}
int sumOfPixels = 0;
for(int i = 1; i < HEIGHT -1; i++) {
for(int j = 1; j < WIDTH -1; j++) {
sumOfPixels = imageData[i+1][j-1] + // top left corner
imageData[i+1][j] + // top center
imageData[i+1][j+1] + // top right corner
imageData[i][j-1] + // center left
imageData[i][j] + // center center
imageData[i][j+1] + // center right
imageData[i-1][j-1] + // bottom left corner
imageData[i-1][j] + // bottom center
imageData[i-1][j+1]; // bottom right corner
filteredData[i][j] = sumOfPixels / 9; // calculateing the average
}
}
}
int main(){
Image img("input.bmp");
img.write("output.bmp");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.