Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
#include <stdio.h>
int main(){
short image[60][40]; // the array to store the text image
FILE * infile = fopen("inimage.txt", "r"); // text image input file
for(int i = 0; i < 60; i++){
for(int j = 0; j < 40; j++){
fscanf(infile, "%d\t", &image[i][j]); // this will read an integer and store it in the array, then read a tab
}
}
short quick_mask[3][3] = { {-1, 0, -1}, {0, 4, 0},{-1, 0, -1} }; // this is the mask which will be convoluted with the image array
short store = 0; // integer to store each output element
FILE * outfile = fopen("outimage.txt", "w"); // text image output folder
// nested for loop to perform the convolution
for(int i = 0; i < 60; i++){
for(int j = 0; j < 40; j++){
if(i < 58 && j < 38) // to skip the edge of the text image
{
// convolution calculation ( mask * image)
store = (image[i-1+1][j-1+1] * quick_mask[0][0]) +
(image[i-1+1][j+1] * quick_mask[0][1]) +
(image[i-1+1][j+1+1] * quick_mask[0][2]) +
(image[i+1][j-1+1] * (quick_mask[1][0]) +
(image[i+1][j+1]) * quick_mask[1][1]) +
(image[i+1][j+1+1] * quick_mask[1][2]) +
(image[i+1+1][j-1+1] * quick_mask[2][0]) +
(image[i+1+1][j+1] * quick_mask[2][1]) +
(image[i+1+1][j+1+1] * quick_mask[2][2]) ;
}
else{
// edges will be always 0
store = 0;
}
if(store < 0) store = 0; // min color is black (0)
else if(store > 255) store = 255; // max color is white ( 255)
fprintf(outfile, "%d\t", store); // write the output into a file
}
fprintf(outfile, "\n"); // add a new line each row
}
return 0; // end of the program
}
Advertisements
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.
AcceptLearn more