Commit 9ab6497a authored by Ahmed's avatar Ahmed

some fixes

parent ce8429ec
......@@ -6,8 +6,7 @@ package de.monticore.lang.monticar.generator.cpp;
*/
public class ConversionHelperSource {
public static String conversionHelperSourceCode = "// convert an OpenCV matrix to Armadillo matrix. NOTE: a copy is made\n" +
"#ifndef CONVHELPER_H\n" +
public static String conversionHelperSourceCode = "#ifndef CONVHELPER_H\n" +
"#define CONVHELPER_H\n" +
"#include <iostream>\n" +
"#include \"armadillo\"\n" +
......
......@@ -55,7 +55,7 @@ public class BoundingRectCommand extends MathCommand{
MathFunctionFixer.fixMathFunctions(accessSymbol, bluePrintCPP);
String nameOfFirstParameter = mathMatrixNameExpressionSymbol.getMathMatrixAccessOperatorSymbol().getMathMatrixAccessSymbols().get(0).getTextualRepresentation();
ComponentConverter.fixVariableType(nameOfFirstParameter, bluePrintCPP, "Q", "vector<cv::Point>", "");
ComponentConverter.fixVariableType(nameOfFirstParameter, bluePrintCPP, "Q", "std::vector<cv::Point>", "");
valueListString += ExecuteMethodGenerator.generateExecuteCode(mathExpressionSymbol, new ArrayList<String>());
List<MathMatrixAccessSymbol> newMatrixAccessSymbols = new ArrayList<>();
......
......@@ -121,7 +121,7 @@ public class CvtColorCommand extends ArgumentNoReturnMathCommand{
}else if(properties.isPreCV()){
finalInstruction += " cv::Mat dstCV;\n" +
" cv::cvtColor(src, dstCV, colorConversion);\n";
if(typeNameOut == "cube"){
if(typeNameOut == "Cube<unsigned char>"){
finalInstruction += " dst = to_armaCube<unsigned char, 3>(dstCV);\n";
} else {
finalInstruction += " dst = to_arma<unsigned char>(dstCV);\n";
......@@ -135,7 +135,7 @@ public class CvtColorCommand extends ArgumentNoReturnMathCommand{
" cv::Mat dstCV;\n" +
" srcCV = to_cvmat<unsigned char>(src);\n" +
" cv::cvtColor(srcCV, dstCV, colorConversion);\n";
if(typeNameOut == "cube"){
if(typeNameOut == "Cube<unsigned char>"){
finalInstruction += " dst = to_armaCube<unsigned char, 3>(dstCV);\n";
} else {
finalInstruction += " dst = to_arma<unsigned char>(dstCV);\n";
......
......@@ -133,7 +133,7 @@ public class DilateCommand extends ArgumentNoReturnMathCommand{
}else if(properties.isPreCV()){
finalInstruction += " cv::Mat dstCV;\n" +
" cv::dilate( src, dstCV, cv::element, cv::Point(-1,-1), iterations );\n";
if(typeNameOut == "cube"){
if(typeNameOut == "Cube<unsigned char>"){
finalInstruction += " dst = to_armaCube<unsigned char, 3>(dstCV);\n";
} else {
finalInstruction += " dst = to_arma<unsigned char>(dstCV);\n";
......@@ -147,7 +147,7 @@ public class DilateCommand extends ArgumentNoReturnMathCommand{
" cv::Mat dstCV;\n" +
" srcCV = to_cvmat<unsigned char>(src);\n" +
" cv::dilate( srcCV, dstCV, element, cv::Point(-1,-1), iterations );\n";
if(typeNameOut == "cube"){
if(typeNameOut == "Cube<unsigned char>"){
finalInstruction += " dst = to_armaCube<unsigned char, 3>(dstCV);\n";
} else {
finalInstruction += " dst = to_arma<unsigned char>(dstCV);\n";
......
......@@ -94,7 +94,7 @@ public class FindContoursCommand extends ArgumentNoReturnMathCommand{
Variable image = new Variable();
method.addParameter(image, "image", "CommonMatrixType", typeNameInConst, MathConverter.curBackend.getIncludeHeaderName());
Variable contours = new Variable();
method.addParameter(contours, "contours", "CommonMatrixType", "vector<vector<cv::Point>>&", MathConverter.curBackend.getIncludeHeaderName());
method.addParameter(contours, "contours", "CommonMatrixType", "std::vector<std::vector<cv::Point>>&", MathConverter.curBackend.getIncludeHeaderName());
Variable mode = new Variable();
method.addParameter(mode,"mode", "Integer", "int", "");
Variable meth = new Variable();
......
......@@ -129,7 +129,7 @@ public class GaussianBlurCommand extends ArgumentNoReturnMathCommand{
finalInstruction = " cv::Mat dstCV;\n" +
" cv::Size sizeO = cv::Size(sizeX, sizeY);\n" +
" cv::GaussianBlur(src, dstCV, sizeO, sigmaX, sigmaY);\n";
if(typeNameOut == "cube"){
if(typeNameOut == "Cube<unsigned char>"){
finalInstruction += " dst = to_armaCube<unsigned char, 3>(dstCV);\n";
} else {
finalInstruction += " dst = to_arma<unsigned char>(dstCV);\n";
......@@ -145,7 +145,7 @@ public class GaussianBlurCommand extends ArgumentNoReturnMathCommand{
" srcCV = to_cvmat<unsigned char>(src);\n" +
" cv::Size sizeO = cv::Size(sizeX, sizeY);\n" +
" cv::GaussianBlur(srcCV, dstCV, sizeO, sigmaX, sigmaY);\n";
if(typeNameOut == "cube"){
if(typeNameOut == "Cube<unsigned char>"){
finalInstruction += " dst = to_armaCube<unsigned char, 3>(dstCV);\n";
} else {
finalInstruction += " dst = to_arma<unsigned char>(dstCV);\n";
......
......@@ -52,7 +52,7 @@ public class LargestContourCommand extends MathCommand {
MathFunctionFixer.fixMathFunctions(accessSymbol, bluePrintCPP);
String nameOfFirstParameter = mathMatrixNameExpressionSymbol.getMathMatrixAccessOperatorSymbol().getMathMatrixAccessSymbols().get(0).getTextualRepresentation();
ComponentConverter.fixVariableType(nameOfFirstParameter, bluePrintCPP, "Q", "vector<vector<cv::Point>>", "");
ComponentConverter.fixVariableType(nameOfFirstParameter, bluePrintCPP, "Q", "std::vector<std::vector<cv::Point>>", "");
Method largestContourMethod = getLargestContourMethod();
valueListString += ExecuteMethodGenerator.generateExecuteCode(mathExpressionSymbol, new ArrayList<String>());
List<MathMatrixAccessSymbol> newMatrixAccessSymbols = new ArrayList<>();
......@@ -73,7 +73,7 @@ public class LargestContourCommand extends MathCommand {
//add parameters
Variable contours = new Variable();
method.addParameter(contours, "contours", "double","const std::vector <std::vector<cv::Point>>&", "");
method.addParameter(contours, "contours", "double","const std::vector<std::vector<cv::Point>>&", "");
//add an instruction to the method
method.addInstruction(methodBody());
......@@ -93,7 +93,7 @@ public class LargestContourCommand extends MathCommand {
" maxAreaContourId = j;\n" +
" }\n" +
" }\n" +
" return contours.at(maxAreaContourId);";
" return contours.at(maxAreaContourId);\n";
}
@Override
......
......@@ -130,7 +130,7 @@ public class RectangleCommand extends MathCommand{
" return src;\n";
} else if(properties.isPreCV()){
finalInstruction = " cv::rectangle(src, rect.tl(), rect.br(), cv::Scalar(color(0), color(1), color(2)), thickness, lineType);\n";
if(typeNameOut == "cube"){
if(typeNameOut == "Cube<unsigned char>"){
finalInstruction += " arma::cube srcCube;\n" +
" srcCube = to_armaCube<unsigned char, 3>(src);\n" +
" return srcCube;\n";
......@@ -148,7 +148,7 @@ public class RectangleCommand extends MathCommand{
finalInstruction = " cv::Mat srcCV;\n" +
" srcCV = to_cvmat<unsigned char>(src);\n" +
" cv::rectangle(srcCV, rect.tl(), rect.br(), cv::Scalar(color(0), color(1), color(2)), thickness, lineType);\n";
if(typeNameOut == "cube"){
if(typeNameOut == "Cube<unsigned char>"){
finalInstruction += " arma::cube srcCube;\n" +
" srcCube = to_armaCube<unsigned char, 3>(srcCV);\n" +
" return srcCube;\n";
......
#ifndef CONVHELPER_H
#define CONVHELPER_H
#include <iostream>
#include "armadillo"
#include <stdarg.h>
#include <initializer_list>
#include <fstream>
using namespace arma;
// convert an OpenCV matrix to Armadillo matrix. NOTE: a copy is made
template <typename T>
arma::Mat<T> to_arma(const cv::Mat_<T>& src) {
arma::Mat<T> dst(reinterpret_cast<double*>(src.data), src.cols, src.rows);
arma::Mat<T> dst(reinterpret_cast<T*>(src.data), src.cols, src.rows);
//src.copyTo({ src.rows, src.cols, dst.memptr() });
return dst;
}
......@@ -26,7 +36,7 @@ Cube<T> to_armaCube(const cv::Mat_<cv::Vec<T, NC>>& src)
// convert an Armadillo cube to OpenCV matrix. NOTE: a copy is made
template <typename T>
cv::Mat to_cvmat(Cube<T>& src) {
cv::Mat to_cvmat(const Cube<T>& src) {
std::vector<cv::Mat_<T>> channels;
for (size_t c = 0; c < src.n_slices; ++c) {
auto* data = const_cast<T*>(src.slice(c).memptr());
......@@ -35,4 +45,7 @@ cv::Mat to_cvmat(Cube<T>& src) {
cv::Mat dst;
cv::merge(channels, dst);
return dst;
}
\ No newline at end of file
}
#endif
\ No newline at end of file
......@@ -9,14 +9,14 @@ using namespace arma;
using namespace std;
class test_math_boundingRectCommandTest{
public:
vector<cv::Point> contour;
double rect;
std::vector<cv::Point> contour;
cv::Rect rect;
void init()
{
}
void execute()
{
rectAngle = (cv::boundingRect(contour));
rect = (cv::boundingRect(contour));
}
};
......
......@@ -10,21 +10,21 @@ using namespace arma;
using namespace std;
class test_math_cvtColorCommandTest{
public:
arma::mat src;
arma::Mat<unsigned char> src;
int colorConversion;
arma::mat dst;
arma::Mat<unsigned char> dst;
void init()
{
src=mat(n,m);
dst=mat(n,m);
src=Mat<unsigned char>(n,m);
dst=Mat<unsigned char>(n,m);
}
void cvtColorHelper(arma::mat src, arma::mat dst, int colorConversion)
void cvtColorHelper(const arma::Mat<unsigned char>& src, arma::Mat<unsigned char>& dst, int colorConversion)
{
cv::Mat srcCV;
cv::Mat dstCV;
srcCV = ConvHelper::to_cvmat(src);
srcCV = to_cvmat<unsigned char>(src);
cv::cvtColor(srcCV, dstCV, colorConversion);
dst = ConvHelper::to_arma(dstCV);
dst = to_arma<unsigned char>(dstCV);
}
void execute()
{
......
......@@ -10,30 +10,30 @@ using namespace arma;
using namespace std;
class test_math_dilateCommandTest{
public:
arma::mat src;
arma::Mat<unsigned char> src;
int dilation_elem;
int iterations;
arma::mat dst;
arma::Mat<unsigned char> dst;
void init()
{
src=mat(n,m);
dst=mat(n,m);
src=Mat<unsigned char>(n,m);
dst=Mat<unsigned char>(n,m);
}
void dilateHelper(arma::mat src, arma::mat dst, int dilation_elem, int iterations)
void dilateHelper(const arma::Mat<unsigned char>& src, arma::Mat<unsigned char>& dst, int dilation_elem, int iterations)
{
int dilation_type = 0;
if( dilation_elem == 0 ){ dilation_type = MORPH_RECT; }
else if( dilation_elem == 1 ){ dilation_type = MORPH_CROSS; }
else if( dilation_elem == 2) { dilation_type = MORPH_ELLIPSE; }
dilation_size = dilation_elem;
cv::mat element = cv::getStructuringElement( dilation_type,
Size( 2*dilation_size + 1, 2*dilation_size+1 ),
Point( -1, -1 ) );
if( dilation_elem == 0 ){ dilation_type = cv::MORPH_RECT; }
else if( dilation_elem == 1 ){ dilation_type = cv::MORPH_CROSS; }
else if( dilation_elem == 2) { dilation_type = cv::MORPH_ELLIPSE; }
int dilation_size = dilation_elem;
cv::Mat element = cv::getStructuringElement( dilation_type,
cv::Size( 2*dilation_size + 1, 2*dilation_size+1 ),
cv::Point( -1, -1 ) );
cv::Mat srcCV;
cv::Mat dstCV;
srcCV = ConvHelper::to_cvmat(src);
cv::dilate( srcCV, dstCV, element, Point(-1,-1), iterations );
dst = ConvHelper::to_arma(dstCV);
srcCV = to_cvmat<unsigned char>(src);
cv::dilate( srcCV, dstCV, element, cv::Point(-1,-1), iterations );
dst = to_arma<unsigned char>(dstCV);
}
void execute()
{
......
......@@ -11,13 +11,13 @@ using namespace arma;
using namespace std;
class test_math_erodeAndGaussianBlurCommandTest{
public:
cube src;
Cube<unsigned char> src;
int erosion_elemIn;
colvec sizeY;
colvec two;
cube dst;
cube outMatrix;
arma::mat out2Matrix;
Cube<unsigned char> dst;
Cube<unsigned char> outMatrix;
arma::Mat<unsigned char> out2Matrix;
void init()
{
src = cube(n, m, 3);
......@@ -25,35 +25,35 @@ sizeY=colvec(3);
two=colvec(2);
dst = cube(n, m, 3);
outMatrix = cube(3, n, m);
out2Matrix=mat(2,m);
out2Matrix=Mat<unsigned char>(2,m);
}
void erodeHelper(cube src, cv::Mat dst, int erosion_elem, int iterations)
void erodeHelper(const Cube<unsigned char>& src, cv::Mat& dst, int erosion_elem, int iterations)
{
int erosion_type = 0;
if( erosion_elem == 0 ){ erosion_type = MORPH_RECT; }
else if( erosion_elem == 1 ){ erosion_type = MORPH_CROSS; }
else if( erosion_elem == 2) { erosion_type = MORPH_ELLIPSE; }
erosion_size = erosion_elem;
if( erosion_elem == 0 ){ erosion_type = cv::MORPH_RECT; }
else if( erosion_elem == 1 ){ erosion_type = cv::MORPH_CROSS; }
else if( erosion_elem == 2) { erosion_type = cv::MORPH_ELLIPSE; }
int erosion_size = erosion_elem;
cv::Mat element = cv::getStructuringElement( erosion_type,
Size( 2*erosion_size + 1, 2*erosion_size+1 ),
Point( -1, -1 ) );
cv::Size( 2*erosion_size + 1, 2*erosion_size+1 ),
cv::Point( -1, -1 ) );
cv::Mat srcCV;
srcCV = ConvHelper::to_cvmat(src);
cv::erode( srcCV, dst, element, Point(-1,-1), iterations );
srcCV = to_cvmat<unsigned char>(src);
cv::erode( srcCV, dst, element, cv::Point(-1,-1), iterations );
}
void dilateHelper(cv::Mat src, cv::Mat dst, int dilation_elem, int iterations)
void dilateHelper(const cv::Mat& src, cv::Mat& dst, int dilation_elem, int iterations)
{
int dilation_type = 0;
if( dilation_elem == 0 ){ dilation_type = MORPH_RECT; }
else if( dilation_elem == 1 ){ dilation_type = MORPH_CROSS; }
else if( dilation_elem == 2) { dilation_type = MORPH_ELLIPSE; }
dilation_size = dilation_elem;
cv::mat element = cv::getStructuringElement( dilation_type,
Size( 2*dilation_size + 1, 2*dilation_size+1 ),
Point( -1, -1 ) );
cv::dilate( src, dst, element, Point(-1,-1), iterations );
if( dilation_elem == 0 ){ dilation_type = cv::MORPH_RECT; }
else if( dilation_elem == 1 ){ dilation_type = cv::MORPH_CROSS; }
else if( dilation_elem == 2) { dilation_type = cv::MORPH_ELLIPSE; }
int dilation_size = dilation_elem;
cv::Mat element = cv::getStructuringElement( dilation_type,
cv::Size( 2*dilation_size + 1, 2*dilation_size+1 ),
cv::Point( -1, -1 ) );
cv::dilate( src, dst, element, cv::Point(-1,-1), iterations );
}
void findContoursHelper(cv::Mat image, vector<vector<cv::Point>> contours, int mode, int method)
void findContoursHelper(const cv::Mat& image, std::vector<std::vector<cv::Point>>& contours, int mode, int method)
{
cv::findContours( image, contours, mode, method );
}
......@@ -68,7 +68,7 @@ cube src4 = (det(src));
erodeHelper(src4, dst, erosion_elem, iterations);
erodeHelper(src, dst, erosion_elem, iterations);
dilateHelper(dst, dst3, dilation_elem, iterations);
cv::findContours(dst3, contours, method, mode);
findContoursHelper(dst3, contours, method, mode);
}
};
......
......@@ -11,24 +11,24 @@ using namespace arma;
using namespace std;
class test_math_findContoursCommandTest{
public:
arma::mat image;
arma::Mat<unsigned char> image;
int mode;
int method;
vector<vector<cv::Point>> contours;
std::vector<std::vector<cv::Point>> contours;
void init()
{
image=mat(n,m);
image=Mat<unsigned char>(n,m);
}
void findContoursHelper(arma::mat image, vector<vector<cv::Point>> contours, int mode, int method)
void findContoursHelper(const arma::Mat<unsigned char>& image, std::vector<std::vector<cv::Point>>& contours, int mode, int method)
{
cv::Mat srcCV;
srcCV = ConvHelper::to_cvmat(src);
cv::findContours( image, contours, mode, method );
srcCV = to_cvmat<unsigned char>(image);
cv::findContours( srcCV, contours, mode, method );
}
void execute()
{
cv::findContours(image, contours, mode, method);
findContoursHelper(image, contours, mode, method);
}
};
#endif
\ No newline at end of file
#endif
......@@ -10,24 +10,25 @@ using namespace arma;
using namespace std;
class test_math_gaussianBlurCommandTest{
public:
cube src;
Cube<unsigned char> src;
int sizeX;
int sizeY;
double sigmaX;
double sigmaY;
cube dst;
Cube<unsigned char> dst;
void init()
{
src = cube(3, n, m);
dst = cube(3, n, m);
src = cube(n, m, 3);
dst = cube(n, m, 3);
}
void gaussianBlurHelper(cube src, cube dst, int sizeX, int sizeY, double sigmaX, double sigmaY)
void gaussianBlurHelper(const Cube<unsigned char>& src, Cube<unsigned char>& dst, int sizeX, int sizeY, double sigmaX, double sigmaY)
{
cv::Mat srcCV;
cv::Mat dstCV;
srcCV = ConvHelper::to_cvmat(src);
cv::gaussianBlur(srcCV, dstCV, Size(sizeX, sizeY), sigmaX, sigmaY);
dst = ConvHelper::to_armaCube(dstCV);
srcCV = to_cvmat<unsigned char>(src);
cv::Size sizeO = cv::Size(sizeX, sizeY);
cv::GaussianBlur(srcCV, dstCV, sizeO, sigmaX, sigmaY);
dst = to_armaCube<unsigned char, 3>(dstCV);
}
void execute()
{
......
......@@ -10,25 +10,25 @@ using namespace arma;
using namespace std;
class test_math_inRangeCommandTest{
public:
arma::mat src;
arma::Mat<unsigned char> src;
colvec lowerBoundary;
colvec upperBoundary;
arma::mat dst;
arma::Mat<unsigned char> dst;
void init()
{
src=mat(n,m);
src=Mat<unsigned char>(n,m);
lowerBoundary=colvec(3);
upperBoundary=colvec(3);
dst=mat(n,m);
dst=Mat<unsigned char>(n,m);
}
void inRangeHelper(cube src, arma::mat dst, colvec lowerB, colvec upperB)
void inRangeHelper(const Cube<unsigned char>& src, arma::Mat<unsigned char>& dst, colvec lowerB, colvec upperB)
{
cv::Mat srcCV;
cv::Mat dstCV;
srcCV = ConvHelper::to_cvmat(src);
srcCV = to_cvmat<unsigned char>(src);
cv::inRange(srcCV, cv::Scalar(lowerB(0), lowerB(1), lowerB(2)),
cv::Scalar(upperB(0), upperB(1), upperB(2)), dstCV);
dst = ConvHelper::to_arma(dstCV);
dst = to_arma<unsigned char>(dstCV);
}
void execute()
{
......@@ -36,4 +36,4 @@ inRangeHelper(src, dst, lowerBoundary, upperBoundary);
}
};
#endif
#endif
\ No newline at end of file
......@@ -9,12 +9,12 @@ using namespace arma;
using namespace std;
class test_math_largestContourCommandTest{
public:
vector<vector<cv::Point>> contours;
vector<cv::Point> lContour;
std::vector<std::vector<cv::Point>> contours;
std::vector<cv::Point> lContour;
void init()
{
}
vector<Point> largestContour(vector <vector<Point>> contours)
std::vector<cv::Point> largestContour(const std::vector<std::vector<cv::Point>>& contours)
{
double maxArea = 0;
int maxAreaContourId = -1;
......@@ -25,8 +25,7 @@ int maxAreaContourId = -1;
maxAreaContourId = j;
}
}
return contours.at(getMaxAreaContourId(contours));
}
return contours.at(maxAreaContourId);
}
void execute()
{
......
......@@ -10,25 +10,25 @@ using namespace arma;
using namespace std;
class test_math_rectangleCommandTest{
public:
cube src;
Cube<unsigned char> src;
Rect rect;
colvec color;
int thickness;
int lineType;
cube outputImg;
Cube<unsigned char> outputImg;
void init()
{
src = cube(3, n, m);
color=colvec(3);
outputImg = cube(3, n, m);
}
cube rectangleHelper(cube src, cv::Rect rect, colvec color, int thickness, int lineType)
Cube<unsigned char> rectangleHelper(const Cube<unsigned char>& src, cv::Rect& rect, colvec color, int thickness, int lineType)
{
cv::Mat srcCV;
srcCV = ConvHelper::to_cvmat(src);
cv::rectangle(srcCV, rect.tl(), rect.br(), Scalar(color(0), color(1), color(2)), thickness, lineType);
srcCV = to_cvmat<unsigned char>(src);
cv::rectangle(srcCV, rect.tl(), rect.br(), cv::Scalar(color(0), color(1), color(2)), thickness, lineType);
arma::cube srcCube;
srcCube = to_armaCube(srcCV);
srcCube = to_armaCube<unsigned char, 3>(srcCV);
return srcCube;
}
void execute()
......@@ -37,5 +37,4 @@ outputImg = (rectangleHelper(src, rect, color, thickness, lineType));
}
};
#endif
#endif
\ No newline at end of file
......@@ -7,6 +7,6 @@ component BoundingRectCommandTest{
out Q rect;
implementation Math {
rectAngle = boundingRect(contour);
rect = boundingRect(contour);
}
}
......@@ -3,12 +3,12 @@ package test.math;
component GaussianBlurCommandTest {
port
in Q^{3,n,m} src,
in Q^{n,m,3} src,
in Z sizeX,
in Z sizeY,
in Q sigmaX,
in Q sigmaY,
out Q^{3,n,m} dst;
out Q^{n,m,3} dst;
implementation Math {
dst = gaussianBlur(src, sizeX, sizeY, sigmaX, sigmaY);
......
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