몇 개의 꼭지점이 있는지 찾아서 꼭지점 수로 모양을 결정하고 싶습니다. 지금까지 'HoughCircle'을 사용하여 원이 있는지 여부를 감지 할 수 있었지만 직사각형, 정사각형 또는 삼각형을 처리 할 수 없습니다.웹캠에서 도형을 감지 할 수 없습니다
여기 코드는 있지만 제대로 처리 할 수 없습니다.
// declerations
vector<vector<Point>> contoursPoly(contours.size());
vector<Rect> boundRect(contours.size());
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
Mat contourimg;
Mat frame, out;
// for webcam
VideoCapture vcap(0);
for (;;){
vcap.read(frame); // display
cvtColor(frame, out, CV_BGR2GRAY); // convert BGR to GRAY scale
// fix noises
GaussianBlur(out, out, Size(9, 9), 2);
threshold(out,out,200,255,CV_THRESH_BINARY_INV | CV_THRESH_OTSU);
findContours(out, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE); // find contours
for (int i = 0; i < contours.size(); i++)
{
approxPolyDP(Mat(contours[i]), contoursPoly[i], 3, true); // find the number of vertecies
if (contoursPoly.size() == 4) // it should be a quadrilateral
{
boundRect[i] = boundingRect(contoursPoly[i]);
drawContours(frame, contours, i, Scalar(0, 0, 255), CV_FILLED, 8, hierarchy, 0, Point());
}
if (contoursPoly.size() == 3) // it should be triange
{
drawContours(frame, contours, i, Scalar(0, 0, 255), CV_FILLED, 8, hierarchy, 0, Point());
}
imshow("test", frame);
if (waitKey(30) == 27) break;
EDIT : 일부 변경 한
..
- 벡터 contoursPoly (contours.size());
- approxPolyDP (매트 (윤곽선 [i]), contours 폴리, arcLength (매트 (윤곽선 [i]) * 0.01, 참));
여기가
.. 2 편집 프로그램에서 이미지입니다 : 매트 contourOutput : 코멘트에
덕분에, 나는 내 코드에이 부분을 추가 한
= threshimg.clone();나는 .. 최근
를이있어하지만 문제는 여전히 존재한다. 그것은 윤곽선을 그리지 않습니다, 왜 모양을 감지 할 수없는 것입니다.
편집 3 : 나는 또한 하프 라인에서 내가 원하는 일을하고 여기에 결과가 있습니다
int main(){
VideoCapture vcap(0);
Mat gray, frame, mask, out, trackbarimg;
vector<vector<Point>> contours;
vector<Point> contPoly;
vector<Vec4i> hierarchy;
RotatedRect rrect;
Point2f vertices[4];
for (;;){
vcap.read(frame);
cvtColor(frame, gray, CV_BGR2GRAY);
GaussianBlur(gray,gray,Size(9,9), 1);
threshold(gray, mask, 220, 255, CV_THRESH_BINARY);
Mat contoursOut = mask.clone();
findContours(contoursOut, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
out = Mat::zeros(contoursOut.size(), CV_8UC3);
for (int i = 0; i < contours.size(); i++){
drawContours(out, contours, i, Scalar(0, 255, 0), CV_FILLED, 8, hierarchy, 1, Point());
rrect = minAreaRect(contours[i]);
rrect.points(vertices);
line(out, vertices[0], vertices[1], cv::Scalar(0, 0, 255));
line(out, vertices[1], vertices[2], cv::Scalar(0, 0, 255));
line(out, vertices[2], vertices[3], cv::Scalar(0, 0, 255));
line(out, vertices[3], vertices[0], cv::Scalar(0, 0, 255));
}
imshow("THRESHOLD", mask);
imshow("BLUR", gray);
imshow("ORIGINAL", frame);
imshow("LINES", out);
if (waitKey(30) == 27) break;
}
return 0;
}
어떤 비논리적 일이나 있습니까 :
이
코드입니다 두 번째 코드의 실수? 실제로, approxPolyDP를 사용하여 내 목적을 달성하고 거기에있는 정점의 수를 결정하고 싶습니다.
'approxPolyDP (Mat (contours [i]), contoursPoly [i], 0.01 * arcLength (Matte (contours [i]), true), true); – PSchn
답장을 보내 주셔서 감사 합니다만 작동하지 않았습니다. – PIC16F84A
그런 다음 몇 가지 샘플 이미지를 제공해주십시오. RHE 소스, 임계 후 이진이나 게 검출 된 윤곽 – PSchn