简体   繁体   English

C / C ++ OpenCV视频处理

[英]C/C++ OpenCV video processing

Good day everyone! 今天是个好日子! So currently I'm working on a project with video processing, so I decided to give a try to OpenCV. 所以目前我正在开发一个带视频处理的项目,所以我决定尝试一下OpenCV。 As I'm new to it, I decided to find few sample codes and test them out. 由于我是新手,我决定找几个示例代码并测试它们。 First one, is C OpenCV and looks like this: 第一个是C OpenCV,看起来像这样:

#include <opencv/cv.h>
#include <opencv/highgui.h>

#include <stdio.h>

int main( void ) {

CvCapture* capture = 0;
IplImage *frame = 0;

    if (!(capture = cvCaptureFromCAM(0)))
    printf("Cannot initialize camera\n");

cvNamedWindow("Capture", CV_WINDOW_AUTOSIZE);

while (1) {

    frame = cvQueryFrame(capture);
    if (!frame)
        break;

    IplImage *temp = cvCreateImage(cvSize(frame->width/2, frame->height/2), frame->depth, frame->nChannels); // A new Image half size

    cvResize(frame, temp, CV_INTER_CUBIC); // Resize
    cvSaveImage("test.jpg", temp, 0); // Save this image
    cvShowImage("Capture", frame); // Display the frame
    cvReleaseImage(&temp);
    if (cvWaitKey(5000) == 27) // Escape key and wait, 5 sec per capture
        break;
}

cvReleaseImage(&frame);
cvReleaseCapture(&capture);

return 0;

} }

So, this one works perfectly well and stores image to hard drive nicely. 所以,这个非常好用,可以很好地将图像存储到硬盘中。 But problems begin with next sample, which uses C++ OpenCV: 但问题始于下一个使用C ++ OpenCV的示例:

#include "opencv2/opencv.hpp"
#include <string>

using namespace cv;

int main(int, char**)
{
    VideoCapture cap(0); // open the default camera
    if(!cap.isOpened())  // check if we succeeded
        return -1;

    Mat edges;
    //namedWindow("edges",1);
    for(;;)
    {
        Mat frame;
        cap >> frame; // get a new frame from camera
        cvtColor(frame, edges, CV_RGB2XYZ);
        imshow("edges", edges);
    //imshow("edges2", frame);
    //imwrite("test1.jpg", frame);
        if(waitKey(1000) >= 0) break;
    }
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
}

So, yeah, generally, in terms of showing video (image frames) there is practically no changes, but when it comes to using im* * functions, some problems arise. 所以,是的,一般来说,在显示视频(图像帧)方面几乎没有变化,但是当涉及到使用im * *功能时,会出现一些问题。

Using cvSaveImage() works out nicely, but the moment I try to use imwrite() , unhandled exception arises in regards of 'access violation reading location'. 使用cvSaveImage()可以很好地工作,但是当我尝试使用imwrite()时 ,出现了关于“访问冲突读取位置”的未处理异常。 Same goes for imread() , when I'm trying to load image. 当我试图加载图像时, imread()也是如此

So, the thing I wanted to ask, is it possible to use most of the functionality with C OpenCV? 那么,我想问的是,是否可以在C OpenCV中使用大部分功能? Or is it necessary to use C++ OpenCV. 或者是否有必要使用C ++ OpenCV。 If yes, is there any solution for the problem I described earlier. 如果是,是否有解决我之前描述的问题的方法。


Also as stated here , images initially are in BGR-format, so conversion needed. 另外,作为陈述这里 ,图像最初是BGR格式,所以转换需要。 But doing BGR2XYZ conversion seems to invert colors, while RGB2XYZ preserve them. 但是做BGR2XYZ转换似乎会反转颜色,而RGB2XYZ保留它们。 Examples: 例子:

images 图片

Or is it necessary to use C++ OpenCV? 或者是否有必要使用C ++ OpenCV?

No, there is no necessity whatsoever. 不,没有任何必要。 You can use any interface you like and you think you are good with it (OpenCV offers C, C++, Python interfaces). 您可以使用任何您喜欢的界面,并且您认为自己很擅长(OpenCV提供C,C ++,Python界面)。

For your problem about imwrite() and imread() : 关于imwrite()imread()

For color images the order channel is normally Blue, Green, Red , this is what imshow() , imread() and imwrite() expect 对于彩色图像,订单通道通常是蓝色,绿色,红色,这就是imshow(),imread()和imwrite()所期望的

Quoted from there 引自那里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM