Python ghi vào bytesio

Tôi muốn lưu một cấu hình RGB [. jpg] vào tệp nhị phân [. bin] và nhận cùng một dữ liệu đã lưu [trong tệp. bin] bằng Python và C++

Nội dung chính Hiển thị

  • Viết hình ảnh nhị phân bằng gối
  • Viết trực tiếp vào Zipfiles
  • Làm cách nào để chuyển đổi cấu hình ảnh thành nhị phân trong Python?
  • Làm cách nào để chuyển một tệp thành nhị phân?
  • Làm cách nào để mở một nhị phân hình ảnh trong Python?
  • File nhị phân trong Python là gì?

Dưới đây là các mã tôi đã sử dụng để lưu hình ảnh vào thùng tệp trong Python và C++ nhưng tôi có kết quả khác nhau khi so sánh hai tệp. thùng rác

con trăn

image = cv2.imread['image.jpg']
filename1 = "/image.bin"   # save data as bin file
bin_file = image.astype['float32'].tofile[filename1]
byte_list = []
with open[filename1, "rb"] as f:
    while [byte := f.read[1]]:
        byte_list.append[byte]

C++

int IMAGE_SIZE = 224;

void matwrite[const string& filename, const Mat& mat]
{
    ofstream fs[filename, fstream::binary];

    // Header
    int type = mat.type[];
    int channels = mat.channels[];
    fs.write[[char*]&mat.rows, sizeof[int]];    // rows
    fs.write[[char*]&mat.cols, sizeof[int]];    // cols
    fs.write[[char*]&type, sizeof[int]];        // type
    fs.write[[char*]&channels, sizeof[int]];    // channels

    // Data
    if [mat.isContinuous[]]
    {
        fs.write[mat.ptr[0], [mat.dataend - mat.datastart]];
    }
    else
    {
        int rowsz = CV_ELEM_SIZE[type] * mat.cols;
        for [int r = 0; r < mat.rows; ++r]
        {
            fs.write[mat.ptr[r], rowsz];
        }
    }
}

int main[]
{
    // Save data

    {
        cv::Mat noisyImg = cv::imread["image.jpg"];
        //randu[m, 0, 5];
        matwrite["bin_file.bin", data];
    }
return 0 ;
}

Điều tôi đang tìm kiếm là lưu ảnh RGB trong cả C++ và Python vào tệp nhị phân [

Cảm ơn

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luậnOpenCV. Đảm bảo bạn đã cài đặt thư viện vào Python của mình. Để biết các bước cài đặt OpenCV tham khảo bài viết này. Thiết lập Opencv với môi trường anaconda

    Cách tiếp cận

    1. Trong bài viết này, chúng tôi sẽ chuyển đổi hình ảnh thành định dạng nhị phân của nó. Một hình ảnh nhị phân là một hình ảnh đơn sắc bao gồm các pixel có thể có một trong hai màu chính xác, thường là màu đen và trắng. Hình ảnh nhị phân còn được gọi là hai cấp hoặc hai cấp. Điều này có nghĩa là mỗi pixel được lưu trữ dưới dạng một bit duy nhất, tức là 0 hoặc 1
    2. Thư viện quan trọng nhất cần thiết để xử lý ảnh trong Python là OpenCV. Hãy chắc chắn rằng bạn đã cài đặt thư viện vào Python của bạn. Đối chiếu với các bước cài đặt OpenCV đề cập đến bài viết này. Thiết lập OpenCV với môi trường Anaconda
    3. Đọc hình ảnh từ vị trí

    Là một hình ảnh màu có các lớp RGB trong đó và phức tạp hơn, hãy chuyển đổi nó sang dạng thang độ xám của nó trước tiên

    Python3

    Thiết lập một dấu ngưỡng, các pixel phía trên dấu đã cho sẽ chuyển sang màu trắng và điểm bên dưới sẽ chuyển sang màu đen

    Dưới đây là công việc đang thực hiện

    
    

    In [1]:

    #For data manipulations
    %pylab inline
    from IPython.display import set_matplotlib_formats
    from io import BytesIO
    import numpy as np
    
    #to compare to scipy's builtin conversions
    from scipy.misc import imsave, toimage
    
    #import pillow
    from PIL import Image
    
    set_cmap['Greys']
    
    4
    
    

    In [1]:

    #For data manipulations
    %pylab inline
    from IPython.display import set_matplotlib_formats
    from io import BytesIO
    import numpy as np
    
    #to compare to scipy's builtin conversions
    from scipy.misc import imsave, toimage
    
    #import pillow
    from PIL import Image
    
    set_cmap['Greys']
    
    5
    
    

    In [1]:

    #For data manipulations
    %pylab inline
    from IPython.display import set_matplotlib_formats
    from io import BytesIO
    import numpy as np
    
    #to compare to scipy's builtin conversions
    from scipy.misc import imsave, toimage
    
    #import pillow
    from PIL import Image
    
    set_cmap['Greys']
    
    6
    
    

    In [1]:

    #For data manipulations
    %pylab inline
    from IPython.display import set_matplotlib_formats
    from io import BytesIO
    import numpy as np
    
    #to compare to scipy's builtin conversions
    from scipy.misc import imsave, toimage
    
    #import pillow
    from PIL import Image
    
    set_cmap['Greys']
    
    7
    
    

    In [1]:

    #For data manipulations
    %pylab inline
    from IPython.display import set_matplotlib_formats
    from io import BytesIO
    import numpy as np
    
    #to compare to scipy's builtin conversions
    from scipy.misc import imsave, toimage
    
    #import pillow
    from PIL import Image
    
    set_cmap['Greys']
    
    8
    
    

    In [1]:

    #For data manipulations
    %pylab inline
    from IPython.display import set_matplotlib_formats
    from io import BytesIO
    import numpy as np
    
    #to compare to scipy's builtin conversions
    from scipy.misc import imsave, toimage
    
    #import pillow
    from PIL import Image
    
    set_cmap['Greys']
    
    9____________
    
    
    

    Populating the interactive namespace from numpy and matplotlib
    
    1
    
    
    

    Populating the interactive namespace from numpy and matplotlib
    
    2
    int IMAGE_SIZE = 224;
    
    void matwrite[const string& filename, const Mat& mat]
    {
        ofstream fs[filename, fstream::binary];
    
        // Header
        int type = mat.type[];
        int channels = mat.channels[];
        fs.write[[char*]&mat.rows, sizeof[int]];    // rows
        fs.write[[char*]&mat.cols, sizeof[int]];    // cols
        fs.write[[char*]&type, sizeof[int]];        // type
        fs.write[[char*]&channels, sizeof[int]];    // channels
    
        // Data
        if [mat.isContinuous[]]
        {
            fs.write[mat.ptr[0], [mat.dataend - mat.datastart]];
        }
        else
        {
            int rowsz = CV_ELEM_SIZE[type] * mat.cols;
            for [int r = 0; r < mat.rows; ++r]
            {
                fs.write[mat.ptr[r], rowsz];
            }
        }
    }
    
    int main[]
    {
        // Save data
    
        {
            cv::Mat noisyImg = cv::imread["image.jpg"];
            //randu[m, 0, 5];
            matwrite["bin_file.bin", data];
        }
    return 0 ;
    }
    
    07
    int IMAGE_SIZE = 224;
    
    void matwrite[const string& filename, const Mat& mat]
    {
        ofstream fs[filename, fstream::binary];
    
        // Header
        int type = mat.type[];
        int channels = mat.channels[];
        fs.write[[char*]&mat.rows, sizeof[int]];    // rows
        fs.write[[char*]&mat.cols, sizeof[int]];    // cols
        fs.write[[char*]&type, sizeof[int]];        // type
        fs.write[[char*]&channels, sizeof[int]];    // channels
    
        // Data
        if [mat.isContinuous[]]
        {
            fs.write[mat.ptr[0], [mat.dataend - mat.datastart]];
        }
        else
        {
            int rowsz = CV_ELEM_SIZE[type] * mat.cols;
            for [int r = 0; r < mat.rows; ++r]
            {
                fs.write[mat.ptr[r], rowsz];
            }
        }
    }
    
    int main[]
    {
        // Save data
    
        {
            cv::Mat noisyImg = cv::imread["image.jpg"];
            //randu[m, 0, 5];
            matwrite["bin_file.bin", data];
        }
    return 0 ;
    }
    
    08
    int IMAGE_SIZE = 224;
    
    void matwrite[const string& filename, const Mat& mat]
    {
        ofstream fs[filename, fstream::binary];
    
        // Header
        int type = mat.type[];
        int channels = mat.channels[];
        fs.write[[char*]&mat.rows, sizeof[int]];    // rows
        fs.write[[char*]&mat.cols, sizeof[int]];    // cols
        fs.write[[char*]&type, sizeof[int]];        // type
        fs.write[[char*]&channels, sizeof[int]];    // channels
    
        // Data
        if [mat.isContinuous[]]
        {
            fs.write[mat.ptr[0], [mat.dataend - mat.datastart]];
        }
        else
        {
            int rowsz = CV_ELEM_SIZE[type] * mat.cols;
            for [int r = 0; r < mat.rows; ++r]
            {
                fs.write[mat.ptr[r], rowsz];
            }
        }
    }
    
    int main[]
    {
        // Save data
    
        {
            cv::Mat noisyImg = cv::imread["image.jpg"];
            //randu[m, 0, 5];
            matwrite["bin_file.bin", data];
        }
    return 0 ;
    }
    
    09
    int IMAGE_SIZE = 224;
    
    void matwrite[const string& filename, const Mat& mat]
    {
        ofstream fs[filename, fstream::binary];
    
        // Header
        int type = mat.type[];
        int channels = mat.channels[];
        fs.write[[char*]&mat.rows, sizeof[int]];    // rows
        fs.write[[char*]&mat.cols, sizeof[int]];    // cols
        fs.write[[char*]&type, sizeof[int]];        // type
        fs.write[[char*]&channels, sizeof[int]];    // channels
    
        // Data
        if [mat.isContinuous[]]
        {
            fs.write[mat.ptr[0], [mat.dataend - mat.datastart]];
        }
        else
        {
            int rowsz = CV_ELEM_SIZE[type] * mat.cols;
            for [int r = 0; r < mat.rows; ++r]
            {
                fs.write[mat.ptr[r], rowsz];
            }
        }
    }
    
    int main[]
    {
        // Save data
    
        {
            cv::Mat noisyImg = cv::imread["image.jpg"];
            //randu[m, 0, 5];
            matwrite["bin_file.bin", data];
        }
    return 0 ;
    }
    
    30
    int IMAGE_SIZE = 224;
    
    void matwrite[const string& filename, const Mat& mat]
    {
        ofstream fs[filename, fstream::binary];
    
        // Header
        int type = mat.type[];
        int channels = mat.channels[];
        fs.write[[char*]&mat.rows, sizeof[int]];    // rows
        fs.write[[char*]&mat.cols, sizeof[int]];    // cols
        fs.write[[char*]&type, sizeof[int]];        // type
        fs.write[[char*]&channels, sizeof[int]];    // channels
    
        // Data
        if [mat.isContinuous[]]
        {
            fs.write[mat.ptr[0], [mat.dataend - mat.datastart]];
        }
        else
        {
            int rowsz = CV_ELEM_SIZE[type] * mat.cols;
            for [int r = 0; r < mat.rows; ++r]
            {
                fs.write[mat.ptr[r], rowsz];
            }
        }
    }
    
    int main[]
    {
        // Save data
    
        {
            cv::Mat noisyImg = cv::imread["image.jpg"];
            //randu[m, 0, 5];
            matwrite["bin_file.bin", data];
        }
    return 0 ;
    }
    
    31
    
    
    

    Populating the interactive namespace from numpy and matplotlib
    
    2
    int IMAGE_SIZE = 224;
    
    void matwrite[const string& filename, const Mat& mat]
    {
        ofstream fs[filename, fstream::binary];
    
        // Header
        int type = mat.type[];
        int channels = mat.channels[];
        fs.write[[char*]&mat.rows, sizeof[int]];    // rows
        fs.write[[char*]&mat.cols, sizeof[int]];    // cols
        fs.write[[char*]&type, sizeof[int]];        // type
        fs.write[[char*]&channels, sizeof[int]];    // channels
    
        // Data
        if [mat.isContinuous[]]
        {
            fs.write[mat.ptr[0], [mat.dataend - mat.datastart]];
        }
        else
        {
            int rowsz = CV_ELEM_SIZE[type] * mat.cols;
            for [int r = 0; r < mat.rows; ++r]
            {
                fs.write[mat.ptr[r], rowsz];
            }
        }
    }
    
    int main[]
    {
        // Save data
    
        {
            cv::Mat noisyImg = cv::imread["image.jpg"];
            //randu[m, 0, 5];
            matwrite["bin_file.bin", data];
        }
    return 0 ;
    }
    
    33

    đầu ra

    
    
    

    Populating the interactive namespace from numpy and matplotlib
    
    3
    
    

    In [1]:

    #For data manipulations
    %pylab inline
    from IPython.display import set_matplotlib_formats
    from io import BytesIO
    import numpy as np
    
    #to compare to scipy's builtin conversions
    from scipy.misc import imsave, toimage
    
    #import pillow
    from PIL import Image
    
    set_cmap['Greys']
    
    7
    
    
    

    Populating the interactive namespace from numpy and matplotlib
    
    5
    
    
    

    Populating the interactive namespace from numpy and matplotlib
    
    6
    
    
    

    Populating the interactive namespace from numpy and matplotlib
    
    0
    
    
    

    Populating the interactive namespace from numpy and matplotlib
    
    8
    
    
    

    Populating the interactive namespace from numpy and matplotlib
    
    9

    int IMAGE_SIZE = 224;
    
    void matwrite[const string& filename, const Mat& mat]
    {
        ofstream fs[filename, fstream::binary];
    
        // Header
        int type = mat.type[];
        int channels = mat.channels[];
        fs.write[[char*]&mat.rows, sizeof[int]];    // rows
        fs.write[[char*]&mat.cols, sizeof[int]];    // cols
        fs.write[[char*]&type, sizeof[int]];        // type
        fs.write[[char*]&channels, sizeof[int]];    // channels
    
        // Data
        if [mat.isContinuous[]]
        {
            fs.write[mat.ptr[0], [mat.dataend - mat.datastart]];
        }
        else
        {
            int rowsz = CV_ELEM_SIZE[type] * mat.cols;
            for [int r = 0; r < mat.rows; ++r]
            {
                fs.write[mat.ptr[r], rowsz];
            }
        }
    }
    
    int main[]
    {
        // Save data
    
        {
            cv::Mat noisyImg = cv::imread["image.jpg"];
            //randu[m, 0, 5];
            matwrite["bin_file.bin", data];
        }
    return 0 ;
    }
    
    00
    
    

    In [1]:

    #For data manipulations
    %pylab inline
    from IPython.display import set_matplotlib_formats
    from io import BytesIO
    import numpy as np
    
    #to compare to scipy's builtin conversions
    from scipy.misc import imsave, toimage
    
    #import pillow
    from PIL import Image
    
    set_cmap['Greys']
    
    7
    
    
    

    Populating the interactive namespace from numpy and matplotlib
    
    5
    
    
    

    Populating the interactive namespace from numpy and matplotlib
    
    6
    
    
    

    Populating the interactive namespace from numpy and matplotlib
    
    0
    
    
    

    Populating the interactive namespace from numpy and matplotlib
    
    8
    
    
    

    Populating the interactive namespace from numpy and matplotlib
    
    9

    Viết hình ảnh nhị phân bằng gối

    Gối là một cái nĩa của Pil

    Đó là một công cụ thao tác hình ảnh hữu ích, nhưng dường như nó có một số lỗi khi đọc hoặc viết dữ liệu nhị phân

    Sổ ghi chép Python này là để phục vụ như một hướng dẫn để thực hiện công việc xung quanh các vấn đề trong Gối

    
    

    In [1]:

    #For data manipulations
    %pylab inline
    from IPython.display import set_matplotlib_formats
    from io import BytesIO
    import numpy as np
    
    #to compare to scipy's builtin conversions
    from scipy.misc import imsave, toimage
    
    #import pillow
    from PIL import Image
    
    set_cmap['Greys']
    
    
    
    

    Populating the interactive namespace from numpy and matplotlib
    
    int IMAGE_SIZE = 224;
    
    void matwrite[const string& filename, const Mat& mat]
    {
        ofstream fs[filename, fstream::binary];
    
        // Header
        int type = mat.type[];
        int channels = mat.channels[];
        fs.write[[char*]&mat.rows, sizeof[int]];    // rows
        fs.write[[char*]&mat.cols, sizeof[int]];    // cols
        fs.write[[char*]&type, sizeof[int]];        // type
        fs.write[[char*]&channels, sizeof[int]];    // channels
    
        // Data
        if [mat.isContinuous[]]
        {
            fs.write[mat.ptr[0], [mat.dataend - mat.datastart]];
        }
        else
        {
            int rowsz = CV_ELEM_SIZE[type] * mat.cols;
            for [int r = 0; r < mat.rows; ++r]
            {
                fs.write[mat.ptr[r], rowsz];
            }
        }
    }
    
    int main[]
    {
        // Save data
    
        {
            cv::Mat noisyImg = cv::imread["image.jpg"];
            //randu[m, 0, 5];
            matwrite["bin_file.bin", data];
        }
    return 0 ;
    }
    
    0
    int IMAGE_SIZE = 224;
    
    void matwrite[const string& filename, const Mat& mat]
    {
        ofstream fs[filename, fstream::binary];
    
        // Header
        int type = mat.type[];
        int channels = mat.channels[];
        fs.write[[char*]&mat.rows, sizeof[int]];    // rows
        fs.write[[char*]&mat.cols, sizeof[int]];    // cols
        fs.write[[char*]&type, sizeof[int]];        // type
        fs.write[[char*]&channels, sizeof[int]];    // channels
    
        // Data
        if [mat.isContinuous[]]
        {
            fs.write[mat.ptr[0], [mat.dataend - mat.datastart]];
        }
        else
        {
            int rowsz = CV_ELEM_SIZE[type] * mat.cols;
            for [int r = 0; r < mat.rows; ++r]
            {
                fs.write[mat.ptr[r], rowsz];
            }
        }
    }
    
    int main[]
    {
        // Save data
    
        {
            cv::Mat noisyImg = cv::imread["image.jpg"];
            //randu[m, 0, 5];
            matwrite["bin_file.bin", data];
        }
    return 0 ;
    }
    
    3
    
    

    In [1]:

    #For data manipulations
    %pylab inline
    from IPython.display import set_matplotlib_formats
    from io import BytesIO
    import numpy as np
    
    #to compare to scipy's builtin conversions
    from scipy.misc import imsave, toimage
    
    #import pillow
    from PIL import Image
    
    set_cmap['Greys']
    
    5
    
    

    In [1]:

    #For data manipulations
    %pylab inline
    from IPython.display import set_matplotlib_formats
    from io import BytesIO
    import numpy as np
    
    #to compare to scipy's builtin conversions
    from scipy.misc import imsave, toimage
    
    #import pillow
    from PIL import Image
    
    set_cmap['Greys']
    
    6
    
    

    In [1]:

    #For data manipulations
    %pylab inline
    from IPython.display import set_matplotlib_formats
    from io import BytesIO
    import numpy as np
    
    #to compare to scipy's builtin conversions
    from scipy.misc import imsave, toimage
    
    #import pillow
    from PIL import Image
    
    set_cmap['Greys']
    
    7
    
    

    In [1]:

    #For data manipulations
    %pylab inline
    from IPython.display import set_matplotlib_formats
    from io import BytesIO
    import numpy as np
    
    #to compare to scipy's builtin conversions
    from scipy.misc import imsave, toimage
    
    #import pillow
    from PIL import Image
    
    set_cmap['Greys']
    
    8
    int IMAGE_SIZE = 224;
    
    void matwrite[const string& filename, const Mat& mat]
    {
        ofstream fs[filename, fstream::binary];
    
        // Header
        int type = mat.type[];
        int channels = mat.channels[];
        fs.write[[char*]&mat.rows, sizeof[int]];    // rows
        fs.write[[char*]&mat.cols, sizeof[int]];    // cols
        fs.write[[char*]&type, sizeof[int]];        // type
        fs.write[[char*]&channels, sizeof[int]];    // channels
    
        // Data
        if [mat.isContinuous[]]
        {
            fs.write[mat.ptr[0], [mat.dataend - mat.datastart]];
        }
        else
        {
            int rowsz = CV_ELEM_SIZE[type] * mat.cols;
            for [int r = 0; r < mat.rows; ++r]
            {
                fs.write[mat.ptr[r], rowsz];
            }
        }
    }
    
    int main[]
    {
        // Save data
    
        {
            cv::Mat noisyImg = cv::imread["image.jpg"];
            //randu[m, 0, 5];
            matwrite["bin_file.bin", data];
        }
    return 0 ;
    }
    
    0
    int IMAGE_SIZE = 224;
    
    void matwrite[const string& filename, const Mat& mat]
    {
        ofstream fs[filename, fstream::binary];
    
        // Header
        int type = mat.type[];
        int channels = mat.channels[];
        fs.write[[char*]&mat.rows, sizeof[int]];    // rows
        fs.write[[char*]&mat.cols, sizeof[int]];    // cols
        fs.write[[char*]&type, sizeof[int]];        // type
        fs.write[[char*]&channels, sizeof[int]];    // channels
    
        // Data
        if [mat.isContinuous[]]
        {
            fs.write[mat.ptr[0], [mat.dataend - mat.datastart]];
        }
        else
        {
            int rowsz = CV_ELEM_SIZE[type] * mat.cols;
            for [int r = 0; r < mat.rows; ++r]
            {
                fs.write[mat.ptr[r], rowsz];
            }
        }
    }
    
    int main[]
    {
        // Save data
    
        {
            cv::Mat noisyImg = cv::imread["image.jpg"];
            //randu[m, 0, 5];
            matwrite["bin_file.bin", data];
        }
    return 0 ;
    }
    
    1

    This is image, inherits is small of it. nhưng bạn có thể thấy không có màu trắng

    int IMAGE_SIZE = 224;
    
    void matwrite[const string& filename, const Mat& mat]
    {
        ofstream fs[filename, fstream::binary];
    
        // Header
        int type = mat.type[];
        int channels = mat.channels[];
        fs.write[[char*]&mat.rows, sizeof[int]];    // rows
        fs.write[[char*]&mat.cols, sizeof[int]];    // cols
        fs.write[[char*]&type, sizeof[int]];        // type
        fs.write[[char*]&channels, sizeof[int]];    // channels
    
        // Data
        if [mat.isContinuous[]]
        {
            fs.write[mat.ptr[0], [mat.dataend - mat.datastart]];
        }
        else
        {
            int rowsz = CV_ELEM_SIZE[type] * mat.cols;
            for [int r = 0; r < mat.rows; ++r]
            {
                fs.write[mat.ptr[r], rowsz];
            }
        }
    }
    
    int main[]
    {
        // Save data
    
        {
            cv::Mat noisyImg = cv::imread["image.jpg"];
            //randu[m, 0, 5];
            matwrite["bin_file.bin", data];
        }
    return 0 ;
    }
    
    2____13
    int IMAGE_SIZE = 224;
    
    void matwrite[const string& filename, const Mat& mat]
    {
        ofstream fs[filename, fstream::binary];
    
        // Header
        int type = mat.type[];
        int channels = mat.channels[];
        fs.write[[char*]&mat.rows, sizeof[int]];    // rows
        fs.write[[char*]&mat.cols, sizeof[int]];    // cols
        fs.write[[char*]&type, sizeof[int]];        // type
        fs.write[[char*]&channels, sizeof[int]];    // channels
    
        // Data
        if [mat.isContinuous[]]
        {
            fs.write[mat.ptr[0], [mat.dataend - mat.datastart]];
        }
        else
        {
            int rowsz = CV_ELEM_SIZE[type] * mat.cols;
            for [int r = 0; r < mat.rows; ++r]
            {
                fs.write[mat.ptr[r], rowsz];
            }
        }
    }
    
    int main[]
    {
        // Save data
    
        {
            cv::Mat noisyImg = cv::imread["image.jpg"];
            //randu[m, 0, 5];
            matwrite["bin_file.bin", data];
        }
    return 0 ;
    }
    
    4

    This is new image. Bây giờ nó là một bảng kiểm tra

    int IMAGE_SIZE = 224;
    
    void matwrite[const string& filename, const Mat& mat]
    {
        ofstream fs[filename, fstream::binary];
    
        // Header
        int type = mat.type[];
        int channels = mat.channels[];
        fs.write[[char*]&mat.rows, sizeof[int]];    // rows
        fs.write[[char*]&mat.cols, sizeof[int]];    // cols
        fs.write[[char*]&type, sizeof[int]];        // type
        fs.write[[char*]&channels, sizeof[int]];    // channels
    
        // Data
        if [mat.isContinuous[]]
        {
            fs.write[mat.ptr[0], [mat.dataend - mat.datastart]];
        }
        else
        {
            int rowsz = CV_ELEM_SIZE[type] * mat.cols;
            for [int r = 0; r < mat.rows; ++r]
            {
                fs.write[mat.ptr[r], rowsz];
            }
        }
    }
    
    int main[]
    {
        // Save data
    
        {
            cv::Mat noisyImg = cv::imread["image.jpg"];
            //randu[m, 0, 5];
            matwrite["bin_file.bin", data];
        }
    return 0 ;
    }
    
    5
    int IMAGE_SIZE = 224;
    
    void matwrite[const string& filename, const Mat& mat]
    {
        ofstream fs[filename, fstream::binary];
    
        // Header
        int type = mat.type[];
        int channels = mat.channels[];
        fs.write[[char*]&mat.rows, sizeof[int]];    // rows
        fs.write[[char*]&mat.cols, sizeof[int]];    // cols
        fs.write[[char*]&type, sizeof[int]];        // type
        fs.write[[char*]&channels, sizeof[int]];    // channels
    
        // Data
        if [mat.isContinuous[]]
        {
            fs.write[mat.ptr[0], [mat.dataend - mat.datastart]];
        }
        else
        {
            int rowsz = CV_ELEM_SIZE[type] * mat.cols;
            for [int r = 0; r < mat.rows; ++r]
            {
                fs.write[mat.ptr[r], rowsz];
            }
        }
    }
    
    int main[]
    {
        // Save data
    
        {
            cv::Mat noisyImg = cv::imread["image.jpg"];
            //randu[m, 0, 5];
            matwrite["bin_file.bin", data];
        }
    return 0 ;
    }
    
    6
    int IMAGE_SIZE = 224;
    
    void matwrite[const string& filename, const Mat& mat]
    {
        ofstream fs[filename, fstream::binary];
    
        // Header
        int type = mat.type[];
        int channels = mat.channels[];
        fs.write[[char*]&mat.rows, sizeof[int]];    // rows
        fs.write[[char*]&mat.cols, sizeof[int]];    // cols
        fs.write[[char*]&type, sizeof[int]];        // type
        fs.write[[char*]&channels, sizeof[int]];    // channels
    
        // Data
        if [mat.isContinuous[]]
        {
            fs.write[mat.ptr[0], [mat.dataend - mat.datastart]];
        }
        else
        {
            int rowsz = CV_ELEM_SIZE[type] * mat.cols;
            for [int r = 0; r < mat.rows; ++r]
            {
                fs.write[mat.ptr[r], rowsz];
            }
        }
    }
    
    int main[]
    {
        // Save data
    
        {
            cv::Mat noisyImg = cv::imread["image.jpg"];
            //randu[m, 0, 5];
            matwrite["bin_file.bin", data];
        }
    return 0 ;
    }
    
    7
    int IMAGE_SIZE = 224;
    
    void matwrite[const string& filename, const Mat& mat]
    {
        ofstream fs[filename, fstream::binary];
    
        // Header
        int type = mat.type[];
        int channels = mat.channels[];
        fs.write[[char*]&mat.rows, sizeof[int]];    // rows
        fs.write[[char*]&mat.cols, sizeof[int]];    // cols
        fs.write[[char*]&type, sizeof[int]];        // type
        fs.write[[char*]&channels, sizeof[int]];    // channels
    
        // Data
        if [mat.isContinuous[]]
        {
            fs.write[mat.ptr[0], [mat.dataend - mat.datastart]];
        }
        else
        {
            int rowsz = CV_ELEM_SIZE[type] * mat.cols;
            for [int r = 0; r < mat.rows; ++r]
            {
                fs.write[mat.ptr[r], rowsz];
            }
        }
    }
    
    int main[]
    {
        // Save data
    
        {
            cv::Mat noisyImg = cv::imread["image.jpg"];
            //randu[m, 0, 5];
            matwrite["bin_file.bin", data];
        }
    return 0 ;
    }
    
    8
    int IMAGE_SIZE = 224;
    
    void matwrite[const string& filename, const Mat& mat]
    {
        ofstream fs[filename, fstream::binary];
    
        // Header
        int type = mat.type[];
        int channels = mat.channels[];
        fs.write[[char*]&mat.rows, sizeof[int]];    // rows
        fs.write[[char*]&mat.cols, sizeof[int]];    // cols
        fs.write[[char*]&type, sizeof[int]];        // type
        fs.write[[char*]&channels, sizeof[int]];    // channels
    
        // Data
        if [mat.isContinuous[]]
        {
            fs.write[mat.ptr[0], [mat.dataend - mat.datastart]];
        }
        else
        {
            int rowsz = CV_ELEM_SIZE[type] * mat.cols;
            for [int r = 0; r < mat.rows; ++r]
            {
                fs.write[mat.ptr[r], rowsz];
            }
        }
    }
    
    int main[]
    {
        // Save data
    
        {
            cv::Mat noisyImg = cv::imread["image.jpg"];
            //randu[m, 0, 5];
            matwrite["bin_file.bin", data];
        }
    return 0 ;
    }
    
    9

    Để viết một tệp nhị phân, trước tiên bạn phải chuyển đổi dữ liệu nhị phân thành kiểu dữ liệu

    int IMAGE_SIZE = 224;
    
    void matwrite[const string& filename, const Mat& mat]
    {
        ofstream fs[filename, fstream::binary];
    
        // Header
        int type = mat.type[];
        int channels = mat.channels[];
        fs.write[[char*]&mat.rows, sizeof[int]];    // rows
        fs.write[[char*]&mat.cols, sizeof[int]];    // cols
        fs.write[[char*]&type, sizeof[int]];        // type
        fs.write[[char*]&channels, sizeof[int]];    // channels
    
        // Data
        if [mat.isContinuous[]]
        {
            fs.write[mat.ptr[0], [mat.dataend - mat.datastart]];
        }
        else
        {
            int rowsz = CV_ELEM_SIZE[type] * mat.cols;
            for [int r = 0; r < mat.rows; ++r]
            {
                fs.write[mat.ptr[r], rowsz];
            }
        }
    }
    
    int main[]
    {
        // Save data
    
        {
            cv::Mat noisyImg = cv::imread["image.jpg"];
            //randu[m, 0, 5];
            matwrite["bin_file.bin", data];
        }
    return 0 ;
    }
    
    34 and transfer to that data type

    Sau đó, bạn cần chuyển đổi bằng phương pháp

    int IMAGE_SIZE = 224;
    
    void matwrite[const string& filename, const Mat& mat]
    {
        ofstream fs[filename, fstream::binary];
    
        // Header
        int type = mat.type[];
        int channels = mat.channels[];
        fs.write[[char*]&mat.rows, sizeof[int]];    // rows
        fs.write[[char*]&mat.cols, sizeof[int]];    // cols
        fs.write[[char*]&type, sizeof[int]];        // type
        fs.write[[char*]&channels, sizeof[int]];    // channels
    
        // Data
        if [mat.isContinuous[]]
        {
            fs.write[mat.ptr[0], [mat.dataend - mat.datastart]];
        }
        else
        {
            int rowsz = CV_ELEM_SIZE[type] * mat.cols;
            for [int r = 0; r < mat.rows; ++r]
            {
                fs.write[mat.ptr[r], rowsz];
            }
        }
    }
    
    int main[]
    {
        // Save data
    
        {
            cv::Mat noisyImg = cv::imread["image.jpg"];
            //randu[m, 0, 5];
            matwrite["bin_file.bin", data];
        }
    return 0 ;
    }
    
    35 sang nhị phân trước khi lưu.
    
    

    In [1]:

    #For data manipulations
    %pylab inline
    from IPython.display import set_matplotlib_formats
    from io import BytesIO
    import numpy as np
    
    #to compare to scipy's builtin conversions
    from scipy.misc import imsave, toimage
    
    #import pillow
    from PIL import Image
    
    set_cmap['Greys']
    
    0
    
    

    In [1]:

    #For data manipulations
    %pylab inline
    from IPython.display import set_matplotlib_formats
    from io import BytesIO
    import numpy as np
    
    #to compare to scipy's builtin conversions
    from scipy.misc import imsave, toimage
    
    #import pillow
    from PIL import Image
    
    set_cmap['Greys']
    
    1

    Viết trực tiếp vào Zipfiles

    
    

    In [1]:

    #For data manipulations
    %pylab inline
    from IPython.display import set_matplotlib_formats
    from io import BytesIO
    import numpy as np
    
    #to compare to scipy's builtin conversions
    from scipy.misc import imsave, toimage
    
    #import pillow
    from PIL import Image
    
    set_cmap['Greys']
    
    2____23

    Làm cách nào để chuyển đổi cấu hình ảnh thành nhị phân trong Python?

    Cách tiếp cận. Đọc hình ảnh từ vị trí. Là một hình ảnh màu có các lớp RGB trong đó và phức tạp hơn, hãy chuyển đổi nó sang dạng thang độ xám của nó trước tiên. Thiết lập một dấu ngưỡng, các pixel phía trên dấu đã cho sẽ chuyển sang màu trắng và điểm bên dưới sẽ chuyển sang màu đen

    Làm cách nào để chuyển một tệp thành nhị phân?

    Cách chuyển đổi tệp văn bản thành nhị phân. .

    Mở tệp văn bản trong Notepad

    Nhấn chuột phải vào văn bản được tô sáng và nhấp vào "Sao chép"

    Chuột phải vào bên trong hộp văn bản chuyển đổi nhị phân và nhấp vào "Dán". Văn bản từ tệp văn bản được dán vào hộp đầu vào chuyển đổi nhị phân

    Làm cách nào để mở một nhị phân hình ảnh trong Python?

    Hàm Open [] mở tệp ở định dạng văn bản theo mặc định. Để mở tệp ở định dạng nhị phân, hãy thêm 'B' vào tham số chế độ. Do đó, chế độ "RB" mở tệp ở định dạng nhị phân để đọc, trong khi chế độ "WB" mở tệp ở định dạng nhị phân để viết. thêm 'b' vào tham số chế độ . Do đó, chế độ "rb" mở tệp ở định dạng nhị phân để đọc, trong khi chế độ "wb" mở tệp ở định dạng nhị phân để ghi.

    File nhị phân trong Python là gì?

    Một tệp nhị phân là một tệp có nội dung ở định dạng nhị phân phân bao gồm một chuỗi tuần tự các byte, mỗi loại có chiều dài tám bit. Nội dung phải được giải thích bởi một chương trình hoặc bộ xử lý phần cứng hiểu trước chính xác cách nội dung được định dạng và cách đọc dữ liệu. tệp có nội dung ở định dạng nhị phân bao gồm một chuỗi byte liên tiếp, mỗi byte dài 8 bit

    Chủ Đề