首页 \ 问答 \ numpy:根据多个条件将值设置为零(numpy: Setting values to zero based on multiple conditions)

numpy:根据多个条件将值设置为零(numpy: Setting values to zero based on multiple conditions)

我有一个RGB图像,我正在尝试使用类似下面的方法为它执行一个简单的阈值处理:

from skimage import filter
def threshold(image):
    r = image[:, :, 0]
    g = image[:, :, 1]
    b = image[:, :, 2]

    rt = filter.threshold_otsu(r)
    gt = filter.threshold_otsu(g)
    bt = filter.threshold_otsu(b)

我现在要做的是制作一个二进制掩码,其中原始图像中小于这些阈值的RGB值应该设置为0。

mask = np.ones(r.shape)

我无法弄清楚如何做的是如何将掩码索引(x,y)设置为零

image[x, y, 0] < rt and image[x, y, 1] < gt and image [x, y, 2] < bt

不知怎的,我需要从这个原始图像中获得满足这个标准的(x,y)像素索引,但我不知道如何做到这一点。


I have an RGB image and I am trying to perform a simple thresholding for it using something like this:

from skimage import filter
def threshold(image):
    r = image[:, :, 0]
    g = image[:, :, 1]
    b = image[:, :, 2]

    rt = filter.threshold_otsu(r)
    gt = filter.threshold_otsu(g)
    bt = filter.threshold_otsu(b)

What I would like to do is now make a binary mask where the RGB values in the original image which are less than these threshold value should be set to 0.

mask = np.ones(r.shape)

What I cannot figure out how to do is how do I set the mask indices (x, y) to zero where the

image[x, y, 0] < rt and image[x, y, 1] < gt and image [x, y, 2] < bt

Somehow I need to get the (x, y) pixel indices from this original image which meets this criteria but I am not sure how to do this.


原文:https://stackoverflow.com/questions/28279438
更新时间:2023-11-06 21:11

最满意答案

你几乎是正确的。 唯一的区别是方向标志( DF )控制是否从EDI增加或减去4(并且它实际上偏离了ES段基础,但你可能不关心):

for (; regs.d.ecx != 0; regs.d.ecx--)
{
    *(unsigned int *)(regs.d.edi) = regs.d.eax;
    regs.d.edi += regs.eflags.df ? -4 : 4;
}

请注意, for (; regs.d.ecx != 0; regs.d.ecx--) { }REP前缀的操作,循环的主体是STOS DWORD...

由于您提出了很多这些问题,我认为您会发现英特尔64和IA-32架构软件开发人员手册第2A和2B卷非常有用。 这些包含每个指令和前缀的描述,包括伪代码描述。


You are almost correct. The only difference is that the direction flag (DF) controls whether 4 is added or subtracted from EDI (and it actually is offset from the ES segment base, but you probably don't care about that):

for (; regs.d.ecx != 0; regs.d.ecx--)
{
    *(unsigned int *)(regs.d.edi) = regs.d.eax;
    regs.d.edi += regs.eflags.df ? -4 : 4;
}

Note that the for (; regs.d.ecx != 0; regs.d.ecx--) { } is the action of the REP prefix, and the body of the loop is the action of STOS DWORD....

Since you are asking a lot of these questions, I think you will find the Intel 64 and IA-32 Architectures Software Developer’s Manual, Volumes 2A and 2B to be useful. These contain descriptions of each instruction and prefix, including pseudo-code descriptions.

相关问答

更多
  • 有一个完整的博客以该指令命名。 第一篇文章描述了它背后的原因: http : //repzret.org/p/repzret/ 基本上,AMD的分支预测器存在一个问题,即当单字节ret立即跟随引用的代码(和其他一些情况)进行条件跳转时,解决方法是添加rep前缀,该前缀被CPU,但修复了预测值惩罚。 There's a whole blog named after this instruction. And the first post describes the reason behind it: http ...
  • 你遇到的瓶颈是因为你不是异步简化你的沟通。 尝试用异步ROUTER <-> DEALER模式替换您的同步REQ <-> REP模式。 这可以更快的原因是,如果客户端可以发送连续的请求,而不必等待中间的每个响应。 处理单个请求/回复的成本分为两部分: 发送消息“通过线路”的成本 根据请求进行处理和/或在客户端和服务器上进行回复的成本 异步模式有助于在运行大量连续请求时大大降低(2)的成本。 The bottleneck you are hitting is because you aren't asynchr ...
  • 你几乎是正确的。 唯一的区别是方向标志( DF )控制是否从EDI增加或减去4(并且它实际上偏离了ES段基础,但你可能不关心): for (; regs.d.ecx != 0; regs.d.ecx--) { *(unsigned int *)(regs.d.edi) = regs.d.eax; regs.d.edi += regs.eflags.df ? -4 : 4; } 请注意, for (; regs.d.ecx != 0; regs.d.ecx--) { }是REP前缀的操作,循 ...
  • 我认为这是R中那些算术不准确的问题之一。问题在于: prop.ref <- 1-prop.int-prop.control prop.ref*10 #[1] 2 floor(prop.ref*10) #[1] 1 因此r认为prop.int+prop.control略大于0.8 你可以解决它 cohort <- rep(1:n.cohorts, ceiling(n.sites)) 但你是对的,它确实看起来像一个严重的错误编辑 - 抱歉意味着SEEM像一个严重的 I think this is one ...
  • 您引用的文件似乎显着不准确。 以下是GCC的实际操作数约束: 输入:汇编操作从该操作数读取。 GCC假定所有读取在组装操作的最初阶段同时发生 。 输出:汇编操作写入此操作数; 完成后,相关变量将具有有意义的值。 (没有办法告诉GCC这个值是什么。)GCC假定所有的写操作在组装操作的最后阶段同时发生 。 Clobber:汇编操作会破坏此操作数中的任何有意义的值。 与写作一样,所有破坏者都被假定在手术结束时同时发生 。 Earlyclobber:除了在操作开始时发生外,与clobber相同。 此外,目前的(GC ...
  • 根据?rep , times=可以是一个向量。 那么,这个怎么样: dat <- data.frame(name=rep(al, times=c(3,3,3,6))) 如果你的“状态”数据在列表中,这也会更方便。 stateData <- list(al,ak,az,ar) Data <- lapply(stateData, function(x) data.frame(name=rep(x, times=c(3,3,3,6)))) Data <- do.call(rbind, Data) Accord ...
  • rep(list(c(2,6)),8)就是答案 - 感谢Nicola的评论。 rep(list(c(2,6)),8) is the answer - thanks to Nicola in comments.
  • 是。 不一定是故意的,但你可以使用它们进行某种优化。 例如,它比使用movs更快(假设rsi,rsi指向正确的位置) mov rax,[whatever1] mov [whatever2],rax 对于其他人,我现在不确定,但我猜想可以查看执行时间。 实际上rsi, rdi增加/减少rsi, rdi会产生副作用。 此外,在低级模式下打印C样式字符串(无格式或特殊字符;直接视频内存访问)如下所示: ; ... _load_char: lodsb or al,al jz _en ...
  • 假设cat是这里唯一的相关分组变量(不是date和loc),你可以这样做: library(dplyr) df = df %>% group_by(cat) %>% mutate(type = rep(1:2, length.out = length(cat))) # Output: date loc cat type 1 2017-01-01 AB a 1 2 ...
  • 该错误是由于V-REP使用Lua 5.1 ,而我们测试它的计算机有不同的数字区域设置(linux有LC_NUMERIC=cs_CZ.UTF-8 ,而mac有可能是en_US )。 这意味着Mac上的Lua将字符串中的浮点数识别为数字,但Linux上具有不同语言环境的Lua没有 - 它没有逗号 (例如-3,513 )作为区域设置所需的小数分隔符,因此它返回没有转换。 修复是在将vrep运行到en_US语言环境之前设置LC_NUMERIC标志,如下所示: ...$ LC_NUMERIC=en_US.UTF-8 ...

相关文章

更多

最新问答

更多
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • Java中的不可变类(Immutable class in Java)
  • 寻求多次出现的表达式(Seeking for more than one occurrence of an expression)
  • linux只知道文件名,不知道在哪个目录,怎么找到文件所在目录
  • Actionscript:检查字符串是否包含域或子域(Actionscript: check if string contains domain or subdomain)
  • 懒惰地初始化AutoMapper(Lazily initializing AutoMapper)
  • 使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
  • Windows Phone 7:检查资源是否存在(Windows Phone 7: Check If Resource Exists)
  • EXCEL VBA 基础教程下载
  • RoR - 邮件中的动态主体(部分)(RoR - Dynamic body (part) in mailer)
  • 无法在Google Script中返回2D数组?(Can not return 2D Array in Google Script?)
  • JAVA环境变量的设置和对path , classpth ,java_home设置作用和目的?
  • mysql 关于分组查询、时间条件查询
  • 如何使用PowerShell匹配运算符(How to use the PowerShell match operator)
  • Effective C ++,第三版:重载const函数(Effective C++, Third edition: Overloading const function)
  • 如何用DELPHI动态建立MYSQL的数据库和表? 请示出源代码。谢谢!
  • 带有简单redis应用程序的Node.js抛出“未处理的错误”(Node.js with simple redis application throwing 'unhandled error')
  • 使用前端框架带来哪些好处,相对于使用jquery
  • Ruby将字符串($ 100.99)转换为float或BigDecimal(Ruby convert string ($100.99) to float or BigDecimal)
  • 高考完可以去做些什么?注意什么?
  • 如何声明放在main之后的类模板?(How do I declare a class template that is placed after the main?)
  • 如何使用XSLT基于兄弟姐妹对元素进行分组(How to group elements based on their siblings using XSLT)
  • 在wordpress中的所有页面的标志(Logo in all pages in wordpress)
  • R:使用rollapply对列组进行求和的问题(R: Problems using rollapply to sum groups of columns)
  • Allauth不会保存其他字段(Allauth will not save additional fields)
  • python中使用sys模块中sys.exit()好像不能退出?
  • 将Int拆分为3个字节并返回C语言(Splitting an Int to 3 bytes and back in C)
  • 在SD / MMC中启用DDR会导致问题吗?(Enabling DDR in SD/MMC causes problems? CMD 11 gives a response but the voltage switch wont complete)
  • sed没有按预期工作,从字符串中间删除特殊字符(sed not working as expected, removing special character from middle of string)
  • 如何将字符串转换为Elixir中的函数(how to convert a string to a function in Elixir)