how to get the same contrast in an image?

Hi! I have image o.png which shows a difference in the contrast of the pixels, there is like a "line" that divide two different parts. I tried with histeq but I don't get any results. Any suggestions? Thank you

9 Comments

The contrast can be adjusted using Image viewer App in MATLAB. The step by step procedure can be found here Adjust Image Contrast in Image Viewer App - MATLAB & Simulink (mathworks.com)
Thank you, I'll try.
I tried also with:
%convert the indexed image to grayscale
I_prova_1 = ind2gray(immagine_1,gray);
J_1 = imadjust(I_prova_1,[0.3 0.5],[0.2 0.9]);
I have done some tests and I have undestood the meaning of low_out and high_out. They are the limits of the new histogram(x axis in histogram.PNG), but what about low_in and high_in? How this function work? I have already seen the official page but I didn't get the meaning.
The values low_in and high_in refer to the intensity values in the input image which you want to map to low_out and high_out in the transformed output image. In the example intensites with value 0.3 in old image will be mapped to be 0.2 and similarly 0.5 will be mapped to 0.9 in the transformed image
Ok. And are the image viewer app and imadjust equivalent?
When the intensities which we want to vary are known beforehand we can use imadjust function. Otherwise image viewer app can be used
Sorry @Abhishek Kolla, I have another question. How could I stop my code until I have modified my image with the App?
immagine_gray = ind2gray(immagine,gray);
immagine_gray_poco_contrasto=immagine_gray;
imtool(immagine_gray)
%HERE
close all
figure(1)
subplot(1,2,1)
imhist(immagine_gray_poco_contrasto)
title('Istogramma immagine poco contrasto')
subplot(1,2,2)
imhist(immagine_gray)
title('Istogramma immagine molto contrasto')
The step by step process to save and export the final modified image can be found here Get Started with Image Viewer App - MATLAB & Simulink (mathworks.com)
Taking the histogram or increasing contrast with histogram equalization or imadjust() will not help you with finding the line. Just think about it.
I'd try thresholding and then do some cleanup with bwareafilt() and sgolayfilt(). I'll give code later today if I have time.

Sign in to comment.

Answers (2)

sir,may be use the hist vector, such as
J = histeq(I,hgram)

1 Comment

In the above function hgram refers to target histogram. We can use it when the histogram of output image has to approximately match target histogram hgram

Sign in to comment.

To find the left edge of that bright strip, try this:
% Demo by Image Analyst
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;
markerSize = 40;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
fileName = 'o.PNG';
grayImage = imread(fileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Extract the blue channel (so the magenta lines will be white).
grayImage = grayImage(:, :, 3);
end
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
impixelinfo;
axis('on', 'image');
title('Original Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
hold on
drawnow;
% Maximize window.
g = gcf;
g.WindowState = 'maximized'
drawnow;
% Get rid of white frame.
frame = grayImage == 240;
grayImage(frame) = 0;
subplot(2, 2, 2);
imhist(grayImage);
grid on;
low = 104;
high = 255;
% [low, high] = threshold(low, high, grayImage)
mask = grayImage > low;
% Take biggest blob
mask = bwareafilt(mask, 1);
% Fill holes.
mask = imfill(mask, 'holes')
% Display the image.
subplot(2, 2, 3);
imshow(mask, []);
impixelinfo;
axis('on', 'image');
title('Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
hold on
drawnow;
% Scan down getting left edge of bright strip.
leftEdges = nan(rows, 1);
hold on;
for row = 1 : rows
t = find(mask(row, :), 1, 'first');
if ~isempty(t)
leftEdges(row) = t;
plot(t, row, 'r.', 'MarkerSize', 8);
end
end
subplot(2, 2, 4);
plot(leftEdges, 'b-', 'LineWidth', 2);
grid on;
title('Left Edges', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('Row', 'FontSize', fontSize, 'Interpreter', 'None');
ylabel('Column of left edge', 'FontSize', fontSize, 'Interpreter', 'None');
hold on
drawnow;

Asked:

on 17 Oct 2021

Answered:

on 12 Nov 2021

Community Treasure Hunt

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

Start Hunting!