From 403ecff01ea9b13684f1f7b43972867ea4daed35 Mon Sep 17 00:00:00 2001 From: kalman Date: Wed, 19 Dec 2018 17:04:16 +0800 Subject: [PATCH] Convert format by mainipulating pixels --- samples/device/camera.cc | 2 -- samples/uvc/camera.cc | 1 - src/mynteye/api/synthetic.cc | 1 - src/mynteye/device/streams.cc | 36 +++++++++++++++++++++++++++++++---- tools/dataset/dataset.cc | 1 - tools/dataset/record.cc | 2 -- tools/dataset/record2.cc | 2 -- 7 files changed, 32 insertions(+), 13 deletions(-) diff --git a/samples/device/camera.cc b/samples/device/camera.cc index 0737ced..62f26af 100644 --- a/samples/device/camera.cc +++ b/samples/device/camera.cc @@ -121,8 +121,6 @@ int main(int argc, char *argv[]) { cv::Mat right_img( right_data.frame->height(), right_data.frame->width(), CV_8UC3, right_data.frame->data()); - cv::cvtColor(left_img, left_img, CV_BGR2RGB); - cv::cvtColor(right_img, right_img, CV_BGR2RGB); cv::hconcat(left_img, right_img, img); } else { return -1; diff --git a/samples/uvc/camera.cc b/samples/uvc/camera.cc index 00d7b16..c87b27f 100644 --- a/samples/uvc/camera.cc +++ b/samples/uvc/camera.cc @@ -144,7 +144,6 @@ int main(int argc, char *argv[]) { // only lastest frame is valid cv::Mat img(400, 1280, CV_8UC3, const_cast(frame->data)); - cv::cvtColor(img, img, CV_BGR2RGB); cv::imshow("frame", img); frame = nullptr; diff --git a/src/mynteye/api/synthetic.cc b/src/mynteye/api/synthetic.cc index 35ab9a5..296e5af 100644 --- a/src/mynteye/api/synthetic.cc +++ b/src/mynteye/api/synthetic.cc @@ -47,7 +47,6 @@ cv::Mat frame2mat(const std::shared_ptr &frame) { return img; } else if (frame->format() == Format::BGR888) { cv::Mat img(frame->height(), frame->width(), CV_8UC3, frame->data()); - cv::cvtColor(img, img, CV_BGR2RGB); return img; } else { return cv::Mat(frame->height(), frame->width(), CV_8UC1, frame->data()); diff --git a/src/mynteye/device/streams.cc b/src/mynteye/device/streams.cc index 5e4ea4b..15e2bc3 100644 --- a/src/mynteye/device/streams.cc +++ b/src/mynteye/device/streams.cc @@ -78,8 +78,8 @@ bool unpack_left_img_pixels( CHECK_NOTNULL(frame); CHECK_EQ(request.format, frame->format()); auto data_new = reinterpret_cast(data); - if (request.format == Format::YUYV || request.format == Format::BGR888) { - std::size_t n = request.format == Format::YUYV ? 2 : 3; + if (request.format == Format::YUYV) { + std::size_t n = 2; std::size_t w = frame->width() * n; std::size_t h = frame->height(); for (std::size_t i = 0; i < h; i++) { @@ -87,6 +87,20 @@ bool unpack_left_img_pixels( frame->data()[i * w + j] = *(data_new + 2 * i * w + j); } } + } else if (request.format == Format::BGR888) { + std::size_t n = 3; + std::size_t w = frame->width(); + 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) * n] = + *(data_new + (2 * i * w + j) * n + 2); + frame->data()[(i * w + j) * n + 1] = + *(data_new + (2 * i * w + j) * n + 1); + frame->data()[(i * w + j) * n + 2] = + *(data_new + (2 * i * w + j) * n); + } + } } else if (request.format == Format::GREY) { std::size_t n = frame->width() * frame->height(); for (std::size_t i = 0; i < n; i++) { @@ -105,8 +119,8 @@ bool unpack_right_img_pixels( CHECK_NOTNULL(frame); CHECK_EQ(request.format, frame->format()); auto data_new = reinterpret_cast(data); - if (request.format == Format::YUYV || request.format == Format::BGR888) { - std::size_t n = request.format == Format::YUYV ? 2 : 3; + if (request.format == Format::YUYV) { + std::size_t n = 2; std::size_t w = frame->width() * n; std::size_t h = frame->height(); for (std::size_t i = 0; i < h; i++) { @@ -114,6 +128,20 @@ bool unpack_right_img_pixels( frame->data()[i * w + j] = *(data_new + (2 * i + 1) * w + j); } } + } else if (request.format == Format::BGR888) { + std::size_t n = 3; + std::size_t w = frame->width(); + 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) * n] = + *(data_new + ((2 * i + 1) * w + j) * n + 2); + frame->data()[(i * w + j) * n + 1] = + *(data_new + ((2 * i + 1) * w + j) * n + 1); + frame->data()[(i * w + j) * n + 2] = + *(data_new + ((2 * i + 1) * w + j) * n); + } + } } else if (request.format == Format::GREY) { std::size_t n = frame->width() * frame->height(); for (std::size_t i = 0; i < n; i++) { diff --git a/tools/dataset/dataset.cc b/tools/dataset/dataset.cc index 9a3a382..7d2065d 100644 --- a/tools/dataset/dataset.cc +++ b/tools/dataset/dataset.cc @@ -80,7 +80,6 @@ void Dataset::SaveStreamData( cv::Mat img( data.frame->height(), data.frame->width(), CV_8UC3, data.frame->data()); - // cv::cvtColor(img, img, CV_BGR2RGB); cv::imwrite(ss.str(), img); } else { cv::Mat img( diff --git a/tools/dataset/record.cc b/tools/dataset/record.cc index c797c18..4c4f13d 100644 --- a/tools/dataset/record.cc +++ b/tools/dataset/record.cc @@ -87,8 +87,6 @@ int main(int argc, char *argv[]) { cv::Mat right_img( right_frame->height(), right_frame->width(), CV_8UC3, right_frame->data()); - cv::cvtColor(left_img, left_img, CV_BGR2RGB); - cv::cvtColor(right_img, right_img, CV_BGR2RGB); cv::hconcat(left_img, right_img, img); } else { return -1; diff --git a/tools/dataset/record2.cc b/tools/dataset/record2.cc index 4f8c647..52b1738 100644 --- a/tools/dataset/record2.cc +++ b/tools/dataset/record2.cc @@ -90,8 +90,6 @@ int main(int argc, char *argv[]) { cv::Mat right_img( right_frame->height(), right_frame->width(), CV_8UC3, right_frame->data()); - cv::cvtColor(left_img, left_img, CV_BGR2RGB); - cv::cvtColor(right_img, right_img, CV_BGR2RGB); cv::hconcat(left_img, right_img, img); } else { return -1;