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
编辑: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!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 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 (한국어)