필터 지우기
필터 지우기

How to extract odd values and even values from a 500x500 black image ?

조회 수: 2 (최근 30일)
Maha Almuaikel
Maha Almuaikel 2022년 3월 6일
편집: DGM 2022년 3월 6일
I want to extract the even and odd numbers from 500x500 black matrix to turn them white without using loops or conditions statement.
  댓글 수: 3
Maha Almuaikel
Maha Almuaikel 2022년 3월 6일
pardon me I was not clear, I mean the odd and even index from the zeros matrix
Image Analyst
Image Analyst 2022년 3월 6일
You're still not clear. Zero is neither an odd number or an even number.

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

답변 (2개)

Image Analyst
Image Analyst 2022년 3월 6일
편집: Image Analyst 2022년 3월 6일
Do you have any other numbers other than 0? If so you can use rem(M, 2). Observe:
M = magic(5)
M = 5×5
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
mCopy = M;
oddIndexes = rem(mCopy, 2) == 1
oddIndexes = 5×5 logical array
1 0 1 0 1 1 1 1 0 0 0 0 1 0 0 0 0 1 1 1 1 0 1 0 1
oddNumbers = M(oddIndexes) % Extract the odd numbers.
oddNumbers = 13×1
17 23 11 5 1 7 13 19 25 21
% Turn the odd numbers to white (255)
mCopy(oddIndexes) = 255
mCopy = 5×5
255 24 255 8 255 255 255 255 14 16 4 6 255 20 22 10 12 255 255 255 255 18 255 2 255
evenIndexes = rem(mCopy, 2) == 0
evenIndexes = 5×5 logical array
0 1 0 1 0 0 0 0 1 1 1 1 0 1 1 1 1 0 0 0 0 1 0 1 0
evenNumbers = M(evenIndexes)
evenNumbers = 12×1
4 10 24 6 12 18 8 14 20 2
% Turn the even numbers to white (255)
mCopy = M; % Reset mCopy so there are no 255's in there anymore.
mCopy(evenIndexes) = 255
mCopy = 5×5
17 255 1 255 15 23 5 7 255 255 255 255 13 255 255 255 255 19 21 3 11 255 25 255 9

DGM
DGM 2022년 3월 6일
편집: DGM 2022년 3월 6일
Consider the matrix:
A = randi([10 99],5,5)
A = 5×5
37 23 33 22 34 58 58 16 79 86 11 39 73 93 97 11 37 92 79 19 32 19 47 48 85
% get elements from odd linear indices
oddelems = A(1:2:numel(A))
oddelems = 1×13
37 11 32 58 37 33 73 47 79 79 34 97 85
% get elements from even linear indices
evenelems = A(2:2:numel(A))
evenelems = 1×12
58 11 23 39 19 16 92 22 93 48 86 19
That's linear indices. Maybe you want to address odd or even rows/columns:
% get elements from odd rows
oddrows = A(1:2:size(A,1),:)
oddrows = 3×5
37 23 33 22 34 11 39 73 93 97 32 19 47 48 85
% get elements from even rows
evenrows = A(2:2:size(A,1),:)
evenrows = 2×5
58 58 16 79 86 11 37 92 79 19
... and so on for columns
EDIT: I'm dumb. I forgot you wanted to set pixels to white.
The above still holds. You're still trying to address your array. Just apply that to an assignment:
B = zeros(5)
B = 5×5
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
B(1:2:numel(B)) = 1 % set odd linear indices to 1
B = 5×5
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
... and so on

카테고리

Help CenterFile Exchange에서 Computer Vision with Simulink에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by