- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我无法理解 OpenCV 的内部工作原理。考虑以下代码:
Scalar getAverageColor(Mat img, vector<Rect>& rois) {
int n = static_cast<int>(rois.size());
Mat avgs(1, n, CV_8UC3);
for (int i = 0; i < n; ++i) {
// What is the correct way to assign the color elements in
// the matrix?
avgs.at<Scalar>(i) = mean(Mat(img, rois[i]));
/*
This seems to always work, but there has to be a better way.
avgs.at<Vec3b>(i)[0] = mean(Mat(img, rois[i]))[0];
avgs.at<Vec3b>(i)[1] = mean(Mat(img, rois[i]))[1];
avgs.at<Vec3b>(i)[2] = mean(Mat(img, rois[i]))[2];
*/
}
// If I access the first element it seems to be set correctly.
Scalar first = avgs.at<Scalar>(0);
// However mean returns [0 0 0 0] if I did the assignment above using scalar, why???
Scalar avg = mean(avgs);
return avg;
}
avgs.at<Scalar>(i) = mean(Mat(img, rois[i]))
对于循环中的赋值,第一个元素看起来是正确的,但是平均计算总是返回零(即使认为第一个元素看起来是正确的)。如果我使用 Vec3b 手动分配所有颜色元素,它似乎可以工作,但为什么???
最佳答案
注: cv::Scalar
是 cv::Scalar_<double>
的 typedef ,源自 cv::Vec<double, 4>
,源自 cv::Matx<double, 4, 1>
.
同样, cv::Vec3b
是 cv::Vec<uint8_t, 3>
源自 cv::Matx<uint8_t, 3, 1>
-- 这意味着我们可以在 cv::Mat::at
中使用这三个中的任何一个并获得相同(正确)的行为。
重要的是要注意 cv::Mat::at
是 basically a reinterpret_cast
在底层数据数组上。您需要非常小心地为模板参数使用适当的数据类型,该类型对应于 cv::Mat
的元素类型(包括 channel 数)。你正在调用它。
该文档提到了以下内容:
Keep in mind that the size identifier used in the at operator cannot be chosen at random. It depends on the image from which you are trying to retrieve the data. The table below gives a better insight in this:
- If matrix is of type
CV_8U
then useMat.at<uchar>(y,x)
.- If matrix is of type
CV_8S
then useMat.at<schar>(y,x)
.- If matrix is of type
CV_16U
then useMat.at<ushort>(y,x)
.- If matrix is of type
CV_16S
then useMat.at<short>(y,x)
.- If matrix is of type
CV_32S
then useMat.at<int>(y,x)
.- If matrix is of type
CV_32F
then useMat.at<float>(y,x)
.- If matrix is of type
CV_64F
then useMat.at<double>(y,x)
.
cv::Vec<...>
(或者更确切地说是提供的 typedef 之一)。
cv::Vec<...>
基本上是围绕给定类型的 N 个值的固定大小数组的包装器。
avgs
是
CV_8UC3
-- 每个元素由 3 个无符号字节值组成(即总共 3 个字节)。但是,通过使用
avgs.at<Scalar>(i)
,您将每个元素解释为 4 个 double (总共 32 个字节)。这意味着:
#include <opencv2/opencv.hpp>
int main()
{
cv::Mat test_mat(cv::Mat::zeros(1, 12, CV_8UC3)); // 12 * 3 = 36 bytes of data
std::cout << "Before: " << test_mat << "\n";
cv::Scalar test_scalar(cv::Scalar::all(1234.5678));
test_mat.at<cv::Scalar>(0, 0) = test_scalar;
std::cout << "After: " << test_mat << "\n";
return 0;
}
Before: [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
After: [173, 250, 92, 109, 69, 74, 147, 64, 173, 250, 92, 109, 69, 74, 147, 64, 173, 250, 92, 109, 69, 74, 147, 64, 173, 250, 92, 109, 69, 74, 147, 64, 0, 0, 0, 0]
at
的错误使用也会触发断言:
OpenCV(3.4.3) Error: Assertion failed (((((sizeof(size_t)<<28)|0x8442211) >> ((traits::Depth<_Tp>::value) & ((1 << 3) - 1))*4) & 15) == elemSize1()) in cv::Mat::at, file D:\code\shit\so07\deps\include\opencv2/core/mat.inl.hpp, line 1102
cv::mean
的结果(这是
cv::Scalar
)到我们的
CV_8UC3
矩阵,我们需要做两件事(不一定按这个顺序):
double
的值至 uint8_t
-- OpenCV 会做一个 saturate_cast
,但考虑到平均值不会超过输入项的最小值/最大值,我们可以使用常规转换。 cv::Matx::get_minor
(文档有点缺乏,但查看
implementation 可以很好地解释它)。结果是
cv::Matx
,所以我们必须使用它而不是
cv::Vec
使用时
cv::Mat::at
.
cv::Matx
至 uint8_t
元素类型。 cv::Scalar
至 cv::Scalar_<uint8_t>
首先,然后去掉第 4 个元素。 #include <opencv2/opencv.hpp>
typedef cv::Matx<uint8_t, 3, 1> Mat31b; // Convenience, OpenCV only has typedefs for double and float variants
int main()
{
cv::Mat test_mat(1, 12, CV_8UC3); // 12 * 3 = 36 bytes of data
test_mat = cv::Scalar(1, 1, 1); // Set all elements to 1
std::cout << "Before: " << test_mat << "\n";
cv::Scalar test_scalar{ 2,3,4,0 };
cv::Matx31d temp = test_scalar.get_minor<3, 1>(0, 0);
test_mat.at<Mat31b>(0, 0) = static_cast<Mat31b>(temp);
// or
// cv::Scalar_<uint8_t> temp(static_cast<cv::Scalar_<uint8_t>>(test_scalar));
// test_mat.at<Mat31b>(0, 0) = temp.get_minor<3, 1>(0, 0);
std::cout << "After: " << test_mat << "\n";
return 0;
}
Before: [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
After: [ 2, 3, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
saturate_cast
s 而不是 3。必须进行一些基准测试(读者练习)。平均值的计算将大大超过它,因此它可能无关紧要。
saturate_cast
s,也许简单但更冗长的方法(对你有用的东西的优化版本)可能在紧密循环中表现得更好。
cv::Vec3b& current_element(avgs.at<cv::Vec3b>(i));
cv::Scalar current_mean(cv::mean(cv::Mat(img, rois[i])));
for (int n(0); n < 3; ++n) {
current_element[n] = static_cast<uint8_t>(current_mean[n]);
}
cv::Mat
的赋值运算符当给定
cv::Scalar
时被向量化(它为所有元素分配相同的值),并忽略
cv::Scalar
的附加 channel 值可持有相对于目标
cv::Mat
类型。 (例如,对于 3 channel
Mat
,它会忽略第四个值)。
Mat
的 1x1 ROI ,并将其赋值为
Scalar
.必要的类型转换将发生,第 4 个 channel 将被丢弃。可能不是最优的,但它是迄今为止最少的代码。
test_mat(cv::Rect(0, 0, 1, 1)) = test_scalar;
关于OpenCV 如何转换矩阵元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56350033/
我正在尝试将一个字符串逐个字符地复制到另一个字符串中。目的不是复制整个字符串,而是复制其中的一部分(我稍后会为此做一些条件......) 但我不知道如何使用迭代器。 你能帮帮我吗? std::stri
我想将 void 指针转换为结构引用。 结构的最小示例: #include "Interface.h" class Foo { public: Foo() : mAddress((uint
这有点烦人:我有一个 div,它从窗口的左上角开始过渡,即使它位于文档的其他任何位置。我试过 usign -webkit-transform-origin 但没有成功,也许我用错了。有人可以帮助我吗?
假设,如果将 CSS3 转换/转换/动画分配给 DOM 元素,我是否可以检测到该过程的状态? 我想这样做的原因是因为我正在寻找类似过渡链的东西,例如,在前一个过渡之后运行一个过渡。 最佳答案 我在 h
最近我遇到了“不稳定”屏幕,这很可能是由 CSS 转换引起的。事实上,它只发生在 Chrome 浏览器 上(可能还有 Safari,因为一些人也报告了它)。知道如何让它看起来光滑吗?此外,您可能会注意
我正在开发一个简单的 slider ,它使用 CSS 过渡来为幻灯片设置动画。我用一些基本样式和一些 javascript 创建了一支笔 here .注意:由于 Codepen 使用 Prefixfr
我正在使用以下代码返回 IList: public IList FindCodesByCountry(string country) { var query =
如何设计像这样的操作: 计算 转化 翻译 例如:从“EUR”转换为“CNY”金额“100”。 这是 /convert?from=EUR&to=CNY&amount=100 RESTful 吗? 最佳答
我使用 jquery 组合了一个图像滚动器,如下所示 function rotateImages(whichHolder, start) { var images = $('#' +which
如何使用 CSS (-moz-transform) 更改一个如下所示的 div: 最佳答案 你可以看看Mozilla Developer Center .甚至还有例子。 但是,在我看来,您的具体示例不
我需要帮助我正在尝试在选中和未选中的汉堡菜单上实现动画。我能够为菜单设置动画,但我不知道如何在转换为 0 时为左菜单动画设置动画 &__menu { transform: translateX(
我正在为字典格式之间的转换而苦苦挣扎:我正在尝试将下面的项目数组转换为下面的结果数组。本质上是通过在项目第一个元素中查找重复项,然后仅在第一个参数不同时才将文件添加到结果集中。 var items:[
如果我有两个定义相同的结构,那么在它们之间进行转换的最佳方式是什么? struct A { int i; float f; }; struct B { int i; float f; }; void
我编写了一个 javascript 代码,可以将视口(viewport)从一个链接滑动到另一个链接。基本上一切正常,你怎么能在那里看到http://jsfiddle.net/DruwJ/8/ 我现在的
我需要将文件上传到 meteor ,对其进行一些图像处理(必要时进行图像转换,从图像生成缩略图),然后将其存储在外部图像存储服务器(s3)中。这应该尽可能快。 您对 nodejs 图像处理库有什么建议
刚开始接触KDB+,有一些问题很难从Q for Mortals中得到。 说,这里 http://code.kx.com/wiki/JB:QforMortals2/casting_and_enumera
我在这里的一个项目中使用 JSF 1.2 和 IceFaces 1.8。 我有一个页面,它基本上是一大堆浮点数字段的大编辑网格。这是通过 inputText 实现的页面上的字段指向具有原始值的值对象
ScnMatrix4 是一个 4x4 矩阵。我的问题是什么矩阵行对应于位置(ScnVector3),旋转(ScnVector4),比例(ScnVector3)。第 4 行是空的吗? 编辑: 我玩弄了
恐怕我是 Scala 新手: 我正在尝试根据一些简单的逻辑将 Map 转换为新 Map: val postVals = Map("test" -> "testing1", "test2" -> "te
输入: This is sample 1 This is sample 2 输出: ~COLOR~[Green]This is sample 1~COLOR~[Red]This is sam
我是一名优秀的程序员,十分优秀!