【发布时间】:2011-03-18 17:58:46
【问题描述】:
我正在输入一个黑/白像素值矩阵,然后我使用平均技术对内部进行模糊处理。我让程序正确循环,但我需要原始矩阵的恒定副本,以便在单步执行时使用 for 循环。我该怎么做?
#include<iostream>
using namespace std;
/****blurImage*****************************************************************************************************
* main -- Program to blur a grayscale image given the pixel values and their location
*
* Arguments: none
*
* returns: 0
*****************************************************************************************************************/
void blurImage(int matrix[100][100], int matRow, int matCol, int image[100][100]);
int main(){
int matRow;
int matCol;
bool checkDataOk;
int matrix[100][100];
int image[100][100];
cout << "Enter Image Width (in pixels):";
cin >> matCol;
cout << "Enter Image Height (in pixels):";
cin >> matRow;
if (matRow <=0 || matCol <=0 )
checkDataOk = false;
else checkDataOk = true;
if(checkDataOk){
cout << "Enter Pixel Values (left-->right):" << endl;
int tmp;
for (int i = 0; i < matRow; i++)
{
for (int j = 0 ; j < matCol; j++)
{
cin >> tmp;
matrix[i][j] = tmp;
}
}
blurImage(matrix, matRow, matCol, image);
cout << endl;
cout << "Output:" << endl;
for(int i=0; i<matRow; i++){
for(int j=0; j<matCol; j++){
cout << matrix[i][j] << endl;
}
}
}
else cout << "Invalid Row/Column size";
return 0;
}
void blurImage(int matrix[100][100], int matRow, int matCol, int image[100][100]){
for(int i=1; i<(matRow-1); i++){ // start index at 1 and stop at -1 so we don't access outside the image
for(int j=1; j<(matCol-1); j++){
int total = 0;
for(int n=(i-1); n<(i+2); n++){ // start at the top left corner of our current index and loop the 3x3 sub-matrix adding to the total
for(int m=(j-1); m<(j+2); m++){
image = matrix;
total += image[n][m];
}
}
int avg = total/9; // get the average, and set the current index to the average
matrix[i][j] = avg;
}
}
}
如果我输入这个 6x5 矩阵:
0 0 255 0 0
0 255 0 255 0
255 255 0 255 255
255 255 255 255 255
255 0 0 0 255
255 0 0 0 255
0 0 255 0 0
我应该得到:
0 0 255 0 0
0 113 141 113 0
255 170 198 170 255
255 170 141 170 255
255 141 85 141 255
255 0 0 0 255
0 0 255 0 0
【问题讨论】:
-
既然您使用的是 C++,有什么特别的原因您不使用向量而不是数组吗?如果您使用向量,那么
image=matrix将完全按照您的意思执行(并且它会简化许多其他代码)。 -
只需删除 image=matrix。修复边缘。