How to extract odd values and even values from a 500x500 black image ?
조회 수: 2 (최근 30일)
이전 댓글 표시
I want to extract the even and odd numbers from 500x500 black matrix to turn them white without using loops or conditions statement.
댓글 수: 3
답변 (2개)
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)
mCopy = M;
oddIndexes = rem(mCopy, 2) == 1
oddNumbers = M(oddIndexes) % Extract the odd numbers.
% Turn the odd numbers to white (255)
mCopy(oddIndexes) = 255
evenIndexes = rem(mCopy, 2) == 0
evenNumbers = M(evenIndexes)
% Turn the even numbers to white (255)
mCopy = M; % Reset mCopy so there are no 255's in there anymore.
mCopy(evenIndexes) = 255
댓글 수: 0
DGM
2022년 3월 6일
편집: DGM
2022년 3월 6일
Consider the matrix:
A = randi([10 99],5,5)
% get elements from odd linear indices
oddelems = A(1:2:numel(A))
% get elements from even linear indices
evenelems = A(2:2:numel(A))
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),:)
% get elements from even rows
evenrows = A(2:2:size(A,1),:)
... 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(1:2:numel(B)) = 1 % set odd linear indices to 1
... and so on
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Computer Vision with Simulink에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!