Problem reading in image correctly [closed]

asked 2018-04-25 08:12:43 -0600

For https://gist.github.com/promach/9d185... or line 172 of the following coding , why am I getting rgb_stream[STREAM_WIDTH] equals to 192, while all other entries of the array are equal to zero ?

// g++ -g host.cpp -o host `pkg-config --cflags --libs opencv`

#include <opencv2/core/core.hpp>
#include <opencv2/imgcodecs/imgcodecs.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <unistd.h>
#include <fcntl.h>
#include <iostream>
#include <fstream>      // std::ifstream, std::ofstream
#include <string>
#include <sys/wait.h>
#include <errno.h>
#include <cmath>

using namespace cv;
using namespace std;

#define LOOPBACK 1
//#define RGB2YUV 1

unsigned int image_width;
unsigned int image_height;

const unsigned int CHNL_NUM = 3;
const unsigned int RED_CHNL = 0;
const unsigned int GREEN_CHNL = 1;
const unsigned int BLUE_CHNL = 2;

const unsigned int STREAM_WIDTH = 128; 
const unsigned int NUM_OF_BITS_PER_BYTE = 8;

const unsigned int PIXEL_VALUE_RANGE = 8; // number of bits occupied by [R, G, B] or [Y, U, V] respectively (8-bit unsigned integer for each components) , https://docs.microsoft.com/en-us/windows-hardware/drivers/display/yuv-rgb-data-range-conversions
const unsigned int NUM_OF_COMPONENTS_IN_A_PIXEL = 3; // input: [R, G, B]     output:[Y, U, V]

const unsigned int PIXEL_NUM_THAT_FITS_STREAM_WIDTH = 5;  // 128-bit stream can at most fits 5 pixels ((PIXEL_NUM_THAT_FITS_STREAM_WIDTH*NUM_OF_COMPONENTS_IN_A_PIXEL*PIXEL_VALUE_RANGE) bits = 120 bits), each pixels contains R, G, B which are encoded in 8 bits for each of the three color components

struct RGB_packet{
  uint8_t R,G,B;
};

struct YUV_packet{
  uint8_t Y,U,V;
};


struct YUV_packet* rgb2yuv(struct RGB_packet rgb_input)  // convert rgb to yuv
{  

    uint8_t red = rgb_input.R;
    uint8_t green = rgb_input.G;
    uint8_t blue = rgb_input.B;

    struct YUV_packet *yuv_result;

    uint8_t Y = yuv_result->Y;
    uint8_t U = yuv_result->U;
    uint8_t V = yuv_result->V;

    // https://www.pcmag.com/encyclopedia/term/55166/yuv-rgb-conversion-formulas
    Y = (uint8_t)(0.299*red + 0.587*green + 0.114*blue);
    U = (uint8_t)(0.492*(blue-Y));
    V = (uint8_t)(0.877*(red-Y));

    return yuv_result;
}

int main(int argc, char *argv[]) {

  int fdr, fdw, rd, wr, donebytes;
  uint8_t *to_buf, *from_buf, *wr_buf, *rd_buf;
  pid_t pid;
  struct RGB_packet *tologic;
  struct YUV_packet *fromlogic;

  fdr = open("/dev/stdout", O_RDONLY);  // will change to /dev/xillybus_read_128
  fdw = open("/dev/stdin", O_WRONLY); // will change to /dev/xillybus_write_128

  if ((fdr < 0) || (fdw < 0)) {
    perror("Failed to open Xillybus device file(s)");
    exit(1);
  }

  // READ in an image file

  String imageName( "lena512color.tiff" ); // by default

  if( argc > 1)
  {
    imageName = argv[1];
  }

  Mat image;

  image = imread( imageName, IMREAD_COLOR ); // Read the file

  if( image.empty() )                      // Check for invalid input
  {
    cout <<  "Could not open or find the image" << std::endl ;
    return -1;
  }

  else
  {
    image_width = image.size().width;
    image_height = image.size().height;
  }

  namedWindow( "Original Image", CV_WINDOW_AUTOSIZE );
  imshow( "Original Image", image );

  Mat rgbchannel[CHNL_NUM];
  // The actual splitting.
  split(image, rgbchannel);

  namedWindow("Blue",CV_WINDOW_AUTOSIZE);
  imshow("Red", rgbchannel[RED_CHNL]);

  namedWindow("Green",CV_WINDOW_AUTOSIZE);
  imshow("Green", rgbchannel[GREEN_CHNL]);

  namedWindow("Red",CV_WINDOW_AUTOSIZE);
  imshow("Blue", rgbchannel[BLUE_CHNL]);

  waitKey(0);  // see all three split channels before feeding in the channel data to xillybus/RIFFA for hardware computation


  pid = fork();

  if (pid < 0) {
    perror("Failed to fork()");
    exit(1);
  }

  if (pid) {
    close(fdr);

    vector<RGB_packet> vTo(sizeof(struct RGB_packet) * image_width * image_height);  // lena.tiff is sized as 512*512*3 ...
(more)
edit retag flag offensive reopen merge delete

Closed for the following reason question is off-topic or not relevant by berak
close date 2018-04-25 08:55:29.865708

Comments

sorry, but we can't help with your fpga problems.

btw, your image is loaded correctly. anything below that looks problematic, BGR vs RGB, fork() from hell, low level ops ...

berak gravatar imageberak ( 2018-04-25 08:55:56 -0600 )edit