Tôi muốn lưu một hình ả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 ++.
Dưới đây là các mã tôi đã sử dụng để lưu hình ảnh vào tệp bin trong Python và C ++ nhưng tôi có kết quả khác nhau khi tôi so sánh hai tệp .bin.
Python
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 hình ảnh RGB trong cả C ++ và Python vào tệp nhị phân [.
Thanks.
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luậnOpenCV. Make sure you have installed the library into your Python. For steps for installing OpenCV refers to this article: Set up Opencv with anaconda environment
Approach:
- Trong bài viết này, chúng tôi sẽ chuyển đổi hình ảnh thà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ũng được gọi là cấp độ 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.
- Thư viện quan trọng nhất cần thiết để xử lý hình ả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 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
- Đọ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 màu trắng và bên dưới điểm sẽ chuyển sang màu đen.
Dưới đây là việc 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
2789
In [2]:
checkerboard = np.mod[np.arange[49].reshape[7,7],2].astype['bool']
0
In [2]:
checkerboard = np.mod[np.arange[49].reshape[7,7],2].astype['bool']
1
Populating the interactive namespace from numpy and matplotlib
2
In [2]:
checkerboard = np.mod[np.arange[49].reshape[7,7],2].astype['bool']
3Output:
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
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']
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 để làm 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
In [2]:
checkerboard = np.mod[np.arange[49].reshape[7,7],2].astype['bool']
In [3]:
matshow[checkerboard]
Out[3]:
In [4]:
#whats the dtype?
checkerboard.dtype
Out[4]:
dtype['bool']
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 ;
}
0int 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Đây là hình ảnh, thừa nhận là nhỏ của nó: 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 ;
}
2int 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 ;
}
3int 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Đây là hình ảnh mới: 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 ;
}
5int 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 ;
}
6int 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 ;
}
7int 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 ;
}
8int 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 cần chuyển đổi dữ liệu nhị phân thành kiểu dữ liệu
In [2]:
checkerboard = np.mod[np.arange[49].reshape[7,7],2].astype['bool']
4 và chuyển sang kiểu dữ liệu đó.Sau đó, bạn cần chuyển đổi bằng phương pháp
In [2]:
checkerboard = np.mod[np.arange[49].reshape[7,7],2].astype['bool']
5 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']
1Viế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
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']
3
Làm cách nào để chuyển đổi 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 màu trắng và bên dưới điểm sẽ chuyển sang màu đen.
Làm cách nào để chuyển đổi 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ấp chuột phải vào văn bản được tô sáng và nhấp vào "Sao chép". Nhấ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 hình ảnh nhị phân trong Python?
Hàm Open [] mở một tệp ở định dạng văn bản theo mặc định.Để mở một tệp ở định dạng nhị phân, 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.add 'b' to the mode parameter. Hence the "rb" mode opens the file in binary format for reading, while the "wb" mode opens the file in binary format for writing.
Tệp 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 bao gồm một loạt các byte tuần tự, 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.a file whose content is in a binary format consisting of a series of sequential bytes, each of which is eight bits in length. The content must be interpreted by a program or a hardware processor that understands in advance exactly how that content is formatted and how to read the data.