[英]C++ / Opencv Understanding the datatype being used
我想學習OpenCV,並將其用於我的一個項目中的車牌檢測系統。 我一直在遵循概述的步驟:
https://github.com/MicrocontrollersAndMore/OpenCV_3_License_Plate_Recognition_Cpp
問題在於,存在一個類: PossibleChar.h
,似乎無法理解該類使用的是哪種數據類型。 我不想在我的算法中使用一個類,因為這不符合指導原則,但是,我需要做的就是在場景中檢測車牌的算法。 該類如下所示:
// PossibleChar.h
#ifndef POSSIBLE_CHAR_H
#define POSSIBLE_CHAR_H
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
class PossibleChar {
public:
// member variables
std::vector<cv::Point> contour;
cv::Rect boundingRect;
int intCenterX;
int intCenterY;
double dblDiagonalSize;
double dblAspectRatio;
static bool sortCharsLeftToRight(const PossibleChar &pcLeft, const PossibleChar & pcRight) {
return(pcLeft.intCenterX < pcRight.intCenterX);
}
bool operator == (const PossibleChar& otherPossibleChar) const {
if (this->contour == otherPossibleChar.contour) return true;
else return false;
}
bool operator != (const PossibleChar& otherPossibleChar) const {
if (this->contour != otherPossibleChar.contour) return true;
else return false;
}
// function prototypes
PossibleChar(std::vector<cv::Point> _contour);
};
#endif // POSSIBLE_CHAR_H
PossibleChar.cpp
PossibleChar::PossibleChar(std::vector<cv::Point> _contour) {
contour = _contour;
boundingRect = cv::boundingRect(contour);
intCenterX = (boundingRect.x + boundingRect.x + boundingRect.width) / 2;
intCenterY = (boundingRect.y + boundingRect.y + boundingRect.height) / 2;
dblDiagonalSize = sqrt(pow(boundingRect.width, 2) + pow(boundingRect.height, 2));
dblAspectRatio = (float)boundingRect.width / (float)boundingRect.height;
}
我最初的想法是所有此類都在給我cv::Point
,效果很好,直到我着手計算車牌上“字符”之間的距離。 這是算法中給出的功能:
double distanceBetweenChars(const PossibleChar &firstChar, const PossibleChar &secondChar) {
int intX = abs(firstChar.intCenterX - secondChar.intCenterX);
int intY = abs(firstChar.intCenterY - secondChar.intCenterY);
return(sqrt(pow(intX, 2) + pow(intY, 2)));
}
如果我使用一個而不使用PossibleChar
:
double distanceBetweenChars(const cv::Point &firstChar, const cv::Point &secondChar) {
cv::Rect boundingRect;
boundingRect = cv::boundingRect(firstChar);
}
我不斷收到錯誤:
error: no viable conversion from 'const cv::Point' (aka 'const Point_<int>') to 'const cv::_InputArray'
因此,這使我相信該類不僅返回cv::point
而且還在執行其他操作。 關於如何解決這個問題並計算距離的任何想法?
PossibleChar
不是cv::Point
。 這是一個包含可能字符信息的類,我們稱其為blob :
std::vector<cv::Point> contour;
斑點的輪廓 cv::Rect boundingRect;
斑點的邊界框(對齊軸) int intCenterX; int intCenterY;
bounding box
中心的坐標 double dblDiagonalSize; double dblAspectRatio;
bounding box
大小和縱橫比 如您所見,它不是Point
,但包含有關給定Blob的形狀和范圍的信息,該Blob可能是一個character 。
功能:
double distanceBetweenChars(const PossibleChar &firstChar, const PossibleChar &secondChar)
給出兩個可能字符中心之間的歐幾里得距離。 如果要使用cv::Point
,可以執行以下操作:
double distanceBetweenChars(const cv::Point &firstChar, const cv::Point &secondChar) {
return cv::norm(firstChar - secondChar);
}
像以前一樣返回兩點之間的歐幾里得距離。
如果您想將一個PossibleChar
的cv::Point
描述為cv::Point
(這可能對您的管道有用),則可以考慮中心的坐標:
cv::Point getPointFromPossibleChar(const PossibleChar& pc) {
return cv::Point(pc.intCenterX, pc.intCenterY);
}
在我看來,他們計算兩個字符之間距離的方式不太理想。
雖然我認為您不應該將cv::Point
與PossibleChar
混合使用。 它們是不同的東西,並且PossibleChar
有更多的信息。 作為該部分的一部分,它是一個cv::Point
,它描述了所述PossibleChar
的質心。
如果您有兩個cv:Point
,則可以通過以下方式計算距離:
cv::Point a(1, 3); cv::Point b(5, 6); double res = cv::norm(ab);//Euclidian distance
如果您決定使用該類,我認為您應該更改
double distanceBetweenChars(const PossibleChar &firstChar, const PossibleChar &secondChar) {
int intX = abs(firstChar.intCenterX - secondChar.intCenterX);
int intY = abs(firstChar.intCenterY - secondChar.intCenterY);
return(sqrt(pow(intX, 2) + pow(intY, 2)));
}
至
double distanceBetweenChars(const PossibleChar &firstChar, const PossibleChar &secondChar) {
cv::Point a(firstChar.intCenterX,firstChar.intCenterY);
cv::Point b(secondChar.intCenterX,secondChar.intCenterY);
return(cv::norm(a-b));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.