how to threshold image in 3*3 window?

1 次查看(过去 30 天)
Hello,
I have a 256x256 noisy image and I want to find threshold value of elements on the middle row and column in 3x3 window and then sliding this window all over the image pixel by pixel and do the same.
I do this work by 2 way : graythresh and one file exchange, but I get in both an error! can any one help me to solve this?
here is my code :
clear all;
close all;
clc;
I=im2double(imread ('cameraman.tif'));
size_I=size(I);
%% adding noises%speckle noise
SI=imnoise(I,'speckle',0.2);
% adding salt and pepper noise or shot noise
Shot_noise = imnoise(I, "salt & pepper", 0.10);
% total noise
noisy_image=I+SI+Shot_noise;
subplot(121)
imshow(I);
title('Original Image')
subplot(122)
imshow(noisy_image)
title("noisy image");
%----------------------------------------------------------------
k=1;
hwin=2*k+1;
im= padarray(noisy_image, [k k],'replicate'); % 'zero-padding'
[row,col] = size(im);
% + Mask
plus=zeros(hwin,hwin);
plus(:,2)=1; plus(2,:)=1;
plus_find=find(plus); % plus pixel's positions
for p=1:row-2
for q=1:col-2
w=im(p:p+(hwin-1),q:q+(hwin-1));%sliding window / filter
sw=w(:)'; %sliding window elements
plus_val=w(plus_find)';
center_val=w(5);
% T=graythresh(sw);
% I have error here!
% [~,T]=imthresh(w);
% and also here
% my code is not complete.
end
end
when I use graythresh it returns this error
and for imthresh function it returns this error :

回答(1 个)

Image Analyst
Image Analyst 2021-9-26
编辑:Image Analyst 2021-9-26
Try beginning at row and columns 2,
for p= (k+1) : row-k
for q = (k+1) : col-k
  2 个评论
majid
majid 2021-9-26
sorry, same error again.
could you run my codes and the attached function on your system,
I think my system have problem.
Image Analyst
Image Analyst 2021-9-26
OK, I just ran your original code and it ran without any errors.

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by