Change Pixel in image - optimize code
이전 댓글 표시
Hi.
Im trying to loop through all the pixels of a thermal camera image i saved. i want to replace certain colors, lets say red with black pixels. the code below kinda works too, it just takes ages to complete (extremely long).
So my question is on how i could optimize it and make it all better. the function magic is the problem that takes so long
impath = "image.jpg";
image = imread(impath);
cropped = imcrop(image);
colors = [];
for i=1:size(cropped,1)
for a=1:size(cropped,2)
%disp("crop data: " + cropped(i, a, 1) + " " + cropped(i, a, 2) + " " + cropped(i, a, 3));
colors = [cropped(i, a, 1) + " " + cropped(i, a, 2) + " " + cropped(i, a, 3), colors];
end
end
colors = unique(colors);
image = magic(image, colors, [0, 0, 0]);
imshow(image);
function result = magic(image, rgb, target)
for i=1:size(image,1)
for a=1:size(image,2)
for b=1:size(rgb, 2)
t1 = reshape(uint8([str2num(rgb(b))]), 1, 1, 3);
t2 = reshape(image(i, a, :), 1, 1, 3);
if reshape(image(i, a, :), 1, 1, 3) == reshape(uint8([str2num(rgb(b))]), 1, 1, 3)
image(i, a, :) = target;
disp("yes");
end
end
end
end
result = image;
end
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Image Data Acquisition에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

