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) if (!api)
return 1; return 1;
api->SetStreamRequest( 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::FRAME_RATE, 25);
// api->SetOptionValue(Option::IMU_FREQUENCY, 500); // api->SetOptionValue(Option::IMU_FREQUENCY, 500);
api->SetOptionValue(Option::IR_CONTROL, 80); api->SetOptionValue(Option::IR_CONTROL, 80);

View File

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

View File

@ -41,8 +41,6 @@ MYNTEYE_BEGIN_NAMESPACE
namespace { namespace {
cv::Mat frame2mat(const std::shared_ptr<device::Frame> &frame) { 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) { if (frame->format() == Format::YUYV) {
cv::Mat img(frame->height(), frame->width(), CV_8UC2, frame->data()); cv::Mat img(frame->height(), frame->width(), CV_8UC2, frame->data());
cv::cvtColor(img, img, cv::COLOR_YUV2BGR_YUY2); cv::cvtColor(img, img, cv::COLOR_YUV2BGR_YUY2);

View File

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

View File

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