How can I vectorize the following code?
조회 수: 1 (최근 30일)
이전 댓글 표시
main.m
I = imread('bear.png');
J = rgb2gray(imread('marked_bear.png'));
mask = I-J;
for r=1:size(I,1)
for c=1:size(I,2)
if(mask(r,c))
I = some_function(I, r, c);
end
end
end
imshow(I);
some_function.m
function I = some_function(I, r, c)
% some processing on image 'I'
I(r,c) = 255;
bear.png
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/166387/image.png)
marked_bear.png
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/166388/image.png)
댓글 수: 0
채택된 답변
Image Analyst
2017년 7월 28일
Like this:
I = imread('bear.png');
J = rgb2gray(imread('marked_bear.png'));
mask = I-J; % Weird, but okay...whatever.
I(mask~=0)=255;
댓글 수: 4
Image Analyst
2017년 7월 29일
OK, then...
function I = some_function(I, J)
mask = I - J;
I(mask ~= 0) = 255;
There. It's vectorized, still has a function, and that function is called "some_function" (instead of MaskI as I had called it), just like you asked for.
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!