确定四边形顶点的方法为:在四边形的内部任意取一个点,将四边形分割成四块,再在对应的块中求出边缘距离该点的最大距离,记为一个角点。缺陷:对于部分钝角的角点,会出现误差,导致钝角的角点不准确,所以可以根据已有的正确点进行重新确认点的位置。主要思路为:在对应的块部分,求取最小的余弦值,纠正距离计算的角点。
一下是简单的代码,没有做优化何处理,纯当作思路思考!
#include<iostream>
#include<string>
#include<vector>
#include "./Include/opencv/cv.h"
#include"./Include/opencv2/highgui/highgui.hpp"
#include"./Include/opencv2/imgcodecs.hpp"
#include"./Include/opencv2/imgproc.hpp"
#pragma comment(lib, "./Lib/Windows/debug/opencv_world300d.lib")
//纠正一个钝角的角点;
void correctObtuseAngle(CvPoint *pCvPointCenter, CvPoint* pCvPointVertex, const CvPoint *pCvPointLeft, \
const CvPoint* pCvPointRight, const CvSeq* pMaxCountor, const int nPosition);
struct Vertor
{
int x;
int y;
Vertor(int _x, int _y)
{
x = _x;
y = _y;
}
};
//获取连通域的中心位置;
void getCenterPoint(CvPoint *pCvPointCenter, CvSeq* pMaxCountor)
{
int nSize = pMaxCountor->total;
float fAVerageX = 0;
float fAverageY = 0;
for (int i = 0; i < nSize; ++i)
{
CvPoint *pTempPoint = (CvPoint*)cvGetSeqElem(pMaxCountor, i);
fAVerageX += pTempPoint->x * 1.0 / nSize;
fAverageY += pTempPoint->y * 1.0 / nSize;
}
pCvPointCenter->x = fAVerageX;
pCvPointCenter->y = fAverageY;
return;
}
//获取一个角度的Cos值;
float getCosValue(CvPoint cvPointMid, CvPoint cvPointLeft, CvPoint cvPointRight)
{
float fCosValue = (cvPointLeft.x - cvPointMid.x) * (cvPointRight.x - cvPointMid.x) + \
(cvPointLeft.y - cvPointMid.y) * (cvPointRight.y - cvPointMid.y);
float fAbsData = sqrt(pow(cvPointLeft.x - cvPointMid.x, 2) + pow(cvPointLeft.y - cvPointMid.y, 2)) * \
sqrt(pow(cvPointRight.x - cvPointMid.x, 2) + pow(cvPointRight.y - cvPointMid.y, 2));
if (fAbsData == 0)
{
return 0;
}
return fCosValue / fAbsData;
}
//修正最大的钝角点 1-左上角, 2-右上角, 3-左下角, 4-右下角;
void correctMaxObtuseAnglePoint(CvPoint* pCvPointCenter, CvPoint* pCvPointLeftUp, CvPoint* pCvPointLeftButtom, \
CvPoint* pCvPointRightUp, CvPoint* pCvPointRightButtom, CvSeq* pMaxCountor)
{
if (pCvPointCenter == NULL || pCvPointLeftUp == NULL || pCvPointLeftButtom == NULL || \
pCvPointRigh