首页 \ 问答 \ 使用字符串定义Numpy数组切片(Using a string to define Numpy array slice)

使用字符串定义Numpy数组切片(Using a string to define Numpy array slice)

我的图像数组的X倍Y形状为2048x2088。 x轴具有两个20个像素区域,一个在开始处,一个在末端,用于校准主图像区域。 要访问这些区域,我可以像这样切片数组:

prescan_area = img[:, :20]
data_area = img[:, 20:2068]
overscan_area = img[:, 2068:]

我的问题是如何在配置文件中定义这些区域,以便将此切片推广到其他可能具有不同预扫描和过扫描区域的摄像机,因此需要不同的切片。

理想情况下,类似下面的字符串将允许在相机特定配置文件中的简单表示,但我不知道如何将这些字符串转换为数组切片。

prescan_area_def = "[:, :20]"
image_area_def = "[:, 20:2068]"
overscan_area_def = "[:, 2068:]"

也许有一些明显的东西让我失踪?

谢谢!


I have an image array that has an X times Y shape of 2048x2088. The x-axis has two 20 pixel regions, one at the start and one at the end, which are used to calibrate the main image area. To access these regions I can slice the array like so:

prescan_area = img[:, :20]
data_area = img[:, 20:2068]
overscan_area = img[:, 2068:]

My question is how to define these areas in a configuration file in order the generalise this slice for other cameras which may have different prescan and overscan areas and therefore require a different slice.

Ideally, something like the strings below would allow a simple representation in the camera specific configuration file, but I am not sure how to translate these strings into array slices.

prescan_area_def = "[:, :20]"
image_area_def = "[:, 20:2068]"
overscan_area_def = "[:, 2068:]"

Maybe there is something obvious that I am missing?

Thanks!


原文:https://stackoverflow.com/questions/43089907
更新时间:2022-08-24 14:08

最满意答案

首先,类名不能以整数开头。 所以重命名它们:

 class A
 {
    public:
        int stuff;
    private:
        class B 
        {
        public:
            int stuffer;
        private:
            int x;
        };
  };

其次,由于嵌套类B在私有部分中,因此您无法从类A的范围之外访问它。 B仅可由A访问。 声明类型B的对象的语法是B bObj;A的范围内。

现在你应该先尝试自己,然后再提出更多问题!


至于你的编辑(增加的问题):它没有编译, 因为当编译器看到B temp ,它还没有看到B的定义,这就是为什么它说B没有被声明!

修复非常简单。 在使用它之前声明B ,如下所示:

class A
{
private:
        class B 
        {
        public:
            int stuffer;
        private:
            int x;
        };
    public:
        int stuff;
        void helper(B temp, int d);

 }; //<--- this is also fixed. your code has 'colon', instead semi-colon!

同时阅读课程结束时的评论!


First of all, class name cannot start with integer. So renaming them:

 class A
 {
    public:
        int stuff;
    private:
        class B 
        {
        public:
            int stuffer;
        private:
            int x;
        };
  };

Second, since the nested class B is in the private section, so you cannot access it from outside the scope of class A. B is accessible to A only. And the syntax of declaring an object of type B would be B bObj; in the scope of A.

Now you should try yourself first, before asking futher questions!


As for your edit (the added question): it's not compiling because by the time the compiler sees B temp, it has not yet seen the definition of B, that is why it says B is not declared!

The fix is very simple. Declare B before it's used, something like this:

class A
{
private:
        class B 
        {
        public:
            int stuffer;
        private:
            int x;
        };
    public:
        int stuff;
        void helper(B temp, int d);

 }; //<--- this is also fixed. your code has 'colon', instead semi-colon!

Also read the comment at the end of the class!

相关问答

更多
  • :用于“初始化”类的成员(此方法也称为成员初始化列表) 使用:和函数体{}之间存在重大差异 initiallizer list :初始化类的成员,而构造函数body {}将值赋给类的成员。 差异可能看起来不是很大,但它实际上是初始化const数据类型和reference数据类型成员的唯一方法(只能在声明期间初始化) 所以当你这样做 class Test { const int i; const string str; public: Test(int x, string y):i{x},str{y}; } ...
  • 从语法上讲,不,除非你想编写一个函数或宏来执行它。 你可以,但如果你问我,只需使用访问器。 此外,您不希望在堆上分配大多数对象。 只需将它们声明为type foo; 或者type foo = type()并让它们在堆栈中。 除非有特别合理的理由,否则不应该使用动态分配将事物放在堆上,因为动态分配的开销很大。 Syntactically, no, not unless you want to write a function or macro to do it. You could, but if you a ...
  • 没有优先选择的问题 ClassExample classExample2(); 和 ClassExample classExample3(void); 声明一个返回ClassExample对象的函数 There's no question of preferred option ClassExample classExample2(); and ClassExample classExample3(void); declares a function returning ClassExample objec ...
  • 你的语法还可以,但是非工作级平衡的问题在机器学习中很常见; 在某种程度上,从较大的类中删除一些对象是唯一可以保证工作的方法,但它仍然可能是错误增加的来源,并且必须小心谨慎地以智能方式进行(在SVM中潜在的支持向量)应该有优先权 - 当然现在有一个问题如何找到它们。 您也可以尝试通过简单的长度比来增加重量,比方说十倍,并检查它是否有助于甚至一点点或幸运地反过来超过另一侧的不平衡。 Your syntax is ok, but the problem of not-working-class-balance i ...
  • 首先,类名不能以整数开头。 所以重命名它们: class A { public: int stuff; private: class B { public: int stuffer; private: int x; }; }; 其次,由于嵌套类B在私有部分中,因此您无法从类A的范围之外访问它。 B仅可由A访问。 声明类型B的对象的语法是B ...
  • Objective-C中没有static方法 - 你需要一个类方法。 替换-在声明前面加上+ ,如下所示: +(NSInteger) compare:(UniqueWord *self)a with:(UniqueWord *self)b; 类方法类似于C ++静态成员函数,但由于方法调度是在Objective-C中更动态地实现的,因此可以在派生类中为它们提供覆盖。 以上将编译。 但是,对于Objective-C来说,这不是惯用的,因为Cocoa使用NSComparisonResult而不是NSInteg ...
  • 使用头文件的一个简单示例(在单独的.cpp文件中实现)看起来像这样: 你的main.cpp文件: #include "OtherClass.h" int main() { OtherClass otherClass; //use otherClass here... } 接下来,您的OtherClass.h文件: class OtherClass { public: OtherClass(); int someFunction(int parameters); }; ...
  • 你的代码错了。 您已经定义了Class_A两次,并且定义不匹配。 这是不允许的 。 [C++11: 3.2/3] : 每个程序应该只包含该程序中使用的每个非内联函数或变量的一个定义; 无需诊断。 该定义可以在程序中明确显示,可以在标准或用户定义的库中找到,或者(在适当的时候)隐式定义(见12.1,12.4和12.8)。 内联函数应在每个使用它的翻译单元中定义。 [C++11: 3.2/5] : 类类型 (第9条),枚举类型(7.2),带内部链接的内联函数(7.1.2),类模板(第14条) 可以有多个定义 , ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)