이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
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!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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 (한국어)