1
votes
#include<highgui.h>
#include<cxcore.h>
#include<cv.h>


#include<stdio.h>
#include<stdlib.h>


int main(int argc, char* argv[])
{
  IplImage* img = cvLoadImage("hello.jpg", CV_WINDOW_FULLSCREEN );
  int *img_data;
  img_data = malloc(sizeof(*img_data)*img->height*img->width*img->nChannels);

  if (!img)
  {
    printf("Image can NOT Load!!!\n");
    return 1;
  }

  cvNamedWindow("myfirstwindow", CV_WINDOW_FREERATIO );
  cvShowImage("myfirstwindow", img);

  printf("Height: %d\nwidth: %d\nnchannels:%d\n",img->height,img->width,img->nChannels);
  cvCvtColor(img,img_data,CV_RGB2XYZ);
  cvWaitKey(0);
  cvReleaseImage(&img);

return 0;
}

In this program I am trying to convert RGB color model to XYZ color model using function cvCvtColor. And i am getting error shown below:

OpenCV Error: Bad argument (Unknown array type) in cvarrToMat, file /build/buildd/opencv-2.3.1/modules/core/src/matrix.cpp, line 646 terminate called after throwing an instance of 'cv::Exception'
what(): /build/buildd/opencv-2.3.1/modules/core/src/matrix.cpp:646: error: (-5) Unknown array type in function cvarrToMat

Any help or suggestion will be greatly appreciated. Thank you.

3
you would have to pass an IplImage as 2nd arg to cvCvtColor, not a pixel pointer ( which is of the wrong type even, uchar* not int* ) . but PLEASE, STOP USING THE OUTDATED C-API, PLEASE !berak
CV_WINDOW_FREERATIO is an invalid arg for cvLoadImage, as haris said beforeberak
Could u please suggest which API to use.Thank u.shreyas
@berak... OP is using pure C language, not C++. So how can C++ api be used in C code.sgarizvi

3 Answers

3
votes

do yourself (and us) a favour, and switch to the c++ api.

the old c-api is no longer developed, and only around for maintenance, don't write any new code in that !

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"

using namespace cv;

int main()
{
    namedWindow("myfirstwindow", CV_WINDOW_FREERATIO );

    Mat img = imread("hello.jpg", CV_IMAGE_LOAD_COLOR);
    imshow("myfirstwindow",img);

    Mat hsv;
    cvtColor(img,hsv,CV_RGB2HSV);

    waitKey(0);
    return 0;
}
2
votes

You are passing enum value CV_WINDOW_FULLSCREEN which is meant for specifying the property of window when calling cvNamedWindow. For image loading, it should be some enum value like CV_LOAD_IMAGE_COLOR or CV_LOAD_IMAGE_GRAYSCALE.

But your code is not crashing here because fortunately, the integral value of CV_WINDOW_FULLSCREEN and CV_LOAD_IMAGE_COLOR is same, i.e. 1, so basically these enum values are equal.

The real cause of error in the code is you are passing raw int* to the function cvCvtColor. All opencv C functions expect IplImage* as arguments. The following line is causing the error.

cvCvtColor(img,img_data,CV_RGB2XYZ);

You might want to do something like this:

int main(int argc, char* argv[])
{
  IplImage* img = cvLoadImage("hello.jpg", CV_LOAD_IMAGE_COLOR);
  IplImage *img_data;
  img_data = cvCreateImage(cvGetSize(img), img->depth, img->nChannels);

  if (!img)
  {
    printf("Image can NOT Load!!!\n");
    return 1;
  }

  cvNamedWindow("myfirstwindow", CV_WINDOW_FREERATIO );
  cvShowImage("myfirstwindow", img);

  printf("Height: %d\nwidth: %d\nnchannels:%d\n",img->height,img->width,img->nChannels);
  cvCvtColor(img,img_data,CV_RGB2XYZ);
  cvWaitKey(0);
  cvReleaseImage(&img);
  cvReleaseImage(&img_data);

  return 0;
}
0
votes

You need to pass CV_LOAD_IMAGE_COLOR to the function cvLoadImage(), which will load color image.