diff --git a/samples/api/camera.cc b/samples/api/camera.cc index 754aa2d..3f9b5fc 100644 --- a/samples/api/camera.cc +++ b/samples/api/camera.cc @@ -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); diff --git a/samples/device/camera.cc b/samples/device/camera.cc index 3178de4..b896bce 100644 --- a/samples/device/camera.cc +++ b/samples/device/camera.cc @@ -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(cv::waitKey(1)); diff --git a/src/api/synthetic.cc b/src/api/synthetic.cc index 7d41451..6e9cb5e 100644 --- a/src/api/synthetic.cc +++ b/src/api/synthetic.cc @@ -41,8 +41,6 @@ MYNTEYE_BEGIN_NAMESPACE namespace { cv::Mat frame2mat(const std::shared_ptr &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); diff --git a/src/internal/streams.cc b/src/internal/streams.cc index 9b4125e..a12459d 100644 --- a/src/internal/streams.cc +++ b/src/internal/streams.cc @@ -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(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(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(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(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( - request.width / 2, request.height, format, nullptr); + int width = request.width; + if (format != Format::GREY) + width /= 2; + data.frame = + std::make_shared(width, request.height, format, nullptr); } stream_datas_map_[stream].push_back(data); } diff --git a/src/public/types.cc b/src/public/types.cc index 296c8a4..818af76 100644 --- a/src/public/types.cc +++ b/src/public/types.cc @@ -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"; }