In matlab, getting negative of an image is fairly easy. Though matlab should has some functions to do that, we'll try to write that code ourself. Here, our aim is to convert black colors to white and vice versa in a gray scale image.
I usually don't read the image in the code itself, but I give it to the code. So, A is the input image and B will be the output in the following code. If you don't remember how to read an image into a matrix, you could look at the "imread" function. So, let's look at the code.
function [B] = ImageNegative(A)
A = double(A);
[row,cols] = size (A);
L = 255;
for k = 1 : row
for l = 1 : cols
%Image is made negative here
% by substracting from L
B(k,l) = (L-A(k,l));
end
end
end
First, I convert the input image to double because when you read an image matlab will load it as 8 bit unsigned integer therefore, you need to convert it to double image to use it in matrix operations. Then, we need to find the size of the image in order to use in for loops. Inside these for loops, all gray scale pixel values are substracted from L which is the value of white. As a result, the output image B's pixel values will be converted to negative.

As a result, the breast image that is taken from Digital Image Processing (Gonzalez & Woods) book is converted to its negative, so the tumor in the breast is much more clear now.
No comments:
Post a Comment