Matlab Image Processing: Widening Intensity Range using Transfer Function

In summary, the conversation discusses using a simple transfer function in Matlab to widen the intensity range of an image with a narrow intensity histogram. The suggested approach is to use a linear function to map the minimum and maximum intensity values to 0 and 255, respectively, and then apply the function to each individual pixel in the image. The code provided uses the double command to convert the pixel values to double precision and then loops through each pixel to apply the transfer function and create a new image with the desired intensity range.
  • #1
foobag
75
0
I have to take an image with a narrow intensity histogram and widen its intensity range using a simple transfer function that scales the dominant histogram intensity range [I_min, I_max] to the range [0, 255].

They tell me to tuse a linear function that maps I_min to 0 and I_max to 255 and all intensites I within that range to 255*(I-I_min)/(I_max - I_min).

The problem I am having is how to make it so that the function reads every individual pixel of the image and transforms it to a new pixel value. I am stuck at this part.

Please help since it is my first time using matlab.

Here is my code so far:

function [ output_args ] = imagehisto(image)
%Function that maps pixel values using a transfer function from 0 to 255 if
%it is out of ranage.
% Input of an image, reading each pixel and transferring it to a new
% pixel value.

pic = imread(image);
colormap(gray);
pic2 = rgb2gray(pic);

I_max = double(max(pic2(:)));
I_min = double(min(pic2(:)));


x = (0:1:255)
x = pic2
y(I_min+1 : I_max+1) = (255*(x(I_min+1 : I_max+1) - I_min)/(I_max - I_min))
y(1 :I_min) = 0
y(I_max+2 : 256) = 255

imagesc(pic3);

%plot(x,y);
%out_image = transfer_function(pic2);

end
 
Physics news on Phys.org
  • #2
First I will admitt it has been a while since I have done image analysis with Matlab but what I think you are asking for is something like this. I'm not familiar with the double command so I'll just assume it is used correctly. Oh and don't use image as an imput and image is a command in matlab.

function [ output_args ] = imagehisto(pic)
I_max = double(max(pic(:)));
I_min = double(min(pic(:)));
[M,N]=size(pic);
for a=1:M
for b=1:N
pic_new(a,b)=255*(pic(a,b)-I_min)/(I_max-I_min);
end
end
imagesc(pic_new)

I used my own dummy data and I got it so change to the range of 0 to 255, so this should work for you.
 
  • #3


Hello,

Thank you for sharing your code and explaining your problem. It seems like you have a good understanding of the concept behind the transfer function and how it works. However, there are a few things that can be improved in your code to make it more efficient and easier to read.

Firstly, instead of using a for loop to iterate through each pixel, you can use the built-in function "imadjust" in MATLAB to apply the transfer function to the entire image at once. This will save you time and make your code more concise. Here's an example of how you can use it:

pic2 = imread(image); % read the image
pic3 = imadjust(pic2, [I_min/255, I_max/255], [0, 1]); % apply the transfer function to the image using the min and max values
imshow(pic3); % display the new image

Another thing to note is that you don't need to convert your image to grayscale using the "rgb2gray" function. The "imread" function already reads the image as a grayscale image by default.

Lastly, you can use the "min" and "max" functions in MATLAB to find the minimum and maximum values in an image instead of using "min" and "max" functions. This will also save you time and make your code more efficient.

I hope this helps and good luck with your project!
 

1. How does a transfer function widen the intensity range of an image in Matlab?

A transfer function in Matlab is a mathematical function that maps the input intensity values of an image to a new set of output intensity values. By adjusting the values of the transfer function, the range of intensities in the image can be expanded or compressed, allowing for a wider range of intensities to be displayed.

2. Can the transfer function be used to adjust the contrast of an image in Matlab?

Yes, the transfer function can be used to adjust the contrast of an image in Matlab. By mapping a narrow range of input intensities to a wider range of output intensities, the image's contrast can be enhanced, making it easier to distinguish between different shades of gray.

3. How can I create a transfer function in Matlab?

To create a transfer function in Matlab, you can use the imadjust function. This function allows you to specify the input and output intensity ranges, as well as the mapping function, to create a custom transfer function. Alternatively, you can also use the stretchlim function to automatically generate a transfer function based on the image's histogram.

4. Is it possible to apply a transfer function to only a specific part of an image in Matlab?

Yes, it is possible to apply a transfer function to only a specific part of an image in Matlab. This can be achieved by first creating a mask or selection of the desired area, and then using the imadjust function with the mask parameter to apply the transfer function only to the selected area.

5. Can I undo the effects of a transfer function on an image in Matlab?

Yes, you can undo the effects of a transfer function on an image in Matlab by simply applying the inverse transfer function. This can be done by using the imadjust function with the inversetransfer parameter, which will reverse the mapping of input and output intensities.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
7K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
5K
  • Linear and Abstract Algebra
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
9K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Back
Top