Problems capturing image from LI-USB30-V024STEREO Leopard Imaging stereo board

asked 2015-08-04 17:26:42 -0600

jordanthompson gravatar image

updated 2015-08-21 14:21:38 -0600

Hi there, I have a LI-USB30-V024STEREO board which interlaces the grayscale image into a single image. I am reading the images from the two cameras and displaying it into several windows, including the original image, but am unable to properly grab the gray-scale video from the source.

I have gotten some information and code from Leopard Imaging, but I think there is still a disconnect. They have provided to me what they are telling me is required to extract the two images from a frame. I have included the code below as well as before and after images of a frame. I am calling their code from ImageProcessing::process(). Any help you may provide would be most appreciated.

Raw, original image:

Processed Image:

#include "ImageProcessor.h"
#include "../handlers/menu/Menu.h"
#include "../utils/WindowUtils.h"

#include <vector>
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <stdio.h>
#include <SDL/SDL.h>

using namespace cv;
using namespace std;

string ImageProcessor::theWindowName = "Video";

ImageProcessor::ImageProcessor()
: videoCapture(0), myScreen(0), myFrameNumber(0) {
  isRunningX = WindowUtils::isRunningX();

  if (!videoCapture.isOpened()) {
    cout << "Cannot open the video cam" << endl;
  }

  if (isRunningX) {
    namedWindow(theWindowName.c_str(), CV_WINDOW_FULLSCREEN);
    cvNamedWindow(theWindowName.c_str(), CV_WINDOW_NORMAL);
    cvSetWindowProperty(theWindowName.c_str(), CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
  } else {
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_ShowCursor(0);
  }
}

#define BYTE uchar
#define WORD ushort
#define TRUE 1
#define FALSE 0
static unsigned short linear_to_gamma[65536];
static double gammaValue = -1;
static int gBPP = 0;
void bayer_to_rgb24(uint8_t *pBay, uint8_t *pRGB24, int width, int height, int pix_order);
int raw_to_bmp_mono(BYTE* in_bytes, BYTE* out_bytes, int width, int height, int bpp,
        bool GammaEna, double gamma);

bool ImageProcessor::process(Mat& matOut) {
// Leopard Imaging
unsigned char* ptr = (unsigned char*) malloc(myMat.cols * myMat.rows * 3);
unsigned char* ptr2 = (unsigned char*) malloc(myMat.cols * myMat.rows * 3);
bayer_to_rgb24(myMat.data, ptr, myMat.cols, myMat.rows, 1);
raw_to_bmp_mono(ptr, ptr2, myMat.cols, myMat.rows, 8, true, 1.6);
  int i = 0;

  for (int row = 0; row < matOut.rows; row++) {
    for (int col = 0; col < matOut.cols; col++) {
      Vec3b colorVect;
      colorVect.val[0] = ptr2[i];
      colorVect.val[1] = ptr2[i];
      colorVect.val[2] = ptr2[i];
      matOut.at<Vec3b>(Point(col, row)) = colorVect;
      i+=2;
    }
  }
  if (myFrameNumber == 10) {
    writePngImage("/tmp/image.png", matOut, false);
    writeInfo("/tmp/image.csv", matOut, false);
    cout << "image snapped" << endl;
  }
  myFrameNumber++;
  free(ptr);
  free(ptr2);
}

void ImageProcessor::writePngImage(const string & fileName,
        const Mat& mat, bool isColor) {
  vector<int> compression_params;
  compression_params.push_back(IMWRITE_PNG_COMPRESSION);

  if (isColor) {
    compression_params.push_back(9);
  } else {
    compression_params.push_back(3);
  }

  try {
    imwrite(fileName, mat, compression_params);
  } catch (...) {
    fprintf(stderr, "Exception converting image to PNG format\n");
  }
}

void ImageProcessor::writeInfo(const std::string & fileName,
        const Mat& mat, bool isColor) {
  FILE* file = fopen(fileName.c_str(), "w");
  uchar* ptr = mat.data;
  // write the header row
  for (int col = 0; col < mat.cols; col++) {
    if (isColor) {
      fprintf(file, "%3d[0]\t%3d[1]\t%3d[2]", col, col, col);
    } else {
      fprintf(file, "%3d[0]\t%3d[1]", col, col);
    }
    if (col < mat.cols - 1) {
      fprintf(file, "\t");
    }
  }
  fprintf(file, "\n");

  int val = 2;
  switch (val) {
    case 1:
      // write the data using the class
      for (int row ...
(more)
edit retag flag offensive close merge delete

Comments

For mat constructor it is row and col at line Mat matLow(matIn.cols, matIn.rows, CV_8UC1);

LBerger gravatar imageLBerger ( 2015-08-05 02:49:58 -0600 )edit

something is wrong here Mat matMid(matIn.rows, matIn.rows, CV_8UC1); May be you shoud write loop like this :

    for (int row = 0; row < matIn.rows; row++) {

   uchar* pLow = matLow.ptr(row);
    uchar* pHigh = matHigh.ptr(row);
    uchar* pIn = matIn.ptr(row);
     for (int col = 0; col < matIn.cols; col++) {
        *pLow = *pIn;
        pLow++;
        pIn++;
        pIn++;
        *pHigh = *pIn;
        pHigh++;
        pIn++;
      }
    }
LBerger gravatar imageLBerger ( 2015-08-05 04:34:47 -0600 )edit

I don't understand. So let's try split plane:

    vector<Mat> colorPlane;
    split( matIn, colorPlane);
    imShow("Blue",colorPlane[0]);
    imShow("Green",colorPlane[1]);
    imShow("Red",colorPlane[2]);
LBerger gravatar imageLBerger ( 2015-08-05 09:28:06 -0600 )edit

I'm not sure. You can try

uchar *pSrc;
unsigned short *pDst1,*pDst2;
for (int i = 0; i < matIn.rows; i++)
{
    pDst1 = (unsigned short*)matLow.ptr(i);
    pDst2 = (unsigned short*)matHigh.ptr(i);
    pSrc = matIn.ptr(i);
    for (int j = 0; j < matIn.cols; j++)
    {
            unsigned  short v=*pSrc++;
            unsigned  short v1=(*pSrc&0xF0)>>4;
            v=v*16+v1;
            v = pow(v/4096.0,1.0/1.6)*4096;
            *pDst1++=v*16;
            v1=(*pSrc&0x0F);
            pSrc++;
            v=v1+(*pSrc++)*16;
            v = pow(v/4096.0,1.0/1.6)*4096;
            *pDst2++=(v*16);
    }

}
LBerger gravatar imageLBerger ( 2015-08-06 15:58:55 -0600 )edit

sorry i forget :

Mat matLow(matIn.rows, matIn.cols, CV_16UC1), matHigh(matIn.rows,matIn.cols,CV_16UC1);

after imshow you have to insert line

waitKey(20);
LBerger gravatar imageLBerger ( 2015-08-06 16:31:45 -0600 )edit

In png image plane 0 and 2 are quite similar and plane 1 is an unknown for me. I cannot see any disparity due to stereo I have try to convert color space using this without success :

for (int i = 0; i < COLOR_COLORCVT_MAX; i++)
{
    Mat mc;
    {
        try
        {
         cvtColor(matIn,mc,i);
        imshow("test" + to_string(i),mc);
        waitKey();
       }
        catch (cv::Exception e)
        {
            std::cerr<<"Error cvtcolor: "<<e.what()<<std::endl;
        }   
    }
}

I have seen your post here. Can you debug this project using visual studio? May be opencv uses a wong driver for this camera

LBerger gravatar imageLBerger ( 2015-08-07 03:12:51 -0600 )edit

Can you send three images first with left blacking camera second with right camera blacking and third with all camera active pointing same scene?

LBerger gravatar imageLBerger ( 2015-08-07 10:01:01 -0600 )edit

first Image 14389661582674824.png second Image 14389661688312743.png plane 0and 2 all pixels are 255 plane 1 some pixels are ranging to 113 161 (button right) third 14389661805812268.png plan like previous image I cannot see disparity. plane 0and 2 all pixels are 255 and plan 1 is an halo. Using Leopardimaging software can you have image like third image?

If you send me good image I think opencv driver is false.

LBerger gravatar imageLBerger ( 2015-08-07 14:28:23 -0600 )edit

I have uploaded the images and verified that this last set is correct. I really appreciate your help with this.

jordanthompson gravatar imagejordanthompson ( 2015-08-10 09:51:37 -0600 )edit

I still think something is wrong. But we can try something. We have an unknown image with three plane X,Y and Z. We can suppose that a specific matriw such that [right Left ]'=M [X Y Z]'. symbol ' is for transpose. We have to identify M a 2 rows by three colum matrix. With saturated image is not a good way (not enough equation). A better way is to take first a stereo image of a scene, second only left image (with right image black) and third a right image (with left image black). After we have a linear system to solve...

Please can you clean your post it is difficult to find your new data (post is very large)

LBerger gravatar imageLBerger ( 2015-08-10 10:38:32 -0600 )edit