필터 지우기
필터 지우기

B&W Image to Excel File with 0 and 1

조회 수: 5 (최근 30일)
Sam Schiavitti
Sam Schiavitti 2021년 9월 9일
댓글: Walter Roberson 2021년 9월 13일
Hello, I want to take a B&W image and convert it to an excel file with 0 and 1. Essentially it will put the image into excel labeling all black areas as a 1 and all white areas as a 0 making the image in excel. I am not sure how'd I go about doing that.

채택된 답변

Walter Roberson
Walter Roberson 2021년 9월 10일
편집: Walter Roberson 2021년 9월 10일
YourImage = imread('TheFileNameGoesHere.jpg');
if ndims(YourImage) > 2; YourImage = rgb2gray(YourImage); end
BW = imbinarize(YourImage);
writematrix(double(BW), 'NameOfExcelFileGoesHere.xlsx')
  댓글 수: 4
Sam Schiavitti
Sam Schiavitti 2021년 9월 13일
Thank you so much! I have one last question if you dont mind. But if I wanted to swap the 0's and 1's ot be vice versa how would I do that?
Walter Roberson
Walter Roberson 2021년 9월 13일
~BW

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Dyuman Joshi
Dyuman Joshi 2021년 9월 10일
편집: Dyuman Joshi 2021년 9월 10일
1) Use imread to read the image. The values in the uint8 (default format) array will be from 0 to 255.
2) Divide the array by 255 to get values from 0 to 1.
3) The matrix will be 3D matrix, which you won't be able to convert to a excel file. Either you can reshape the matrix to a 2D matrix and then write or you can write three files/sheets as you wish (Sheets).
  댓글 수: 2
Walter Roberson
Walter Roberson 2021년 9월 10일
If the image is truly black and white, then the matrix will not be 3D.
However, it is common for grayscale images (especially jpeg) to be represented as RGB images that happen to have all three color planes the same.
Instead of worrying about 3D under the circumstances,
YourImage = imread(TheFileNameGoesHere);
if ndims(YourImage) > 2; YourImage = rgb2gray(YourImage); end
Now you can count on YourImage not being 3D.
Dyuman Joshi
Dyuman Joshi 2021년 9월 10일
I was going to make that distinction in the answer initially, but I did not and linked the imread webpage (where the specifics related to this are mentioned) hoping that if OP might comment/ask again, I can reply with the reference.
Due to the 2nd point you mentioned, I presumed that the matrix will (most probably) be a 3D matrix. However, your answer here is more fitting in the general sense (as always).

댓글을 달려면 로그인하십시오.

제품


릴리스

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by