#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
using namespace cv;
#include<vector>
#include<iostream>
using namespace std;
int main(int argc, char*argv[])
{
//第一步:输入图像
Mat img = imread("img3.jpg", CV_LOAD_IMAGE_GRAYSCALE);
if (!img.data)
return -1;
//第二步:边缘检测,得到边缘二值图
GaussianBlur(img, img, Size(3, 3), 0.5);
Mat binaryImg;
Canny(img, binaryImg, 50, 200);
imshow("显示边缘", binaryImg);
imwrite("cannyEdge.jpg", binaryImg);
//第三步:边缘的轮廓
vector<vector<Point>> contours;
vector<Vec4i> hei;
findContours(binaryImg, contours, hei, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
//第四步:对每一个轮廓作拟合,这里用旋转矩形
int num = contours.size();//轮廓的数量
for (int i = 0; i < num; i++)
{
//最小外包直立矩形
Rect rect = boundingRect(Mat(contours[i]));
int area = rect.area();
if (area>20000&& area<30000)//筛选出面积大于 目标的矩形
{
//在原图中画出外包矩形
rectangle(img, rect, Scalar(255), 2);
cout << rect.area() << endl;
}
}
imshow("img", img);
imwrite("img0.jpg", img);
waitKey(0);
return 0;
}
import cv2
import numpy as np
cap=cv2.VideoCapture(0)
while True:
ret,frame=cap.read()
cv2.imshow("camera",frame)
num=cv2.waitKey(1)
if num==27:
break
elif num==ord('s'):
roi=cv2.selectROI("camera",frame)
x,y,w,h=roi
print(x,y,w,h)
cap.release()
cv2.destroyAllWindows()
points=np.array([[[1,1]],[[5,10]],[[5,1]],[[1,10]],[[2,5]]],np.float32)
rect=cv2.boundingRect(points)
print(rect)