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
最满意答案
你几乎是正确的。 唯一的区别是方向标志(
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 fromEDI
(and it actually is offset from theES
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 theREP
prefix, and the body of the loop is the action ofSTOS 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?)[2022-08-14]
有一个完整的博客以该指令命名。 第一篇文章描述了它背后的原因: 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前缀的操作,循 ...
-
为什么rep()与这个简单的R例子行为不一致?(why does rep() behave inconsistently with this simple R example?)[2023-07-22]
我认为这是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 ... -
rep_movsl的Clobber列表(Clobber list for rep_movsl)[2022-04-05]
您引用的文件似乎显着不准确。 以下是GCC的实际操作数约束: 输入:汇编操作从该操作数读取。 GCC假定所有读取在组装操作的最初阶段同时发生 。 输出:汇编操作写入此操作数; 完成后,相关变量将具有有意义的值。 (没有办法告诉GCC这个值是什么。)GCC假定所有的写操作在组装操作的最后阶段同时发生 。 Clobber:汇编操作会破坏此操作数中的任何有意义的值。 与写作一样,所有破坏者都被假定在手术结束时同时发生 。 Earlyclobber:除了在操作开始时发生外,与clobber相同。 此外,目前的(GC ... -
使用rep()函数(Working with the rep() function)[2022-05-01]
根据?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()创建列表(Create list using rep())[2022-04-21]
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 ...