Ask Your Question

ns's profile - activity

2017-01-15 23:02:10 -0600 commented question While running template matching algorithm i'm not able to recover highest matching area in source image

yes but it's showing results in the form of following result matrices . In the first column, the darkest is the better match, for the other two columns, the brighter a location, the higher the match.

what i require is highest match as shown in http://docs.opencv.org/master/de/da9/tutorial_template_matching.html (http://docs.opencv.org/master/de/da9/...) 3rd part of result thanks for help

2017-01-15 11:11:02 -0600 commented question While running template matching algorithm i'm not able to recover highest matching area in source image

berak it means that code is not generating highest / best match for a given template in source image .It just generates result matrices.

2017-01-15 00:27:50 -0600 asked a question While running template matching algorithm i'm not able to recover highest matching area in source image

I've been trying to execute template matching algorithm with code

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;

/// Global Variables
Mat img; Mat templ; Mat result;
char* image_window = "Source Image";
char* result_window = "Result window";

int match_method;
int max_Trackbar = 5;

/// Function Headers
void MatchingMethod( int, void* );

/** @function main */
int main( int argc, char** argv )
{
  /// Load image and template
  img = imread( argv[1], 1 );
  templ = imread( argv[2], 1 );

  /// Create windows
  namedWindow( image_window, CV_WINDOW_AUTOSIZE );
  namedWindow( result_window, CV_WINDOW_AUTOSIZE );

  /// Create Trackbar
  char* trackbar_label = "Method: \n 0: SQDIFF \n 1: SQDIFF NORMED \n 2: TM CCORR \n 3: TM CCORR NORMED \n 4: TM COEFF \n 5: TM COEFF NORMED";
  createTrackbar( trackbar_label, image_window, &match_method, max_Trackbar, MatchingMethod );

  MatchingMethod( 0, 0 );

  waitKey(0);
  return 0;
}

/**
 * @function MatchingMethod
 * @brief Trackbar callback
 */
void MatchingMethod( int, void* )
{
  /// Source image to display
  Mat img_display;
  img.copyTo( img_display );

  /// Create the result matrix
  int result_cols =  img.cols - templ.cols + 1;
  int result_rows = img.rows - templ.rows + 1;

  result.create( result_rows, result_cols, CV_32FC1 );

  /// Do the Matching and Normalize
  matchTemplate( img, templ, result, match_method );
  normalize( result, result, 0, 1, NORM_MINMAX, -1, Mat() );

  /// Localizing the best match with minMaxLoc
  double minVal; double maxVal; Point minLoc; Point maxLoc;
  Point matchLoc;

  minMaxLoc( result, &minVal, &maxVal, &minLoc, &maxLoc, Mat() );

  /// For SQDIFF and SQDIFF_NORMED, the best matches are lower values. For all the other methods, the higher the better
  if( match_method  == CV_TM_SQDIFF || match_method == CV_TM_SQDIFF_NORMED )
    { matchLoc = minLoc; }
  else
    { matchLoc = maxLoc; }

  /// Show me what you got
  rectangle( img_display, matchLoc, Point( matchLoc.x + templ.cols , matchLoc.y + templ.rows ), Scalar::all(0), 2, 8, 0 );
  rectangle( result, matchLoc, Point( matchLoc.x + templ.cols , matchLoc.y + templ.rows ), Scalar::all(0), 2, 8, 0 );

  imshow( image_window, img_display );
  imshow( result_window, result );

  return;
}

. I've been only able to generate the result matrices. After this code should provide highest matching portion in source image but code doesn't generate it .Please help...

2016-11-12 10:32:10 -0600 asked a question I've been trying to run principal component analysis code in opencv 3.1 with vs2015. I've already linked both softwares but output of algo is incorrect

code: written in c++

following error is shown during ouput

OpenCV Error: Assertion failed ((unsigned)(i1 * DataType<_Tp>::channels) < (unsigned)(size.p[1] * channels())) in cv::Mat::at, file c:\users\administrator\documents\opencv\build\include\opencv2\core\mat.inl.hpp, line 918

please help me out

 #include <iostream>
    #include <opencv2/opencv.hpp>
    using namespace std;
    using namespace cv;
    // Function declarations
    void drawAxis(Mat&, Point, Point, Scalar, const float);
    double getOrientation(const vector<Point> &, Mat&);
    void drawAxis(Mat& img, Point p, Point q, Scalar colour, const float scale = 0.2)
    {
        double angle;
        double hypotenuse;
        angle = atan2((double)p.y - q.y, (double)p.x - q.x); // angle in radians
        hypotenuse = sqrt((double)(p.y - q.y) * (p.y - q.y) + (p.x - q.x) * (p.x - q.x));
        //    double degrees = angle * 180 / CV_PI; // convert radians to degrees (0-180 range)
        //    cout << "Degrees: " << abs(degrees - 180) << endl; // angle in 0-360 degrees range
        // Here we lengthen the arrow by a factor of scale
        q.x = (int)(p.x - scale * hypotenuse * cos(angle));
        q.y = (int)(p.y - scale * hypotenuse * sin(angle));
        line(img, p, q, colour, 1, CV_AA);
        // create the arrow hooks
        p.x = (int)(q.x + 9 * cos(angle + CV_PI / 4));
        p.y = (int)(q.y + 9 * sin(angle + CV_PI / 4));
        line(img, p, q, colour, 1, CV_AA);
        p.x = (int)(q.x + 9 * cos(angle - CV_PI / 4));
        p.y = (int)(q.y + 9 * sin(angle - CV_PI / 4));
        line(img, p, q, colour, 1, CV_AA);
    }
    double getOrientation(const vector<Point> &pts, Mat &img)
    {
        //Construct a buffer used by the pca analysis
        int sz = static_cast<int>(pts.size());
        Mat data_pts = Mat(sz, 2, CV_64FC1);
        for (int i = 0; i < data_pts.rows; ++i)
        {
            data_pts.at<double>(i, 0) = pts[i].x;
            data_pts.at<double>(i, 1) = pts[i].y;
        }
        //Perform PCA analysis
        PCA pca_analysis(data_pts, Mat(), CV_PCA_DATA_AS_ROW);
        //Store the center of the object
        Point cntr = Point(static_cast<int>(pca_analysis.mean.at<double>(0, 0)),
            static_cast<int>(pca_analysis.mean.at<double>(0, 1)));
        //Store the eigenvalues and eigenvectors
        vector<Point2d> eigen_vecs(2);
        vector<double> eigen_val(2);
        for (int i = 0; i < 2; ++i)
        {
            eigen_vecs[i] = Point2d(pca_analysis.eigenvectors.at<double>(i, 0),
                pca_analysis.eigenvectors.at<double>(i, 1));
            eigen_val[i] = pca_analysis.eigenvalues.at<double>(0, i);
        }
        // Draw the principal components
        circle(img, cntr, 3, Scalar(255, 0, 255), 2);
        Point p1 = cntr + 0.02 * Point(static_cast<int>(eigen_vecs[0].x * eigen_val[0]), static_cast<int>(eigen_vecs[0].y * eigen_val[0]));
        Point p2 = cntr - 0.02 * Point(static_cast<int>(eigen_vecs[1].x * eigen_val[1]), static_cast<int>(eigen_vecs[1].y * eigen_val[1]));
        drawAxis(img, cntr, p1, Scalar(0, 255, 0), 1);
        drawAxis(img, cntr, p2, Scalar(255, 255, 0), 5);
        double angle = atan2(eigen_vecs[0].y, eigen_vecs[0].x); // orientation in radians
        return angle;
    }
    int main(int, char** argv)
    {
        // Load image
        //    Mat src = imread("pca_test1.jpg");
        Mat src = imread(argv[1]);
        // Check if image is loaded successfully
        if (!src.data || src.empty())
        {
            cout << "Problem loading image!!!" << endl;
            return EXIT_FAILURE;
        }
        imshow("src", src);
        // Convert image to grayscale
        Mat gray ...
(more)