Skip to content

Instantly share code, notes, and snippets.

@Lait-au-Cafe
Created April 15, 2019 02:33
Show Gist options
  • Save Lait-au-Cafe/c494ae315fa8156fe692c97ed7f673f5 to your computer and use it in GitHub Desktop.
Save Lait-au-Cafe/c494ae315fa8156fe692c97ed7f673f5 to your computer and use it in GitHub Desktop.
Solve linear equation by OpenCV.
#include <iostream>
#include <opencv2/opencv.hpp>
int main() {
// solve the equations below.
// x+y+z=6
// x-y-z=-4
// x+y+2z=9
cv::Mat1f coef_mat(3, 4);
cv::Mat1f answer(3, 1);
coef_mat(0, 0) = 1;
coef_mat(0, 1) = 1;
coef_mat(0, 2) = 1;
coef_mat(0, 3) = 6;
coef_mat(1, 0) = 1;
coef_mat(1, 1) = -1;
coef_mat(1, 2) = -1;
coef_mat(1, 3) = -4;
coef_mat(2, 0) = 1;
coef_mat(2, 1) = 1;
coef_mat(2, 2) = 2;
coef_mat(2, 3) = 9;
cv::solve(
cv::Mat(coef_mat, cv::Rect(0, 0, 3, 3)),
cv::Mat(coef_mat, cv::Rect(3, 0, 1, 3)),
answer);
std::cout << answer << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment