I am trying to pass the IplImage
object to another program as arguments using the system library call. The code snippet is as shown below.
#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 = %ld\n", 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;
}
In the code if we see the line where we are making system
invocation, we are trying to pass the IplImage class objects as an arguments to another program executable. The other program snippet is given below.
#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;
}
Now this is throwing up 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
.
So How to solve this issue? Any help appreciated.
If anyone is interested still, once can write an IplImage
as a ppm file and then read it in the other program. This can be done easily using the OpenCV imread()
and imwrite()
functions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With