关于OpenCV Error:Assertion failed的错误

hujilin1229 2012-01-15 04:45:18
最近在读一个程序,加上编程的基础不好,所以在板上发帖可能会有点频繁。这段程序我已经能够编译通过,不过在执行时出现了如下错误:
OpenCV Error:Assertion failed (scn ==3 || scn==4) in unknown function, file ..\..\..\modules\imgproc\src\color.cpp, line 2433
实在纠结,不知道问题出现在哪里,还请高手帮助,下面附上我的主程序

#include "cv.h"
#include "highgui.h"
#include "tld_utils.h"
#include <iostream>
#include <sstream>
#include "TLD.h"
#include "CameraDS.h"
#include <stdio.h>
using namespace cv;
using namespace std;

//Global variables
Rect box;
bool drawing_box = false;
bool gotBB = false;
bool tl = true;
bool rep = false;
bool fromfile=false;
string video;
IplImage *pFrame;

void readBB(char* file){
ifstream bb_file (file);
string line;
getline(bb_file,line);
istringstream linestream(line);
string x1,y1,x2,y2;
getline (linestream,x1, ',');
getline (linestream,y1, ',');
getline (linestream,x2, ',');
getline (linestream,y2, ',');
int x = atoi(x1.c_str());// = (int)file["bb_x"];
int y = atoi(y1.c_str());// = (int)file["bb_y"];
int w = atoi(x2.c_str())-x;// = (int)file["bb_w"];
int h = atoi(y2.c_str())-y;// = (int)file["bb_h"];
box = Rect(x,y,w,h);
}
//bounding box mouse callback
void mouseHandler(int event, int x, int y, int flags, void *param)
{
switch( event )
{
case CV_EVENT_MOUSEMOVE:
if (drawing_box){
box.width = x-box.x;
box.height = y-box.y;
}
break;
case CV_EVENT_LBUTTONDOWN:
drawing_box = true;
box = Rect( x, y, 0, 0 );
break;
case CV_EVENT_LBUTTONUP:
drawing_box = false;
if( box.width < 0 ){
box.x += box.width;
box.width *= -1;
}
if( box.height < 0 ){
box.y += box.height;
box.height *= -1;
}
gotBB = true;
break;
}
}

void print_help(char** argv){
printf("use:\n %s -p /path/parameters.yml\n",argv[0]);
printf("-s source video\n-b bounding box file\n-tl track and learn\n-r repeat\n");
}

void read_options(int argc, char** argv,VideoCapture& capture,FileStorage &fs)
{
for (int i=0;i<argc;i++)
{
if (strcmp(argv[i],"-b")==0)
{
if (argc>i)
{
readBB(argv[i+1]);
gotBB = true;
}
else
print_help(argv);
}
if (strcmp(argv[i],"-s")==0)
{
if (argc>i){
video = string(argv[i+1]);
capture.open(video);
fromfile = true;
}
else
print_help(argv);

}
if (strcmp(argv[i],"-p")==0)
{
if (argc>i)
{
fs.open(argv[i+1], FileStorage::READ);
}
else
print_help(argv);
}
if (strcmp(argv[i],"-no_tl")==0)
{
tl = false;
}
if (strcmp(argv[i],"-r")==0)
{
rep = true;
}
}
}

int main(int argc,char * argv[])
{
CCameraDS camera;
VideoCapture capture;
//capture.open(0);

FileStorage fs;
//Read options
read_options(argc,argv,capture,fs);
//Init camera
if(!camera.OpenCamera(0, true))
//if (!capture.isOpened())
{
cout << "capture device failed to open!" << endl;
return 1;
}
//Register mouse callback to draw the bounding box
cvNamedWindow("TLD",CV_WINDOW_AUTOSIZE);
cvSetMouseCallback( "TLD", mouseHandler, NULL );
//TLD framework
TLD tld;
//Read parameters file
tld.read(fs.getFirstTopLevelNode());
Mat frame;
Mat last_gray;
Mat first;
if (fromfile)
{
capture >> frame;
cvtColor(frame, last_gray, CV_RGB2GRAY);
frame.copyTo(first);
}
else//从摄像头读取
{
//capture.set(CV_CAP_PROP_FRAME_WIDTH,340);
//capture.set(CV_CAP_PROP_FRAME_HEIGHT,240);
pFrame = camera.QueryFrame();
first=pFrame;
}

///Initialization
GETBOUNDINGBOX:
while(!gotBB)
{
if (!fromfile)
{
capture >> frame;
}
else
//first.copyTo(frame);
frame=pFrame;

cvtColor(frame, last_gray, CV_RGB2GRAY);
drawBox(frame,box);
imshow("TLD", frame);
if (cvWaitKey(33) == 'q')
return 0;
}
if (min(box.width,box.height)<(int)fs.getFirstTopLevelNode()["min_win"])
{
cout << "Bounding box too small, try again." << endl;
gotBB = false;
goto GETBOUNDINGBOX;
}
//Remove callback
cvSetMouseCallback( "TLD", NULL, NULL );
printf("Initial Bounding Box = x:%d y:%d h:%d w:%d\n",box.x,box.y,box.width,box.height);
//Output file
FILE *bb_file = fopen("bounding_boxes.txt","w");
//TLD initialization
tld.init(last_gray,box,bb_file);

///Run-time
Mat current_gray;
BoundingBox pbox;
vector<Point2f> pts1;
vector<Point2f> pts2;
bool status=true;
int frames = 1;
int detections = 1;
REPEAT:
while(capture.read(frame))
{
//get frame
cvtColor(frame, current_gray, CV_RGB2GRAY);
//Process Frame
tld.processFrame(last_gray,current_gray,pts1,pts2,pbox,status,tl,bb_file);
//Draw Points
if (status){
drawPoints(frame,pts1);
drawPoints(frame,pts2,Scalar(0,255,0));
drawBox(frame,pbox);
detections++;
}
//Display
imshow("TLD", frame);
//swap points and images
swap(last_gray,current_gray);
pts1.clear();
pts2.clear();
frames++;
printf("Detection rate: %d/%d\n",detections,frames);
if (cvWaitKey(33) == 'q')
break;
}
if (rep)
{
rep = false;
tl = false;
fclose(bb_file);
bb_file = fopen("final_detector.txt","w");
//capture.set(CV_CAP_PROP_POS_AVI_RATIO,0);
capture.release();
capture.open(video);
goto REPEAT;
}
fclose(bb_file);
return 0;
}

...全文
28355 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
frank1208 2014-08-07
  • 打赏
  • 举报
回复
我也遇到这个问题了,同求大神支招呀。
zjlgzhh 2014-07-21
  • 打赏
  • 举报
回复
我也遇到了这个问题,楼主是怎么解决的?
ganruan 2014-05-21
  • 打赏
  • 举报
回复
楼主,你好!请问你的问题是怎么解决的啊 ?
Best_ 2014-03-12
  • 打赏
  • 举报
回复
“opencv error assertion failed (scn == 3 scn == 4) in unknown function”,出现这种问题,基本上是由于cvtColor()使用不当造成的,主要有两种情况: 第一种如2楼所述,图像通道没设置对; 第二种,如果程序运行到某一帧frame为空的情况下,再执行cvtColor(frame, current_gray, CV_RGB2GRAY)也会报错。
gglong 2013-12-25
  • 打赏
  • 举报
回复
hello 楼主解决这个问题了吗? 我也遇到相同的问题了
txwd0033 2012-01-15
  • 打赏
  • 举报
回复
OpenCV中你用cvtColor(frame, current_gray, CV_RGB2GRAY);在转彩色到灰度的时候, 要求frame是3通道或者4通道的, 你看看你的frame是彩图吗?

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧