C++ 中的图像模糊。矩阵副本的问题
我输入一个黑/白像素值矩阵,然后使用平均技术模糊内部。我让程序正确循环,但我需要原始矩阵的常量副本,以便 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
I am inputting a matrix of black/white pixel values and then I am blurring the inside using an averaging technique. I have the program looping through properly but I need a constant copy of the original matrix for the for loops to use when stepping through. How can I do this?
#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;
}
}
}
If I input this 6x5 matrix:
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
I should get:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这一行:
实际上并不复制数组。如果我没记错的话,这是一个指针赋值(意味着
image
现在将指向matrix
),而不是数据副本。(我认为)你想要的是:
由于图像和矩阵是相同类型的数组,我们可以进行内存复制来传输内容。您还可以使用循环来执行复制,但这更简单、更快。
另外,这个操作在四个嵌套循环内会非常慢,所以这可能不是您想要做的。
注意:您需要
#include
才能使用 memcpy。This line:
does not actually copy the array. If I'm not mistaken, it is a pointer assignment (meaning
image
will point tomatrix
now), not a data copy.What (I think) you want is:
Since image and matrix are arrays of the same type, we can do a memory copy to transfer contents over. You could also use loops to perform the copy, but this is simpler and faster.
Also, this operation would be very slow inside of four nested loops, so that's probably not what you meant to do.
Note: You'll need to
#include <string.h>
to use memcpy.是的!所以不要修改模糊函数中的输入('matrix')。仅修改输出('image')。
当blurImage()返回时,'matrix'将保持不变,而'image ' 将包含结果。
因此,当您循环打印输出时,请使用“图像”而不是“矩阵”。如果您确实想将“图像”复制到“矩阵”中,请按照 Joey Adams 的建议进行操作。
Yes! So don't modify your input ('matrix') in your blur function. Only modify the output ('image').
When blurImage() returns, 'matrix' will be unmodified, and 'image' will contain the result.
So, when you loop to print your output, use 'image' instead of 'matrix'. If you really want to copy 'image' into 'matrix', do as Joey Adams suggests.
这与您的问题无关,但无论如何您最终都会遇到这个问题,所以我会提醒您。执行过滤的方式大约需要
matCol * matRow * k * k
操作,其中k
是模糊内核的宽度。虽然当k=3
时这是可以的,但它很快就会变得太慢。使其更快的一个简单方法是首先仅水平模糊图像,然后垂直模糊中间结果。这将采用
2 * matCol * matRow * k
运算的顺序,并给出相同的结果(为避免舍入,请勿在过程中途除以k
- 保留中间像素总和)如果您想要解决一个有趣的难题,有一个巧妙的方法可以做到这一点,它大约需要相同的时间,无论给定的是什么
k
。This is only tangentially related to your question, but you're bound to encounter the issue eventually anyway, so I'll give you a heads up. The way you're performing the filtering takes about
matCol * matRow * k * k
operations, wherek
is the width of your blur kernel. While this is ok whenk=3
, it will quickly become too slow.A simple way to make it faster is to first blur the image only horizontally, and then blur the intermediate result vertically. This will take on the order of
2 * matCol * matRow * k
operations, and gives the same result (to avoid roundoff, don't divide byk
halfway through the process - keep the intermediate pixel sums)If you're up for an interesting puzzle, there is a neat way to do this which takes about the same time, regardless of the given
k
.