Convert format by mainipulating pixels

This commit is contained in:
kalman 2018-12-19 17:04:16 +08:00
parent e32138c70a
commit 403ecff01e
7 changed files with 32 additions and 13 deletions

View File

@ -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;

View File

@ -144,7 +144,6 @@ int main(int argc, char *argv[]) {
// only lastest frame is valid
cv::Mat img(400, 1280, CV_8UC3, const_cast<void *>(frame->data));
cv::cvtColor(img, img, CV_BGR2RGB);
cv::imshow("frame", img);
frame = nullptr;

View File

@ -47,7 +47,6 @@ cv::Mat frame2mat(const std::shared_ptr<device::Frame> &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());

View File

@ -78,8 +78,8 @@ bool unpack_left_img_pixels(
CHECK_NOTNULL(frame);
CHECK_EQ(request.format, frame->format());
auto data_new = reinterpret_cast<const std::uint8_t *>(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<const std::uint8_t *>(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++) {

View File

@ -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(

View File

@ -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;

View File

@ -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;