“These are the 5 days that I participated in the First Challenge 2022. For details, see: First Challenge 2022.”

I. Problem description

Small blue has a black and white image composed of N ×m pixels, including n rows from top to bottom, and m columns from left to right for each row. Each pixel is represented by a grayscale value between 0 and 255.

Now, Blue is ready to blur the image using the following method:

For each pixel, the sum of all the pixels in the 3×3 region centered around it (which may be 9 pixels or less) is divided by the number of pixels in this range (rounded) to get the blurred result.

Note that the sum is calculated for each pixel using the grayscale value in the original image.

Two, the title requirements

Input: Input two integers n,m in the first line (where, 1≤n,m≤100), the second line to n+1 contains m positive integers, representing the gray value of each pixel

Output: output N lines, each line of M integers, separated by Spaces between adjacent integers, indicating the blurred image.

Third, problem analysis

Use a two-dimensional array to store the pixels to be entered. For each pixel, add up all the pixel values in its 3×3 region and divide by the total.

There’s a problem with that. You can cross the line when you look. For example, a pixel value in the first row, no value above, and a pixel value in the first column, no value to the left. Only the ones in the middle satisfy 3 by 3. Therefore, in the judgment of the condition to add a is out of range.

Define an array of 8 * 2, int c [8] [2] = {0, 0, 1, 1,0,1,0,1,1, 1, 1, 1, 1, 1, 1}; As a direction of extension. In the for loop x= I +c[k][0], y=j+ C [k][1], judge up, down, left, right and other total 8 direction coordinates.

For x, the range of y x>=1&&x<=n&&y>=1&&y<=m;

The number of pixels divided by the number of pixels, and they want to round it down, and just divide it by int. For general rounding problems, you can call floor (rounding down) and ceil (rounding up) in #include<math.h>.

Four, coding implementation

#include<iostream>
using namespace std;
int main(a)
{
	int a[105] [105],b[105] [105];// Define two two-dimensional arrays
	int c[8] [2] = {0.1.0.- 1.- 1.0.1.0.1.1.- 1.1.- 1.- 1.1.- 1};// Direction array
	int n,m,i,j,k;
	cin>>n>>m;// Enter data
	for(i=1; i<=n; i++) {for(j=1; j<=m; j++) { cin>>a[i][j];/ / input}}int ans,sum=0;// Define the number of surrounding pixels and sums
	for(i=1; i<=n; i++) {for(j=1; j<=m; j++) { sum=a[i][j];// Initialize the pixel value
			ans=1;// The number of pixels is initialized
			for(k=0; k<8; k++) {int x=i+c[k][0];/ / x coordinate
				int y=j+c[k][1];/ / y
				if(x>=1&&x<=n&&y>=1&&y<=m)// Check whether the coordinates are in range
				{
					ans++;// Number of pixels ++
					sum+=a[x][y];// Pixel value ++
				}
			}
			b[i][j]=sum/ans;// Update the variable value}}for(i=1; i<=n; i++) {for(j=1; j<=m; j++) { cout<<b[i][j]<<"";// Output an array
		}cout<<"\n";
	}
	return 0;
}
Copy the code

4. Output results

Test case: Given an array of three rows and four columns, print the correct result

3, 4,

0 0 0. 255

255 0 0 0

0, 30, 255, 255