Change Pixel in image - optimize code

3 views (last 30 days)
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

Image Analyst
Image Analyst on 19 Dec 2022
Try the Color Thresholder on the Apps tab of the Tool ribbon. Use LAB color space.
% Converts RGB image to LAB colorspace, then changes red to black.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox'); % license('test','Statistics_toolbox'), license('test','Signal_toolbox')
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
%===============================================================================
% Read in a standard MATLAB color demo image.
folder = fileparts(which('peppers.png')); % Determine where demo folder is (works with all versions).
baseFileName = 'peppers.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorChannels] = size(rgbImage);
% Display the original color image.
subplot(2, 1, 1);
imshow(rgbImage);
axis on;
title('Original Color Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Mask out the red pixels.
[mask, maskedRGBImage] = createMask(rgbImage);
% Display the masked image where red is now black.
subplot(2, 1, 2);
imshow(maskedRGBImage);
hp = impixelinfo();
title('Masked Image', 'FontSize', fontSize, 'Interpreter', 'None');
%=================================================================================================
function [BW,maskedRGBImage] = createMask(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the colorThresholder app. The colorspace and
% range for each channel of the colorspace were set within the app. The
% segmentation mask is returned in BW, and a composite of the mask and
% original RGB images is returned in maskedRGBImage.
% Auto-generated by colorThresholder app on 19-Dec-2022
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2lab(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 1.844;
channel1Max = 100.000;
% Define thresholds for channel 2 based on histogram settings
channel2Min = -56.604;
channel2Max = 16.859;
% Define thresholds for channel 3 based on histogram settings
channel3Min = -64.147;
channel3Max = 94.387;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end
  5 Comments
Image Analyst
Image Analyst on 22 Dec 2022
Can you attach those two images?
Marcel
Marcel on 2 Mar 2023
Hi im sorry im not in the office for the next 6 months, therefore dont have my company computer and no matlab license as well as the images etc :c

Sign in to comment.

More Answers (0)

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!