Vector type compatibility issue

How do I go around the following Vector type compatibility error ?

phung@UbuntuHW15:~/Documents/taylor$ g++ -g host.cpp -o host `pk
g-config --cflags --libs opencv`
host.cpp: In function ‘int main(int, char**)’:
host.cpp:52:13: error: cannot convert ‘std::vector<unsigned char>’ to ‘packet*’ in assignment
     tologic = vector<uint8_t>(sizeof(struct packet) * N * N, 0);  // lena.tiff 
             ^
host.cpp:123:15: error: cannot convert ‘std::vector<packet>’ to ‘packet*’ in assignment
     fromlogic = vector<packet>(sizeof(struct packet) * N * N);
               ^
phung@UbuntuHW15:~/Documents/taylor$ 



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// 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>

using namespace cv;
using namespace std;


#define N 512

struct packet{
  uint8_t r,g,b;
};


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

  int fdr, fdw, rc, donebytes;
  char *buf;
  pid_t pid;
  struct packet *tologic, *fromlogic;
  int i;
  float a, da;

  fdr = open("/dev/stdin", O_RDONLY);
  fdw = open("/dev/stdout", O_WRONLY);

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

  pid = fork();

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

  if (pid) {
    close(fdr);

    tologic = vector<uint8_t>(sizeof(struct packet) * N * N, 0);  // lena.tiff is sized as 512*512*3
    if (!tologic) {
      fprintf(stderr, "Failed to allocate memory\n");
      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;
    }

	 namedWindow( "Original Image", CV_WINDOW_AUTOSIZE );
	 imshow( "Original Image", image );
	 
	 Mat rgbchannel[3];
	 // The actual splitting.
	 split(image, rgbchannel);
	 
	 namedWindow("Blue",CV_WINDOW_AUTOSIZE);
	 imshow("Red", rgbchannel[0]);
	 
	 namedWindow("Green",CV_WINDOW_AUTOSIZE);
	 imshow("Green", rgbchannel[1]);
	 
	 namedWindow("Red",CV_WINDOW_AUTOSIZE);
	 imshow("Blue", rgbchannel[2]);

	tologic->r = *(rgbchannel[0].data);
	tologic->g = *(rgbchannel[1].data);
	tologic->b = *(rgbchannel[2].data);

    buf = (char *) tologic;
    donebytes = 0;

	while (donebytes < sizeof(struct packet) * N) {
	  rc = write(fdw, buf + donebytes, sizeof(struct packet) * N - donebytes);

	  if ((rc < 0) && (errno == EINTR))
		continue;

	  if (rc <= 0) {
		perror("write() failed");
		exit(1);
	  }

	  donebytes += rc;
	}

	sleep(1); // Let debug output drain (if used)

	close(fdw);

	return 0;
  } 

  else {
    close(fdw);

    fromlogic = vector<packet>(sizeof(struct packet) * N * N);
    if (!fromlogic) {
      fprintf(stderr, "Failed to allocate memory\n");
      exit(1);
    }

    buf = (char *) fromlogic;
    donebytes = 0;

    while (donebytes < sizeof(struct packet) * N) {
      rc = read(fdr, buf + donebytes, sizeof(struct packet) * N - donebytes);

      if ((rc < 0) && (errno == EINTR))
		continue;

      if (rc < 0) {
		perror("read() failed");
		exit(1);
      }

      if (rc == 0) {
		fprintf(stderr, "Reached read EOF!? Should never happen.\n");
		exit(0);
      }

      donebytes += rc;
    }

   // for (i=0; i<N; i++)
      printf("R:%d G:%d B:%d \n", fromlogic[0].r, fromlogic[0].g, fromlogic[0].b);
      printf("R:%d G:%d B:%d \n", fromlogic[68499].r, fromlogic[68499].g, fromlogic[68499].b);
    sleep(1); // Let debug output drain (if used)

    close(fdr);

    return 0;
  }
}
Last edited on
A vector is a vector. It's not a pointer of any kind so it can't be assigned to one.
1
2
vector<packet> vTo(sizeof(packet) * N * N, 0);
vector<packet> vFrom(sizeof(packet) * N * N);

If you need to pass a raw packet* C-style array, you can use:
 
vFrom.data()

But maybe you don't really want to use a vector. I'm not sure if there's any advantage here since the main advantage of a vector is that it can change size dynamically.

You're using open and printf so you seem to be more of a C programmer. Are you trying to rewrite this in C++?
Last edited on
Yes, I am helping to rewrite this piece of code in C++.

I am still trying to understand the logic designed by the previous c code author.

By the way, thanks for the vFrom.data() tips.
sleep(1); // Let debug output drain (if used)

What do you have in mind regarding this line of code ?
If it needs the sleep it needs the sleep.

Why are you rewriting this in C++? I don't see the point.
@tpb

because openCV C API is no longer being maintained by its developers.

By the way, how to go around the following error ?


phung@UbuntuHW15:~/Documents/taylor$ g++ -g host.cpp -o host `pkg-config --cflags --
libs opencv`
host.cpp: In function ‘YUV_packet* rgb2yuv(RGB_packet)’:
host.cpp:32:26: error: expected primary-expression before ‘.’ token
  uint8_t red = RGB_packet.R;
                          ^
host.cpp:33:28: error: expected primary-expression before ‘.’ token
  uint8_t green = RGB_packet.G;
                            ^
host.cpp:34:27: error: expected primary-expression before ‘.’ token
  uint8_t blue = RGB_packet.B;
                           ^
phung@UbuntuHW15:~/Documents/taylor$ 



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// 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 N 512

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

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


struct YUV_packet* rgb2yuv(struct RGB_packet)  // convert rgb to yuv
{  
	uint8_t red = RGB_packet.R;
	uint8_t green = RGB_packet.G;
	uint8_t blue = RGB_packet.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, rc, donebytes;
  char *buf;
  pid_t pid;
  struct RGB_packet *tologic;
  struct YUV_packet *fromlogic;

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

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

  pid = fork();

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

  if (pid) {
	close(fdr);

	vector<RGB_packet> vTo(sizeof(struct RGB_packet) * N * N);  // lena.tiff is sized as 512*512*3

    tologic = vTo.data();

    if (!tologic) {
      fprintf(stderr, "Failed to allocate memory\n");
      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;
    }

	namedWindow( "Original Image", CV_WINDOW_AUTOSIZE );
	imshow( "Original Image", image );
	 
	Mat rgbchannel[3];
	// The actual splitting.
	split(image, rgbchannel);
	 
	namedWindow("Blue",CV_WINDOW_AUTOSIZE);
	imshow("Red", rgbchannel[0]);
	 
	namedWindow("Green",CV_WINDOW_AUTOSIZE);
	imshow("Green", rgbchannel[1]);
	 
	namedWindow("Red",CV_WINDOW_AUTOSIZE);
	imshow("Blue", rgbchannel[2]);

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

	tologic->R = *(rgbchannel[0].data);
	tologic->G = *(rgbchannel[1].data);
	tologic->B = *(rgbchannel[2].data);

    buf = (char *) tologic;
    donebytes = 0;

	while (donebytes < sizeof(struct RGB_packet) * N * N) {
	  rc = write(fdw, buf + donebytes, sizeof(struct RGB_packet) * N * N - donebytes);

	  if ((rc < 0) && (errno == EINTR))
		continue;

	  if (rc <= 0) {
		perror("write() failed");
		exit(1);
	  }

	  donebytes += rc;
	}

	sleep(1); // Let debug output drain (if used)

	close(fdw);

	return 0;
  } 

  else {
    close(fdw);

	vector<YUV_packet> vFrom(sizeof(struct YUV_packet) * N * N);

    fromlogic = vFrom.data();

    if (!fromlogic) {
      fprintf(stderr, "Failed to allocate memory\n");
      exit(1);
    }

    buf = (char *) fromlogic;
    donebytes = 0;

    while (donebytes < sizeof(struct YUV_packet) * N * N) {
      rc = read(fdr, buf + donebytes, sizeof(struct YUV_packet) * N * N - donebytes);

      if ((rc < 0) && (errno == EINTR))
		continue;

      if (rc < 0) {
		perror("read() failed");
		exit(1);
      }

      if (rc == 0) {
		fprintf(stderr, "Reached read EOF!? Should never happen.\n");
		exit(0);
      }

      donebytes += rc;
    }

    for (int i = 0; i < (sizeof(struct RGB_packet) * N * N); i++)  // check the perfomance of hardware with respect to software computation
	{
		if((abs(rgb2yuv(tologic[i])->Y - fromlogic[i].Y) > 1) ||
		   (abs(rgb2yuv(tologic[i])->U - fromlogic[i].U) > 1) ||
		   (abs(rgb2yuv(tologic[i])->V - fromlogic[i].V) > 1)) // rgb2yuv conversion hardware tolerance fails by more than 1 compared to software computation
		{
			printf("R:%d G:%d B:%d \n", tologic[i].R, tologic[i].G, tologic[i].B);
      		printf("Y:%d U:%d V:%d \n", fromlogic[i].Y, fromlogic[i].U, fromlogic[i].V);
		}
	}

    sleep(1); // Let debug output drain (if used)

    close(fdr);

    return 0;
  }
}
To get around that error you need to define an actual struct, you cannot use the struct name as an object, you have to declare it first:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Define an RGB_packet called packet in parameters
YUV_packet rgb2yuv(RGB_packet packet)  // convert rgb to yuv
{  
	uint8_t red = packet.R;
	uint8_t green = packet.G;
	uint8_t blue = packet.B;

	YUV_packet yuv_result; // Declare a YUV_packet called yuv_result

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

	return yuv_result;
}



Last edited on
Topic archived. No new replies allowed.