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

Tutorial3 more or less working

parent 2c167edb
Branches master
No related tags found
No related merge requests found
......@@ -143,6 +143,10 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Quelle.cpp" />
<ClCompile Include="Quelle1.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="myHeader.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
......
......@@ -18,5 +18,13 @@
<ClCompile Include="Quelle.cpp">
<Filter>Quelldateien</Filter>
</ClCompile>
<ClCompile Include="Quelle1.cpp">
<Filter>Quelldateien</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="myHeader.h">
<Filter>Headerdateien</Filter>
</ClInclude>
</ItemGroup>
</Project>
\ No newline at end of file
#include<iostream>
#include<opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include "myHeader.h"
using namespace cv;
using namespace std;
int threshold_value = 159;
int threshold_value = 80;
int threshold_type = 0;
int threshold_normal_or_adaptive = 0;
int const max_value = 255;
int const max_type = 4;
int const max_binary_value = 255;
int adapt_tresh_mean_or_gau = 0;
int adapt_tresh_block_size = 7;
int adapt_tresh_C_value = 2;
int adapt_tresh_block_size = 34;
int adapt_tresh_C_value = 5;
const char* trackbar_type = "Type: \n 0: Binary \n 1: Binary Inverted \n 2: Truncate \n 3: To Zero \n 4: To Zero Inverted";
......@@ -35,80 +36,86 @@ 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);
vector<vector<Point>> boundRect;
double approxValue = 1.0;
Mat tmp = contours.clone();
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) {
int area = contourArea(poly[i]);
if (poly[i].size() == 4 && area > 200) {
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)));
vector < vector<Point>> subdividingPoints(4);
double l0x = ((double)tmpR[1].x - (double)tmpR[0].x) / 7.0;
double l0y = ((double)tmpR[1].y - (double)tmpR[0].y) / 7.0;
double l1x = ((double)tmpR[2].x - (double)tmpR[1].x) / 7.0;
double l1y = ((double)tmpR[2].y - (double)tmpR[1].y) / 7.0;
double l2x = ((double)tmpR[3].x - (double)tmpR[2].x) / 7.0;
double l2y = ((double)tmpR[3].y - (double)tmpR[2].y) / 7.0;
double l3x = ((double)tmpR[0].x - (double)tmpR[3].x) / 7.0;
double l3y = ((double)tmpR[0].y - (double)tmpR[3].y) / 7.0;
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);
dist_X = tmpR[0].x + x* l0x;
dist_Y = tmpR[0].y + x * l0y;
p.x = dist_X;
p.y = dist_Y;
circle(tmp, p, 4, (0, 0, 255), CV_FILLED);
subdividingPoints[0].push_back(p);
//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);
dist_X = tmpR[1].x + x * l1x;
dist_Y = tmpR[1].y + x * l1y;
p.x = dist_X;
p.y = dist_Y;
circle(tmp, p, 4, (0, 0, 255), CV_FILLED);
subdividingPoints[1].push_back(p);
//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);
dist_X = tmpR[2].x + x * l2x;
dist_Y = tmpR[2].y + x * l2y;
p.x = dist_X;
p.y = dist_Y;
circle(tmp, p, 4, (0, 0, 255), CV_FILLED);
subdividingPoints[2].push_back(p);
//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);
dist_X = tmpR[3].x + x * l3x;
dist_Y = tmpR[3].y + x * l3y;
p.x = dist_X;
p.y = dist_Y;
circle(tmp, p, 4, (0, 0, 255), CV_FILLED);
subdividingPoints[3].push_back(p);
//circle(tmp, p, 4, (0, 0, 255), CV_FILLED);
}
polylines(tmp, boundRect, true, Scalar(0, 0, 255), 2);
tmp=getStripes(window_name, subdividingPoints, tmpR, frameThreshold, tmp);
}
polylines(tmp, boundRect, true, Scalar(0, 0, 255), 2);
imshow(window_name, tmp);
}
imshow(window_name, tmp);
}
......
#include<iostream>
#include<opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include "myHeader.h"
using namespace cv;
using namespace std;
struct stripeSizeAndVector {
cv::Size stripeSize;
cv::Point2f stripeVecX;
cv::Point2f stripeVecY;
};
auto getStripSizeAndVector(double dx, double dy)
{
struct stripeSizeAndVector s;
cv::Size stripeSize;
cv::Point2f stripeVecX;
cv::Point2f stripeVecY;
double a = dy * dy;
double b = dx * dx;
int stripeLength = (int)(0.8 * sqrt(a+b));
if (stripeLength < 5)
stripeLength = 5;
stripeSize.width = 3;
stripeSize.height = stripeLength;
double diffLength = sqrt(dx * dx + dy * dy);
stripeVecX.x = dx / diffLength;
stripeVecX.y = dy / diffLength;
stripeVecY.x = stripeVecX.y;
stripeVecY.y = -stripeVecX.x;
s.stripeSize = stripeSize;
s.stripeVecX = stripeVecX;
s.stripeVecY = stripeVecY;
return s;
}
Mat getStripes(const char* window_name, vector<vector<Point>> subdividingPoints, vector<Point> corners, Mat img_gray, Mat tmp)
{
cv::Size stripeSize;
cv::Point2f stripeVecX;
cv::Point2f stripeVecY;
cv::Mat output;
stripeSizeAndVector s;
for (int i = 0; i < 4; i++) { //For each edge
Point tmpLine = corners[i];
vector<Point> subdivThisLine = subdividingPoints[i];
double dx = ((double)corners[(i + 1) % 4].x - (double)corners[i].x);
double dy = ((double)corners[(i + 1) % 4].y - (double)corners[i].y);
dy = dy / 7.0;
dx = dx / 7.0;
s = getStripSizeAndVector(dx, dy);
stripeSize = s.stripeSize;
stripeVecX = s.stripeVecX;
stripeVecY = s.stripeVecY;
for (int j = 1; j < 7; j++) { //For each subdividing point
cv::Mat iplStripe(stripeSize, CV_8UC1);
double px = (double)corners[i].x + (double)j * dx;
double py = (double)corners[i].y + (double)j * dy;
Point usedPoint;
usedPoint.x = (int)px;
usedPoint.y = (int)py;
for (int m = -1; m <= 1; ++m) {
int nStart = -stripeSize.height / 2;
int nStop = stripeSize.height/2;
for (int n = nStart; n < nStop; ++n) {
cv::Point2f subPixel;
subPixel.x = usedPoint.x;
subPixel.y = usedPoint.y;
subPixel = subPixel + n * stripeVecX + m * stripeVecY;
int pixel = subpixSampleSafe(tmp, subPixel);
int w = m + 1;
int h = n + (stripeSize.height >> 1);
iplStripe.at<uchar>(h, w) = (uchar)pixel;
}
}
Mat iplStripe_afterSobel;
Sobel(iplStripe, iplStripe_afterSobel, -1, 1, 0);
vector<uchar> center_col = iplStripe_afterSobel.col(1);
int curr_max = -1;
int curr_index = -1;
for (int k = 1; k < center_col.size() - 1; k++) {
if (center_col[k] > curr_max) {
curr_max = center_col[k];
curr_index = k;
}
}
int neighLeft = curr_max -1;
int neighRight = curr_max +1;
Point newPoint;
newPoint.x = 0;
for (int l = 0; l < iplStripe_afterSobel.cols; ++l) {
if (2 * neighLeft * l + curr_max == 0) {
newPoint.x = l;
break;
}
}
newPoint.y = neighLeft * pow(newPoint.x, 2) + ((double)curr_max * (double)newPoint.x) + neighRight;
int pixel = subpixSampleSafe(img_gray, newPoint);
const string contoursWindow = "Contours";
namedWindow(contoursWindow, CV_WINDOW_FREERATIO);
Point test = usedPoint;
test.x =test.x + newPoint.x;
test.y = test.y + newPoint.y;
circle(tmp, test, 4, (0, 0, 125), CV_FILLED);
output = tmp;
imshow(contoursWindow, center_col);
}
}
return output;
}
int subpixSampleSafe(const cv::Mat& pSrc, const cv::Point2f& p)
{
int x = int(floorf(p.x));
int y = int(floorf(p.y));
if (x < 0 || x >= pSrc.cols - 1 ||
y < 0 || y >= pSrc.rows - 1)
return 127;
int dx = int(256 * (p.x - floorf(p.x)));
int dy = int(256 * (p.y - floorf(p.y)));
unsigned char* i = (unsigned char*)((pSrc.data + y * pSrc.step) + x);
int a = i[0] + ((dx * (i[1] - i[0])) >> 8);
i += pSrc.step;
int b = i[0] + ((dx * (i[1] - i[0])) >> 8);
return a + ((dy * (b - a)) >> 8);
}
\ No newline at end of file
#ifndef MY_HEADER
#define MY_HEADER
using namespace cv;
using namespace std;
Mat getStripes(const char* window_name, vector<vector<Point>> subdividingPoints, vector<Point> corners, Mat img_gray, Mat tmp);
int subpixSampleSafe(const Mat& pSrc, const Point2f& p);
#endif
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