Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Usce C + + and please run your program For simplicity, we will use a basic edge detection kernel. The image will be a 2

Usce C++ and please run your program
For simplicity, we will use a basic "edge detection" kernel. The image will be a 2D array with grayscale values between 0(black) and 255(white). The following 3x3 kernel is provided in the code.cpp file:
std::vector> kernel {
{-1,-1,-1},
{-1,8,-1},
{-1,-1,-1}
};
We want to implement the "Convolution Operation".
Tasks:
1. In main(), declare and initialize a 2D std::vector> array, named inputImage, to represent a sample grayscale image. For testing purposes, use the following 5x10 image:
std::vector> inputImage{
{50,50,50,50,50,200,200,200,200,200},
{50,50,50,50,50,200,200,200,200,200},
{50,50,50,50,50,200,200,200,200,200},
{50,50,50,50,50,200,200,200,200,200},
{50,50,50,50,50,200,200,200,200,200}
};
2. In main(), declare a 2D array, outputImage, to store the output image after filtering.
3. In the code.cpp file, write a function, printImage(), that prints the input image to the sout stream. Use Tab ('\t') characters to line up the columns. Since this is a conceptual exercise, you will be printing numbers, but the normalized filtered image should show the effects of edge detection.
4. In the code.cpp file, write a function, applyKernel(), that takes the input image and applies the 3x3 kernel to produce the output image. Use nested loops to traverse the image and apply the kernel. Apply the kernel to each pixel in the image except the boundary pixels. Note that, for boundary pixels, part of the kernel does not overlap with the input image, which would result in out-of-bounds error. The outer border (boundary pixels) of the output image should be 0. The boundary pixels are labeled with red color in the 2D array below.
5.
The function applyKernel() implements the
the convolution operation. For each pixel (except the borders), multiply its value and the values of its 8 neighbors by the corresponding value in the kernel, then sum these values. The result is the new value for that pixel in the output image.
You will need to use four nested loops: two for traversing the image and two more for applying the kernel to the current pixel and its neighbors. Make sure to handle edge cases, literally! Pixels on the border of the image won't have 8 neighbors, so just skip them or handle them in a way that avoids out-of-bounds array access.
6. In the code.cpp file, write a function, minMaxNormalization(), to normalize the image so that the pixel values are between 0 and 255. Use the formula:
newVal=old-min/max-min(newMaxnewMin)+newMin
where OldVal is the original pixel value, min and max are the minimum and maximum values in the output image, newMin is 0, and newMax is 255 for our case.
In main(), the provided code prints the original, filtered, and normalized images (as 2D arrays) to the console. An example is below:
Original Image:
5050505050200200200200200
5050505050200200200200200
5050505050200200200200200
5050505050200200200200200
5050505050200200200200200
Filtered Image:
0000000000
0000-4504500000
0000-4504500000
0000-4504500000
0000000000
Normalized and Filtered Output Image:
127127127127127127127127127127
1271271271270255127127127127
1271271271270255127127127127
1271271271270

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

Students also viewed these Databases questions

Question

What is a DNS resource record?

Answered: 1 week ago

Question

b. Where did they come from?

Answered: 1 week ago