利用opencv检测出矩形
#include <opencv2/opencv.hpp>
#include <iostream>
#include <windows.h>
using namespace cv;
using namespace std;
int* findRectInfo(std::vector<cv::Point> rect)
{
int rectInfo[4] = {0};
int x[4]= {0},y[4]= {0};
int maxX = 0,maxY = 0,minX = 2000,minY = 2000;
for(int i=0;i<4;i++)
{
x[i] = rect[i].x;
y[i] = rect[i].y;
if(maxX<x[i])
maxX = x[i];
if(maxY<y[i])
maxY = y[i];
if(minX>x[i])
minX = x[i];
if(minY>y[i])
minY = y[i];
}
rectInfo[0] = minY;
rectInfo[1] = minX;
rectInfo[2] = maxY - minY;
rectInfo[3] = maxX - minX;
cout<<"minY="<<minY<<endl;
cout<<"minX="<<minX<<endl;
cout<<"maxY - minY="<<maxY - minY<<endl;
cout<<"maxX - minX="<<maxX - minX<<endl;
return rectInfo;
}
double angle(Point pt1, Point pt2, Point pt0)
{
double dx1 = pt1.x - pt0.x;
double dy1 = pt1.y - pt0.y;
double dx2 = pt2.x - pt0.x;
double dy2 = pt2.y - pt0.y;
return (dx1*dx2 + dy1*dy2) / sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10);
}
int main(int argc, char* argv[])
{
VideoCapture cap;
cap.open(0);
if (!cap.isOpened())
{
return -1;
}
double w = 320, h = 320;
cap.set(CV_CAP_PROP_FRAME_WIDTH, w);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, h);
namedWindow("Video");
bool stop = false;
Mat frame1,frame2,frame3;
CvMemStorage* storage = NULL;
while (!stop)
{
cap>> frame1;
cvtColor(frame1, frame2, CV_BGR2GRAY);
GaussianBlur(frame2, frame3, cv::Size(5, 5), 0, 0);
int gbins = 16;
int histSize[] = { gbins };
float granges[] = { 0,256 };
const float* ranges[] = { granges };
cv::MatND hist;
int channels[] = { 0 };
calcHist(&frame2, 1, channels, cv::Mat(),
hist, 1, histSize, ranges,
true,
false);
double maxVal = 0;
minMaxLoc(hist, 0, &maxVal, 0, 0);
int scale = 20;
cv::Mat histImg;
histImg.create(500, gbins*scale, CV_8UC3);
for (int g = 0; g<gbins; g++) {
float binVal = hist.at<float>(g, 0);
int intensity = cvRound(binVal * 255);
rectangle(histImg, cv::Point(g*scale, 0),
cv::Point((g + 1)*scale - 1, binVal / maxVal * 400),
CV_RGB(0, 0, 0),
CV_FILLED);
}
imshow("histImg", histImg);
Mat hsvRe;
threshold(frame3, hsvRe, 150, 255, cv::THRESH_BINARY);
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(hsvRe, contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
imshow("Video1", frame1);
imshow("Video2", frame2);
imshow("Video3", hsvRe);
vector<Point> approx;
vector<vector<Point>> squares;
for (size_t i = 0; i < contours.size(); i++)
{
approxPolyDP(Mat(contours[i]), approx,
arcLength(Mat(contours[i]), true)*0.02, true);
if (approx.size() == 4 &&
fabs(contourArea(Mat(approx))) > 1000 &&
isContourConvex(Mat(approx)))
{
double maxCosine = 0;
for( int j = 2; j < 5; j++ )
{
double cosine = fabs(angle(approx[j%4],approx[j-2], approx[j-1]));
maxCosine = MAX(maxCosine, cosine);
}
if( maxCosine < 0.1 )
squares.push_back(approx);
}
}
std::vector<int> compression_params;
compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
compression_params.push_back(9);
Mat image(3210,3210,CV_8UC3,Scalar(0));
for(int i=0;i<squares.size();i++)
{
int rect[4],*tmp;
tmp = findRectInfo(squares[i]);
cout<<i<<endl;
for(int j=0;j<4;j++)
{
rect[j] = *(tmp+j);
}
cv::Rect roi(rect[1],rect[0],rect[3],rect[2]);
cout<<"w:"<<roi.width<<endl;
cout<<"w:"<<roi.height<<endl;
}
if (waitKey(27) >= 0)
stop = true;
}
return 0;
}
这个程序是自己在网上各种查阅和拼凑在一起能检测出矩形的程序。