I would like to output the coordinates of the square after the outline is enclosed in a square with opencv c++.

Asked 2 years ago, Updated 2 years ago, 95 views

Same as the URL program.
It's made up to the point where the outline is surrounded by a rectangle!
I want to get the coordinates from there, but it doesn't work very well.
Thank you.
https://docs.opencv.org/2.4/doc/tutorials/imgproc/shapedescriptors/bounding_rects_circles/bounding_rects_circles.html

#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 19:29

1 Answers

I was able to compile and run it on cygwin64 as follows, so I think I can manage it if I do the same.

$catpiyo.cpp
# include <iostream>
# include "opencv2/highgui/highgui.hpp"

int main() {
  cv::Rector;
  r.x = 10;
  r.y = 20;
  r.width = 30;
  r.height = 40;
  std::cout<<r<<std::endl;
}

$ g++piyo.cpp`pkg-config --libs openv`
$ ./a.exe
[30 x 40 from (10,20)]
$

boundRect[i] should look cool if you display it the same way.

The 輪郭contour 」 calculation results in the question statement are stored in the variable boundRect ( std::vector<cv::Rect> in case there are multiple bits), so you can retrieve the calculation results and display them before they are discarded.Do you know where the contour is calculated and where it is discarded?

rectangle(drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0);
cout<<boundRect[i]<endl;// Add this line here

You may want to add it to for(inti=0;i<contours.size();i++) as shown in .This is the standard output, but be careful where the standard output is (it may be a console screen or a debugger, so be careful when you want it to look like a balloon on the GUI screen) and you need to design and implement it.


2022-09-30 19:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.