RP Store Tìm linh kiện theo mã, gửi BOM nhận báo giá Gửi BOM

Vấn Đề Nhiễu Ảnh-Image Noise

Collapse
X
 
  • Giờ
  • Show
Clear All
new posts
  • thanh_nam1977
    Đệ tử 2 túi
    • Apr 2007
    • 27

    #1

    Vấn Đề Nhiễu Ảnh-Image Noise

    - Nhiễu là một hiện tượng ngẫu nhiên đều có mặt trong mọi hệ thống xử lý tín hiệu thực (real signal)
    - Có rất nhiều nguồn tạo ra nhiễu, ví dụ như do sự thay đổi độ nhạy của đầu dò, do sự biến đổi của môi trường, do sai số lượng tử hóa hay sai số truyền.v.v..
    -- Nhiễu ảnh sẽ làm chi tiết của quang cảnh trong ảnh sẽ không thích hợp
    Phân loại nhiễu :
    1. Nhiễu độc lập với dữ liệu ảnh (Independent Noise):
    -- Là một loại nhiễu cộng (additive noise): ảnh thu được f(i,j) là tổng của ảnh đúng (true image) và nhiễu n(i,j): f(i,j) = s(i,j) + n(i,j)
    - Thông tin ảnh có tần số thấp, còn nhiễu ảnh hưởng đến tần số cao và ảnh hưởng này có thể được giảm đi khi sử dụng bộ lọc thông thấp
    - Lọc nhiễu bằng bộ lọc tần số (frequency filter) hay bộ lọc không gian (spatial filter)
    Nhiễu phụ thuộc vào dữ liệu (data dependent noise):

    - Nhiễu xuất hiện khi có sự bức xạ đơn sắc nằm rải rác trên bề mặt ảnh, độ lởm chởm trên bề mặt tùy thuộc vào bước sóng của điểm ảnh.

    - Do có sự giao thoa giữa các sóng ảnh nên làm xuất hiện những vết lốm đốm trên ảnh
    2.Nhiễu Gauss:
    - Nhiễu này có được do bản chất rời rạc của bức xạ (hệ thống ghi ảnh bằng cách đếm các photon (lượng tử ánh sáng)

    - Là nhiễu cộng và độc lập (independent, additive noise) – nhiễu n(i,j) có phân tán Gauss (trung bình = zero) được mô tả bởi độ lệch chuẩn (standard deviation), hay phương sai

    - Mỗi pixel trong ảnh nhiễu là tổng giá trị pixel đúng (true pixel) và pixel ngẫu nhiên

    3.Nhiễu muối – tiêu (Salt & Pepper noise):
    -Nhiễu này sinh ra do xảy ra sai số trong quá trình truyền dữ liệu.
    -Những pixel đơn được set luân phiên mang giá trị zero hay giá trị maximum tạo ra hình muối tiêu trên ảnh.

    Các bộ lọc nhiễu :Giới thiệu bộ lọc Median (Trung vị) và bộ lọc mean
    1.Bộ lọc Median

    -Những hình ảnh ta thu được thường hay bị nhiễu.

    - Khi hình ảnh bị nhiễu thì điều chúng ta cần làm là khắc phục hay giảm ảnh hưởng của nhiễu.

    - Nếu nhiễu là nhiễu cộng thì ta cần áp dụng mạch xử lí tuyến tính để lọc nhiễu, còn đối với nhiễu nhân thì cần phải dùng mạch xử lí phi tuyến.

    Ở đây ta sẽ tìm hiểu quá trình xử lí phi tuyến dùng để khắc phục nhiễu nhân, đó là phương pháp lọc Median.

    Mô tả khái quát phương pháp:
    Một trong những khó khăn của phương pháp làm nhẵn ảnh là nó làm nhòe đường biên của ảnh.
    Nếu ảnh vật thể cần làm giảm nhiễu và không bị nhòe (blurring), thì sử dụng lọc median.
    Ở một mức độ nào đó nó có phần giống mạch lọc trung bình. Tuy nhiên, nó làm việc tốt hơn mạch lọc trung bình trong lĩnh vực sử dụng cho việc xử lý ảnh.
    Nguyên tắc hoạt động:
    Mức xám của mỗi pixel được thay bằng giá trị trung vị của các mức xám trong lân cận của pixel đó.

    Phương pháp median được tính toán như sau:
    Sắp xếp tất cả các giá trị pixel trong ma trận trượt ảnh thành trật tự số học.
    Thay thế giá trị pixel đang được xem xét với giá trị trung vị.
    Nhận xét :
    Lọc median cho kết quả không tốt khi lọc ảnh với nhiễu Gauss.
    Lọc median chỉ thực sự cho kết quả tốt khi lọc ảnh với nhiễu xuất hiện làm cho pixel ảnh có cường độ thay đổi mạnh nằm ngoài các giá trị pixel khác
    Trong lọc median thì mỗi pixel đơn không có tính chất đặc trưng thì sẽ không ảnh hưởng đến mức giữa giá trị tín hiệu.

    Giá trị giữa chính xác sẽ là giá trị của một trong các pixels lân cận, lọc median sẽ không tạo ra pixel mới có giá trị không chân thật khi bộ lọc tăng mức độ lọc. Vì lí do này mà lọc median thì tốt hơn trong việc đảm bảo mức sắc nét của hình ảnh.

    Bộ lọc median cho phép phần lớn các chi tiết ảnh có tần số không gian cao đi qua trong khi đó nó sẽ loại trừ có hiệu quả nhiễu trên ảnh (kết quả của việc này là bộ lọc median ít có tác dụng trong việc khử nhiễu xảy ra trên ảnh với nhiễu Gauss).
    Một trong các vấn đề chủ yếu với bộ lọc median là chi phí thiết kế đắt tiền và tính toán phức tạp. Để thực thi lọc thì phương pháp median cần phải xắp xếp tất cả các giá trị ảnh lân cận thành trật tự số học và như thế việc sử lý rất chậm .

    2. Giới thiệu mạch lọc Mean
    Khái niệm:
    Bộ lọc Mean là bộ lọc có cửa sổ không gian trượt thay thế giá trị trung tâm của cửa sổ bằng giá trị trung bình của các phần tử (pixel) trong cửa sổ.
    Ưu điểm:
    Là mạch lọc tuyến tính đơn giản trong tính toán.
    Ảnh đạt độ trơn mịn.

    Khuyết điểm:
    Không hoàn toàn loại bỏ tác dụng của nhiễu, các pixel đơn lẻ (nhiễu) sẽ ảnh hưởng đến giá trị trung bình của các pixel lân cận.
    Độ sắc nét của ảnh kém
    Độ tương phản thấp.

    Khắc phục:
    Sử dụng bộ lọc Median.
    Chương trình matlab :
    %%chuong trinh giao dien chinh
    close
    fz=25; %fontsize
    fn='VNI-Times';
    h=figure;
    %whitebg([.1 .3 .1]);
    set(h,'position',[10,30,635,430],...
    'MenuBar','none',...
    'resize','off',...
    'NumberTitle','off',...
    'Name','XU LY ANH SO');
    uicontrol('style','text',...
    'ForeGroundcolor','r',...
    'position',[70,380,480,30],...
    'fontsize',13,...
    'fontname','VNI-Times',...
    'string','TRƯỜNG ĐẠI HỌC BÁCH KHOA THÀNH PHỐ HỒ CHÍ MINH');
    uicontrol('style','text',...
    'ForeGroundcolor','r',...
    'position',[150,350,320,30],...
    'fontsize',13,...
    'fontname','VNI-Times',...
    'string','PHÒNG ĐÀO TẠO SAU ĐẠI HỌC');
    uicontrol('style','text',...
    'ForeGroundcolor','b',...
    'position',[250,280,90,30],...
    'fontsize',13,...
    'fontname','VNI-Souvir',...
    'string','ĐỀ TÀI: ');
    t1=text(.48, .6,'LỌC NHIỄU ');
    t11=text(.488, .605,'LỌC NHIỄU ');
    set(gca,...
    'visible','off');
    set(t11,...
    'color','b',...
    'fontname',fn,...
    'fontsize',fz,...
    'fontweight','bold',...
    'HorizontalAlignment','center',...
    'VerticalAlignment','middle');
    set(t1,...
    'color',[1 1 1],...
    'fontname',fn,...
    'fontsize',fz,...
    'fontweight','bold',...
    'HorizontalAlignment','center',...
    'VerticalAlignment','middle');
    t2=text(.5, .5,' DÙNG BỘ LỌC MEDIAN');
    t22=text(.505, .505,'DÙNG BỘ LỌC MEDIAN');
    set(t22,...
    'color','b',...
    'fontname',fn,...
    'fontsize',fz,...
    'fontweight','bold',...
    'HorizontalAlignment','center',...
    'VerticalAlignment','middle');
    set(t2,...
    'color',[1 1 1],...
    'fontname',fn,...
    'fontsize',fz,...
    'fontweight','bold',...
    'HorizontalAlignment','center',...
    'VerticalAlignment','middle');
    uicontrol('style','text',...
    'ForeGroundcolor','m',...
    'position',[100,120,450,25],...
    'fontsize',13,...
    'fontname','VNI-Souvir',...
    'string','GVHD : GS-TSKH. NGUYỄN KIM SÁCH');
    uicontrol('style','text',...
    'ForeGroundcolor','m',...
    'position',[100,90,450,25],...
    'fontsize',13,...
    'fontname','VNI-Souvir',...
    'string','LỚP CH TỰ ĐỘNG HÓA ');
    %>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    uicontrol('style','push',...
    'position',[190 20 100 30],...
    'fontname','VNI-Souvir',...
    'string','THỰC HIỆN',...
    'callback','thuchiendemo');
    uicontrol('style','push',...
    'position',[350 20 120 30],...
    'fontname','VNI-Souvir',...
    'string','THOÁT',...
    'callback','closeall');
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%chuong trinh giao thực hiện mơ phỏng
    function thuchiendemo(action, varargin)
    if nargin<1,
    action='InitializeNRFILTDEMO';
    end;
    feval(action,varargin{:})
    return ;
    %%% Sub-function - InitializeNRFILTDEMO
    function InitializeNRFILTDEMO()
    % If nrfiltdemo is already running, bring it to the foreground
    h = findobj(allchild(0), 'tag', 'XU LY ANH');
    if ~isempty(h)
    figure(h(1))
    return
    end
    screenD = get(0, 'ScreenDepth');
    if screenD>8
    grayres=256;
    else
    grayres=128;
    end
    NrfiltDemoFig = figure( ...
    'Name','XU LY ANH', ...
    'NumberTitle','off', 'HandleVisibility', 'on', ...
    'tag', 'Noise Reduction Filtering Demo', ...
    'Visible','off', 'Resize', 'off',...
    'BusyAction','Queue','Interruptible','off', ...
    'Color', [.8 .8 .8], ...
    'IntegerHandle', 'off', ...
    'DoubleBuffer', 'on', ...
    'Colormap', gray(grayres));
    figpos = get(NrfiltDemoFig, 'position');
    figpos(3:4) = [560 420];
    % Adjust the size of the figure window
    horizDecorations = 10; % resize controls, etc.
    vertDecorations = 45; % title bar, etc.
    screenSize = get(0,'ScreenSize');
    if (screenSize(3) <= 1)
    % No display connected (apparently)
    screenSize(3:4) = [100000 100000]; % don't use Inf because of vms
    end
    if (((figpos(3) + horizDecorations) > screenSize(3)) | ...
    ((figpos(4) + vertDecorations) > screenSize(4)))
    % Screen size is too small for this demo!
    delete(fig);
    error(['Screen resolution is too low ', ...
    '(or text fonts are too big) to run this demo']);
    end
    dx = screenSize(3) - figpos(1) - figpos(3) - horizDecorations;
    dy = screenSize(4) - figpos(2) - figpos(4) - vertDecorations;
    if (dx < 0)
    figpos(1) = max(5,figpos(1) + dx);
    end
    if (dy < 0)
    figpos(2) = max(5,figpos(2) + dy);
    end
    set(NrfiltDemoFig, 'position', figpos);

    rows = figpos(4); cols = figpos(3);
    % Colors
    bgcolor = [0.45 0.45 0.45]; % Background color for frames
    wdcolor = [.8 .8 .8]; % Window color
    fgcolor = [1 1 1]; % For text

    hs = (cols-(3*128)) / 4; % Horizantal Spacing
    vs = hs/2; % Vertical Spacing
    ifs = hs/3; % Intraframe spacing
    %====================================
    % Parameters for all buttons and menus

    Std.Interruptible = 'off';
    Std.BusyAction = 'queue';

    % Defaults for image axes
    Ax = Std;
    Ax.Units = 'Pixels';
    Ax.Parent = NrfiltDemoFig;
    Ax.ydir = 'reverse';
    Ax.XLim = [.5 128.5];
    Ax.YLim = [.5 128.5];
    Ax.CLim = [0 1];
    Ax.XTick = [];
    Ax.YTick = [];

    Img = Std;
    Img.CData = [];
    Img.Xdata = [1 128];
    Img.Ydata = [1 128];
    Img.CDataMapping = 'Scaled';
    Img.Erasemode = 'none';

    Ctl = Std;
    Ctl.Units = 'Pixels';
    Ctl.Parent = NrfiltDemoFig;

    Btn = Ctl;
    Btn.Style = 'pushbutton';
    Btn.Enable = 'off';

    Edit = Ctl;
    Edit.Style = 'edit';
    Edit.HorizontalAlignment = 'right';
    Edit.BackgroundColor = 'white';
    Edit.ForegroundColor = 'black';

    Menu = Ctl;
    Menu.Style = 'Popupmenu';

    Text = Ctl;
    Text.Style = 'text';
    Text.HorizontalAlignment = 'left';
    Text.BackgroundColor = bgcolor;
    Text.ForegroundColor = fgcolor;

    btnHt = 26;
    txtHt = 18;
    menuHt = 26;
    editHt = 21;

    %================================
    % Original Image
    ud.hOriginalAxes = axes(Ax, ...
    'Position', [hs rows-vs*1.3-128 128 128],...
    'fontname','VNI-Souvir');
    title('ẢNH GỐC');
    ud.hOriginalImage = image(Img, ...
    'Parent', ud.hOriginalAxes);

    %================================
    % Corrupted Image
    ud.hCorruptedAxes = axes(Ax, ...
    'Position', [2*hs+128 rows-vs*1.3-128 128 128],...
    'fontname','VNI-Souvir');
    title('ẢNH NHIỄU');
    ud.hCorruptedImage = image(Img, ...
    'Parent', ud.hCorruptedAxes);
    ud.CorruptedImageIsStale = 1;

    %================================
    % Filtered Image
    ud.hFilteredAxes = axes(Ax, ...
    'Position', [3*hs+256 rows-vs*1.3-128 128 128],...
    'fontname','VNI-Souvir');
    title('ẢNH ĐƯỢC LỌC');
    ud.hFilteredImage = image(Img, ...
    'Parent', ud.hFilteredAxes);

    %=================================
    % The frame
    fleft = ifs;
    fbot = btnHt+3.2*ifs;
    fwid = cols - 2*hs/3;
    fht = rows-vs-hs/2-128-btnHt-3.2*ifs;
    ud.hControlFrame = uicontrol(Std, ...
    'Parent', NrfiltDemoFig, ...
    'Style', 'Frame', ...
    'Units', 'pixels', ...
    'Position', [fleft fbot fwid fht], ...
    'BackgroundColor', bgcolor);

    menuWid = (fwid-4*ifs)/3;
    menuBot = fbot+fht-vs-menuHt;
    labelBot = fbot+fht-22; % For the labels above the three top menus
    labelWid = menuWid/2; % For the labels in front of edit boxes
    %%% The three menus at the top of the frame:
    %================================
    % Image popup menu
    ipleft = fleft+ifs;
    ud.hImgPop = uicontrol(Menu, ...
    'Position',[ipleft menuBot menuWid menuHt],'fontname','VNI-Souvir',..., ...
    'String','tire|moon|saturn|bag|text|Quarter|Bone Marrow|Aluminum', ...
    'Callback','thuchiendemo(''LoadNewImage'')');
    % Text label for Image Menu Popup
    uicontrol( Text, ...
    'Position',[ipleft labelBot menuWid txtHt],'fontname','VNI-Souvir',...
    'String','Chọn Ảnh:');
    %================================
    % Add Noise menu:
    anleft = fleft+2*ifs+menuWid;
    ud.hAddNoisePop = uicontrol(Menu, ...
    'Position',[anleft menuBot menuWid menuHt], ...
    'String','Salt & Pepper|Gaussian|Speckle', ...
    'Callback','thuchiendemo(''UpdateNoiseType'')');
    % Text label for Image Menu Popup
    uicontrol( Text, ...
    'Position',[anleft labelBot menuWid txtHt],'fontname','VNI-Souvir', ...
    'String','LOẠI NHIỄU ẢNH:');

    %================================
    % Noise Removal menu:
    nrleft = fleft+3*ifs+2*menuWid;
    ud.hNoiseRemovalPop = uicontrol(Menu, ...
    'Position',[nrleft menuBot menuWid menuHt], ...
    'String','Median|Averaging|Adaptive', ...
    'Callback','thuchiendemo(''UpdateFilterType'')');
    % Text label for Image Menu Popup
    uicontrol( Text, ...
    'Position',[nrleft labelBot menuWid txtHt],'fontname','VNI-Souvir', ...
    'String','PHƯƠNG PHÁP LỌC NHIỄU:');

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
    %%% The controls for the different types of noise
    %%%

    %=================================================
    % Mean and Variance edit boxes for Gaussian noise
    ud.hGaussianMean = uicontrol(Edit, ...
    'Position',[anleft+labelWid menuBot-editHt-ifs menuWid-labelWid editHt], ...
    'String','0.0',...
    'callback','thuchiendemo(''EditBoxUpdate'',''Gauss ianMean'')');
    ud.hTxtGaussianMean = uicontrol( Text, ...
    'Position',[anleft menuBot-editHt-ifs labelWid txtHt], ...
    'String','Mean:');
    ud.GaussianMean = 0.0;

    ud.hGaussianVariance = uicontrol(Edit, ...
    'Position',[anleft+labelWid menuBot-2*editHt-2*ifs menuWid-labelWid editHt], ...
    'String','0.005',...
    'callback','thuchiendemo(''EditBoxUpdate'',''Gauss ianVariance'')');
    ud.hTxtGaussianVariance = uicontrol( Text, ...
    'Position',[anleft menuBot-2*editHt-2*ifs labelWid txtHt], ...
    'String','Variance:');
    ud.GaussianVariance =0.005;
    %=================================================
    % Noise Density for Salt & Pepper noise
    ud.hSaltNPepperDensity = uicontrol(Edit, ...
    'Position',[anleft+labelWid menuBot-editHt-ifs menuWid-labelWid editHt], ...
    'String','0.1',...
    'callback','thuchiendemo(''EditBoxUpdate'',''SaltN PepperDensity'')');
    ud.hTxtSaltNPepperDensity = uicontrol( Text, ...
    'Position',[anleft menuBot-editHt-ifs labelWid txtHt],'fontname','VNI-Souvir', ...
    'String','Cường độ nhiễu:');
    ud.SaltNPepperDensity = 0.1;
    %=================================================
    % Noise Variance for Speckle noise
    ud.hSpeckleVariance = uicontrol(Edit, ...
    'Position',[anleft+labelWid menuBot-editHt-ifs menuWid-labelWid editHt], ...
    'String','0.04',...
    'callback','thuchiendemo(''EditBoxUpdate'',''Speck leVariance'')');
    ud.hTxtSpeckleVariance = uicontrol( Text, ...
    'Position',[anleft menuBot-editHt-ifs labelWid txtHt], ...
    'String','Variance:');
    ud.SpeckleVariance = 0.04;
    %==========================================
    % Add noise button
    ud.hAddNoiseBtn = uicontrol(Btn, ...
    'Position',[anleft fbot+ifs menuWid btnHt],'fontname','VNI-Souvir', ...
    'String','THÊM NHIỄU', ...
    'Callback','thuchiendemo(''AddNoise'')');

    %%% The controls for the noise removal filters
    %================================================= ==
    % Neighborhood selection for median filter

    ud.hNeighborhoodPop = uicontrol(Menu, ...
    'Position',[nrleft menuBot-menuHt-ifs-txtHt menuWid menuHt], ...
    'String','3-by-3|5-by-5|7-by-7', ...
    'Callback','thuchiendemo(''UpdateNeighborhoodSize' ')');
    % Text label for Image Menu Popup
    uicontrol( Text, ...
    'Position',[nrleft menuBot-ifs-txtHt menuWid txtHt],'fontname','VNI-Souvir', ...
    'String','VÙNG LÂN CẬN LỌC:');
    %==========================================
    % Apply Filter button
    ud.hApplyFilterBtn = uicontrol(Btn, ...
    'Position',[nrleft fbot+ifs menuWid btnHt],'fontname','VNI-Souvir', ...
    'String','THỰC HIỆN', ...
    'Callback','thuchiendemo(''ApplyFilter'')');
    %====================================
    % Frame for Info and Close
    spac = ifs*2/3;
    ficleft = cols/2;
    ficbot = ifs;
    ficht = btnHt+2*spac;
    ficwid = cols/2-ifs;
    ud.hInfoCloseFrame = uicontrol(Std, ...
    'Parent', NrfiltDemoFig, ...
    'Style', 'Frame', ...
    'Units', 'pixels', ...
    'Position', [ficleft ficbot ficwid ficht], ...
    'BackgroundColor', bgcolor);

    %====================================
    % Buttons - Info and Close
    ud.hInfo=uicontrol(Btn, ...
    'Position',[ficleft+spac ficbot+spac (ficwid-3*spac)/2 btnHt], ...
    'String','Info', ...
    'Callback','helpwin thuchiendemo');

    ud.hClose=uicontrol(Btn, ...
    'Position',[ficleft+ficwid/2+spac/2 ficbot+spac (ficwid-3*spac)/2 btnHt], ...
    'fontname','VNI-Souvir','String','THOÁT', ...
    'Callback','closeall');



    %====================================
    % Status bar
    ud.hStatus = uicontrol(Std, ...
    'Parent', NrfiltDemoFig, ...
    'Style','text', ...
    'Units','pixels', ...
    'Position',[ifs ifs cols/2-2*ifs txtHt*2], ...
    'Foreground', [.8 0 0], ...
    'Background',wdcolor, ...
    'Horiz','center', ...
    'Tag', 'Status', ...
    'String','Initializing thuchiendemo...');

    set(NrfiltDemoFig, 'UserData', ud);
    UpdateNoiseType(NrfiltDemoFig);
    UpdateFilterType(NrfiltDemoFig);
    UpdateNeighborhoodSize(NrfiltDemoFig);
    set(NrfiltDemoFig, 'visible','on','HandleVisibility','callback');

    LoadNewImage(NrfiltDemoFig)
    set([ud.hAddNoiseBtn ud.hApplyFilterBtn ud.hInfo ud.hClose], 'Enable', 'on');
    return

    %%% Sub-Function - AddNoise
    function AddNoise(DemoFig)
    % Add noise to the original image. Optional input is Demo figure handle.

    if nargin<1
    DemoFig = gcbf;
    end
    set(DemoFig,'Pointer','watch');
    setstatus(DemoFig,'Adding noise to the original image...');
    ud=get(DemoFig,'Userdata');
    orig = getimage(ud.hOriginalImage);

    switch ud.NoiseType
    case 'Salt & Pepper'
    noisy = imnoise(orig, 'salt & pepper', ud.SaltNPepperDensity);
    case 'Gaussian'
    noisy = imnoise(orig, 'gaussian', ud.GaussianMean, ud.GaussianVariance);
    case 'Speckle'
    noisy = imnoise(orig, 'speckle', ud.SpeckleVariance);
    end

    set(ud.hCorruptedImage, 'Cdata', noisy);
    AddNoiseOff_ApplyFilterOn(DemoFig);
    set(DemoFig,'Pointer','arrow');
    setstatus(DemoFig,'Press "Apply Filter" to denoise the corrupted image.');
    ud.CorruptedImageIsStale = 0;
    set(DemoFig, 'UserData', ud);

    drawnow
    return;

    %%% Sub-Function - ApplyFilter

    function ApplyFilter(DemoFig)
    % Apply noise removal filter to the corrupted image.
    % Optional input is Demo figure handle.

    if nargin<1
    DemoFig = gcbf;
    end
    set(DemoFig,'Pointer','watch');
    setstatus(DemoFig,'Applying noise removal filter to the corrupted image...');
    ud=get(DemoFig,'Userdata');
    noisy = getimage(ud.hCorruptedImage);
    hood = ud.NeighborhoodSize;

    switch ud.FilterType
    case 'Median'
    filtered = medfilt2(noisy, [hood hood]);
    case 'Averaging'
    myfilt = ones(hood)/(hood^2); % Boxcar filter
    filtered = filter2(myfilt,noisy,'same');
    case 'Adaptive'
    filtered = wiener2(noisy,[hood hood]);
    end

    set(ud.hFilteredImage, 'Cdata', filtered);

    setstatus(DemoFig,'');
    set(ud.hApplyFilterBtn, 'enable', 'off');
    set(DemoFig,'Pointer','arrow');
    drawnow
    return;

    %%% Sub-Function - LoadNewImage

    function LoadNewImage(DemoFig)
    % Load a new image from a mat-file

    if nargin<1
    DemoFig = gcbf;
    end

    set(DemoFig,'Pointer','watch');
    ud=get(DemoFig,'Userdata');
    v = get(ud.hImgPop,{'value','String'});
    name = deblank(v{2}(v{1},);
    drawnow

    switch name
    case 'tire'
    tire = []; % Hint for the parser to avoid confusing it when load
    load imdemos tire % creates a new variable.
    img = tire;
    case 'moon'
    moon = []; % Hint for the parser to avoid confusing it when load
    load imdemos moon % creates a new variable.
    img = moon;
    case 'saturn',
    saturn = [];
    load imdemos saturn
    img = saturn;
    case 'bag',
    bag = [];
    load imdemos bag
    img = bag;
    case 'text',
    text = [];
    load imdemos text
    img = text;
    case 'Aluminum',
    alumgrns = [];
    load imdemos alumgrns
    img = alumgrns;
    case 'Flower',
    flower = [];
    load imdemos flower
    img = flower;
    case 'Rice',
    rice = [];
    load imdemos rice
    img = rice;
    case 'Bone Marrow',
    bonemarr = [];
    load imdemos bonemarr
    img = bonemarr;
    otherwise
    error('thuchiendemo: Unknown Image Option!');
    end

    img = double(img)/255;
    set(ud.hOriginalImage, 'Cdata', img);
    set(ud.hAddNoiseBtn, 'Enable', 'on');
    AddNoise(DemoFig);
    ApplyFilter(DemoFig);
    return;



    %%%
    %%% Sub-Function - EditBoxUpdate
    %%%

    function EditBoxUpdate(whichBox)
    % Update the values in the userdata structure from the string in the
    % whichBox edit box. whichBox is the string which specifies which
    % edit box the callback came from.

    DemoFig = gcbf;
    ud=get(DemoFig,'Userdata');
    switch whichBox
    case 'SpeckleVariance'
    str = get(ud.hSpeckleVariance, 'String');
    new = str2double(str);
    if isempty(new) | prod(size(new))>1 | new>1 | new<0
    errordlg('The variance for Speckle noise should be a scalar in [0,1]');
    else
    ud.SpeckleVariance = real(new(1));
    end
    set(ud.hSpeckleVariance, 'String', num2str(ud.SpeckleVariance));
    case 'SaltNPepperDensity'
    str = get(ud.hSaltNPepperDensity, 'String');
    new = str2double(str);
    if isempty(new) | prod(size(new))>1 | abs(new)>1
    errordlg('The noise density for Salt & Pepper noise should be a scalar in [0,1]');
    else
    ud.SaltNPepperDensity = real(new(1));
    end
    set(ud.hSaltNPepperDensity, 'String', num2str(ud.SaltNPepperDensity));
    case 'GaussianVariance'
    str = get(ud.hGaussianVariance, 'String');
    new = str2double(str);
    if isempty(new) | prod(size(new))>1 | new>1 | new<0
    errordlg('The variance for Gaussian noise should be a scalar in [0,1]');
    else
    ud.GaussianVariance = real(new(1));
    end
    set(ud.hGaussianVariance, 'String', num2str(ud.GaussianVariance))
    case 'GaussianMean'
    str = get(ud.hGaussianMean, 'String');
    new = str2double(str);
    if isempty(new) | prod(size(new))>1 | abs(new)>1
    errordlg('The mean for Gaussian noise should be a scalar in [-1,1]');
    else
    ud.GaussianMean = real(new(1));
    end
    set(ud.hGaussianMean, 'String', num2str(ud.GaussianMean))
    end
    AddNoiseOn_ApplyFilterOff(DemoFig);
    ud.CorruptedImageIsStale = 1;
    setstatus('Press "THÊM NHIỄU" to add noise to the original image.');
    set(DemoFig,'Userdata',ud);

    %%% Sub-Function - AddNoiseOff_ApplyFilterOn

    function AddNoiseOff_ApplyFilterOn(DemoFig)
    % Enable Apply Filter button, disable the Add Noise button.

    ud = get(DemoFig, 'UserData');
    set(ud.hAddNoiseBtn, 'enable', 'off');
    set(ud.hApplyFilterBtn, 'enable', 'on');

    %%% Sub-Function - AddNoiseOn_ApplyFilterOff

    function AddNoiseOn_ApplyFilterOff(DemoFig)
    % Disable Apply Filter button, enable the Add Noise button.

    ud = get(DemoFig, 'UserData');
    set(ud.hAddNoiseBtn, 'enable', 'on');
    set(ud.hApplyFilterBtn, 'enable', 'off');
    return

    %%% Sub-Function - UpdateNoiseType

    function UpdateNoiseType(DemoFig)
    % Update the noise type in the userdata structure based on the
    % value of the noise popup.

    if nargin<1
    DemoFig = gcbf;
    end
    set(DemoFig,'Pointer','watch');
    ud=get(DemoFig,'Userdata');
    v = get(ud.hAddNoisePop,{'value','String'});
    noisetype = deblank(v{2}(v{1},);
    ud.NoiseType = noisetype;
    drawnow

    switch noisetype
    case 'Salt & Pepper'
    BringUpSaltNPepperControls(DemoFig);
    case 'Gaussian'
    BringUpGaussianControls(DemoFig);
    case 'Speckle'
    BringUpSpeckleControls(DemoFig);
    end
    AddNoiseOn_ApplyFilterOff(DemoFig);
    setstatus('Press "THÊM NHIỄU" to add noise to the original image.');
    ud.CorruptedImageIsStale = 1;
    set(DemoFig,'Pointer','arrow','Userdata', ud);
    return

    %%% Sub-Function - BringUpGaussianControls

    function BringUpGaussianControls(DemoFig)

    if nargin<1
    DemoFig = gcbf;
    end
    ud = get(DemoFig,'Userdata');
    % Salt & Pepper
    set([ud.hSaltNPepperDensity ud.hTxtSaltNPepperDensity], ...
    'Enable', 'off', 'visible', 'off');
    %Speckle
    set([ud.hSpeckleVariance ud.hTxtSpeckleVariance], ...
    'Enable', 'off', 'visible', 'off');
    % Gaussian
    set([ud.hGaussianMean ud.hTxtGaussianMean ud.hGaussianVariance ...
    ud.hTxtGaussianVariance], 'Enable', 'on', 'visible', 'on');
    return


    %%% Sub-Function - BringUpSaltNPepperControls

    function BringUpSaltNPepperControls(DemoFig)

    if nargin<1
    DemoFig = gcbf;
    end
    ud = get(DemoFig,'Userdata');
    % Gaussian
    set([ud.hGaussianMean ud.hTxtGaussianMean ud.hGaussianVariance ...
    ud.hTxtGaussianVariance], 'Enable', 'off', 'visible', 'off');
    %Speckle
    set([ud.hSpeckleVariance ud.hTxtSpeckleVariance], ...
    'Enable', 'off', 'visible', 'off');
    % Salt & Pepper
    set([ud.hSaltNPepperDensity ud.hTxtSaltNPepperDensity], ...
    'Enable', 'on', 'visible', 'on');

    %%% Sub-Function - BringUpSpeckleControls

    function BringUpSpeckleControls(DemoFig)

    if nargin<1
    DemoFig = gcbf;
    end
    ud = get(DemoFig,'Userdata');
    % Gaussian
    set([ud.hGaussianMean ud.hTxtGaussianMean ud.hGaussianVariance ...
    ud.hTxtGaussianVariance], 'Enable', 'off', 'visible', 'off');
    % Salt & Pepper
    set([ud.hSaltNPepperDensity ud.hTxtSaltNPepperDensity], ...
    'Enable', 'off', 'visible', 'off');
    %Speckle
    set([ud.hSpeckleVariance ud.hTxtSpeckleVariance], ...
    'Enable', 'on', 'visible', 'on');

    %%% Sub-Function - UpdateFilterType

    function UpdateFilterType(DemoFig)
    % Update the filter type in the userdata structure based on the
    % value of the filter type popup.

    if nargin<1
    DemoFig = gcbf;
    end
    ud=get(DemoFig,'Userdata');
    v = get(ud.hNoiseRemovalPop,{'value','String'});
    ud.FilterType = deblank(v{2}(v{1},);
    if ~ud.CorruptedImageIsStale
    set(ud.hApplyFilterBtn, 'Enable', 'on');
    setstatus('Press "THỰC HIỆN" to denoise the corrupted image.');
    end
    set(DemoFig, 'UserData', ud);

    %%% Sub-Function - UpdateNeighborhoodSize

    function UpdateNeighborhoodSize(DemoFig)
    % Update the nbhood size in the userdata structure based on the
    % value of the Nieghborhood popup.

    if nargin<1
    DemoFig = gcbf;
    end
    set(DemoFig,'Pointer','watch');
    ud=get(DemoFig,'Userdata');
    v = get(ud.hNeighborhoodPop,{'value','String'});
    neighborhood = deblank(v{2}(v{1},);
    ud.NeighborhoodSize = str2double(neighborhood(1));
    if ~ud.CorruptedImageIsStale
    set(ud.hApplyFilterBtn, 'Enable', 'on');
    setstatus('Press "THỰC HIỆN" to denoise the corrupted image.');
    end
    set(DemoFig, 'UserData', ud, 'Pointer', 'arrow');
    Attached Files
  • bakabond
    Nhập môn đệ tử
    • Feb 2008
    • 5

    #2
    tuyệt quá !

    Comment

    • sonbk806
      Nhập môn đệ tử
      • Sep 2008
      • 2

      #3
      Thanks nhieu nha.

      Comment

      • anhnt
        Nhập môn đệ tử
        • Jan 2009
        • 2

        #4
        Rất hay bạn ạ
        Lao động vất vả, thành quả ngọt ngào!

        Comment

        • vanloielec
          Nhập môn đệ tử
          • Dec 2008
          • 4

          #5
          Cam on nhiều a!
          Các bác chỉ cụ thể hơn nuă được không ạ?

          Comment

          • sonha_qy
            Nhập môn đệ tử
            • Aug 2009
            • 2

            #6
            bạn ơi có thể cho mình một đoạn nhỏ thực hiện lọc nhiễu với bộ lọc điêm giữa dùng hàm medfilt2 không .Mình mới nghiên cứu linh vực này cám ơn bạn

            Comment

            • cosocola
              Nhập môn đệ tử
              • Jan 2011
              • 1

              #7
              xử lý ảnh nhiễu!

              Cám ơn anh nhiều!Em tìm quá chừng, giờ mới có người nói cho em cái em muốn biết

              Comment

              • khuyên
                Nhập môn đệ tử
                • Nov 2012
                • 1

                #8
                bạn ơi.cho mình hỏi chương trình bạn viết chạy trên j thế(((

                Comment

                • hieu_tm
                  Nhập môn đệ tử
                  • Oct 2011
                  • 1

                  #9
                  cái code này sao ko chạy được vậy hè,với ai làm median filter bằng verilog với up code lên tham khảo được không

                  Comment

                  Working...