Skip to content
Snippets Groups Projects
Commit 2c167edb authored by Katja's avatar Katja
Browse files

Ex 2working

parent 36236107
No related branches found
No related tags found
No related merge requests found
......@@ -6,8 +6,8 @@ using namespace cv;
using namespace std;
int threshold_value = 127;
int threshold_type = 3;
int threshold_value = 159;
int threshold_type = 0;
int threshold_normal_or_adaptive = 0;
int const max_value = 255;
int const max_type = 4;
......@@ -32,6 +32,85 @@ Mat frameIfNotFound;
Mat frameGray;
Mat frameThreshold;
static void FindAndDrawContour()
{
Mat contours = frameThreshold.clone();
vector<Vec4i> hierarcy;
vector<vector<Point> > contoursVector;
Mat dstImg(frameThreshold.size(), CV_8UC3, Scalar::all(0));//pure black image
findContours(contours, contoursVector, hierarcy, RETR_TREE, CHAIN_APPROX_NONE);
vector<vector<Point>> poly(contoursVector.size());//for storing polyline point
vector<vector<Point>> boundRect(0);
double approxValue = 1.0;
for (int i = 0; i < contoursVector.size(); i++)
{
approxValue =arcLength(contoursVector[i], true)*0.02;
approxPolyDP(Mat(contoursVector[i]), poly[i], approxValue, true);
int checkArea = boundingRect(poly[i]).width * boundingRect(poly[i]).height;
Rect test = boundingRect(poly[i]);
if (poly[i].size() == 4 && checkArea >= 1) {
boundRect.push_back(poly[i]);
}
//drawContours(dstImg, poly, i, Scalar(128, 255, 128), 2, 8); //Draw
}
if (boundRect.size() > 0) {
Mat tmp = contours.clone();
cvtColor(contours, tmp, cv::COLOR_GRAY2BGR);
for (int j = 0; j < boundRect.size(); j++) {
vector<Point> tmpR = boundRect[j];
Point tmp0 = tmpR[0];
Point tmp1 = tmpR[1];
Point tmp2 = tmpR[2];
Point tmp3 = tmpR[3];
double l1 = sqrt(pow(tmp2.x - tmp1.x, 2) + pow(tmp2.y - tmp1.y, 2) * 1.0);
double l0 = sqrt(pow(tmp1.x - tmp0.x, 2) + pow(tmp1.y - tmp0.y, 2) * 1.0);
double l2 = sqrt(pow(tmp3.x - tmp2.x, 2) + pow(tmp3.y - tmp2.y, 2) * 1.0);
double l3 = sqrt(pow(tmp0.x - tmp3.x, 2) + pow(tmp0.y - tmp3.y, 2) * 1.0);
Point u0 = (tmp0-tmp1) / (sqrt(pow(tmp0.x - tmp1.x,2)+ pow(tmp0.y - tmp1.y, 2)));
Point u1 = (tmp1-tmp2) / (sqrt(pow(tmp2.x - tmp2.x, 2) + pow(tmp1.y - tmp2.y, 2)));
Point u2 = (tmp2- tmp3) / (sqrt(pow(tmp2.x - tmp3.x, 2) + pow(tmp2.y - tmp3.y, 2)));
Point u3 = (tmp3-tmp0) / (sqrt(pow(tmp3.x - tmp0.x, 2) + pow(tmp3.y - tmp0.y, 2)));
Point p;
double dist_X;
double dist_Y;
for (int x = 1; x < 7; x++) {
dist_X = tmp0.x - u0.x * ((l0 / 7) * x);
dist_Y = tmp0.y - u0.y * ((l0 / 7) * x);
p.x = dist_X;
p.y = dist_Y;
circle(tmp, p, 4, (0, 0, 255), CV_FILLED);
dist_X = tmp1.x - u1.x * ((l1 / 7) * x);
dist_Y = tmp1.y - u1.y * ((l1 / 7) * x);
p.x = dist_X;
p.y = dist_Y;
circle(tmp, p, 4, (0, 0, 255), CV_FILLED);
dist_X = tmp2.x - u2.x * ((l2 / 7) * x);
dist_Y = tmp2.y - u2.y * ((l2 / 7) * x);
p.x = dist_X;
p.y = dist_Y;
circle(tmp, p, 4, (0, 0, 255), CV_FILLED);
dist_X = tmp3.x - u3.x * ((l3 / 7) * x);
dist_Y = tmp3.y - u3.y * ((l3 / 7) * x);
p.x = dist_X;
p.y = dist_Y;
circle(tmp, p, 4, (0, 0, 255), CV_FILLED);
}
}
polylines(tmp, boundRect, true, Scalar(0, 0, 255), 2);
imshow(window_name, tmp);
}
}
static void Adaptive_Threshold_Demo(int, void*, int, int) //Gau_or_Mean
{
......@@ -41,6 +120,7 @@ static void Adaptive_Threshold_Demo(int, void*, int, int) //Gau
adaptiveThreshold(frameGray, frameThreshold, max_binary_value, adapt_tresh_mean_or_gau, threshold_type, adapt_tresh_block_size, adapt_tresh_C_value);
namedWindow(window_name, CV_WINDOW_AUTOSIZE);
imshow(window_name, frameThreshold);
FindAndDrawContour();
}
static void Threshold_Demo(int, void*)
......@@ -55,6 +135,7 @@ static void Threshold_Demo(int, void*)
threshold(frameGray, frameThreshold, threshold_value, max_binary_value, threshold_type);
namedWindow(window_name, CV_WINDOW_AUTOSIZE);
imshow(window_name, frameThreshold);
FindAndDrawContour();
}
int main(int argc, char** argv)
......@@ -117,7 +198,7 @@ int main(int argc, char** argv)
if (threshold_type > 1) {
threshold_type = 0;
}
window_name = "Adaptive treshold";
window_name = "Adaptive threshold";
//adapt_tresh_mean_or_gau, threshold_type, adapt_tresh_block_size, adapt_tresh_C_value
// Choose between normal and adaptive
createTrackbar(trackbar_tresh_type,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment