无法将 IplImage 类作为参数传递



我正在尝试使用系统库调用将IplImage对象作为参数传递给另一个程序。代码片段如下所示。

  #include <stdio.h>
  #include <opencv/highgui.h>
  #include <unistd.h>
  #include "libuvc/libuvc.h"
  #include <opencv/cv.h>
  #include <opencv2/calib3d/calib3d_c.h>
  #include <stdlib.h>

void cb(uvc_frame_t *frame, void *ptr) {
uvc_frame_t *bgr, *bgr2, *dup;
uvc_error_t ret, ret2;
IplImage* cvImg;
IplImage* cvImg2;
IplImage* im1,im2;
dup=frame;
//  printf("callback! length = %d, ptr = %ldn", frame->data_bytes, (int) ptr);
bgr = uvc_allocate_frame(frame->width * frame->height);
bgr2 = uvc_allocate_frame(frame->width * frame->height);
if (!bgr) {
printf("unable to allocate bgr frame!");
return;
}
if (!bgr2) {
printf("unable to allocate bgr2 frame!");
return;
}
ret = uvc_yuyv2y(frame,bgr);
if (ret) {
uvc_perror(ret, "uvc_yuyv2y");
uvc_free_frame(bgr);
return;
}
ret2 = uvc_yuyv2uv(dup,bgr2);
if (ret2) {
uvc_perror(ret, "uvc_yuyv2uv");
uvc_free_frame(bgr2);
return;
}
cvImg = cvCreateImageHeader(cvSize(bgr->width, bgr->height),IPL_DEPTH_8U,1);
cvSetData(cvImg, bgr->data, bgr->width); 
cvImg2 = cvCreateImageHeader(cvSize(bgr2->width, bgr2->height),IPL_DEPTH_8U,1);
cvSetData(cvImg2, bgr2->data, bgr2->width); 
cvEqualizeHist(cvImg,cvImg);
cvEqualizeHist(cvImg2,cvImg2);
//   cvSaveImage("left.png",cvImg);
//   cvSaveImage("right.png",cvImg2);
//  cvShowImage("left", cvImg);
//  cvShowImage("right", cvImg2);
//   cvWaitKey(10);
int status=system("./../../exe cvImg cvImg2");
cvReleaseImageHeader(&cvImg);
cvReleaseImageHeader(&cvImg2);
uvc_free_frame(bgr);
uvc_free_frame(bgr2);
}
int main(int argc, char **argv) {
uvc_context_t *ctx;
uvc_error_t res;
uvc_device_t *dev;
uvc_device_handle_t *devh;
uvc_stream_ctrl_t ctrl;
res = uvc_init(&ctx, NULL);
if (res < 0) {
uvc_perror(res, "uvc_init");
return res;
}
puts("UVC initialized");
res = uvc_find_device(
  ctx, &dev,
  0, 0, NULL);
if (res < 0) {
uvc_perror(res, "uvc_find_device");
} else {
puts("Device found");
res = uvc_open(dev, &devh);
if (res < 0) {
  uvc_perror(res, "uvc_open");
} else {
  puts("Device opened");
  uvc_print_diag(devh, stderr);
  res = uvc_get_stream_ctrl_format_size(
      devh, &ctrl, UVC_FRAME_FORMAT_YUYV, 640, 480, 30
  );
  uvc_print_stream_ctrl(&ctrl, stderr);
  if (res < 0) {
    uvc_perror(res, "get_mode");
  } else {
    res = uvc_start_streaming(devh, &ctrl, cb, (void *)12345, 0);
    if (res < 0) {
      uvc_perror(res, "start_streaming");
    } else {
      puts("Streaming for 10 seconds...");
      uvc_error_t resAEMODE = uvc_set_ae_mode(devh, 1);
      uvc_perror(resAEMODE, "set_ae_mode");
      int i;
      for (i = 1; i <= 10; i++) {
        /* uvc_error_t resPT = uvc_set_pantilt_abs(devh, i * 20 * 3600, 0); */
        /* uvc_perror(resPT, "set_pt_abs"); */
        uvc_error_t resEXP = uvc_set_exposure_abs(devh, 20 + i * 5);
        uvc_perror(resEXP, "set_exp_abs");
        sleep(1);
      }
      sleep(1);
      uvc_stop_streaming(devh);
  puts("Done streaming.");
    }
  }
  uvc_close(devh);
  puts("Device closed");
 }
  uvc_unref_device(dev);
 }
 uvc_exit(ctx);
 puts("UVC exited");
 return 0;
 }

在代码中,如果我们看到进行system调用的行,我们将尝试将 IplImage 类对象作为参数传递给另一个程序可执行文件。下面给出了另一个程序片段。

#include "opencv2/core.hpp"
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <opencv2/calib3d.hpp>
#include <opencv2/opencv.hpp>
#include <python2.7/Python.h>
using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
Mat img1;
Mat img2;
Mat g1, g2,color;
Mat disp1, disp18,disp2,disp28,disparity,disparity1,falsemap;
Mat falseColorsMap, sfalseColorsMap;
//img1 = imread(argv[1],CV_8UC1);
//img2 = imread(argv[2],CV_8UC1);
img1 = cvarrToMat(argv[1]);
img2 = cvarrToMat(argv[2]);
imshow( "windowDisparity", disp18);
waitKey(1000);
return 0;
}

现在这正在抛出OpenCV Error: Bad argument (Unknown array type) in cvarrToMat, file /home/pi/opencv-3.3.0/modules/core/src/matrix.cpp, line 975 terminate called after throwing an instance of 'cv::Exception' what(): /home/pi/opencv-3.3.0/modules/core/src/matrix.cpp:975: error: (-5) Unknown array type in function cvarrToMat.

那么如何解决这个问题呢?任何帮助表示赞赏。

如果有人仍然感兴趣,一次可以将IplImage写为 ppm 文件,然后在另一个程序中读取它。这可以使用OpenCV imread()imwrite()功能轻松完成。

最新更新