Question
Code in C++ I'm getting error when I try to do it. My solution is at the bottom of the question. Gaussian Blur In image
Code in C++
I'm getting error when I try to do it. My solution is at the bottom of the question.
Gaussian Blur
In image editing software, an image can be blurred by replacing a pixel's value with the average of a group of pixels around it. The following code provides you with an "image" as a two-dimensional array. Your task is to write a function which takes this image and blurs it using the following algorithm:
For every pixel, replace it's value with the average of five pixels including the pixel itself, as well as the four surrounding pixels above, below, to the left and to the right of it. Average these five values together and update the value. For pixels touching an edge or corner, assume the values of pixels outside of the array bounds have a value of zero.
Implement the blur function to achieve the expected output.
Starter Code:
#include#include #include #include #include constexpr size_t SIZE = 20; static void printImage(int data[SIZE][SIZE]); static void blurImage(int data[SIZE][SIZE]); int main() { int image[SIZE][SIZE] = { {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, {0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 99, 99, 0, 0, 0, 0, 0, 0, 0, 0, }, {0, 0, 0, 0, 0, 0, 0, 99, 99, 0, 0, 0, 99, 99, 0, 0, 0, 0, 0, 0, }, {0, 0, 0, 0, 0, 99, 99, 0, 0, 0, 0, 0, 0, 0, 99, 99, 0, 0, 0, 0, }, {0, 0, 0, 0, 99, 99, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 99, 0, 0, }, {0, 0, 0, 99, 99, 0, 0, 0, 0, 0, 0, 99, 0, 0, 0, 0, 99, 0, 0, 0, }, {0, 0, 99, 99, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 0, 0, 0, 0, }, {0, 0, 99, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 0, 0, 0, 0, 0, }, {0, 0, 99, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 0, 0, 0, 0, 0, 0, }, {0, 0, 99, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 0, 0, 0, 0, 0, 0, 0, }, {0, 0, 99, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 0, 0, 0, 0, 0, 0, }, {0, 0, 99, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 0, 0, 0, 0, 0, }, {0, 0, 0, 99, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 0, 0, 0, 0, }, {0, 0, 0, 99, 99, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 0, 0, 0, }, {0, 0, 0, 0, 99, 99, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 99, 0, 0, }, {0, 0, 0, 0, 0, 99, 99, 0, 0, 0, 0, 0, 0, 0, 99, 99, 0, 0, 0, 0, }, {0, 0, 0, 0, 0, 0, 0, 99, 99, 0, 0, 0, 99, 99, 0, 0, 0, 0, 0, 0, }, {0, 0, 0, 0, 0, 0, 0, 0, 0, 99, 99, 99, 0, 0, 0, 0, 0, 0, 0, 0, }, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, }; printImage(image); blurImage(image); printImage(image); } static void printImage(int data[SIZE][SIZE]) { for (auto i = 0; i < SIZE; i++) { for (auto j = 0; j < SIZE; j++) { std::cout << std::setw(3) << data[i][j]; } std::cout << " "; } } static void blurImage(int data[SIZE][SIZE]) { // TODO: implement blur algorithm }
Expected Output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 99 99 99 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 99 99 0 0 0 99 99 0 0 0 0 0 0 0 0 0 0 0 99 99 0 0 0 0 0 0 0 99 99 0 0 0 0 0 0 0 0 99 99 0 0 0 0 0 0 0 0 0 0 99 99 0 0 0 0 0 99 99 0 0 0 0 0 0 99 0 0 0 0 99 0 0 0 0 0 99 99 0 0 0 0 0 0 0 0 0 0 0 99 0 0 0 0 0 0 99 0 0 0 0 0 0 0 0 0 0 0 99 0 0 0 0 0 0 0 99 0 0 0 0 0 0 0 0 0 0 99 0 0 0 0 0 0 0 0 99 0 0 0 0 0 0 0 0 0 99 0 0 0 0 0 0 0 0 0 99 0 0 0 0 0 0 0 0 0 0 99 0 0 0 0 0 0 0 0 99 0 0 0 0 0 0 0 0 0 0 0 99 0 0 0 0 0 0 0 0 99 0 0 0 0 0 0 0 0 0 0 0 99 0 0 0 0 0 0 0 99 99 0 0 0 0 0 0 0 0 0 0 0 99 0 0 0 0 0 0 0 99 99 0 0 0 0 0 0 0 0 0 0 99 99 0 0 0 0 0 0 0 99 99 0 0 0 0 0 0 0 99 99 0 0 0 0 0 0 0 0 0 0 0 99 99 0 0 0 99 99 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 99 99 99 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 19 19 19 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 19 39 39 59 39 39 19 0 0 0 0 0 0 0 0 0 0 0 19 39 39 39 39 19 39 39 39 39 19 0 0 0 0 0 0 0 0 39 59 39 39 19 0 0 0 19 39 39 39 39 19 0 0 0 0 0 39 59 59 39 0 0 0 0 19 0 0 19 39 59 39 19 0 0 0 39 59 59 39 0 0 0 0 19 19 19 0 0 39 39 39 0 0 0 19 59 59 39 0 0 0 0 0 0 19 0 0 39 19 39 0 0 0 0 19 59 39 0 0 0 0 0 0 0 0 0 39 19 39 0 0 0 0 0 19 59 19 0 0 0 0 0 0 0 0 39 19 39 0 0 0 0 0 0 19 59 19 0 0 0 0 0 0 0 19 19 59 0 0 0 0 0 0 0 19 59 19 0 0 0 0 0 0 0 0 39 19 39 0 0 0 0 0 0 19 39 39 0 0 0 0 0 0 0 0 0 39 19 39 0 0 0 0 0 0 39 39 39 0 0 0 0 0 0 0 0 0 39 19 39 0 0 0 0 0 19 59 59 39 0 0 0 0 0 0 0 0 0 39 39 39 0 0 0 0 0 39 59 59 39 0 0 0 0 0 0 0 19 39 59 39 19 0 0 0 0 0 39 59 39 39 19 0 0 0 19 39 39 39 39 19 0 0 0 0 0 0 0 19 39 39 39 39 19 39 39 39 39 19 0 0 0 0 0 0 0 0 0 0 0 19 39 39 59 39 39 19 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 19 19 19 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
This solution is not working: How do I test this code in the terminal? What do I need to input the terminal to test the code? Please provide an example.
Is there anything that I'm missing or the code is wrong? Please fix it and provide the full working code. Thank you.
#include
constexpr size_t SIZE = 20;
void blurImage(int data[SIZE][SIZE]) {
int temp[SIZE][SIZE];
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
int sum = 0;
int count = 0;
for (int ii = i - 1; ii <= i + 1; ii++) {
if (ii >= 0 && ii < SIZE) {
for (int jj = j - 1; jj <= j + 1; jj++) {
if (jj >= 0 && jj < SIZE) {
sum += data[ii][jj];
count++;
}
}
}
}
temp[i][j] = sum / count;
}
}
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
data[i][j] = temp[i][j];
}
}
}
int main() {
int data[SIZE][SIZE] = {0};
blurImage(data);
return 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