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.

相关问答

更多

“rep ret”是什么意思?(What does `rep ret` mean?)

有一个完整的博客以该指令命名。 第一篇文章描述了它背后的原因: 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:...

提高ZeroMQ REQ / REP性能(Improve ZeroMQ REQ/REP performance)

你遇到的瓶颈是因为你不是异步简化你的沟通。 尝试用异步ROUTER <-> DEALER模式替换您的同步REQ <-> REP模式。 这可以更快的原因是,如果客户端可以发送连续的请求,而不必等待中间的每个响应。 处理单个请求/回复的成本分为两部分: 发送消息“通过线路”的成本 根据请求进行处理和/或在客户端和服务器上进行回复的成本 异步模式有助于在运行大量连续请求时大大降低(2)的成本。 The bottleneck you are hitting is because you aren't as...

汇编如何将REP STOS转换为C代码(Assembly How to convert REP STOS to C code)

你几乎是正确的。 唯一的区别是方向标志( 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前缀...

为什么rep()与这个简单的R例子行为不一致?(why does rep() behave inconsistently with this simple R example?)

我认为这是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...

rep_movsl的Clobber列表(Clobber list for rep_movsl)

您引用的文件似乎显着不准确。 以下是GCC的实际操作数约束: 输入:汇编操作从该操作数读取。 GCC假定所有读取在组装操作的最初阶段同时发生 。 输出:汇编操作写入此操作数; 完成后,相关变量将具有有意义的值。 (没有办法告诉GCC这个值是什么。)GCC假定所有的写操作在组装操作的最后阶段同时发生 。 Clobber:汇编操作会破坏此操作数中的任何有意义的值。 与写作一样,所有破坏者都被假定在手术结束时同时发生 。 Earlyclobber:除了在操作开始时发生外,与clobber相同。 此外,目...

使用rep()函数(Working with the rep() function)

根据?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) A...

使用rep()创建列表(Create list using rep())

rep(list(c(2,6)),8)就是答案 - 感谢Nicola的评论。 rep(list(c(2,6)),8) is the answer - thanks to Nicola in comments.

有没有理由使用没有重复前缀的MOVS / CMPS / STOS / SCAS?(Is there any reason to use MOVS/CMPS/STOS/SCAS without a repeat prefix?)

是。 不一定是故意的,但你可以使用它们进行某种优化。 例如,它比使用movs更快(假设rsi,rsi指向正确的位置) mov rax,[whatever1] mov [whatever2],rax 对于其他人,我现在不确定,但我猜想可以查看执行时间。 实际上rsi, rdi增加/减少rsi, rdi会产生副作用。 此外,在低级模式下打印C样式字符串(无格式或特殊字符;直接视频内存访问)如下所示: ; ... _load_char: lodsb or al,al j...

通过dplyr在组内应用rep()(Applying rep() within groups through dplyr)

假设cat是这里唯一的相关分组变量(不是date和loc),你可以这样做: library(dplyr) df = df %>% group_by(cat) %>% mutate(type = rep(1:2, length.out = length(cat))) # Output: date loc cat type <fctr> <fctr> <fctr> <int> 1 2017-01-01 AB a ...

V-REP的Lua`tonumber`在Linux上返回nil(V-REP's Lua `tonumber` returns nil on Linux)

该错误是由于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.U...

相关文章

更多

Java-based 实现的索引复制

solr 包含一个基于 Java-based 实现的索引复制,通过http方式完成。 这 ...

Solr 4.6 | Setting Up an External ZooKeeper Ensemble | upgrade solr to Solr4.6

4.1-----&gt;4.6 Solr从4.1到4.6还是有不少改变的。。。 一、solr.xml ...

ext 的store中 baseParams.conditions 是什么意思啊?

小弟最近做个练习 在表格分页的时候 有个很帅的分页工具栏 最右边是一个刷新的按钮 调了下 发现他是 ...

Setting up Nutch 2.1 with MySQL to handle UTF-8

原文地址:http://nlp.solutions.asia/?p=180 These instruc ...

Guava Multimap类-映射到多个值

System.out.println(key)

solr 从零学习开始

开源企业搜索引擎SOLR的 应用教程 2010-10 目 录 1 概述... 4 1.1 企业搜索引擎 ...

solr 从零学习开始

1 概述 1.1 企业搜索引擎方案选型 由于搜索引擎功能在门户社区中对提高用户体验有着重在门户社区中 ...

Solr4:Tomcat7与Solr之多核配置(Multiple Cores)

1. 背景 多核,官方说法,让你只用一个Solr实例,实现多配置多索引的功能,为不同的应用保留不同的 ...

Guava Booleans类-布尔型基本的实用工具类

static int countTrue(boolean... values)返回为true值的数目

最新问答

更多

获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)

我用Google搜索了一个解决方案。 “EnumDisplayModeProvider”是我自己设置网站的各种模式的枚举。 public EnumDisplayModeProvider GetDisplayModeId() { foreach (var mode in DisplayModeProvider.Instance.Modes) if (mode.CanHandleContext(HttpContext)) {

如何通过引用返回对象?(How is returning an object by reference possible?)

这相对简单:在类的构造函数中,您可以分配内存,例如使用new 。 如果你制作一个对象的副本,你不是每次都分配新的内存,而是只复制指向原始内存块的指针,同时递增一个也存储在内存中的引用计数器,使得每个副本都是对象可以访问它。 如果引用计数降至零,则销毁对象将减少引用计数并仅释放分配的内存。 您只需要一个自定义复制构造函数和赋值运算符。 这基本上是共享指针的工作方式。 This is relatively easy: In the class' constructor, you allocate m

矩阵如何存储在内存中?(How are matrices stored in memory?)

正如它在“熵编码”中所说的那样,使用Z字形图案,与RLE一起使用,在许多情况下,RLE已经减小了尺寸。 但是,据我所知,DCT本身并没有给出稀疏矩阵。 但它通常会增强矩阵的熵。 这是compressen变得有损的点:输入矩阵用DCT传输,然后量化量化然后使用霍夫曼编码。 As it says in "Entropy coding" a zig-zag pattern is used, together with RLE which will already reduce size for man

每个请求的Java新会话?(Java New Session For Each Request?)

你是如何进行重定向的? 您是否事先调用了HttpServletResponse.encodeRedirectURL()? 在这里阅读javadoc 您可以使用它像response.sendRedirect(response.encodeRedirectURL(path)); The issue was with the path in the JSESSIONID cookie. I still can't figure out why it was being set to the tomca

css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)

我认为word-break ,如果你想在一个单词中打破行,你可以指定它,这样做可以解决问题: .column { word-break:break-all; } jsFiddle演示。 您可以在此处阅读有关word-break属性的更多信息。 I think word-break, with which you can specify if you want to break line within a word, will do the trick: .column { word-break

无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)

我认为您忘记在分类时间内缩放输入图像,如train_test.prototxt文件的第11行所示。 您可能应该在C ++代码中的某个位置乘以该因子,或者使用Caffe图层来缩放输入(请查看ELTWISE或POWER图层)。 编辑: 在评论中进行了一次对话之后,结果发现在classification.cpp文件中错误地删除了图像均值,而在原始训练/测试管道中没有减去图像均值。 I think you have forgotten to scale the input image during cl

xcode语法颜色编码解释?(xcode syntax color coding explained?)

转到: Xcode => Preferences => Fonts & Colors 您将看到每个语法高亮颜色旁边都有一个简短的解释。 Go to: Xcode => Preferences => Fonts & Colors You'll see that each syntax highlighting colour has a brief explanation next to it.

在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)

你考虑过第三方拼写检查吗? 您可以将在C#中开发的自定义WinForms控件插入访问数据库吗? VB6控件怎么样? 如果你能找到一个使用第三方库进行拼写检查的控件,那可能会有效。 Have you considered a third party spell checker? Can you insert a custom WinForms controls developed in C# into an access database? What about a VB6 control? If

从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)

我有同样的问题,因为我在远程服务器上有两个图像,我需要在每天的预定义时间复制到我的本地服务器,这是我能够提出的代码... try { if(@copy('url/to/source/image.ext', 'local/absolute/path/on/server/' . date("d-m-Y") . ".gif")) { } else { $errors = error_get_last(); throw new Exception($err

从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))

我不确定我完全明白你在说什么。 你能编辑你的帖子并包含你正在做的Subversion命令/操作的特定顺序吗? 最好使用命令行svn客户端,以便容易为其他人重现问题。 如果您只是想获取文件的旧副本(即使该文件不再存在),您可以使用如下命令: svn copy ${repo}/trunk/moduleA/file1@${rev} ${repo}/trunk/moduleB/file1 其中${repo}是您的存储库的URL, ${rev}是您想要的文件的版本。 这将恢复该文件的旧版本,包括最高版本