MATLAB中编写均值滤波的程序
imageData=imread('file.png');imageSize=size(imageData);imageHeight=imageSize(1);image...
imageData = imread ('file.png');
imageSize = size(imageData);
imageHeight = imageSize(1);
imageWidth = imageSize(2);
for y = 1:imageHeight;
for x = 1:imageWidth;
if 1 < y <imageHeight && 1 < x < imageWidth;
for y1 = y-1 : y+1;
for x1 = x-1 : x+1;
imageData(y,x,1) = (sum (y1,x1,1))/9;
imageData(y,x,2) = (sum (y1,x1,2))/9;
imageData(y,x,3) = (sum (y1,x1,3))/9;
end
end
end
end
end
imwrite (imageData,'final.png')
这是我的思路,但是在赋值的时候有错,不太懂,小白一枚,希望大神能够解答 展开
imageSize = size(imageData);
imageHeight = imageSize(1);
imageWidth = imageSize(2);
for y = 1:imageHeight;
for x = 1:imageWidth;
if 1 < y <imageHeight && 1 < x < imageWidth;
for y1 = y-1 : y+1;
for x1 = x-1 : x+1;
imageData(y,x,1) = (sum (y1,x1,1))/9;
imageData(y,x,2) = (sum (y1,x1,2))/9;
imageData(y,x,3) = (sum (y1,x1,3))/9;
end
end
end
end
end
imwrite (imageData,'final.png')
这是我的思路,但是在赋值的时候有错,不太懂,小白一枚,希望大神能够解答 展开
1个回答
展开全部
imageData(y,x,1) = (sum (y1,x1,1))/9;
imageData(y,x,2) = (sum (y1,x1,2))/9;
imageData(y,x,3) = (sum (y1,x1,3))/9;
这一部分没有进行求和的操作啊,根本没有imageData这个量。。。if后边的循环没有实现对3*3的块求和,而是每次赋一个值而已。还有for后边不能加分号,if后边不能加分号,1<x<imageWidth这种语句尽量分成两句:x>1 && x<imageWidth。另建议新建一个矩阵进行存储,可修改如下:
imageData = imread ('file.png');
imageSize = size(imageData);
imageHeight = imageSize(1);
imageWidth = imageSize(2);
Result = imageData;
for y = 1:imageHeight
for x = 1:imageWidth
if (y>1 && y <imageHeight && x>1 && x < imageWidth)
Result(y,x,1) = sum(sum(imageData(y-1:y+1,x-1:x+1,1)))/9;
Result(y,x,2) = sum(sum(imageData(y-1:y+1,x-1:x+1,2)))/9;
Result(y,x,3) = sum(sum(imageData(y-1:y+1,x-1:x+1,3)))/9;
end
end
end
imwrite (Result,'final.png');
有问题可以继续问我,望采纳~
imageData(y,x,2) = (sum (y1,x1,2))/9;
imageData(y,x,3) = (sum (y1,x1,3))/9;
这一部分没有进行求和的操作啊,根本没有imageData这个量。。。if后边的循环没有实现对3*3的块求和,而是每次赋一个值而已。还有for后边不能加分号,if后边不能加分号,1<x<imageWidth这种语句尽量分成两句:x>1 && x<imageWidth。另建议新建一个矩阵进行存储,可修改如下:
imageData = imread ('file.png');
imageSize = size(imageData);
imageHeight = imageSize(1);
imageWidth = imageSize(2);
Result = imageData;
for y = 1:imageHeight
for x = 1:imageWidth
if (y>1 && y <imageHeight && x>1 && x < imageWidth)
Result(y,x,1) = sum(sum(imageData(y-1:y+1,x-1:x+1,1)))/9;
Result(y,x,2) = sum(sum(imageData(y-1:y+1,x-1:x+1,2)))/9;
Result(y,x,3) = sum(sum(imageData(y-1:y+1,x-1:x+1,3)))/9;
end
end
end
imwrite (Result,'final.png');
有问题可以继续问我,望采纳~
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询