gpt4 book ai didi

image - 如何使用 OpenCV 找到图像上每个轮廓的高度和宽度

转载 作者:行者123 更新时间:2023-12-01 13:10:51 25 4
gpt4 key购买 nike

img link

在上图中,如果指定整个宽度为 30'5"。如何使用 opencv 计算该图像上每个单独轮廓的高度和宽度

最佳答案

我的方法是使用 minAreaRect :

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

using namespace cv;
using namespace std;


int main()
{

Mat src; Mat src_gray;
int thresh = 100;
RNG rng(12345);
/// Load source image and convert it to gray
src = imread( "/ur/img/directory/image.jpg", 1 );
Mat original = src.clone();
/// Convert image to gray and blur it
cvtColor( src, src_gray, CV_BGR2GRAY );

Mat threshold_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;

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

/// Find the rotated rectangles for each contour
vector<RotatedRect> minRect( contours.size() );

for( int i = 0; i < contours.size(); i++ )
minRect[i] = minAreaRect( Mat(contours[i]) );

/// Draw contours + rotated rects
Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
Mat result_zero = Mat::zeros( threshold_output.size(), CV_8UC3 );

for( int i = 0; i< contours.size(); i++ )
{
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
// detect contours
drawContours( drawing, contours, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
// detect rectangle for each contour
Point2f rect_points[4]; minRect[i].points( rect_points );

double length_1 = cv::norm(cv::Mat(rect_points[0]),cv::Mat(rect_points[1]));
double length_2 = cv::norm(cv::Mat(rect_points[1]),cv::Mat(rect_points[2]));


for( int j = 0; j < 4; j++ )
{
int temp1 = (int)length_1;
int temp2 = (int)length_2;

if(length_1>length_2)
putText(original,to_string(temp1),rect_points[0],FONT_HERSHEY_SIMPLEX,1.0,Scalar(0,255,255),2);
else
putText(original,to_string(temp2),rect_points[0],FONT_HERSHEY_SIMPLEX,1.0,Scalar(0,255,255),2);

line( result_zero, rect_points[j], rect_points[(j+1)%4], color, 1, 8 );
}

}

/// Show in windows
imshow("First",original);
imshow( "Contours", drawing );
waitKey(0);
return(0);
}

源图像:

enter image description here

每条线检测到的矩形:

enter image description here

线长按像素:

enter image description here

关于image - 如何使用 OpenCV 找到图像上每个轮廓的高度和宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60149864/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com