Change Pixel in image - optimize code
Show older comments
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
Accepted Answer
More Answers (0)
Categories
Find more on Image Data Acquisition in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
