The process of obtaining the size of an object in OpenCV using C++ involves the following steps:
Here is an example code snippet that demonstrates how to obtain the size of a rectangle object in OpenCV using C++:
#include <opencv2/opencv.hpp>
using namespace cv;
int main() {
// load image containing rectangle object
Mat image = imread("rectangle.jpg");
// convert to grayscale
Mat gray;
cvtColor(image, gray, COLOR_BGR2GRAY);
// apply edge detection
Mat edges;
Canny(gray, edges, 50, 150);
// define ROI around the rectangle object
Rect roi(100, 100, 200, 100);
// crop image to ROI
Mat crop = edges(roi);
// calculate size of object
int width = crop.size().width;
int height = crop.size().height;
std::cout << "Width: " << width << std::endl;
std::cout << "Height: " << height << std::endl;
return 0;
}
Asked: 2022-12-14 11:00:00 +0000
Seen: 8 times
Last updated: Nov 14 '21