0

Good afternoon,

I am trying to run OpenCV through a DLL and use it in a LabVIEW application.

I have correctly acquired an image in LV and passed the byte array to the DLL.

I can loop and print out in a text file the values for every pixel and match them to the output in LV, so I know that all my pixels are in the right position, for the exception that LV adds 2 columns at the beginning, with the first 2 values reserved for height and width and the rest are arbitrary numbers. But all this should do is produce a streak on the left side of the image.

Next, I am using the following lines to convert and display the image.

a[0], a[1]... etc. are channels.

The output image comes out as a very horizontally stretched out image with pixels spaced equally 15-20 pixels apart and surrounded by black pixels. I attached a screenshot

_declspec (dllexport)  double  imageProcess(int **a, int &x, int &y, int &cor,int &cog,int &cob,int &cow, int th, int hth)
{

    y = a[0][0];
    x = a[0][1];


    Mat image(y, x, CV_8U, a[0]);


    namedWindow( "Display window", CV_WINDOW_NORMAL );  // Create a window for display.
    imshow( "Display window", image );                 // Show our image inside it.

    return (0);
}

Additionally I tried using this code with the same effect:

IplImage* cv_image = cvCreateImageHeader(cvSize(x,y), IPL_DEPTH_8U, 1);
cvSetData(cv_image, a[0], cv_image->widthStep);
Mat image = Mat(cv_image, false);

Can anyone please help me explain why this is happening during my image creation?

Note, Unfortunately, I cannot provide the original image/capture from LV, but I can say that it doesn't look anything like that and I am working with everything in grayscale.

Output Image:

Output

Philipp Wendler
  • 11,184
  • 7
  • 52
  • 87
Mich
  • 3,188
  • 4
  • 37
  • 85

2 Answers2

0

your input ( a ) is a matrix of ints, while opencv wants uchars there.

the way you do it currently, each int (from a) gets spread over 4 consecutive bytes, ( that's exactly, what i see in that picture ) also it's only using the 1st 1/4 of the input data

you probably won't get away with just feeding the pixel pointer into your cv::Mat there, looping over a[0], casting each pixel to uchar, and then assigning it to the opencv-pixel should work, imho

berak
  • 39,159
  • 9
  • 91
  • 89
  • Thank you for the response, I'm surprised that I did not notice that. It seems that that correct way to fix this would be to call my DLL using char instead of int. But this is causing Labview datatype problems (not accepting char or uint8_t), which I've already posted on their forums. If that fails to work, I would have a very basic question that is causing me issues: If I were to loop through a[0] and do the casting myself, how would I create another array of the same exact number of elements as a[0], but of type char. As far as I know, I can't declare an array in C++ of unknown size. – Mich Feb 11 '13 at 21:10
0

You could convert your image to uchar or simple use an int image by replacing CV_8U by CV_32S and then:

int offset = 0;
int scale = 0;
cv::Mat image8U;
image.convertTo(image8U, CV_8UC1, scale, offset );
Tobias Senst
  • 2,665
  • 17
  • 38
  • Thank you for the response, unfortunately, this is displaying a blank image for me, but the dimensions seem correct. I believe this may be caused by having an image that is Signed to begin with. – Mich Feb 11 '13 at 21:14
  • I found the issue, the Scale is not actually a X and Y image scale, it's a scale of the intensity values of each pixel, so when you set it to 0, it makes all of the pixels black. I set Scale to 1 and everything worked. Thank you very much – Mich Feb 11 '13 at 21:28