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!

