# How can I smooth out the colour of a image?

조회 수: 17 (최근 30일)
Lorenz Hipp 2018년 7월 31일
편집: DGM 2022년 11월 27일
Hi everyone. I want to smooth out little differences in a image of a green wall. It would be ideal if in the end there is in every value of the pixels the same colour value. It's a image of a green wall with two red points in it.
Below I tried to lay a filter for every colourbase (red, green and blue) to smoothen out the differences. But in the end there is in every value of every colourbase the value 32. Just at the rimvalues there are the original values.
Can somebody tell me, why my code isnt working? thank you for your help in advance and sorry for my english.
Best regards Lorenz
pixel = 10;
while loop
picture = camera.snapshot;
picture = imresize(picture,[pixel,pixel]);
FilterRed = picture(:,:,1);
for r = 2:(pixel-1)
for c = 2:(pixel-1)
FilterRed(r,c) = (1/8)*(picture(r-1,c)+picture(r-1,c+1)+...
picture(r,c+1)+picture(r+1,c+1)+picture(r+1,c)+...
picture(r+1,c-1)+picture(r,c-1)+picture(r-1,c-1));
end
end
FilterGreen = picture(:,:,2);
for r = 2:(pixel-1)
for c = 2:(pixel-1)
FilterGreen(r,c) = (1/8)*(picture(r-1,c)+picture(r-1,c+1)+...
picture(r,c+1)+picture(r+1,c+1)+picture(r+1,c)+...
picture(r+1,c-1)+picture(r,c-1)+picture(r-1,c-1));
end
end
FilterBlue = picture(:,:,3);
for r = 2:(pixel-1)
for c = 2:(pixel-1)
FilterBlue(r,c) = (1/8)*(picture(r-1,c)+picture(r-1,c+1)+...
picture(r,c+1)+picture(r+1,c+1)+picture(r+1,c)+...
picture(r+1,c-1)+picture(r,c-1)+picture(r-1,c-1));
end
end
Filter = zeros(pixel,pixel,3,'uint8');
Filter(:,:,1) = FilterRed;
Filter(:,:,2) = FilterGreen;
Filter(:,:,3) = FilterBlue;
image(Filter);
truesize([530 530]);
end
##### 댓글 수: 2없음 표시없음 숨기기
KALYAN ACHARJYA 2018년 7월 31일
What you exactly want? Share the test image? What is wrong? Have you applied any image smoothing filters?
Lorenz Hipp 2018년 7월 31일
For this image the code should get a average value of each pixel with the direct neighbour pixel.
I try to sum up the values of the neigbourpixels and then divide the answer trough the number of the neighbourpixels.
Because of the loop i try to do this for every pixel and also for the red, green and blue matrix.
The problem is that after the algorithm, i get a colourmatrix with the value 32 in every pixel that was changed.
P.S. I changed the image to a 10x10 image (imresize) so i could get a better look.
I hope this helps you! Thank you

댓글을 달려면 로그인하십시오.

### 답변 (1개)

DGM 2022년 11월 27일
편집: DGM 2022년 11월 27일
There are a number of things going wrong, but the reason why it's black is simple.
FilterRed = picture(:,:,1); % this is uint8
for r = 2:(pixel-1) % avoiding the edges by 1px
for c = 2:(pixel-1)
% there are 9px in a 3x3 neighborhood, but you're only sampling 8
% the RHS always samples from picture(:,:,1), not from FilterRed
% the sum of the neighborhood will be truncated due to its class
FilterRed(r,c) = (1/8)*(picture(r-1,c)+picture(r-1,c+1)+...
picture(r,c+1)+picture(r+1,c+1)+picture(r+1,c)+...
picture(r+1,c-1)+picture(r,c-1)+picture(r-1,c-1));
end
end
The only reason why there is a 1px ring around the image is because those pixels are never filtered due to the indexing limits. Sampling from the wrong channel is going to be a problem as well.
The fact that you're operating in uint8 means that you'll end up truncating everything when you take the sum. The maximal value you can represent in uint8 is 255. For most image regions, 9x the average intensity exceeds 255, so the sum will always be clipped to 255. You then take the clipped sum and divide by 8, resulting in 32. That's why everything is 32.
In the end, none of this is necessary.
% filter the image
fk = fspecial('average',[5 5]); % using 5x5 so it's easier to see here
outpict = imfilter(inpict,fk); % no loops, no splitting
imshow(outpict)
As to what the giant red circle with a transparent border is about, I have no idea. Be aware that transparent regions have color too. They might not have the color you think they do, so if you go blurring them, you might end up with problems. In this case, the transparent regions are black, not red.

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Image Filtering에 대해 자세히 알아보기

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by