c++ - Trackbar 结果没有出现 C++/Opencv

标签 c++ opencv imshow trackbar

我从 openCV documentation site 复制了 Harris 角点检测器的代码,但我想让轨迹栏及其输出出现在同一个窗口中,所以我将代码更新为

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

using namespace cv;
using namespace std;

/// Global variables
Mat src, src_gray;
int thresh = 100;
int max_thresh = 255;

char* corners_window = "Corners detected";


void cornerHarris_demo( int, void* )
{

    Mat dst, dst_norm, dst_norm_scaled;
    dst = Mat::zeros( src.size(), CV_32FC1 );

    /// Detector parameters
    int blockSize = 2;
    int apertureSize = 3;
    double k = 0.04;

    /// Detecting corners
    cornerHarris( src_gray, dst, blockSize, apertureSize, k, BORDER_DEFAULT );

    /// Normalizing
    normalize( dst, dst_norm, 0, 255, NORM_MINMAX, CV_32FC1, Mat() );
    convertScaleAbs( dst_norm, dst_norm_scaled );

    /// Drawing a circle around corners
    for( int j = 0; j < dst_norm.rows ; j++ )
    { for( int i = 0; i < dst_norm.cols; i++ )
    {
        if( (int) dst_norm.at<float>(j,i) > thresh )
        {
            circle( src, Point( i, j ), 5, Scalar(255,0,0), 2, 8, 0 );
        }
    }
    }
    /// Showing the result
    imshow( corners_window, src);
}
/** @function main */
int main( int argc, char** argv )
{
    /// Load source image and convert it to gray
    char* filename = "myimage.jpg";
    src_gray = imread( filename, 0);
    cvtColor(src_gray,src,CV_GRAY2RGB);

    /// Create a window and a trackbar
    namedWindow( corners_window, CV_WINDOW_AUTOSIZE);
    createTrackbar( "Threshold: ", corners_window, &thresh, max_thresh, cornerHarris_demo );
    cornerHarris_demo( 0, 0 );

    waitKey(0);
    return(0);
}

问题是输出窗口处于非事件状态,我无法打开它并使用轨迹栏进行操作,但我可以从任务栏中看到它。 enter image description here

最佳答案

这次更新有效

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

using namespace cv;
using namespace std;

/// Global variables
Mat src, src_gray;
int thresh = 200;
int max_thresh = 255;


/// Function header
void cornerHarris_demo( int, void* );

/** @function main */
int main( int argc, char** argv )
{
  /// Load source image and convert it to gray
  src = imread("chessboard.jpg");
  cvtColor( src, src_gray, CV_BGR2GRAY );

  /// Create a window and a trackbar
  namedWindow( "Source image", CV_WINDOW_AUTOSIZE );
  createTrackbar( "Threshold: ", "Source image", &thresh, max_thresh, cornerHarris_demo );

  cornerHarris_demo( 0, 0 );

  waitKey(0);
  return(0);
}

/** @function cornerHarris_demo */
void cornerHarris_demo( int, void* )
{

  Mat dst, dst_norm, dst_norm_scaled, temp;
  src.copyTo(temp);
  dst = Mat::zeros( src.size(), CV_32FC1 );

  /// Detector parameters
  int blockSize = 2;
  int apertureSize = 3;
  double k = 0.04;

  /// Detecting corners
  cornerHarris( src_gray, dst, blockSize, apertureSize, k, BORDER_DEFAULT );

  /// Normalizing
  normalize( dst, dst_norm, 0, 255, NORM_MINMAX, CV_32FC1, Mat() );
  convertScaleAbs( dst_norm, dst_norm_scaled );

  /// Drawing a circle around corners
  for( int j = 0; j < dst_norm.rows ; j++ )
     { for( int i = 0; i < dst_norm.cols; i++ )
          {
            if( (int) dst_norm.at<float>(j,i) > thresh )
              {
               circle( temp, Point( i, j ), 5,  Scalar(255,0,0), 2, 8, 0 );
              }
          }
     }
  /// Showing the result
  imshow( "Source image", temp );
}

仍然不知道为什么第一个没有。

关于c++ - Trackbar 结果没有出现 C++/Opencv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38384150/

相关文章:

c++ - 引用TC++PL第3版10.4.6.3 "copying members"部分

opencv - 如何为OpenCV模板匹配选择稳定区域?

python - 具有精确的移动距离和方向变化如何填充OpenCV透视变换矩阵(disparity-to-depth)?

opencv - 正交摄影机和透视摄影机在结构上与运动上的模型有何不同?

python - 如何删除子图之间的 "empty"空间?

c++ - QTreeView在主要功能之外不起作用

c++ - C/C++ 中静态关键字的输出问题

c++ - C++中如何访问类的私有(private)数据成员

python - 替换 Numpy 图像中的像素值

python - Matplotlib imshow - 'speed up' 某些值范围内的颜色变化