support RGB

This commit is contained in:
Kalman 2018-08-06 02:23:27 +08:00
parent 00aa72166d
commit 26d95ec0ac
5 changed files with 78 additions and 36 deletions

View File

@ -25,7 +25,7 @@ int main(int argc, char *argv[]) {
if (!api)
return 1;
api->SetStreamRequest(
Resolution::RES_2560x800, Format::YUYV, FrameRate::RATE_20_FPS);
Resolution::RES_1280x400, Format::RGB888, FrameRate::RATE_20_FPS);
// api->SetOptionValue(Option::FRAME_RATE, 25);
// api->SetOptionValue(Option::IMU_FREQUENCY, 500);
api->SetOptionValue(Option::IR_CONTROL, 80);

View File

@ -48,10 +48,11 @@ int main(int argc, char *argv[]) {
*/
// device->LogOptionInfos();
// device->RunOptionAction(Option::ZERO_DRIFT_CALIBRATION);
// device->RunOptionAction(Option::ERASE_CHIP);
std::size_t left_count = 0;
device->SetStreamRequest(
Resolution::RES_2560x800, Format::YUYV, FrameRate::RATE_30_FPS);
Resolution::RES_1280x400, Format::RGB888, FrameRate::RATE_30_FPS);
device->SetStreamCallback(
Stream::LEFT, [&left_count](const device::StreamData &data) {
CHECK_NOTNULL(data.img);
@ -114,17 +115,38 @@ int main(int argc, char *argv[]) {
<< ", temperature: " << data.imu->temperature;
}
cv::Mat left_img(
left_data.frame->height(), left_data.frame->width(), CV_8UC2,
left_data.frame->data());
cv::Mat right_img(
right_data.frame->height(), right_data.frame->width(), CV_8UC2,
right_data.frame->data());
cv::Mat img;
cv::cvtColor(left_img, left_img, cv::COLOR_YUV2BGR_YUY2);
cv::cvtColor(right_img, right_img, cv::COLOR_YUV2BGR_YUY2);
cv::hconcat(left_img, right_img, img);
if (left_data.frame->format() == Format::GREY) {
cv::Mat left_img(
left_data.frame->height(), left_data.frame->width(), CV_8UC1,
left_data.frame->data());
cv::Mat right_img(
right_data.frame->height(), right_data.frame->width(), CV_8UC1,
right_data.frame->data());
cv::hconcat(left_img, right_img, img);
} else if (left_data.frame->format() == Format::YUYV) {
cv::Mat left_img(
left_data.frame->height(), left_data.frame->width(), CV_8UC2,
left_data.frame->data());
cv::Mat right_img(
right_data.frame->height(), right_data.frame->width(), CV_8UC2,
right_data.frame->data());
cv::cvtColor(left_img, left_img, cv::COLOR_YUV2BGR_YUY2);
cv::cvtColor(right_img, right_img, cv::COLOR_YUV2BGR_YUY2);
cv::hconcat(left_img, right_img, img);
} else if (left_data.frame->format() == Format::RGB888) {
cv::Mat left_img(
left_data.frame->height(), left_data.frame->width(), CV_8UC3,
left_data.frame->data());
cv::Mat right_img(
right_data.frame->height(), right_data.frame->width(), CV_8UC3,
right_data.frame->data());
cv::hconcat(left_img, right_img, img);
} else {
return -1;
}
cv::imshow("frame", img);
char key = static_cast<char>(cv::waitKey(1));

View File

@ -41,8 +41,6 @@ MYNTEYE_BEGIN_NAMESPACE
namespace {
cv::Mat frame2mat(const std::shared_ptr<device::Frame> &frame) {
// TODO(JohnZhao) Support different format frame to cv::Mat
CHECK_EQ(frame->format(), Format::YUYV);
if (frame->format() == Format::YUYV) {
cv::Mat img(frame->height(), frame->width(), CV_8UC2, frame->data());
cv::cvtColor(img, img, cv::COLOR_YUV2BGR_YUY2);

View File

@ -29,8 +29,6 @@ namespace {
bool unpack_stereo_img_data(
const void *data, const StreamRequest &request, ImgData *img) {
CHECK_NOTNULL(img);
CHECK_EQ(request.format, Format::YUYV);
auto data_new = reinterpret_cast<const std::uint8_t *>(data);
std::size_t data_n =
request.width * request.height * bytes_per_pixel(request.format);
@ -60,7 +58,7 @@ bool unpack_stereo_img_data(
for (std::size_t i = 2, n = packet_n - 2; i <= n; i++) { // content: [2,9]
checksum = (checksum ^ packet[i]);
}
/*
if (img_packet.checksum != checksum) {
LOG(WARNING) << "Image packet checksum should be 0x" << std::hex
<< std::uppercase << std::setw(2) << std::setfill('0')
@ -69,7 +67,6 @@ bool unpack_stereo_img_data(
<< static_cast<int>(checksum) << " now";
return false;
}
*/
img->frame_id = img_packet.frame_id;
img->timestamp = img_packet.timestamp;
@ -80,32 +77,52 @@ bool unpack_stereo_img_data(
bool unpack_left_img_pixels(
const void *data, const StreamRequest &request, Streams::frame_t *frame) {
CHECK_NOTNULL(frame);
CHECK_EQ(request.format, Format::YUYV);
CHECK_EQ(frame->format(), Format::YUYV);
CHECK_EQ(request.format, frame->format());
auto data_new = reinterpret_cast<const std::uint8_t *>(data);
std::size_t w = frame->width() * 2;
std::size_t h = frame->height();
for (std::size_t i = 0; i < h; i++) {
for (std::size_t j = 0; j < w; j++) {
frame->data()[i * w + j] = *(data_new + 2 * i * w + j);
if (request.format == Format::YUYV || request.format == Format::RGB888) {
std::size_t n = request.format == Format::YUYV ? 2 : 3;
std::size_t w = frame->width() * n;
std::size_t h = frame->height();
for (std::size_t i = 0; i < h; i++) {
for (std::size_t j = 0; j < w; j++) {
frame->data()[i * w + j] = *(data_new + 2 * i * w + j);
}
}
} else if (request.format == Format::GREY) {
std::size_t n = frame->width() * frame->height();
for (std::size_t i = 0; i < n; i++) {
frame->data()[i] = *(data_new + (i * 2));
}
} else {
return false;
}
return true;
}
bool unpack_right_img_pixels(
const void *data, const StreamRequest &request, Streams::frame_t *frame) {
CHECK_NOTNULL(frame);
CHECK_EQ(request.format, Format::YUYV);
CHECK_EQ(frame->format(), Format::YUYV);
CHECK_EQ(request.format, frame->format());
auto data_new = reinterpret_cast<const std::uint8_t *>(data);
std::size_t w = frame->width() * 2;
std::size_t h = frame->height();
for (std::size_t i = 0; i < h; i++) {
for (std::size_t j = 0; j < w; j++) {
frame->data()[i * w + j] = *(data_new + (2 * i + 1) * w + j);
if (request.format == Format::YUYV || request.format == Format::RGB888) {
std::size_t n = request.format == Format::YUYV ? 2 : 3;
std::size_t w = frame->width() * n;
std::size_t h = frame->height();
for (std::size_t i = 0; i < h; i++) {
for (std::size_t j = 0; j < w; j++) {
frame->data()[i * w + j] = *(data_new + (2 * i + 1) * w + j);
}
}
} else if (request.format == Format::GREY) {
std::size_t n = frame->width() * frame->height();
for (std::size_t i = 0; i < n; i++) {
frame->data()[i] = *(data_new + (i * 2 + 1));
}
} else {
return false;
}
return true;
}
@ -150,13 +167,13 @@ bool Streams::PushStream(const Capabilities &capability, const void *data) {
switch (capability) {
case Capabilities::STEREO_COLOR: {
// alloc left
AllocStreamData(Stream::LEFT, request, Format::YUYV);
AllocStreamData(Stream::LEFT, request);
auto &&left_data = stream_datas_map_[Stream::LEFT].back();
// unpack img data
if (unpack_img_data_map_[Stream::LEFT](
data, request, left_data.img.get())) {
// alloc right
AllocStreamData(Stream::RIGHT, request, Format::YUYV);
AllocStreamData(Stream::RIGHT, request);
auto &&right_data = stream_datas_map_[Stream::RIGHT].back();
*right_data.img = *left_data.img;
// unpack frame
@ -288,8 +305,11 @@ void Streams::AllocStreamData(
data.img = nullptr;
}
if (!data.frame) {
data.frame = std::make_shared<frame_t>(
request.width / 2, request.height, format, nullptr);
int width = request.width;
if (format != Format::GREY)
width /= 2;
data.frame =
std::make_shared<frame_t>(width, request.height, format, nullptr);
}
stream_datas_map_[stream].push_back(data);
}

View File

@ -167,6 +167,8 @@ std::size_t bytes_per_pixel(const Format &value) {
return 1;
case Format::YUYV:
return 2;
case Format::RGB888:
return 3;
default:
LOG(FATAL) << "Unknown format";
}