gpt4 book ai didi

c++ - 实时视频上的 OpenCV Canny Edge

转载 作者:搜寻专家 更新时间:2023-10-31 00:45:10 24 4
gpt4 key购买 nike

我正在尝试对来自网络摄像头的实时视频实现 opencv 的 canny 边缘检测。但是,我收到此错误:

OpenCV 错误:未知函数中不支持的格式或格式组合 <>,文件 .......\ocv\opencv\src\cv\cvcanny.cpp,第 66 行

我想这是一个格式问题。我可以将 3 channel 8 位 RGB 图像转换为 1 channel 灰度图像帧,然后对结果进行边缘检测。但是,我也无法在图像上实现 rgb2grayscale 转换; <此转换代码的 channel 数不正确>

下面是我在 VS2008 中实现的代码。关于如何解决此错误的任何想法?

#pragma once

#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <conio.h>
#include <iomanip>
#include <cv.h>
#include <cvaux.h>
#include <cxcore.h>
#include <highgui.h>
#include <math.h>
#include <string.h>
#include <cxtypes.h>


using namespace std;
using namespace cv;

int main(int, char**)
{

cvNamedWindow("Edges", CV_WINDOW_AUTOSIZE);
CvCapture* capture = cvCaptureFromCAM(0);

IplImage* frame;
while(1) {
frame = cvQueryFrame( capture );


int depth_img =frame->depth;
int height_img =frame->height;
int width_img =frame->width;
int size_img =frame->imageSize;
int nchan_img =frame->nChannels;
int nsize_img =frame->nSize;

cout << setw(15) << "depth" << depth_img << endl;
cout << setw(15) << "height" << height_img << endl;
cout << setw(15) << "width" << width_img << endl;
cout << setw(15) << "size" << size_img << endl;
cout << setw(15) << "nchan" << nchan_img << endl;
cout << setw(15) << "nsize" << nsize_img << endl;


IplImage* out = cvCreateImage( cvGetSize(frame), IPL_DEPTH_8U, 3 );
cvSmooth( frame, out, CV_GAUSSIAN, 11, 11 );
cvCvtColor(out ,out, CV_RGB2GRAY);
cvCanny( out, out, 10, 10, 3 );

if( !frame ) break;
cvShowImage( "Edge", out );
char c = cvWaitKey(33);
if( c == 27 ) break;
}
cvReleaseCapture( &capture );
cvDestroyWindow( "Edge" );
return 0;
}

最佳答案

问题是你每时每刻都在传递一个 3 channel 图像

IplImage* out = cvCreateImage( cvGetSize(frame), IPL_DEPTH_8U, 3 );

创建另外两个单 channel 图像并使用它们:

IplImage* gray_out = cvCreateImage( cvGetSize(frame), IPL_DEPTH_8U, 1 );
IplImage* canny_out = cvCreateImage( cvGetSize(frame), IPL_DEPTH_8U, 1 );

并将其用于:

cvSmooth( frame, out, CV_GAUSSIAN, 11, 11 );
cvCvtColor(out , gray_out, CV_RGB2GRAY);
cvCanny( gray_out, canny_out, 10, 10, 3 );

if( !frame ) break;
cvShowImage( "Edge", canny_out );

这对我有用:

#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <cv.h>
#include <cvaux.h>
#include <cxcore.h>
#include <highgui.h>
#include <math.h>
#include <string.h>


using namespace std;
using namespace cv;

int main(int, char**)
{

cvNamedWindow("Edges", CV_WINDOW_AUTOSIZE);
CvCapture* capture = cvCaptureFromCAM(0);

IplImage* frame;
while(1) {
frame = cvQueryFrame( capture );

int depth_img =frame->depth;
int height_img =frame->height;
int width_img =frame->width;
int size_img =frame->imageSize;
int nchan_img =frame->nChannels;
int nsize_img =frame->nSize;

cout << setw(15) << "depth" << depth_img << endl;
cout << setw(15) << "height" << height_img << endl;
cout << setw(15) << "width" << width_img << endl;
cout << setw(15) << "size" << size_img << endl;
cout << setw(15) << "nchan" << nchan_img << endl;
cout << setw(15) << "nsize" << nsize_img << endl;


IplImage* out = cvCreateImage( cvGetSize(frame), IPL_DEPTH_8U, 3 );
IplImage* gray_out = cvCreateImage( cvGetSize(frame), IPL_DEPTH_8U, 1 );
IplImage* canny_out = cvCreateImage( cvGetSize(frame), IPL_DEPTH_8U, 1 );
cvSmooth( frame, out, CV_GAUSSIAN, 11, 11 );
cvCvtColor(out , gray_out, CV_RGB2GRAY);
cvCanny( gray_out, canny_out, 10, 10, 3 );

if( !frame ) break;
cvShowImage( "Edge", canny_out );
char c = cvWaitKey(33);
if( c == 27 ) break;
}
cvReleaseCapture( &capture );
cvDestroyWindow( "Edge" );
return 0;
}

关于c++ - 实时视频上的 OpenCV Canny Edge,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7351705/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com