-2

My goal is to create yield maps using 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? 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. If I have 16 boxes (4x4 grid) I want to make a Mat which is as big as the 16 boxes.

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
  • OS: Ubuntu 20.04
  • Language: C++
Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Epsi
  • 115
  • 7

2 Answers2

1

You can create rectangle with OpenCV function.

Basic Geometric Drawing 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

EliaTolin
  • 526
  • 3
  • 15
  • Yeah this makes one rectangle, this I know. But I want to make a Picture (cv::Mat) which consists of multiple rectangles, not just one. Thanks for the answer though! – Epsi Jan 19 '22 at 11:04
  • so just draw more of them? I'm inclined to flag this question as a duplicate. you need to give your question more detail. – Christoph Rackwitz Jan 19 '22 at 11:17
  • My question is how build a Mat purely on the rectangles, without a starting image. For example: if I have 4 boxes (2x2 grid) I want to automatically make a Mat which is as big as the 4 boxes. If I have 16 boxes (4x4 grid) I want to make a Mat which is as big as the 16 boxes. And if it is even possible – Epsi Jan 19 '22 at 11:23
  • I understand from your question that you want draw a rectangle but not more. – EliaTolin Jan 19 '22 at 12:56
0

OpenCV has cv::hconcat and cv::vconcat. Use them like numpy's 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]
Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36