Write a CC code to perform convolution on a 2D image in a pp

Write a C/C++ code to perform convolution on a 2D image (in a .ppm file P6 with 11400^+6800 pixels) using a gaussian blur filter (averaged) with standard deviation 40 pixels. Since I have limited background in C/C++ im facing difficulties in this course (GPU Programming). I am taking an online course for this, but i have to subit the assignment before monday. Thanks.. Programming Assignment 1:2D Convolution of Images Your first programming assignment is the implementation of a 2D Gaussian image convolution algorithm. Your program should work on very large images with very large kernels. Several images are provided on the course website. In this programming assignment, you will implement a 2D Gaussian convolution at takes as input parameters the image filename and standard deviation of the convolution kernel: >> convolve imageFile.ppm 40 For example, the above command will convolve an image named \"imageFile.ppm\" with a 2D Gaussian kernel with standard deviation =40 pixels. Command line arguments are passed to your C/C++ main() function as text strings by the operating system. Reading them is straightforward: #include int main(int argc, char* argv if(argc!=3)//there should be three arguments return 1;//exit and return an error std::stringfilename(argv[1]); intsigma = atoi(argv[0]); In addition, images can be read using the Netp bm PPM image format. This format is easy to read and the specification is provided on the Wikipedia page: In short, the first few lines of text contain the image parameters, including image size, and the rest of the file stores the raw pixel data as a series of binary unsigned char values. Three values are stored per pixel, representing red, green, and blue color values. You can read, convert, and display images in PPM format using GIMP: Your program will perform both a CPU and GPU based convolution (both should be implemented as separable filters). Output both the result of the GPU based algorithm as an image and the time required to calculate both the CPU and GPU algorithms. Make sure that you handle edge cases robustly and comment your code!

Solution

#include<iostream>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;

int reflect(int M, int x)
{
if(x < 0)
{
return -x - 1;
}
if(x >= M)
{
return 2*M - x - 1;
}
return x;
}

int circular(int M, int x)
{
if (x<0)
return x+M;
if(x >= M)
return x-M;
return x;
}


void noBorderProcessing(Mat src, Mat dst, float KernelData[][3])
{

float sum;
for(int y = 1; y < src.rows - 1; y++){
for(int x = 1; x < src.cols - 1; x++){
sum = 0.0;
for(int k = -1; k <= 1;k++){
for(int j = -1; j <=1; j++){
sum = sum + KernelData[j+1][k+1]*src.at<uchar>(y - j, x - k);
}
}
dst.at<uchar>(y,x) = sum;
}
}
}

void refletedIndexing(Mat src, Mat dst, float KernelData[][3])
{
float sum, x1, y1;
for(int y = 0; y < src.rows; y++){
for(int x = 0; x < src.cols; x++){
sum = 0.0;
for(int k = -1;k <= 1; k++){
for(int j = -1;j <= 1; j++ ){
x1 = reflect(src.cols, x - j);
y1 = reflect(src.rows, y - k);
sum = sum + KernelData[j+1][k+1]*src.at<uchar>(y1,x1);
}
}
dst.at<uchar>(y,x) = sum;
}
}
}

void performCircukarIndexing(Mat src, Mat dst, float KernelData[][3])
{
float sum, x1, y1;
for(int y = 0; y < src.rows; y++){
for(int x = 0; x < src.cols; x++){
sum = 0.0;
for(int k = -1;k <= 1; k++){
for(int j = -1;j <= 1; j++ ){
x1 = circular(src.cols, x - j);
y1 = circular(src.rows, y - k);
sum = sum + KernelData[j+1][k+1]*src.at<uchar>(y1,x1);
}
}
dst.at<uchar>(y,x) = sum;
}
}
}

int main(int argc, char* argv[])
{
   if(argc != 3){
       std::string filename(argv[1]);
       int sigma = stoi(argv[0]);
Mat src, dst;

  
/// Loading image
src = imread(filename, CV_LOAD_IMAGE_GRAYSCALE);

if( !src.data )
{ return -1; }


float KernelData[3][3] = {
{1/9.0, 1/9.0, 1/9.0},
{1/9.0, 1/9.0, 1/9.0},
{1/9.0, 1/9.0, 1/9.0}
};

dst = src.clone();
for(int y = 0; y < src.rows; y++)
for(int x = 0; x < src.cols; x++)
dst.at<uchar>(y,x) = 0.0;

performCircukarIndexing(src, dst, KernelData);

namedWindow(\"final\");
imshow(\"final\", dst);

namedWindow(\"initial\");
imshow(\"initial\", src);

waitKey();

   }
return 0;
}

 Write a C/C++ code to perform convolution on a 2D image (in a .ppm file P6 with 11400^+6800 pixels) using a gaussian blur filter (averaged) with standard devia
 Write a C/C++ code to perform convolution on a 2D image (in a .ppm file P6 with 11400^+6800 pixels) using a gaussian blur filter (averaged) with standard devia
 Write a C/C++ code to perform convolution on a 2D image (in a .ppm file P6 with 11400^+6800 pixels) using a gaussian blur filter (averaged) with standard devia

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site