opencvc++ I want to draw the center of gravity of the rectangle and the leftmost x coordinate of the rectangle and the rightmost y coordinate.

Asked 2 years ago, Updated 2 years ago, 124 views

rectangle(drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0);

So, the coordinates of the upper left and lower right of the rectangle are shown, but I don't know how to use them to show the center of gravity.Br contains two data: x and y coordinates. Similarly, tl contains two data.
Can you send only the x coordinates of br and only the x coordinates of tl as x1 and x2 respectively?

#include "opencv2/highgui/highgui.hpp"
# include "opencv2/imgproc/imgproc.hpp"
# include <iostream>
# include <stdio.h>
# include <stdlib.h>

using namespace cv;
using namespace std;

Mat src; Mat src_gray;
int threshold = 100;
int max_thresh = 255;
RNGrng (12345);

/// Function header
void threshold_callback(int, void*);

/** @ function main */
int main (int argc, char**argv)
{
  /// Load source image and convert it to gray
  src = imread(argv[1], 1);

  /// Convert image to gray and blurit
  cvtColor(src, src_gray, CV_BGR2GRAY);
  blur(src_gray, src_gray, Size(3,3));

  /// Create Window
  char*source_window="Source";
  namedWindow(source_window, CV_WINDOW_AUTOSIZE);
  imshow(source_window,src);

  createTrackbar("Threshold:", "Source", & threshold, max_thresh, threshold_callback);
  threshold_callback(0,0);

  waitKey(0);
  return(0);
}

/** @functionthresh_callback*/
void threshold_callback(int, void*)
{
  Mat threshold_output;
  vector<vector>Point>>contours;
  vector<Vec4i>hierarchy;

  /// Detect edges using Threshold
  threshold(src_gray, threshold_output, threshold, 255, THRESH_BINARY);
  /// Find contents
  findContours(threshold_output, contents, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0,0);

  /// Approximate contents to policing + get bounding rects and circuits
  vector<vector<Point>>contours_poly(contours.size());
  vector<Rect>boundRect(contours.size());
  vector<Point2f>center(contours.size());
  vector<float>radius(contours.size());

  for (inti=0; i<contours.size(); i++)
     { approxPolyDP(Mat(contours[i]), content_poly[i], 3, true);
       boundRect[i] = boundingRect(Mat(contours_poly[i]);
       minEnclosureCircle((Mat)contours_poly[i], center[i], radius[i]);
     }


  /// Draw polygonal tour + bonding rects + circuits
  Mat drawing = Mat::zeros (threshold_output.size(), CV_8UC3);
  for (inti=0; i<contours.size(); i++)
     {
       Scalar color = Scalar(rng.uniform(0,255), rng.uniform(0,255), rng.uniform(0,255);
       drawContours(drawing,contours_poly,i,color,1,8,vector<Vec4i>(),0,Point();
       rectangle(drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0);
       circuit(drawing, center[i], (int) radius[i], color, 2, 8, 0);
     }

  /// Show in a window
  namedWindow("Contours", CV_WINDOW_AUTOSIZE);
  imshow("Contours", drawing);
}

c++ opencv

2022-09-30 21:38

1 Answers

"As an oira who is going to take a ball-hanging test, I would like a strict definition of ""center of gravity,"" but normally, the center of gravity of a uniform rectangle is the diagonal intersection, and it's just the center."

http://opencv.jp/opencv-2svn/cpp/basic_structures.html
Rect_ is a class template with the upper left coordinates (x,y) and size width,height on its members.The Rect I'm using now is typedef Rect_<int>Rect;, so I can see that it's materialized by int.

So when Rector; is present, the center coordinate (center of gravity) of the Point p(r.x+r.width/2, r.y+r.height/2); sounds good, but is this the expected answer?I don't dare to tell you exactly how to apply it to the presentation code.

If you have the upper left coordinates (x,y) and the size width,height, it seems obvious that the upper right coordinates are (x+width,y) and the lower left coordinates are (x,y+height) (Is there anything bothering you)?In opencv, coordinates are indicated by the cv::Point_ class template, so if you want an integer system,

Point left_bottom(x,y+height);

It's easy to imagine that you would write

When Rector; is present, the bottom left is Point lb1(r.x,r.y+r.height); and so on
When Rects; is present, Point lb2(s.x, s.y+s.height); is probably
When Rect; is present, the lower left corner is Point lb3(t.x, t.y+t.height); and it should be
If so, when boundRect[i] is present, the lower left corner is... hereafter abbreviated

Isn't the center of gravity the same as the upper right? Of course, you should be able to apply that much (although it's the basis of the basics and not the application).

If you don't know yet, please edit the questionnaire instead of the comment.


2022-09-30 21:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.