現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
How to change a matrix consisting of 3 elements into another matrix of 3 different elements?
1 回表示 (過去 30 日間)
古いコメントを表示
I have a 47x47 matrix. The matrix entries consist of the values 0, 96, and 255. All entries with the value of 255 I want to transform to the value of 0. All entries with the value of 96 I want to transform to the value of 3. All entries with the value of 0 I want to transform to the value of 1. Does anyone how I could do this easily?
採用された回答
Ameer Hamza
2020 年 11 月 8 日
編集済み: Ameer Hamza
2020 年 11 月 8 日
Try this
A; % 47x47 matrix
A_new = zeros(size(A));
A_new(A==255) = 0;
A_new(A==96) = 3;
A_new(A==0) = 1;
Read about logical indexing: https://www.mathworks.com/company/newsletters/articles/matrix-indexing-in-matlab.html
21 件のコメント
Borys Bulka
2020 年 11 月 8 日
Hey! Thank you for your answer but It tells me "Error: Incorrect use of '=' opperation. To assign a value to a variable, use '='. To compare values for equality, use '=='.
Borys Bulka
2020 年 11 月 8 日
Oh but it does work when I write it as a code and not in the command window!
Borys Bulka
2020 年 11 月 8 日
編集済み: Borys Bulka
2020 年 11 月 8 日
However, now that I have this matrix A and have another matrix B (also 47x47). It doesnt let me create a matrix C=A+B. It says: 'Intergers can only be combined with integers of the same class, or scalar doubles.'
Borys Bulka
2020 年 11 月 8 日
I did confirm that they are both equal size with the size function. I will copy and past the code in a second.
Borys Bulka
2020 年 11 月 8 日
A=imread('cm_young.png');
B=imresize(A, [611 611]);
C=[B([7:13:605],[7:13:605])];
am_young=zeros(size(C));
am_young(C==255)=0;
am_young(C==96)=0.5;
am_young(C==0)=1;
D=imread('cm_chung.png');
E=imresize(D, [517 517]);
am_chung=[E([6:11:512],[6:11:512])];
[r,c]=size(am_chung);
for i=1:r
for j=1:c
if am_chung(i,j)<180
am_chung(i,j)=1;
else
am_chung(i,j)=0
end
end
end
am_new=am_young+am_chung
Borys Bulka
2020 年 11 月 8 日
Everything works as it supposed to work, expect I get an error at an_new. It doesn't calculate am_new for me.
Borys Bulka
2020 年 11 月 8 日
I changed the 3 to a 0.5 by the way, since I see it must be a 0.5 instead of a 3.
Borys Bulka
2020 年 11 月 8 日
It says that am_young is a class: double and am_chung is a class: uint8. Is it because of that? I am not very experienced with Matlab so I don't know all those things that are mentioned there.
Ameer Hamza
2020 年 11 月 8 日
Ok. I get it. You are mixing two things here. By default, MATLAB loads images in unit8 format in which the pixel intensities vary from 0 to 255. It appears that you want the image intensities in range 0 to 1. Although there are several ways to handle this situation, However, I recommend to do everything by considering that pixel intensity vary from 0 to 255
A=imread('cm_young.png');
B=imresize(A, [611 611]);
C=[B([7:13:605],[7:13:605])];
am_young=zeros(size(C));
am_young(C==255)=0;
am_young(C==96)=0.5*255;
am_young(C==0)=1*255;
am_young = uint8(am_young);
D=imread('cm_chung.png');
E=imresize(D, [517 517]);
am_chung=[E([6:11:512],[6:11:512])];
[r,c]=size(am_chung);
for i=1:r
for j=1:c
if am_chung(i,j)<180
am_chung(i,j)=255;
else
am_chung(i,j)=0
end
end
end
am_new=am_young+am_chung
Borys Bulka
2020 年 11 月 8 日
編集済み: Borys Bulka
2020 年 11 月 8 日
it does calculate the am_new matrix then but the values then aren't as they supposed to be. Becuase now the values 255 and 128 are in the matrix am_new. While the am_new matrix should only contain values 0, 0.5, and 1 after adding the am_chung matrix to the am_young matrix.
Borys Bulka
2020 年 11 月 8 日
編集済み: Borys Bulka
2020 年 11 月 8 日
When doing 0.5*255, the value becomes 128, but this value must remain 0.5. When doing 1*255, the value becomes 255, but this value must remain 1.
Borys Bulka
2020 年 11 月 8 日
編集済み: Borys Bulka
2020 年 11 月 8 日
When i read the images first and used different forms of thresholds and or transformation I could normally just add the two matrices up. But when using this kind of transformation:
am_young=zeros(size(C));
am_young(C==255)=0;
am_young(C==96)=0.5*255;
am_young(C==0)=1*255;
am_young = uint8(am_young);
It somehow changed the class of the matrix such that I could not add them up anymore.
Ameer Hamza
2020 年 11 月 8 日
What does the value of 1 represent in your code? Does it mean maximum brightness of pixel? For a unit8 image, value of 1 is equivalent to 255. If you look at the values in matrix 'C'. You will see they do not vary from 0 to 1, they vary from 0 to 255.
Borys Bulka
2020 年 11 月 8 日
編集済み: Borys Bulka
2020 年 11 月 8 日
cm_young is an image of a black grey white coloured 47x47 connectivity matrix with black, grey and white squares. I read this image in matlab and resized it such that I could take each middle pixel of each square in the 47x47 connectivity matrix image. a black pixel has in matlab a value of 0 and thus I wanted to transform every 0 to a 1. every grey pixel has a value of 96, so I want to transform that to a 0.5. Every white pixel has a value of 255, so I wanted to transform that to a value of 0. Doing this I should receive a adjacency matrix that was used to construct the original image (am_young).
So this matrix:
A=imread('cm_young.png');
B=imresize(A, [611 611]);
C=[B([7:13:605],[7:13:605])];
am_young=zeros(size(C));
am_young(C==255)=0;
am_young(C==96)=0.5*255;
am_young(C==0)=1*255;
am_young = uint8(am_young);
does not give me the same output when using:
A=imread('cm_young.png');
B=imresize(A, [611 611]);
C=[B([7:13:605],[7:13:605])];
am_young=zeros(size(C));
am_young(C==255)=0;
am_young(C==96)=0.5;
am_young(C==0)=1;
Ameer Hamza
2020 年 11 月 8 日
Oh! If you wanted to do that then you can directly use im2double()
A=imread('cm_young.png');
B=imresize(A, [611 611]);
C=[B([7:13:605],[7:13:605])];
C=im2double(C);
it will automatically scale the value between 0 and 1. Similarly, you can do the same for
am_chung=[E([6:11:512],[6:11:512])];
am_chung=im2double(am_chung);
However, it will linearly map values between 0 to 1. If you must want 96 at 0.5 then try the following code
A=imread('cm_young.png');
B=imresize(A, [611 611]);
C=[B([7:13:605],[7:13:605])];
am_young=zeros(size(C));
am_young(C==255)=0;
am_young(C==96)=0.5;
am_young(C==0)=1;
D=imread('cm_chung.png');
E=imresize(D, [517 517]);
am_chung=[E([6:11:512],[6:11:512])];
am_chung_ = zeros(size(am_chung));
[r,c]=size(am_chung);
for i=1:r
for j=1:c
if am_chung(i,j)<180
am_chung_(i,j)=1;
else
am_chung_(i,j)=0
end
end
end
am_new=am_young+am_chung_
Borys Bulka
2020 年 11 月 8 日
I used the last code you wrote and it work! Thank you very much for your patients and help I appreciate it a lot! I am however a bit frustrated with that I do not understand why this works and my first attempt did not work. If you'd have the will still to be willing to explain that in baby steps I would highly appreciate it since I'd like to understand. Or perhaps know a link that would explain that better for me? If not, this is perfectly fine! :)
Borys Bulka
2020 年 11 月 8 日
So I can the only difference was to add the zeros(size()) part. Why did that change it so that I then could add them up and beforehand I couldn't?
Ameer Hamza
2020 年 11 月 8 日
Here is a brief explanation.
These lines
A=imread('cm_young.png');
B=imresize(A, [611 611]);
C=[B([7:13:605],[7:13:605])];
load image as uint8 matrix, in which the pixel value change from 0 to 255. Note that uint8 can only save integer values, i.e., you cannot save 0.5. But then you run the line
am_young=zeros(size(C));
By default, zeros() function contain 'double' datatype matrix, i.e., it can save fractional values, therefore, following lines work properly
am_young(C==255)=0;
am_young(C==96)=0.5;
am_young(C==0)=1;
Then again you load the image
D=imread('cm_chung.png');
E=imresize(D, [517 517]);
am_chung=[E([6:11:512],[6:11:512])];
and it again create uint8 matrix. But when you run
am_chung_ = zeros(size(am_chung));
it create am_chung_ with double datatype. am_chung_ and am_young both have double datatype, therefore, the code works properly.
Borys Bulka
2020 年 11 月 8 日
Thank you very much for your time and effort! I appreciate it alot, it makes much more sense to me know!
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)