Commit f379a9e6 authored by Nadir Dalla Pozza's avatar Nadir Dalla Pozza
Browse files

Capstan detection.

parent ff2e2913
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
width="210mm"
height="297mm"
viewBox="0 0 210 297"
version="1.1"
id="svg5"
inkscape:export-filename="/Users/nadir/Documents/MPAI-CAE/AIMs/VideoAnalyser/input/tapeShape.png"
inkscape:export-xdpi="96"
inkscape:export-ydpi="96"
inkscape:version="1.1.2 (b8e25be8, 2022-02-05)"
sodipodi:docname="tapeShape.svg"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns="http://www.w3.org/2000/svg"
xmlns:svg="http://www.w3.org/2000/svg">
<sodipodi:namedview
id="namedview7"
pagecolor="#000000"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageshadow="2"
inkscape:pageopacity="0"
inkscape:pagecheckerboard="0"
inkscape:document-units="px"
showgrid="false"
inkscape:zoom="1"
inkscape:cx="370.5"
inkscape:cy="233.5"
inkscape:window-width="1920"
inkscape:window-height="1027"
inkscape:window-x="0"
inkscape:window-y="25"
inkscape:window-maximized="0"
inkscape:current-layer="layer1" />
<defs
id="defs2" />
<g
inkscape:label="Livello 1"
inkscape:groupmode="layer"
id="layer1">
<rect
style="font-variation-settings:'wght' 700;fill:#000000;stroke:#ffffff;stroke-width:0.264583;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none"
id="rect123"
width="163.77708"
height="10.847917"
x="0.13229164"
y="0.1322915"
inkscape:export-filename="/Users/nadir/Documents/MPAI-CAE/AIMs/VideoAnalyser/input/tapeShape.png"
inkscape:export-xdpi="96"
inkscape:export-ydpi="96" />
</g>
</svg>
#include "vector"
#include "string.h"
#include "fstream"
#include <vector>
#include <string.h>
#include <fstream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
......
This diff is collapsed.
#include "rapidxml-1.13/rapidxml.hpp"
#include "vector"
#include "string.h"
#include "fstream"
#include <vector>
#include <string.h>
#include <fstream>
namespace fs = std::__fs::filesystem;
using namespace cv;
using namespace rapidxml;
using namespace std;
......@@ -184,6 +184,123 @@ int findFileName(std::string videoPath, std::string &fileName, std::string &exte
COMPUTER VISION FUNCTIONS
------------------------------------------------------------------------------ */
// Function to detect shape in frame
void detectShape(Ptr<GeneralizedHoughGuil> alg, Mat templateShape, int posThresh, vector<Vec4f> &positivePositions, Mat &positiveVotes, vector<Vec4f> &negativePositions, Mat &negativeVotes, Mat processingArea) {
alg -> setPosThresh(posThresh);
alg -> setTemplate(templateShape);
int oldSizePositive = 0;
int i = 0;
int maxVote = 0;
// Process shapes with positive angles
alg -> setMinAngle(0);
alg -> setMaxAngle(5);
while (true) {
alg -> detect(processingArea, positivePositions, positiveVotes);
int currentSize = positivePositions.size();
if (currentSize == 1) {
// We detected the most interesting shape
break;
} else if (currentSize == 0 && oldSizePositive > 0) {
// It is not possible to detect only one shape with the current parameters
alg -> setPosThresh(posThresh+i-1); // Decrease position value
alg -> detect(processingArea, positivePositions, positiveVotes); // Detect all available shapes
break;
} else if (currentSize == 0 && oldSizePositive == 0) {
// Impossible to found with these parameters
cout << "Not found." << endl;
break;
}
oldSizePositive = currentSize;
// Find maximum vote
for (int j = 0; j < positiveVotes.cols / 3; j++) {
if (positiveVotes.at<int>(3*j) > maxVote)
maxVote = positiveVotes.at<int>(3*j);
}
if (currentSize > 10) {
i += 5; // To speed up computation when there are too many matches
} else if (maxVote - (posThresh + i) > 100) {
i += 100; // To speed up computation when there are few super high matches
} else {
i++;
}
alg -> setPosThresh(posThresh+i);
cout << "\rPositive CurrentSize: " << currentSize << flush;
}
cout << endl;
int oldSizeNegative = 0;
// Reset incremental position value
i = 0;
maxVote = 0;
// Process shapes with negative angles
alg -> setMinAngle(355);
alg -> setMaxAngle(360);
while (true) {
alg -> detect(processingArea, negativePositions, negativeVotes);
int currentSize = negativePositions.size();
if (currentSize == 1) {
// We detected the most interesting shape
break;
} else if (currentSize == 0 && oldSizeNegative > 0) {
// It is not possible to detect only one shape with the current parameters
alg -> setPosThresh(posThresh+i-1); // Decrease position value
alg -> detect(processingArea, negativePositions, negativeVotes); // Detect all available shapes
break;
} else if (currentSize == 0 && oldSizeNegative == 0) {
// Impossible to found with these parameters
cout << "Not found." << endl;
break;
}
oldSizeNegative = currentSize;
// Find maximum vote
for (int j = 0; j < positiveVotes.cols / 3; j++) {
if (positiveVotes.at<int>(3*j) > maxVote)
maxVote = positiveVotes.at<int>(3*j);
}
if (currentSize > 10) {
i += 5; // To speed up computation when there are too many matches
} else if (maxVote - (posThresh + i) > 100) {
i += 100; // To speed up computation when there are few super high matches
} else {
i++;
}
alg -> setPosThresh(posThresh+i);
cout << "\rNegative CurrentSize: " << currentSize << flush;
}
cout << endl;
}
// Function to draw detected shapes in a frame
void drawShapes(Mat frame, vector<Vec4f> &positions, Scalar color, Mat templateShape, int offsetX, int offsetY) {
RotatedRect rr;
Point2f rrpts[4];
for (int i = 0; i < positions.size(); i++) {
Point2f pos(positions[i][0]+offsetX+11, positions[i][1]+offsetY+46);
float scale = positions[i][2];
float angle = positions[i][3];
rr.center = pos;
rr.size = Size2f((templateShape.cols-22) * scale, (templateShape.rows-92) * scale);
rr.angle = angle;
rr.points(rrpts);
line(frame, rrpts[0], rrpts[1], color, 2);
line(frame, rrpts[1], rrpts[2], color, 2);
line(frame, rrpts[2], rrpts[3], color, 2);
line(frame, rrpts[3], rrpts[0], color, 2);
}
}
// Function to separate even and odd frame half planes
void separateFrame(cv::Mat frame, cv::Mat &frame_dispari, cv::Mat &frame_pari) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment