opencv 3.0 findContours函数在窗口中不起作用
opencv 3.0 findContours function not working in window
我使用的是visual studio 15,在opencv 3.0中工作,我的代码中出现了访问违规错误,甚至这个函数也不能使用opencv中给出的示例代码。
#include"stdafx.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <math.h>
#include <iostream>
using namespace cv;
using namespace std;
static void help()
{
cout
<< "nThis program illustrates the use of findContours and drawContoursn"
<< "The original image is put up along with the image of drawn contoursn"
<< "Usage:n"
<< "./contours2n"
<< "nA trackbar is put up which controls the contour level from -3 to 3n"
<< endl;
}
const int w = 500;
int levels = 3;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
static void on_trackbar(int, void*)
{
Mat cnt_img = Mat::zeros(w, w, CV_8UC3);
int _levels = levels - 3;
drawContours(cnt_img, contours, _levels <= 0 ? 3 : -1, Scalar(128, 255, 255),
3, LINE_AA, hierarchy, std::abs(_levels));
imshow("contours", cnt_img);
}
int main(int argc, char**)
{
Mat img = Mat::zeros(w, w, CV_8UC1);
if (argc > 1)
{
help();
return -1;
}
//Draw 6 faces
for (int i = 0; i < 6; i++)
{
int dx = (i % 2) * 250 - 30;
int dy = (i / 2) * 150;
const Scalar white = Scalar(255);
const Scalar black = Scalar(0);
if (i == 0)
{
for (int j = 0; j <= 10; j++)
{
double angle = (j + 5)*CV_PI / 21;
line(img, Point(cvRound(dx + 100 + j * 10 - 80 * cos(angle)),
cvRound(dy + 100 - 90 * sin(angle))),
Point(cvRound(dx + 100 + j * 10 - 30 * cos(angle)),
cvRound(dy + 100 - 30 * sin(angle))), white, 1, 8, 0);
}
}
ellipse(img, Point(dx + 150, dy + 100), Size(100, 70), 0, 0, 360, white, -1, 8, 0);
ellipse(img, Point(dx + 115, dy + 70), Size(30, 20), 0, 0, 360, black, -1, 8, 0);
ellipse(img, Point(dx + 185, dy + 70), Size(30, 20), 0, 0, 360, black, -1, 8, 0);
ellipse(img, Point(dx + 115, dy + 70), Size(15, 15), 0, 0, 360, white, -1, 8, 0);
ellipse(img, Point(dx + 185, dy + 70), Size(15, 15), 0, 0, 360, white, -1, 8, 0);
ellipse(img, Point(dx + 115, dy + 70), Size(5, 5), 0, 0, 360, black, -1, 8, 0);
ellipse(img, Point(dx + 185, dy + 70), Size(5, 5), 0, 0, 360, black, -1, 8, 0);
ellipse(img, Point(dx + 150, dy + 100), Size(10, 5), 0, 0, 360, black, -1, 8, 0);
ellipse(img, Point(dx + 150, dy + 150), Size(40, 10), 0, 0, 360, black, -1, 8, 0);
ellipse(img, Point(dx + 27, dy + 100), Size(20, 35), 0, 0, 360, white, -1, 8, 0);
ellipse(img, Point(dx + 273, dy + 100), Size(20, 35), 0, 0, 360, white, -1, 8, 0);
}
//show the faces
namedWindow("image", 1);
imshow("image", img);
//Extract the contours so that
//vector<vector<Point> > contours0;
vector<cv::Mat> coutours;
findContours(img, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
contours.resize(contours.size());
for (size_t k = 0; k < contours.size(); k++)
approxPolyDP(Mat(contours[k]), contours[k], 3, true);
namedWindow("contours", 1);
createTrackbar("levels+3", "contours", &levels, 7, on_trackbar);
on_trackbar(0, 0);
waitKey();
return 0;
}
我使用x64体系结构,并链接了所有库.lib和d.lib(调试库)。
我认为问题来自于您的"contoures"变量。您将其声明为vector<cv::Mat>
,但轮廓不是用矩阵表示的,而是用一系列点表示的。
看看这个例子:http://docs.opencv.org/2.4/doc/tutorials/imgproc/shapedescriptors/find_contours/find_contours.html
他们将轮廓声明为vector<vector<Point> > contours;
另请查看函数的声明(http://docs.opencv.org/2.4/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=findcontours#findcontours),参数轮廓定义为:contours – Detected contours. Each contour is stored as a vector of points.
相关文章:
- 代码编译但不起作用!cmd窗口只是理想和理想,但什么也没发生
- Gtk::带有set_icon_name的窗口图标集在 wayland 上不起作用
- Opencv在2013年第5季度不起作用.应用程序总是在窗口中崩溃
- 命名窗口正在显示,但不起作用
- SDL2 窗口在失焦时不起作用
- 为什么 OpenService() 在我检查窗口服务状态时不起作用?
- gtkmm 和多窗口不起作用
- 为什么 random() 在窗口中不起作用
- DirectX 呈现参数窗口=假不起作用
- 窗口处于全屏模式时,QT连接不起作用
- 窗口可防止多个实例代码不起作用
- 创建窗口不起作用,但不提供错误消息
- SFML 通过构造函数获取另一个类的窗口不起作用
- gtkmm 2.4 开罗时钟示例在修改为在 HBox 内部而不是主窗口内绘制时不起作用
- 创建窗口不起作用
- 枚举子窗口不起作用,但枚举Windows可以...?
- 在子窗口的 OnShowWindow 中将焦点设置为父窗口不起作用
- 查找窗口不起作用 C++
- 尝试将光标剪辑到我的窗口不起作用?
- 将窗口置于前面 -> raise(),show(),激活窗口() 不起作用