Commit 08f9912c authored by Ahmed's avatar Ahmed

add LargestContour Command

parent 2df1956f
......@@ -146,7 +146,7 @@ public class LanguageUnitCPP extends LanguageUnit {
if(!bluePrint.cvIncludeStrings.isEmpty()){
resultString += "using namespace cv;\n";
}
}//TODO add here using namespace std;
//class definition start
resultString += "class " + bluePrint.getName();
......
......@@ -67,6 +67,7 @@ public class MathCommandRegisterCPP extends MathCommandRegister {
registerMathCommand(new InRangeCommand());
registerMathCommand(new PutTextCommand());
registerMathCommand(new ThresholdCommand());
registerMathCommand(new LargestContourCommand());
//for fixing some errors
......
package de.monticore.lang.monticar.generator.cpp.commands;
import de.monticore.lang.math._symboltable.expression.MathExpressionSymbol;
import de.monticore.lang.math._symboltable.matrix.MathMatrixAccessSymbol;
import de.monticore.lang.math._symboltable.matrix.MathMatrixNameExpressionSymbol;
import de.monticore.lang.monticar.generator.*;
import de.monticore.lang.monticar.generator.cpp.BluePrintCPP;
import de.monticore.lang.monticar.generator.cpp.MathFunctionFixer;
import de.monticore.lang.monticar.generator.cpp.converter.ExecuteMethodGenerator;
import de.monticore.lang.monticar.generator.cpp.converter.MathConverter;
import de.monticore.lang.monticar.generator.cpp.symbols.MathStringExpression;
import de.se_rwth.commons.logging.Log;
import java.util.ArrayList;
import java.util.List;
/**
* @author Ahmed Diab
*/
public class LargestContourCommand extends MathCommand {
public LargestContourCommand() {
setMathCommandName("largestContour");
}
@Override
public void convert(MathExpressionSymbol mathExpressionSymbol, BluePrint bluePrint) {
String backendName = MathConverter.curBackend.getBackendName();
if (backendName.equals("OctaveBackend")) {
convertUsingOctaveBackend(mathExpressionSymbol, bluePrint);
} else if (backendName.equals("ArmadilloBackend")) {
convertUsingArmadilloBackend(mathExpressionSymbol, bluePrint);
}
}
@Override
public boolean isCVMathCommand(){
return true;
}
public void convertUsingOctaveBackend(MathExpressionSymbol mathExpressionSymbol, BluePrint bluePrint) {
Log.error("No implementation for Octave Backend");
}
public void convertUsingArmadilloBackend(MathExpressionSymbol mathExpressionSymbol, BluePrint bluePrint) {
MathMatrixNameExpressionSymbol mathMatrixNameExpressionSymbol = (MathMatrixNameExpressionSymbol) mathExpressionSymbol;
mathMatrixNameExpressionSymbol.setNameToAccess("");
String valueListString = "";
for (MathMatrixAccessSymbol accessSymbol : mathMatrixNameExpressionSymbol.getMathMatrixAccessOperatorSymbol().getMathMatrixAccessSymbols())
MathFunctionFixer.fixMathFunctions(accessSymbol, (BluePrintCPP) bluePrint);
Method largestContourMethod = getLargestContourMethod();
valueListString += ExecuteMethodGenerator.generateExecuteCode(mathExpressionSymbol, new ArrayList<String>());
List<MathMatrixAccessSymbol> newMatrixAccessSymbols = new ArrayList<>();
MathStringExpression stringExpression = new MathStringExpression("largestContour" + valueListString,mathMatrixNameExpressionSymbol.getMathMatrixAccessOperatorSymbol().getMathMatrixAccessSymbols());
newMatrixAccessSymbols.add(new MathMatrixAccessSymbol(stringExpression));
mathMatrixNameExpressionSymbol.getMathMatrixAccessOperatorSymbol().setMathMatrixAccessSymbols(newMatrixAccessSymbols);
((BluePrintCPP) bluePrint).addCVIncludeString("opencv2/imgproc");
bluePrint.addMethod(largestContourMethod);
}
private Method getLargestContourMethod(){
Method method = new Method("largestContour", "vector<Point>");
//add parameters
Variable contours = new Variable();
method.addParameter(contours, "contours", "double","vector <vector<Point>>", "");
//add an instruction to the method
method.addInstruction(methodBody());
return method;
}
private Instruction methodBody() {
return new Instruction() {
@Override
public String getTargetLanguageInstruction() {
return "double maxArea = 0;\n" +
"int maxAreaContourId = -1;\n" +
" for (int j = 0; j < contours.size(); j++) {\n" +
" double newArea = contourArea(contours.at(j));\n" +
" if (newArea > maxArea) {\n" +
" maxArea = newArea;\n" +
" maxAreaContourId = j;\n" +
" }\n" +
" }\n" +
" return contours.at(getMaxAreaContourId(contours));\n" +
"}\n";
}
@Override
public boolean isConnectInstruction() {
return false;
}
};
}
}
......@@ -274,4 +274,9 @@ public class ArmadilloFunctionTest extends AbstractSymtabTest {
public void testThresholdCommand() throws IOException{
testMathCommand("threshold");
}
@Test
public void testLargestContourCommand() throws IOException{
testMathCommand("largestContour");
}
}
#ifndef TEST_MATH_LARGESTCONTOURCOMMANDTEST
#define TEST_MATH_LARGESTCONTOURCOMMANDTEST
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#include "armadillo"
#include "opencv2/imgproc.hpp"
using namespace arma;
using namespace cv;
class test_math_largestContourCommandTest{
public:
double contours;
double lContour;
void init()
{
}
vector<Point> largestContour(vector <vector<Point>> contours)
{
double maxArea = 0;
int maxAreaContourId = -1;
for (int j = 0; j < contours.size(); j++) {
double newArea = contourArea(contours.at(j));
if (newArea > maxArea) {
maxArea = newArea;
maxAreaContourId = j;
}
}
return contours.at(getMaxAreaContourId(contours));
}
}
void execute()
{
lContour = (largestContour(contours));
}
};
#endif
......@@ -13,7 +13,7 @@ public:
mat image;
int mode;
int method;
mat hierarchy;
mat hierarchy; //Try without hierarchy
mat contours;
void init()
{
......
/* (c) https://github.com/MontiCore/monticore */
package test.math;
component LargestContourCommandTest{
port
in Q contours,
out Q lContour;
implementation Math{
lContour = largestContour(contours);
}
}
Markdown is supported
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