Beruflich Dokumente
Kultur Dokumente
Contours can be explained simply as a curve joining all the continuous points (along the
boundary), having same color or intensity. The contours are a useful tool for shape analysis and
object detection and recognition.
For better accuracy, use binary images. So before finding contours, apply threshold or
canny edge detection.
findContours function modifies the source image. So if you want source image even after
finding contours, already store it to some other variables.
In OpenCV, finding contours is like finding white object from black background. So
remember, object to be found should be white and background should be black.
int
mode,
int
method,
Point
offset=Point())
Parameters:
image Source, an 8-bit single-channel image. Non-zero pixels are treated as 1s. Zero
pixels remain 0s, so the image is treated as binary . You can use compare() , inRange() ,
threshold() , adaptiveThreshold() , Canny() , and others to create a binary image out of a
grayscale or color one. The function modifies the image while extracting the contours.
mode Contour retrieval mode (if you use Python see also a note below).
o CV_RETR_EXTERNAL retrieves only the extreme outer contours. It sets
hierarchy[i][2]=hierarchy[i][3]=-1 for all the contours.
method Contour approximation method (if you use Python see also a note below). .
CODE
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <cmath>
#include <iostream>
pt
Point(0,
baseline),
pt
Point(text.width,
CV_RGB(255,255,255), CV_FILLED);
putText(im, label, pt, font, scale, CV_RGB(0,0,0), thickness, 8);
}
int main() {
VideoCapture cap(0);
Mat frame;
while (true) {
cap.read(frame);
Mat image = frame.clone();
//Mat image = imread("shape1.png");
-text.height),
//imshow("test", image);
//waitKey(10);
if (image.empty()) {return -1;}
// Convert to grayscale
Mat gray;
cvtColor(image, gray, CV_BGR2GRAY);
contours,
CV_RETR_EXTERNAL,
sort(cos.begin(), cos.end());
// Get the lowest and the highest cosine
double mincos = cos.front();
double maxcos = cos.back();
// Use the degrees obtained above and the number of vertices
// to determine the shape of the contour
if (sisi == 3 && mincos >= 0 && maxcos <= 1)
setLabel(result, "S3", contours[i]);
else if (sisi == 4 && mincos >= -0.26 && maxcos <= 0)
setLabel(result, "S4", contours[i]);
else if (sisi == 5 && mincos >= -0.34 && maxcos <= -0.27)
setLabel(result, "S5", contours[i]);
else if (sisi == 6 && mincos >= -0.6 && maxcos <= -0.45)
setLabel(result, "S6", contours[i]);
else if (sisi == 7 && mincos >= -0.66 && maxcos <= -0.6)
setLabel(result, "S7", contours[i]);
}
else
{
imshow("image", image);
imshow("bw", bw);
imshow("result", result);
waitKey(250);
}
return 0;
}
CODE EXPLANATIONS
TESTING