热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

matlab画图nan,在Matlab中过滤包含NaN的图像?

我最终使用的技术是Matlab的FileExchange中的功能nanconv.m.它完全符合我的要求:它以一种忽略NaN的方式运行过滤器,就像Matlab的内置函数

我最终使用的技术是Matlab的File Exchange中的功能

nanconv.m.它完全符合我的要求:它以一种忽略NaN的方式运行过滤器,就像Matlab的内置函数nanmean那样.这很难从功能的文档中解读,这有点神秘.

这是我如何使用它:

filtWidth = 7;

filtSigma = 5;

imageFilter=fspecial('gaussian',filtWidth,filtSigma);

dataFiltered = nanconv(data,imageFilter, 'nanout');

我正在粘贴下面的nanconv函数(它由the BSD license覆盖).当我有机会的时候,我会张贴图片等,只是想发布我最后做的事情,对于那些好奇我做了什么的人.

与其他答案的比较

使用gnovice’s solution,结果看起来非常直观,但是边缘有一些定量的亮点是一个值得关注的问题.在实践中,超出边缘的图像外推导致我的数据边缘处的许多虚假的高值.

使用krisdestruction’s suggestion用原始数据替换丢失的位,看起来也相当不错(特别是对于非常小的滤波器),但是(按设计)你最终会在边缘处得到未经滤波的数据,这对我的应用来说是一个问题.

nanconv

function c = nanconv(a, k, varargin)

% NANCONV Convolution in 1D or 2D ignoring NaNs.

% C = NANCONV(A, K) convolves A and K, correcting for any NaN values

% in the input vector A. The result is the same size as A (as though you

% called 'conv' or 'conv2' with the 'same' shape).

%

% C = NANCONV(A, K, 'param1', 'param2', ...) specifies one or more of the following:

% 'edge' - Apply edge correction to the output.

% 'noedge' - Do not apply edge correction to the output (default).

% 'nanout' - The result C should have NaNs in the same places as A.

% 'nonanout' - The result C should have ignored NaNs removed (default).

% Even with this option, C will have NaN values where the

% number of consecutive NaNs is too large to ignore.

% '2d' - Treat the input vectors as 2D matrices (default).

% '1d' - Treat the input vectors as 1D vectors.

% This option only matters if 'a' or 'k' is a row vector,

% and the other is a column vector. Otherwise, this

% option has no effect.

%

% NANCONV works by running 'conv2' either two or three times. The first

% time is run on the original input signals A and K, except all the

% NaN values in A are replaced with zeros. The 'same' input argument is

% used so the output is the same size as A. The second convolution is

% done between a matrix the same size as A, except with zeros wherever

% there is a NaN value in A, and ones everywhere else. The output from

% the first convolution is normalized by the output from the second

% convolution. This corrects for missing (NaN) values in A, but it has

% the side effect of correcting for edge effects due to the assumption of

% zero padding during convolution. When the optional 'noedge' parameter

% is included, the convolution is run a third time, this time on a matrix

% of all ones the same size as A. The output from this third convolution

% is used to restore the edge effects. The 'noedge' parameter is enabled

% by default so that the output from 'nanconv' is identical to the output

% from 'conv2' when the input argument A has no NaN values.

%

% See also conv, conv2

%

% AUTHOR: Benjamin Kraus (bkraus@bu.edu, ben@benkraus.com)

% Copyright (c) 2013, Benjamin Kraus

% $Id: nanconv.m 4861 2013-05-27 03:16:22Z bkraus $

% Process input arguments

for arg = 1:nargin-2

switch lower(varargin{arg})

case 'edge'; edge = true; % Apply edge correction

case 'noedge'; edge = false; % Do not apply edge correction

case {'same','full','valid'}; shape = varargin{arg}; % Specify shape

case 'nanout'; nanout = true; % Include original NaNs in the output.

case 'nonanout'; nanout = false; % Do not include NaNs in the output.

case {'2d','is2d'}; is1D = false; % Treat the input as 2D

case {'1d','is1d'}; is1D = true; % Treat the input as 1D

end

end

% Apply default options when necessary.

if(exist('edge','var')~=1); edge = false; end

if(exist('nanout','var')~=1); nanout = false; end

if(exist('is1D','var')~=1); is1D = false; end

if(exist('shape','var')~=1); shape = 'same';

elseif(~strcmp(shape,'same'))

error([mfilename ':NotImplemented'],'Shape ''%s'' not implemented',shape);

end

% Get the size of 'a' for use later.

sza = size(a);

% If 1D, then convert them both to columns.

% This modification only matters if 'a' or 'k' is a row vector, and the

% other is a column vector. Otherwise, this argument has no effect.

if(is1D);

if(~isvector(a) || ~isvector(k))

error('MATLAB:conv:AorBNotVector','A and B must be vectors.');

end

a = a(:); k = k(:);

end

% Flat function for comparison.

o = ones(size(a));

% Flat function with NaNs for comparison.

on = ones(size(a));

% Find all the NaNs in the input.

n = isnan(a);

% Replace NaNs with zero, both in 'a' and 'on'.

a(n) = 0;

on(n) = 0;

% Check that the filter does not have NaNs.

if(any(isnan(k)));

error([mfilename ':NaNinFilter'],'Filter (k) contains NaN values.');

end

% Calculate what a 'flat' function looks like after convolution.

if(any(n(:)) || edge)

flat = conv2(on,k,shape);

else flat = o;

end

% The line above will automatically include a correction for edge effects,

% so remove that correction if the user does not want it.

if(any(n(:)) && ~edge); flat = flat./conv2(o,k,shape); end

% Do the actual convolution

c = conv2(a,k,shape)./flat;

% If requested, replace output values with NaNs corresponding to input.

if(nanout); c(n) = NaN; end

% If 1D, convert back to the original shape.

if(is1D && sza(1) == 1); c = c.'; end

end



推荐阅读
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • IjustinheritedsomewebpageswhichusesMooTools.IneverusedMooTools.NowIneedtoaddsomef ... [详细]
  • 本文讨论了如何在codeigniter中识别来自angularjs的请求,并提供了两种方法的代码示例。作者尝试了$this->input->is_ajax_request()和自定义函数is_ajax(),但都没有成功。最后,作者展示了一个ajax请求的示例代码。 ... [详细]
  • Summarize function is doing alignment without timezone ?
    Hi.Imtryingtogetsummarizefrom00:00otfirstdayofthismonthametric, ... [详细]
  • 本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • 本文详细介绍了如何使用MySQL来显示SQL语句的执行时间,并通过MySQL Query Profiler获取CPU和内存使用量以及系统锁和表锁的时间。同时介绍了效能分析的三种方法:瓶颈分析、工作负载分析和基于比率的分析。 ... [详细]
  • Ihavethefollowingonhtml我在html上有以下内容<html><head><scriptsrc..3003_Tes ... [详细]
  • 本文介绍了在iOS开发中使用UITextField实现字符限制的方法,包括利用代理方法和使用BNTextField-Limit库的实现策略。通过这些方法,开发者可以方便地限制UITextField的字符个数和输入规则。 ... [详细]
  • 本文介绍了绕过WAF的XSS检测机制的方法,包括确定payload结构、测试和混淆。同时提出了一种构建XSS payload的方法,该payload与安全机制使用的正则表达式不匹配。通过清理用户输入、转义输出、使用文档对象模型(DOM)接收器和源、实施适当的跨域资源共享(CORS)策略和其他安全策略,可以有效阻止XSS漏洞。但是,WAF或自定义过滤器仍然被广泛使用来增加安全性。本文的方法可以绕过这种安全机制,构建与正则表达式不匹配的XSS payload。 ... [详细]
  • 纠正网上的错误:自定义一个类叫java.lang.System/String的方法
    本文纠正了网上关于自定义一个类叫java.lang.System/String的错误答案,并详细解释了为什么这种方法是错误的。作者指出,虽然双亲委托机制确实可以阻止自定义的System类被加载,但通过自定义一个特殊的类加载器,可以绕过双亲委托机制,达到自定义System类的目的。作者呼吁读者对网上的内容持怀疑态度,并带着问题来阅读文章。 ... [详细]
  • 本文介绍了使用C++Builder实现获取USB优盘序列号的方法,包括相关的代码和说明。通过该方法,可以获取指定盘符的USB优盘序列号,并将其存放在缓冲中。该方法可以在Windows系统中有效地获取USB优盘序列号,并且适用于C++Builder开发环境。 ... [详细]
  • 本文整理了Java中org.apache.solr.common.SolrDocument.setField()方法的一些代码示例,展示了SolrDocum ... [详细]
  • 花瓣|目标值_Compose 动画边学边做夏日彩虹
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了Compose动画边学边做-夏日彩虹相关的知识,希望对你有一定的参考价值。引言Comp ... [详细]
  • C语言的经典程序有哪些
    本篇内容介绍了“C语言的经典程序有哪些”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何 ... [详细]
author-avatar
手机用户2502908311
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有