简体   繁体   中英

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. 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:

#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:

#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.

Using cvSaveImage() works out nicely, but the moment I try to use imwrite() , unhandled exception arises in regards of 'access violation reading location'. Same goes for imread() , when I'm trying to load image.

So, the thing I wanted to ask, is it possible to use most of the functionality with C OpenCV? Or is it necessary to use 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. But doing BGR2XYZ conversion seems to invert colors, while RGB2XYZ preserve them. Examples:

images

Or is it necessary to use 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).

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

For color images the order channel is normally Blue, Green, Red , this is what imshow() , imread() and imwrite() expect

Quoted from there

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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