- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试理解 Python 中的 cv2
的索贝尔卷积。
根据documentation索贝尔核是
-1 0 1
-2 0 2
-1 0 1
因此,我尝试将其应用于以下 img
(二进制 3x3
数组):
0 1 0
1 0 1
0 1 0
现在,我在解释输出时遇到问题。我手工计算并得到了不同的结果。据我所知,我必须将内核集中在每个像素 (i,j)
上,并按元素相乘并求和。
因此,输出中的第一个条目应该是 2
。程序返回0
。
我错了吗?我希望如此。
代码
import cv2
import numpy as np
img = np.array([[0,1,0],[1,0,1],[0,1,0]]).astype(float)
# Output dtype = cv2.CV_8U
sobelx8u = cv2.Sobel(img,cv2.CV_8U,1,0,ksize=3)
# Output dtype = cv2.CV_64F. Then take its absolute and convert to cv2.CV_8U
sobelx64f = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=3)
abs_sobel64f = np.absolute(sobelx64f)
sobel_8u = np.uint8(abs_sobel64f)
print 'img'
print img
print 'sobelx8u'
print sobelx8u
print 'sobelx64f'
print sobelx64f
print 'abs_sobel64f'
print abs_sobel64f
print 'sobel_8u'
print sobel_8u
输出
img
[[ 0. 1. 0.]
[ 1. 0. 1.]
[ 0. 1. 0.]]
sobelx8u
[[0 0 0]
[0 0 0]
[0 0 0]]
sobelx64f
[[ 0. 0. 0.]
[ 0. 0. 0.]
[ 0. 0. 0.]]
abs_sobel64f
[[ 0. 0. 0.]
[ 0. 0. 0.]
[ 0. 0. 0.]]
sobel_8u
[[0 0 0]
[0 0 0]
[0 0 0]]
最佳答案
阅读您的 documentation 的第二段页面:
Another common feature of the functions and classes described in this section is that, unlike simple arithmetic functions, they need to extrapolate values of some non-existing pixels. For example, if you want to smooth an image using a Gaussian 3x3 filter, then, when processing the left-most pixels in each row, you need pixels to the left of them, that is, outside of the image. You can let these pixels be the same as the left-most image pixels (“replicated border” extrapolation method), or assume that all the non-existing pixels are zeros (“constant border” extrapolation method), and so on. OpenCV enables you to specify the extrapolation method. For details, see the function
borderInterpolate()
and discussion of theborderType
parameter in the section and various functions below.
为了让它按照您期望的方式工作,您必须明确指定您想要用零值插入边框。像这样:
import cv2
import numpy as np
img = np.array([[0,1,0],[1,0,1],[0,1,0]]).astype(float)
border = cv2.borderInterpolate(0, 1, cv2.BORDER_CONSTANT)
sobelx64f = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=3, borderType=border)
print 'img'
print img
print 'sobelx64f'
print sobelx64f
输出:
img
[[ 0. 1. 0.]
[ 1. 0. 1.]
[ 0. 1. 0.]]
sobelx64f
[[ 2. 0. -2.]
[ 2. 0. -2.]
[ 2. 0. -2.]]
borderType
的默认值为 BORDER_DEFAULT
,在我的机器上与 BORDER_REFLECT_101
相同。您可以运行此脚本在您的计算机上进行确认:
import cv2
for var in dir(cv2):
if not var.startswith('BORDER_'): continue
if cv2.__dict__[var] == cv2.BORDER_DEFAULT:
print 'BORDER_DEFAULT ==', var
输出:
BORDER_DEFAULT == BORDER_DEFAULT
BORDER_DEFAULT == BORDER_REFLECT101
BORDER_DEFAULT == BORDER_REFLECT_101
并且 BORDER_REFLECT_101
的工作方式与您的结果完全一致。以下是不同边框类型的说明:
BORDER_REPLICATE: aaaaaa|abcdefgh|hhhhhhh
BORDER_REFLECT: fedcba|abcdefgh|hgfedcb
BORDER_REFLECT_101: gfedcb|abcdefgh|gfedcba
BORDER_WRAP: cdefgh|abcdefgh|abcdefg
BORDER_CONSTANT: iiiiii|abcdefgh|iiiiiii with some specified 'i'
因此,默认的边框插值类型(BORDER_REFLECT_101
)使您的数组在计算之前看起来像这样:
0 1 0 1 0
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
0 1 0 1 0
通过简单的算术,您可以确认将 Sobel 内核应用于内部 3x3 像素后的正确值全部为零 - 这就是您通过运行脚本得到的结果。
关于python - 从 cv2 解读索贝尔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51179926/
引言 深拷贝是指创建一个新对象,该对象的值与原始对象完全相同,但在内存中具有不同的地址。这意味着如果您对原始对象进行更改,则不会影响到复制的对象 常见的C#常见的深拷贝方式有以下4类:
人工智能是一种未来性的技术,目前正在致力于研究自己的一套工具。一系列的进展在过去的几年中发生了:无事故驾驶超过300000英里并在三个州合法行驶迎来了自动驾驶的一个里程碑;IBM Waston击败了
我已经阅读了所有 HERE Maps API 文档,但找不到答案。 HERE实时流量REST API输出中的XML标签是什么意思? 有谁知道如何解释这个输出(我在我的请求中使用了接近参数)? 最佳答
我的 iPad 应用程序工作正常,我将其留在现场进行测试,但现在崩溃了[保存时?] 这是崩溃日志, Incident Identifier: 80FC6810-9604-4EBA-A982-2009A
我的程序需要 qsort 的功能才能运行,但到目前为止还没有完成它的工作。 我实际上是在对单个字符值的数组进行排序,以便将它们分组,这样我就可以遍历数组并确定每个属性的计数。我的问题是 qsort 返
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
我正在尝试使用 AVR 代码对 Arduino Uno 进行编程,因为我不被允许在 9 月份开始的高级项目中使用 Arduino 库。我找到了数据表,让数字引脚正常工作,然后尝试通过 USB 串行连接
我遇到了多次崩溃,似乎 native iOS 方法正在从第三方库调用函数。这是一个例子: Thread: Unknown Name (Crashed) 0 libsystem_kernel.d
我理解如何按照 Dijkstra 算法的解释找到从头到尾的最短路径,但我不明白的是解释。在这里,从图中的图形来看,从 A 到 E 添加到我已知集合的顺序是 A,C,B,D,F,H,G,E 我没有得到的
我正在查看一些 Django 源代码并遇到了 this . encoding = property(lambda self: self.file.encoding) 究竟是做什么的? 最佳答案 其他两
Sentry 提供了很好的图表来显示消息频率,但关于它们实际显示的内容的信息很少。 这些信息是每分钟吗? 5分钟? 15分钟?小时? 最佳答案 此图表按分钟显示。这是负责存储该图数据的模型。 http
我对 JavaScript 和 Uniswap 还很陌生。我正在使用 Uniswap V3 从 DAI/USDC 池中获取价格。我的“主要”功能如下所示: async function main()
我正在尝试弄清楚我下载的 Chrome 扩展程序是如何工作的(这是骗子用来窃取 CS:GO 元素的东西,并不重要...)。我想知道使用什么电子邮件地址(或使用什么其他通信方式)来提交被钓鱼的数据。 这
引言 今天同事问了我一个问题, System.Windows.Forms.Timer 是前台线程还是后台线程,我当时想的是它是跟着UI线程一起结束的,应该是前台线程吧? 我确实没有仔
我需要一些使用 scipy.stats.t.interval() 函数的帮助 http://docs.scipy.org/doc/scipy/reference/generated/scipy.sta
当我在 Oracle 查询计划中看到类似的内容时: HASH JOIN TABLE1 TABLE2 这两个表中的哪一个是 hashed ? Oracle 文档指的是通常被散列的“较小”
我想知道 GridSearchCV 返回的分数与按如下方式计算的 R2 指标之间的差异。在其他情况下,我收到的网格搜索分数非常负(同样适用于 cross_val_score),我将不胜感激解释它是什么
本文分享自华为云社区《 多主创新,让云数据库性能更卓越 》,作者: GaussDB 数据库。 华为《Taurus MM: bringing multi-master to the clou
我真的需要一些帮助来破译这个崩溃报告: Process: Farm Hand [616] Path: /Applications/Farm
我写了一个从 YUV_420_888 到 Bitmap 的转换,考虑到以下逻辑(据我所知): 总结该方法:内核的坐标 x 和 y 与 Y 平面(2d 分配)的非填充部分的 x 和 y 以及输出位图的
我是一名优秀的程序员,十分优秀!