- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
给定一个灰色的 cv::Mat (CV_8UC1),我想返回另一个包含元素平方根的 cv::Mat (CV_32FC1),我想用 SSE2 内在函数来完成。我在从 8 位值转换为 32 浮点值以执行平方根时遇到了一些问题。我真的很感激任何帮助。这是我现在的代码(它没有给出正确的值):
uchar *source = (uchar *)cv::alignPtr(image.data, 16);
float *sqDataPtr = cv::alignPtr((float *)Squared.data, 16);
for (x = 0; x < (pixels - 16); x += 16) {
__m128i a0 = _mm_load_si128((__m128i *)(source + x));
__m128i first8 = _mm_unpacklo_epi8(a0, _mm_set1_epi8(0));
__m128i last8 = _mm_unpackhi_epi8(a0, _mm_set1_epi8(0));
__m128i first4i = _mm_unpacklo_epi16(first8, _mm_set1_epi16(0));
__m128i second4i = _mm_unpackhi_epi16(first8, _mm_set1_epi16(0));
__m128 first4 = _mm_cvtepi32_ps(first4i);
__m128 second4 = _mm_cvtepi32_ps(second4i);
__m128i third4i = _mm_unpacklo_epi16(last8, _mm_set1_epi16(0));
__m128i fourth4i = _mm_unpackhi_epi16(last8, _mm_set1_epi16(0));
__m128 third4 = _mm_cvtepi32_ps(third4i);
__m128 fourth4 = _mm_cvtepi32_ps(fourth4i);
// Store
_mm_store_ps(sqDataPtr + x, _mm_sqrt_ps(first4));
_mm_store_ps(sqDataPtr + x + 4, _mm_sqrt_ps(second4));
_mm_store_ps(sqDataPtr + x + 8, _mm_sqrt_ps(third4));
_mm_store_ps(sqDataPtr + x + 12, _mm_sqrt_ps(fourth4));
}
最佳答案
SSE 代码看起来不错,只是您没有处理最后 16 个像素:
for (x = 0; x < (pixels - 16); x += 16)
应该是:
for (x = 0; x <= (pixels - 16); x += 16)
请注意,如果您的图像宽度不是 16 的倍数,那么您需要处理最后一个完整 vector 之后的所有剩余像素。
另请注意,您正在使用 0..255 范围内的 sqrt
值。您可能需要 0..1.0 范围内的标准化值,在这种情况下,您需要相应地缩放值。
关于c++ - 使用 SSE 的 OpenCV 灰度图像的平方根,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28987941/
出于某种原因,我的平方根程序得到的答案与大多数输入应得到的答案略有不同。我不确定这是为什么。只有某些输入是错误的。在给出答案后的最后我也遇到了段错误,我不确定为什么会这样。 #include #inc
我目前正在实现一个可以处理与物理单位相关的数字数据的类。 我想实现一种计算实例平方根的方法。假设您有一个具有属性值和名称的类的实例: from math import sqrt class Foo:
我正在使用 HTML + CSS + AngularJS 制作一个简单的计算器。一切正常,但我想添加一个 SquareRoot 函数。这是一些代码: function _solve(){ switc
问题是关于在通用数值接口(interface)中定义平方根算法的问题的策略方法。我知道存在解决不同条件下问题的算法。我对以下算法感兴趣: 仅使用选定的函数解决问题; 不关心操作的对象是整数、 floa
好吧,我已经研究了一段时间了,我知道我的逻辑是正确的,但是,我似乎无法生成正数的正确底平方根。 public int mySqrt(int x) { if(x 0) uppe
我找不到让这个函数 Math.sqrt(value) 工作的方法。 (╯°□°)╯︵┻━┻我不确定,但问题似乎出在运营商。我还尝试在 const calculation 中添加该函数,但它也不起作用。
我发现了这段获得平方根的代码,令我惊讶的是它的工作方式,使用 union 和位移这是代码: float sqrt3(const float x) { union { int i;
在 python 中使用 sqrt 函数时,我遇到了“distance ValueError: math domain error”问题。 这是我的代码: from math import sqrt
我一直在做一些研究,寻找一种对大整数进行运算的相对快速的平方根算法。我在这里找到了几个例程。第一个(下面)是用 C 语言编写的... int isqrt(int n) { int b = 0;
好吧,我想知道 math.h 平方根与其中包含神奇数字的那个(因 Quake 而出名,但由 SGI 制作)相比有多快。 但这对我来说是一个受伤的世界。 我首先在 Mac 上尝试了此操作,其中 math
有谁知道如何解决这个复发? 大定理在这里不起作用。 最佳答案 这在 O(1) 中似乎很明显,因为 T(n) = T(n - sqrt(n)) = T(m) with 0 < m < n 通过归纳,你得
我是一名优秀的程序员,十分优秀!