私はopenCVを初めて使用するので、過去3〜4日間苦労しています。すでに用紙の境界を検出しています。コーナーに4つの円を描きたいと思っています。
このコードから境界を描きます
const cv::Point* p = &squares[i][0];
int n = (int)squares[i].size();
polylines(image, &p,&n, 1, true, Scalar(255,255,0), 5, CV_AA);
私は openCV の初心者です。私の意見では、左上隅のポイント p->x と p->y があります。しかし、他のコーナーを取得する方法、このポリライン メソッドのパラメータ &n についても混乱しています。完全な長方形を描く?
境界四角形を使用すると、紙の側面に小さなスペースができるため、完璧ではありません。
どんな助けでも本当に感謝しています
コードは:
- (cv::Mat)finshWork:(cv::Mat &)image
{
// read in the apple (change path to the file)
Mat img0 =image;// imread("/home/philipp/img/apple.jpg", 1);
Mat img1;
cvtColor(img0, img1, CV_RGB2GRAY);
// apply your filter
Canny(img1, img1, 100, 200);
// find the contours
vector< vector<cv::Point> > contours;
findContours(img1, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
/////for SQUARE CODE
std::vector<std::vector<cv::Point> > squares;
std::vector<cv::Point> approx;
for( size_t i = 0; i < contours.size(); i++ )
{
cv::approxPolyDP(cv::Mat(contours[i]), approx, arcLength(cv::Mat(contours[i]), true)*0.02, true);
if( approx.size() == 4 && fabs(contourArea(cv::Mat(approx))) > 1000 && cv::isContourConvex(cv::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.3 ) {
squares.push_back(approx);
cv::Point newPoint = approx[0];
NSLog(@"x is %d and y is %d",newPoint.x,newPoint.y);
}
}
}
const cv::Point* p = &squares[0][0];
int n = (int)squares[0].size();
NSLog(@"%d",n);
//THIS IS WORKING CODE
polylines(image, &p,&n, 1, true, Scalar(0,0,255), 10, CV_AA);
//polylines(image, &p,&n, 1, true, Scalar(255,255,0), 5, CV_AA);
////////////
}
ありがとう