opencv pillow读取 rgb planner 二进制数据 resize 重采样
#include <fstream>
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(int argc, char **argv)
{
const uint32_t cols = 640;
const uint32_t rows = 426;
ifstream fin0("000000000139.planner", ios::in | ios::binary);
if(!fin0.is_open())
{
cout << "open fail!\n";
return 1;
}
Mat img0_r = Mat::zeros(rows, cols, CV_8UC1);
Mat img0_g = Mat::zeros(rows, cols, CV_8UC1);
Mat img0_b = Mat::zeros(rows, cols, CV_8UC1);
fin0.read(&img0_r.at< char >(0, 0), sizeof(char) * rows * cols);
fin0.read(&img0_g.at< char >(0, 0), sizeof(char) * rows * cols);
fin0.read(&img0_b.at< char >(0, 0), sizeof(char) * rows * cols);
fin0.close();
Mat channels[3] = { img0_b, img0_g, img0_r };
Mat img0;
merge(channels, 3, img0);
Mat imgDst = Mat::zeros(224, 224, CV_8UC3);
resize(img0, imgDst, imgDst.size(), 0, 0, INTER_LINEAR);
imwrite("out2.jpg", imgDst);
return 0;
}
================================================================================================
# -*- encoding=utf-8 -*-
from PIL import Image
import numpy as np
def resize():
in_width = 640
in_height = 426
out_width = 224
out_height = 224
rgb_planner = np.fromfile("00351300139.planner",
dtype=np.uint8, count=in_width*in_height*3)
rgb_planner = rgb_planner.reshape(3, in_height, in_width)
rgb_888 = np.zeros((in_height, in_width, 3), dtype=np.uint8)
for h in range(in_height):
for w in range(in_width):
rgb_888[h, w, 0] = rgb_planner[0, h, w]
rgb_888[h, w, 1] = rgb_planner[1, h, w]
rgb_888[h, w, 2] = rgb_planner[2, h, w]
rgb_888.tofile("00351300139.img")
pil_image = Image.fromarray(rgb_888)
new_image = pil_image.resize((out_width, out_height), Image.BILINEAR)
pil_image.save("in.jpg")
new_image.save("out.jpg")
if __name__ == "__main__":
resize()