Cleaned up code for haris corner/edge detector, added cross product (3d only)

This commit is contained in:
novak_99 2021-12-17 23:39:14 -08:00
parent 52c4a8c682
commit b7f7e10b73
6 changed files with 45 additions and 20 deletions

View File

@ -321,9 +321,7 @@ namespace MLPP{
return deriv;
}
std::vector<std::vector<std::string>> Convolutions::harrisCornerDetection(std::vector<std::vector<double>> input){
double const k = 0.05; // Empirically determined wherein k -> [0.04, 0.06], though conventionally 0.05 is typically used as well.
std::vector<std::vector<std::vector<double>>> Convolutions::computeM(std::vector<std::vector<double>> input){
double const SIGMA = 1;
double const GAUSSIAN_SIZE = 3;
@ -338,8 +336,15 @@ namespace MLPP{
std::vector<std::vector<double>> yyDeriv = convolve(alg.hadamard_product(yDeriv, yDeriv), gaussianFilter, 1, GAUSSIAN_PADDING);
std::vector<std::vector<double>> xyDeriv = convolve(alg.hadamard_product(xDeriv, yDeriv), gaussianFilter, 1, GAUSSIAN_PADDING);
std::vector<std::vector<double>> det = alg.subtraction(alg.hadamard_product(xxDeriv, yyDeriv), alg.hadamard_product(xyDeriv, xyDeriv));
std::vector<std::vector<double>> trace = alg.addition(xxDeriv, yyDeriv);
std::vector<std::vector<std::vector<double>>> M = {xxDeriv, yyDeriv, xyDeriv};
return M;
}
std::vector<std::vector<std::string>> Convolutions::harrisCornerDetection(std::vector<std::vector<double>> input){
double const k = 0.05; // Empirically determined wherein k -> [0.04, 0.06], though conventionally 0.05 is typically used as well.
LinAlg alg;
std::vector<std::vector<std::vector<double>>> M = computeM(input);
std::vector<std::vector<double>> det = alg.subtraction(alg.hadamard_product(M[0], M[1]), alg.hadamard_product(M[2], M[2]));
std::vector<std::vector<double>> trace = alg.addition(M[0], M[1]);
// The reason this is not a scalar is because xxDeriv, xyDeriv, yxDeriv, and yyDeriv are not scalars.
std::vector<std::vector<double>> r = alg.subtraction(det, alg.scalarMultiply(k, alg.hadamard_product(trace, trace)));

View File

@ -23,6 +23,7 @@ namespace MLPP{
std::vector<std::vector<double>> gradMagnitude(std::vector<std::vector<double>> input);
std::vector<std::vector<double>> gradOrientation(std::vector<std::vector<double>> input);
std::vector<std::vector<std::vector<double>>> computeM(std::vector<std::vector<double>> input);
std::vector<std::vector<std::string>> harrisCornerDetection(std::vector<std::vector<double>> input);
std::vector<std::vector<double>> getPrewittHorizontal();

View File

@ -871,6 +871,17 @@ namespace MLPP{
return c;
}
std::vector<double> LinAlg::cross(std::vector<double> a, std::vector<double> b){
// Cross products exist in R^7 also. Though, I will limit it to R^3 as Wolfram does this.
std::vector<std::vector<double>> mat = {onevec(3), a, b};
double det1 = det({{a[1], a[2]}, {b[1], b[2]}}, 2);
double det2 = -det({{a[0], a[2]}, {b[0], b[2]}}, 2);
double det3 = det({{a[0], a[1]}, {b[0], b[1]}}, 2);
return {det1, det2, det3};
}
std::vector<double> LinAlg::abs(std::vector<double> a){
std::vector<double> b;
b.resize(a.size());

View File

@ -146,6 +146,8 @@ namespace MLPP{
double dot(std::vector<double> a, std::vector<double> b);
std::vector<double> cross(std::vector<double> a, std::vector<double> b);
std::vector<double> abs(std::vector<double> a);
std::vector<double> zerovec(int n);

BIN
a.out

Binary file not shown.

View File

@ -216,7 +216,7 @@ int main() {
// LinReg model(alg.transpose(inputSet), outputSet); // Can use Lasso, Ridge, ElasticNet Reg
// model.gradientDescent(0.001, 30000, 1);
// model.gradientDescent(0.001, 30000, 0);
// model.SGD(0.001, 30000, 1);
// model.MBGD(0.001, 10000, 2, 1);
// model.normalEquation();
@ -612,22 +612,28 @@ int main() {
// alg.printMatrix(conv.gradOrientation(A));
std::vector<std::vector<double>> A =
{
{1,0,0,0},
{0,0,0,0},
{0,0,0,0},
{0,0,0,1}
};
// std::vector<std::vector<double>> A =
// {
// {1,0,0,0},
// {0,0,0,0},
// {0,0,0,0},
// {0,0,0,1}
// };
// std::vector<std::vector<std::string>> h = conv.harrisCornerDetection(A);
// for(int i = 0; i < h.size(); i++){
// for(int j = 0; j < h[i].size(); j++){
// std::cout << h[i][j] << " ";
// }
// std::cout << std::endl;
// } // Harris detector works. Life is good!
std::vector<double> a = {3,4,4};
std::vector<double> b= {4,4,4};
alg.printVector(alg.cross(a,b));
std::vector<std::vector<std::string>> h = conv.harrisCornerDetection(A);
for(int i = 0; i < h.size(); i++){
for(int j = 0; j < h[i].size(); j++){
std::cout << h[i][j] << " ";
}
std::cout << std::endl;
} // Harris detector works. Life is good!
return 0;