Commit 1c630295 authored by Nadir Dalla Pozza's avatar Nadir Dalla Pozza
Browse files

Update.

parent f379a9e6
No preview for this file type
......@@ -2,7 +2,7 @@
"Brands": true,
"IrregularityFileInput": "../input/BERIO060.mov_IrregularityFileInput.json",
"OutputPath": "../output/",
"PreservationAudioVisualFile": "../input/LNONO044.mov",
"PreservationAudioVisualFile": "../input/BERIO058.mov",
"Speed": 15,
"ThresholdPercentual": 80,
"ThresholdPercentualPinchRoller": 90,
......
......@@ -75,8 +75,7 @@ json irregularityFileInput;
json irregularityFileOutput1;
json irregularityFileOutput2;
// RotatedRect identifying the processing area
RotatedRect rect;
RotatedRect capstanRect;
RotatedRect rect, rectTape, rectCapstan;
......@@ -342,14 +341,7 @@ bool findProcessingAreas(json configurationFile) {
// Obtain grayscale version of myFrame
Mat myFrameGrayscale;
cvtColor(myFrame, myFrameGrayscale, COLOR_BGR2GRAY);
// Rect capstanAreaRect(590, 270, 130, 250);
// // Obtain grayscale version of tapeProcessingArea
// Mat capstan = myFrameGrayscale(capstanAreaRect);
// imshow("myFrame", myFrameGrayscale);
// imshow("tapeProcessingAreaGrayscale", capstan);
// imwrite("/users/nadir/desktop/capstanBERIO058prova.png", capstan);
// waitKey();
// Downsample myFrameGrayscale in half pixels
// Downsample myFrameGrayscale in half pixels for performance reasons
Mat myFrameGrayscaleHalf;
pyrDown(myFrameGrayscale, myFrameGrayscaleHalf, Size(myFrame.cols/2, myFrame.rows/2));
......@@ -387,54 +379,43 @@ bool findProcessingAreas(json configurationFile) {
alg -> setTemplate(templateShape);
vector<Vec4f> positions, positions2;
vector<Vec4f> positionsPos, positionsNeg;
Mat votesPos, votesNeg;
TickMeter tm;
int oldPosThresh = posThresh;
std::cout << "\033[1;36mReading head\033[0m" << endl;
tm.start();
// Parameters are quite slack, therefore more than one match should be expected.
// By inspecting different angles (only between +10 and -10 degrees of maximum inclination) or increasing the position threshold,
// the algorithm should eventually identify only one region.
while (positions.size() != 1) {
alg -> setMinAngle(0);
alg -> setMaxAngle(10);
alg -> detect(processingImage, positions);
if (positions.size() == 1)
break;
alg -> setMinAngle(350);
alg -> setMaxAngle(360);
alg -> detect(processingImage, positions);
if (positions.size() == 1)
break;
oldPosThresh += 10;
alg -> setPosThresh(oldPosThresh);
}
detectShape(alg, templateShape, posThresh, positionsPos, votesPos, positionsNeg, votesNeg, myFrameGrayscaleHalf);
tm.stop();
std::cout << "Reading head detection time : " << tm.getTimeMilli() << " ms" << endl;
Point2f pos(positions[0][0], positions[0][1]);
float scale = positions[0][2];
float angle = positions[0][3];
rect.center = pos * 2; // * 2 since the processed image is half the original one
rect.size = Size2f(templateShape.cols * scale * 2, templateShape.rows * scale * 2); // * 2 for the same reason
rect.angle = angle;
RotatedRect rectPos = drawShapes(myFrame, positionsPos, Scalar(0, 0, 255), templateImageHalf.cols, templateImageHalf.rows, 0, 0, 2);
RotatedRect rectNeg = drawShapes(myFrame, positionsNeg, Scalar(128, 128, 255), templateImageHalf.cols, templateImageHalf.rows, 0, 0, 2);
Point2f pts[4];
if (rectPos.size.width > 0)
if (rectNeg.size.width > 0)
if (votesPos.at<int>(0) > votesNeg.at<int>(0)) {
cout << "Positive is best" << endl;
rect = rectPos;
} else {
cout << "Negative is best" << endl;
rect = rectNeg;
}
else {
cout << "Positive is the only choice." << endl;
rect = rectPos;
}
else if (rectNeg.size.width > 0) {
cout << "Negative is the only choice." << endl;
rect = rectNeg;
} else {
return false;
}
cout << endl;
rect.points(pts);
// Red for the reading head
line(myFrame, pts[0], pts[1], Scalar(0, 0, 255), 2);
line(myFrame, pts[1], pts[2], Scalar(0, 0, 255), 2);
line(myFrame, pts[2], pts[3], Scalar(0, 0, 255), 2);
line(myFrame, pts[3], pts[0], Scalar(0, 0, 255), 2);
/******************************************* TAPE AREA DETECTION *******************************************/
// Defining the processing area for identifying the tape under the reading head.
......@@ -477,94 +458,50 @@ bool findProcessingAreas(json configurationFile) {
oldPosThresh = posThreshTape;
vector<Vec4f> positionsTapePos, positionsTapeNeg;
Mat votesTapePos, votesTapeNeg;
std::cout << "\033[1;36mTape\033[0m" << endl;
tm.reset();
tm.start();
for (int i = 0; i < 10; i++) {
alg -> setMinAngle(0);
alg -> setMaxAngle(10);
alg -> detect(tapeProcessingAreaGrayscale, positions2);
if (positions2.size() == 1) {
found = true;
break;
}
alg -> setMinAngle(350);
alg -> setMaxAngle(360);
alg -> detect(tapeProcessingAreaGrayscale, positions2);
if (positions2.size() == 1) {
found = true;
break;
}
oldPosThresh += 10;
alg -> setPosThresh(oldPosThresh);
}
detectShape(alg, templateShape, posThreshTape, positionsTapePos, votesTapePos, positionsTapeNeg, votesTapeNeg, tapeProcessingAreaGrayscale);
tm.stop();
std::cout << "Tape detection time : " << tm.getTimeMilli() << " ms" << endl;
for (int i = 0; i < positions2.size(); i++) {
Point2f pos2(positions2[i][0], positions2[i][1]);
scale = positions2[i][2];
angle = positions2[i][3];
rect.center = pos2;
rect.size = Size2f(templateShape.cols * scale, templateShape.rows * scale);
rect.angle = angle;
rect.points(pts);
// Update points with tape processing area coordinates
pts[0] = Point2f(pts[0].x+tapeProcessingAreaX, pts[0].y+tapeProcessingAreaY);
pts[1] = Point2f(pts[1].x+tapeProcessingAreaX, pts[1].y+tapeProcessingAreaY);
pts[2] = Point2f(pts[2].x+tapeProcessingAreaX, pts[2].y+tapeProcessingAreaY);
pts[3] = Point2f(pts[3].x+tapeProcessingAreaX, pts[3].y+tapeProcessingAreaY);
// Update rect
rect = RotatedRect(pts[0], pts[1], pts[2]);
line(myFrame, pts[0], pts[1], Scalar(0, 255, 0), 2);
line(myFrame, pts[1], pts[2], Scalar(0, 255, 0), 2);
line(myFrame, pts[2], pts[3], Scalar(0, 255, 0), 2);
line(myFrame, pts[3], pts[0], Scalar(0, 255, 0), 2);
RotatedRect rectTapePos = drawShapes(myFrame, positionsTapePos, Scalar(0, 255, 0), templateShape.cols, templateShape.rows, tapeProcessingAreaX, tapeProcessingAreaY, 1);
RotatedRect rectTapeNeg = drawShapes(myFrame, positionsTapeNeg, Scalar(128, 255, 128), templateShape.cols, templateShape.rows, tapeProcessingAreaX, tapeProcessingAreaY, 1);
if (rectTapePos.size.width > 0)
if (rectTapeNeg.size.width > 0)
if (votesTapePos.at<int>(0) > votesTapeNeg.at<int>(0)) {
cout << "Positive is best" << endl;
rectTape = rectTapePos;
} else {
cout << "Negative is best" << endl;
rectTape = rectTapeNeg;
}
else {
cout << "Positive is the only choice." << endl;
rectTape = rectTapePos;
}
else if (rectTapeNeg.size.width > 0) {
cout << "Negative is the only choice." << endl;
rectTape = rectTapeNeg;
} else {
return false;
}
cout << endl;
/******************************************* CAPSTAN DETECTION *******************************************/
// Process only right portion of the image, wherw the capstain always appears
int capstanProcessingAreaRectX = myFrame.cols*3/4;
int capstanProcessingAreaRectY = myFrame.rows/2;
int capstanProcessingAreaRectWidth = myFrame.cols/4;
int capstanProcessingAreaRectHeight = myFrame.rows/2;
Rect capstanProcessingAreaRect(capstanProcessingAreaRectX, capstanProcessingAreaRectY, capstanProcessingAreaRectWidth, capstanProcessingAreaRectHeight);
// Examine myFrameGrayscale only in its left half
Mat capstanProcessingAreaGrayscale = myFrameGrayscale(capstanProcessingAreaRect);
// imshow("capstanProcessingAreaGrayscale", capstanProcessingAreaGrayscale);
// Read template image - it is smaller than before, therefore there is no need to downsample
templateShape = imread("../input/capstanBERIO058prova.png", IMREAD_GRAYSCALE);
// imshow("capstanProcessingAreaGrayscale", capstanProcessingAreaGrayscale);
// imshow("capstan", templateShape);
// Obtain Canny versions of templateImage and capstanProcessingArea
Mat templateImageCanny, capstanProcessingAreaCanny, templateImageCannyCanny;
// Canny(capstanProcessingAreaGrayscale, capstanProcessingAreaCanny, 1, 50);
// imshow("1 50", capstanProcessingAreaCanny);
// Canny(capstanProcessingAreaGrayscale, capstanProcessingAreaCanny, 20, 100);
// imshow("20 100", capstanProcessingAreaCanny);
// Canny(capstanProcessingAreaGrayscale, capstanProcessingAreaCanny, 25, 150);
// imshow("25 150", capstanProcessingAreaCanny);
// Canny(capstanProcessingAreaGrayscale, capstanProcessingAreaCanny, 50, 150);
// imshow("50 150", capstanProcessingAreaCanny);
// Canny(capstanProcessingAreaGrayscale, capstanProcessingAreaCanny, 50, 250);
// imshow("50 250", capstanProcessingAreaCanny);
// Canny(capstanProcessingAreaGrayscale, capstanProcessingAreaCanny, 100, 250);
// imshow("100 250", capstanProcessingAreaCanny);
// Canny(capstanProcessingAreaGrayscale, capstanProcessingAreaCanny, 150, 300);
// imshow("50 200", capstanProcessingAreaCanny);
// Canny(capstanProcessingAreaGrayscale, capstanProcessingAreaCanny, 200, 300);
// imshow("200 300", capstanProcessingAreaCanny);
// Canny(templateShape, templateImageCanny, 50, 250);
// imshow("capstanCanny", templateImageCanny);
// Reset algorithm and set parameters
alg = createGeneralizedHoughGuil();
......@@ -591,97 +528,38 @@ bool findProcessingAreas(json configurationFile) {
oldPosThresh = posThreshCapstan;
vector<Vec4f> positionsC1pos, positionsC1neg, positionsC2, positionsC3, positionsC4, positionsC5, positionsC6, positionsC7, positionsC8;
Mat votesC1pos, votesC1neg, votesC2, votesC3, votesC4, votesC5, votesC6, votesC7, votesC8;
vector<Vec4f> positionsC1Pos, positionsC1Neg;
Mat votesC1Pos, votesC1Neg;
std::cout << "\033[1;36mCapstan\033[0m" << endl;
tm.reset();
tm.start();
cout << "capstanBERIO058 blue" << endl;
detectShape(alg, templateShape, posThreshCapstan, positionsC1pos, votesC1pos, positionsC1neg, votesC1neg, capstanProcessingAreaGrayscale);
drawShapes(myFrame, positionsC1pos, Scalar(255, 0, 0), templateShape, capstanProcessingAreaRectX, capstanProcessingAreaRectY);
drawShapes(myFrame, positionsC1neg, Scalar(255, 255, 0), templateShape, capstanProcessingAreaRectX, capstanProcessingAreaRectY);
// cout << "capstanBERIO060 light blue" << endl;
// templateShape = imread("../input/capstanBERIO060.png", IMREAD_GRAYSCALE);
// detectShape(alg, templateShape, posThreshCapstan, positionsC2, votesC2, capstanProcessingAreaGrayscale);
// cout << "capstanBERIO236 yellow" << endl;
// templateShape = imread("../input/capstanBERIO236.png", IMREAD_GRAYSCALE);
// detectShape(alg, templateShape, posThreshCapstan, positionsC3, votesC3, capstanProcessingAreaGrayscale);
// cout << "capstanBERIO297 violet" << endl;
// templateShape = imread("../input/capstanBERIO297.png", IMREAD_GRAYSCALE);
// detectShape(alg, templateShape, posThreshCapstan, positionsC4, votesC4, capstanProcessingAreaGrayscale);
// cout << "capstanBERIO319 gray" << endl;
// templateShape = imread("../input/capstanBERIO319.png", IMREAD_GRAYSCALE);
// detectShape(alg, templateShape, posThreshCapstan, positionsC5, votesC5, capstanProcessingAreaGrayscale);
// cout << "capstanBERIO333 pink" << endl;
// templateShape = imread("../input/capstanBERIO333.png", IMREAD_GRAYSCALE);
// detectShape(alg, templateShape, posThreshCapstan, positionsC6, votesC6, capstanProcessingAreaGrayscale);
// cout << "capstanBERIO415 indigo" << endl;
// templateShape = imread("../input/capstanBERIO415.png", IMREAD_GRAYSCALE);
// detectShape(alg, templateShape, posThreshCapstan, positionsC7, votesC7, capstanProcessingAreaGrayscale);
// cout << "capstanLNONO044 dark violet" << endl;
// templateShape = imread("../input/capstanLNONO044.png", IMREAD_GRAYSCALE);
// detectShape(alg, templateShape, posThreshCapstan, positionsC8, votesC8, capstanProcessingAreaGrayscale);
detectShape(alg, templateShape, posThreshCapstan, positionsC1Pos, votesC1Pos, positionsC1Neg, votesC1Neg, capstanProcessingAreaGrayscale);
tm.stop();
std::cout << "Capstan detection time : " << tm.getTimeMilli() << " ms" << endl;
// Draw in white the one with highest position value
// int positionVote = 0;
// Vec4f finalPosition;
// for (int i = 0; i < positionsC1.size(); i++) {
// if (votesC1.at<int>(3*i) > positionVote)
// finalPosition = positionsC1[i];
// }
// for (int i = 0; i < positionsC2.size(); i++) {
// if (votes2.at<int>(3*i) > positionVote)
// finalPosition = positionsC2[i];
// }
// for (int i = 0; i < positionsC3.size(); i++) {
// if (votes3.at<int>(3*i) > positionVote)
// finalPosition = positionsC3[i];
// }
// for (int i = 0; i < positionsC4.size(); i++) {
// if (votes4.at<int>(3*i) > positionVote)
// finalPosition = positionsC4[i];
// }
// for (int i = 0; i < positionsC5.size(); i++) {
// if (votes5.at<int>(3*i) > positionVote)
// finalPosition = positionsC5[i];
// }
// for (int i = 0; i < positionsC6.size(); i++) {
// if (votes6.at<int>(3*i) > positionVote)
// finalPosition = positionsC6[i];
// }
// for (int i = 0; i < positionsC7.size(); i++) {
// if (votes7.at<int>(3*i) > positionVote)
// finalPosition = positionsC7[i];
// }
// for (int i = 0; i < positionsC8.size(); i++) {
// if (votes8.at<int>(3*i) > positionVote)
// finalPosition = positionsC8[i];
// }
// Point2f posFinal(finalPosition[0]+capstanProcessingAreaRectX, finalPosition[1]+capstanProcessingAreaRectY);
// scale = finalPosition[2];
// angle = finalPosition[3];
// RotatedRect finalRect;
// finalRect.center = posFinal;
// finalRect.size = Size2f(templateShape.cols * scale, templateShape.rows * scale);
// finalRect.angle = angle;
// finalRect.points(pts);
// Update points with tape processing area coordinates
// pts[0] = Point2f(pts[0].x, pts[0].y);
// pts[1] = Point2f(pts[1].xRectX, pts[1].y+capstanProcessingAreaRectY);
// pts[2] = Point2f(pts[2].x+capstanProcessingAreaRectX, pts[2].y+capstanProcessingAreaRectY);
// pts[3] = Point2f(pts[3].x+capstanProcessingAreaRectX, pts[3].y+capstanProcessingAreaRectY);
// // Update capstanRect
// capstanRect = RotatedRect(pts[0], pts[1], pts[2]);
// line(myFrame, pts[0], pts[1], Scalar(255, 255, 255), 2);
// line(myFrame, pts[1], pts[2], Scalar(255, 255, 255), 2);
// line(myFrame, pts[2], pts[3], Scalar(255, 255, 255), 2);
// line(myFrame, pts[3], pts[0], Scalar(255, 255, 255), 2);
RotatedRect rectCapstanPos = drawShapes(myFrame, positionsC1Pos, Scalar(255, 0, 0), templateShape.cols-22, templateShape.rows-92, capstanProcessingAreaRectX+11, capstanProcessingAreaRectY+46, 1);
RotatedRect rectCapstanNeg = drawShapes(myFrame, positionsC1Neg, Scalar(255, 128, 0), templateShape.cols-22, templateShape.rows-92, capstanProcessingAreaRectX+11, capstanProcessingAreaRectY+46, 1);
if (rectCapstanPos.size.width > 0)
if (rectCapstanNeg.size.width > 0)
if (votesC1Pos.at<int>(0) > votesC1Neg.at<int>(0)) {
cout << "Positive is best" << endl;
rectCapstan = rectCapstanPos;
} else {
cout << "Negative is best" << endl;
rectCapstan = rectCapstanNeg;
}
else {
cout << "Positive is the only choice." << endl;
rectCapstan = rectCapstanPos;
}
else if (rectTapeNeg.size.width > 0) {
cout << "Negative is the only choice." << endl;
rectCapstan = rectCapstanNeg;
} else {
return false;
}
cout << endl;
imshow("Tape area(s)", myFrame);
waitKey();
......@@ -742,6 +620,7 @@ int main(int argc, char** argv) {
std::cout << " Speed: " << speed << std::endl;
std::cout << " ThresholdPercentual: " << thresholdPercentual << std::endl;
std::cout << " ThresholdPercentualPinchRoller: " << thresholdPercentualPinchRoller << std::endl;
std::cout << std::endl;
// Read input JSON
iJSON >> irregularityFileInput;
......@@ -779,11 +658,11 @@ int main(int argc, char** argv) {
myFile << ts; // No endline character for avoiding middle blank line.
if (found) {
cout << "Processing area found!" << endl;
std::cout << "Processing area found!" << endl;
myFile << "Processing area found!" << endl;
myFile.close();
} else {
cout << "Processing area not found. Try changing JSON parameters." << endl;
std::cout << "Processing area not found. Try changing JSON parameters." << endl;
myFile << "Processing area not found." << endl;
myFile.close();
return 1; // Program terminated early
......@@ -808,7 +687,7 @@ int main(int argc, char** argv) {
float sec = (endTimer - startTimer) % 60;
std::string result("Processing elapsed time: " + std::to_string((int)min) + ":" + std::to_string((int)sec));
cout << endl << result << endl;
std::cout << endl << result << endl;
myFile.open("log.txt", ios::app);
myFile << result << std::endl << std::endl;
......
......@@ -197,6 +197,7 @@ void detectShape(Ptr<GeneralizedHoughGuil> alg, Mat templateShape, int posThresh
// Process shapes with positive angles
alg -> setMinAngle(0);
alg -> setMaxAngle(5);
std::cout << "Processing positive angles..." << endl;
while (true) {
alg -> detect(processingArea, positivePositions, positiveVotes);
int currentSize = positivePositions.size();
......@@ -210,7 +211,7 @@ void detectShape(Ptr<GeneralizedHoughGuil> alg, Mat templateShape, int posThresh
break;
} else if (currentSize == 0 && oldSizePositive == 0) {
// Impossible to found with these parameters
cout << "Not found." << endl;
std::cout << "\033[0;31mNot found for positive angles.\033[0m" << endl;
break;
}
oldSizePositive = currentSize;
......@@ -228,9 +229,7 @@ void detectShape(Ptr<GeneralizedHoughGuil> alg, Mat templateShape, int posThresh
i++;
}
alg -> setPosThresh(posThresh+i);
cout << "\rPositive CurrentSize: " << currentSize << flush;
}
cout << endl;
int oldSizeNegative = 0;
// Reset incremental position value
......@@ -239,6 +238,7 @@ void detectShape(Ptr<GeneralizedHoughGuil> alg, Mat templateShape, int posThresh
// Process shapes with negative angles
alg -> setMinAngle(355);
alg -> setMaxAngle(360);
std::cout << "Processing negative angles..." << endl;
while (true) {
alg -> detect(processingArea, negativePositions, negativeVotes);
int currentSize = negativePositions.size();
......@@ -252,7 +252,7 @@ void detectShape(Ptr<GeneralizedHoughGuil> alg, Mat templateShape, int posThresh
break;
} else if (currentSize == 0 && oldSizeNegative == 0) {
// Impossible to found with these parameters
cout << "Not found." << endl;
std::cout << "\033[0;31mNot found for negative angles.\033[0m" << endl;
break;
}
oldSizeNegative = currentSize;
......@@ -271,25 +271,22 @@ void detectShape(Ptr<GeneralizedHoughGuil> alg, Mat templateShape, int posThresh
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 drawShapes(Mat frame, vector<Vec4f> &positions, Scalar color, int width, int height, int offsetX, int offsetY, float processingScale) {
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);
Point2f pos(positions[i][0]+offsetX, positions[i][1]+offsetY);
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.center = pos * processingScale;
rr.size = Size2f(width * scale * processingScale, height * scale * processingScale);
rr.angle = angle;
rr.points(rrpts);
......@@ -299,6 +296,8 @@ void drawShapes(Mat frame, vector<Vec4f> &positions, Scalar color, Mat templateS
line(frame, rrpts[2], rrpts[3], color, 2);
line(frame, rrpts[3], rrpts[0], color, 2);
}
return rr;
}
// Function to separate even and odd frame half planes
......
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