opencv 保存读取mat

mat保存到txt


	写入f_out.txt
	fstream f_out("f_out.txt", ios::out);
	if (!f_out.fail())
	{
		cout << "start writing f_out.txt" << endl;
		for (int i = 0; i < I.rows; i++)
		{
			for (int j = 0; j < I.cols; j++)
			{
				f_out << int(I.at(i, j)) << "\t";
			}
			f_out << std::endl;
		}
		cout << "finish writing f_out.txt" << endl;
	}
	else
		cout << "can not open" << endl;
	f_out.close();

txt转mat:

	ifstream fin("I_matlab.txt");
	for (int i = 0; i < gray.rows; i++)
	{
		for (int j = 0; j < gray.cols; j++)
		{
			int a;
			fin >> a;
			gray.at(i, j) = a;//
		}
	}

python 保存txt

import cv2
import numpy as np


def save_jzdl_imginput(save_path, img_path, shape):
    img = cv2.imread(img_path)
    img = cv2.resize(img, shape)
    cv2.imshow('test', img)
    cv2.waitKey(0)
    img = img.flatten()
    with open(save_path, 'w') as f:
        f.write('unsigned char image[')
        f.write(str(img.size))
        f.write('] = {')

        for v in img:
            f.write(str(v) + ', ')

        f.write('};')


if __name__ == '__main__':
    save_jzdl_imginput('./img_input.h', './test.jpg', (512, 288))

感谢博文:

https://blog.csdn.net/DylanYang_BBY/article/details/107827171

你可能感兴趣的:(opencv)