Finding, accessing and replacing specific pixel values in image?

9 views (last 30 days)
I am working on a task and need help in syntax for finding specific pixel values,then accessing them and finally replace them.
I have implemented this in a long iterative fashion. But it is very cumbersome and if I need to make some changes in the assigned colors I have to make changes in a lot of places.
'I' is the input image a 527x527 logical indexed image. G is the true color output image of the same size that I am creating. Its code is the first block.
'totalLabels' is a vector of size 8,each value being unique, e.g. [1 4 14 24 56 77 110 120]. These are the labels that I have assigned to my input image 'I' and then I replace each of them with a color of my choosing in the new RGB image 'G'.
I also sort of brain stormed a little :-), and implemented a shorter version using 'find' and 'switch' commands. That is second block of code. But it gives an error.
G = zeros(w,l,3);
%Much longer iterative version
for rr = 1:w
for cc = 1:l
px = I(rr,cc);
if px ~= 0
if px==totalLabels(1)
G(rr,cc,1) = 1.0;
G(rr,cc,2) = 0;
G(rr,cc,3) = 0;
end
if px==totalLabels(2)
G(rr,cc,1) = 0;
G(rr,cc,2) = 1.0;
G(rr,cc,3) = 0;
end
if px==totalLabels(3)
G(rr,cc,1) = 0;
G(rr,cc,2) = 0;
G(rr,cc,3) = 1.0;
end
if px==totalLabels(4)
G(rr,cc,1) = 0.2;
G(rr,cc,2) = 0.8;
G(rr,cc,3) = 0.1;
end
if px==totalLabels(5)
G(rr,cc,1) = 0.3;
G(rr,cc,2) = 0.5;
G(rr,cc,3) = 0.1;
end
if px==totalLabels(6)
G(rr,cc,1) = 0.9;
G(rr,cc,2) = 0.8;
G(rr,cc,3) = 0.4;
end
if px==totalLabels(7)
G(rr,cc,1) = 0.4;
G(rr,cc,2) = 0.2;
G(rr,cc,3) = 0.1;
end
if px==totalLabels(8)
G(rr,cc,1) = 0.6;
G(rr,cc,2) = 0.6;
G(rr,cc,3) = 0.1;
end
end
end
end
figure;imshow(G);
%shorter crisp code with errors
G = zeros(w,l,3);
for xx = 1:numel(totalLabels)
[r,c] = find(I==totalLabels(xx));
switch xx
case 1
G(r,c) = 20;
case 2
G(r,c) = 55;
case 3
G(r,c) = 85;
case 4
G(r,c) = 100;
case 5
G(r,c) = 125;
case 6
G(r,c) = 140;
case 7
G(r,c) = 160;
case 8
G(r,c) = 180;
otherwise
G(r,c) = 0;
end
end
My question is this: Is there a crisper,shorter way of doing what I am trying to do?
Although the iterative approach works but it is very long. I have attached the Input labelled logical image 'I' and 'totalLabels' vector for any one interested in trying. ('w' and 'l' are found by [w,l,~] = size(I);)

Answers (1)

Brattv
Brattv on 17 Feb 2016
Edited: Brattv on 17 Feb 2016
Hi, try this code
% Loading data
G = load('image_plus_labels.mat');
% Image
image = G.I;
% Labels
totalLabels = G.totalLabels;
% Making a cell of colors
colors = {[1 0 0], [0 1 0], [0 0 1], [0.2 0.8 0.1], [0.3 0.5 0.1],...
[0.9 0.8 0.4], [0.4 0.2 0.1], [0.6 0.6 0.1]}
% Size of image
[w l] = size(image);
% Making the zero matrix
G = zeros(w,l,3);
for i=1:length(totalLabels)
% Finding the index of labels
indexOfInterest = find(image == totalLabels(i));
% Offset to access all dimensions.
indexOffset = 0;
for j=1:3
G(indexOfInterest+indexOffset) = colors{i}(j);
indexOffset = indexOffset + 277729;
end
end
imshow(G)
Notice that you can change the 277729 integer in indexOffset with the total amount of pixels in the image. I think
indexOffset = indexOffset + length(image(:))
might be a working alternative. Hope it gives you the result you were looking for.
  2 Comments
Hassan Javaid
Hassan Javaid on 18 Feb 2016
This generates a 3D image as output. Like real 3D with depth perception. I am looking for a planar RGB image.
Hassan Javaid
Hassan Javaid on 18 Feb 2016
Okay I tweaked the code. And got the result that I needed. And in good time also i.e. 0.138424 seconds. Your syntax for cell arrays helped.
% Making a cell of colors
colors = {[1 0 0], [0 1 0], [0 0 1], [0.2 0.8 0.1], [0.3 0.5 0.1],...
[0.9 0.8 0.4], [0.4 0.2 0.1], [0.6 0.6 0.1]};
for i=1:numel(totalLabels)
% Finding the index of labels
[rows,cols] = find(I == totalLabels(i));
% Iterating through indices to assign color.
len = numel(rows);
for j=1:len
G(rows(j),cols(j),:) = colors{i};
end
end

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!