简体   繁体   English

如何从彩色矩形构建 cv::Mat

[英]How to build cv::Mat from coloured rectangles

My goal is to create yield maps using OpenCV.我的目标是使用 OpenCV 创建产量图。 These yield maps need to be built with coloured rectangles to indicate yield.这些产量图需要用彩色矩形来表示产量。 An example of a Mat built by rectangles here .这里是一个由矩形构建的垫子的例子。

So is it possible to create a cv::Mat with coloured rectangles?那么是否可以创建一个带有彩色矩形的 cv::Mat 呢? The amount of rectangles isn't constant, thus changes with every use.矩形的数量不是恒定的,因此每次使用都会发生变化。

To make the question clear: if I have 4 boxes (2x2 grid) I want to automatically make a Mat which is as big as the 4 boxes.为了明确问题:如果我有 4 个盒子(2x2 网格),我想自动制作一个与 4 个盒子一样大的垫子。 If I have 16 boxes (4x4 grid) I want to make a Mat which is as big as the 16 boxes.如果我有 16 个盒子(4x4 网格),我想做一个和 16 个盒子一样大的垫子。

I couldn't find a way to make it work, so I hope somebody here knows if it is possible.我找不到让它工作的方法,所以我希望这里有人知道这是否可能。

If somebody can help me that would be great, if it is not possible alternatives are also welcome!如果有人可以帮助我,那就太好了,如果不可能,也欢迎替代品! Thanks谢谢

Some info:一些信息:

  • OpenCV version:4.5.3 OpenCV 版本:4.5.3
  • OS: Ubuntu 20.04操作系统:Ubuntu 20.04
  • Language: C++语言:C++

You can create rectangle with OpenCV function.您可以使用 OpenCV function 创建矩形。

Basic Geometric Drawing OpenCV 基本几何图 OpenCV

int x = 0;
int y = 0;
int width = 10;
int height = 20;
// our rectangle...
cv::Rect rect(x, y, width, height);
// and its top left corner...
cv::Point pt1(x, y);
// and its bottom right corner.
cv::Point pt2(x + width, y + height);
// These two calls...
cv::rectangle(img, pt1, pt2, cv::Scalar(0, 255, 0));
// essentially do the same thing
cv::rectangle(img, rect, cv::Scalar(0, 255, 0))

ref 参考

OpenCV has cv::hconcat and cv::vconcat . OpenCV 有cv::hconcatcv::vconcat Use them like numpy's hstack/vstack.像 numpy 的 hstack/vstack 一样使用它们。

make sure your parts have the same type (and number of channels).确保您的零件具有相同的类型(和通道数)。

The documentation has a code example. 该文档有一个代码示例。

cv::Mat matArray[] = { cv::Mat(4, 1, CV_8UC1, cv::Scalar(1)),
                       cv::Mat(4, 1, CV_8UC1, cv::Scalar(2)),
                       cv::Mat(4, 1, CV_8UC1, cv::Scalar(3)),};
cv::Mat out;
cv::hconcat( matArray, 3, out );
//out:
//[1, 2, 3;
// 1, 2, 3;
// 1, 2, 3;
// 1, 2, 3]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM