Histogram equalization function - matlab

I'm trying to implement the histogram equalization using this code:
clc
A=input('please enter image adress','s');
Iimg=imread(A);
Iimg1=double(Iimg);
histi=imhist(Iimg);
mmax=max(Iimg1(:));
h=histi/numel(Iimg1)
cdf=cumsum(h)
cdf=cdf*double(mmax);
c=uint8(cdf);
subplot(1,3,1)
bar(c)
subplot(1,3,2)
imhist(Iimg)
subplot(1,3,3)
imhist(histeq(Iimg))
Is my code wrong?
I don't get expected results.

I get this from your code:
What is wrong? unless your image is rgb, the code works (I used cameraman).

i find correct code for this and write here for others
clc
A=input('please enter image adress: ','s');
GIm=imread(A);
[x, y ,m]=size(GIm);
if m==3
GIm=rgb2gray(GIm);
end
inf=whos('GIm');
Isize=0;
if inf.class=='uint8'
Isize=256;
else if inf.class=='uint68'
Isize=65565;
end
end
HIm=uint8(zeros(size(GIm,1),size(GIm,2)));
freq=zeros(256,1);
probf=zeros(256,1);
probc=zeros(256,1);
cum=zeros(256,1);
output=zeros(256,1);
freq=imhist(GIm);%histogram
sum=0;
no_bins=255;
probc=cumsum(freq)/numel(GIm);
output=round(probc*no_bins);
HIm(:)=output(GIm(:)+1);
%show
figure
subplot(2,2,1)
imshow(GIm);
title('original image');
subplot(2,2,2)
imshow(HIm);
title('Image equalization');
subplot(2,2,3)
imhist(GIm);
title('origina');
subplot(2,2,4)
imhist(HIm);
title('histogram equalization');
figure
subplot(2,2,1)
imshow(histeq(GIm));
title('matlab equalization');
subplot(2,2,2)
imshow(HIm);
title('my code equalization');
subplot(2,2,3)
imhist(histeq(GIm));
title('hist matlab eq');
subplot(2,2,4)
imhist(HIm);
title('hist my code eq');
figure
subplot(2,2,1)
imshow(GIm);
title('origina');
subplot(2,2,2)
imshow(HIm);
title('Image equalization');
x=(1:Isize);
subplot(2,2,3)
plot(x,output);
title('transform function');
subplot(2,2,4)
plot(x,freq);
title('transform function');

Related

How can I color all the first bar by the same color, and the second color by another in subplot?

I want to create a subplot, I have 6 figures, in each figure I have 3 bars, I want to color all the first bars by the same color and add a legend. please help me, this is the code:
x1=[1;2;3]
y1 = [91,25,3];
y2 = [71,22,13];
y3 = [81,22,33];
y4 = [71,12,63];
y5 = [61,42,23];
y6 = [51,21,23];
figure;
subplot(3,2,1);
b1=bar(x1,y1);
ylabel('Cost per Byte (%)');
ylim([0 max(y1)+10]);
title('Aaa');
subplot(3,2,2);
b2=bar(x1,y2);
ylabel('Security (%)');
ylim([0 max(y2)+10]);
title('Second plot');
subplot(3,2,3);
b3=bar(x1,y3);
ylabel('Data rate (kbps)');
ylim([0 max(y3)+10]);
title('Third plot');
subplot(3,2,4);
b4=bar(x1,y4);
ylabel('Delay (ms)');
ylim([0 max(y4)+10]);
title('Fourth plot');
subplot(3,2,5);
b5=bar(x1,y5);
ylabel('Jitter (ms)');
ylim([0 max(y5)+10]);
title('Fifth plot');
subplot(3,2,6);
b6=bar(x1,y6);
ylabel('Loss ratio 10^6 (ms)');
ylim([0 max(y6)+10]);
title('Sixth plot');
legend('peaks');
set(b1,'FaceColor','red');
I want colorate all the first bars by a color and add a legend like the picture :
Please help me.
You will have to access the Bar's CData per subplots as shown here:
https://www.mathworks.com/help/matlab/ref/bar.html
Example:
b1.CData(2,:) = [0.5 0 0.5]; % With the RGB code.

Matlab Countour Plot with trace

How can I plot the following data onto a contour instead?
close all;
clear all;
clc;
syms x
f=#(x) -100.*(x(2)-x(1).^2).^2+ (1-x(1)).^2;
x0=[-9, 1];
options=optimset('display', 'iter', 'TolX',1e-6);
%options = optimset('PlotFcns',#optimplotfval);
x=fminsearch(f, x0, options)
a= %need help pulling this from fminsearch [a,b,c]=fminsearch(f,x0,options] ???
b=%need help pulling this from fminsearch
fcontour(f, 'Fill', 'On');
hold on;
plot(a,b,'*-r');
xlim([-50 50]);
ylim([0 45]);plot
I would like to plot the point (a,b) for which the function is minimum on a contour tracing convergence towards the center.

Octave/Matlab High Boost filtering

I have to use a Gaussian lowpass filter for the blurring step and then I have to improve the sharpness of the result using high-boost filtering.
Here is what I have so far:
I=imread('blurry-moon.tif');
A = fft2(double(I));
Ashift=fftshift(A);
[m n]=size(A);
R=10;
X=0:n-1;
Y=0:m-1;
[X Y]=meshgrid(X,Y);
Cx=0.5*n;
Cy=0.5*m;
LoF=exp(-((X-Cx).^2+(Y-Cy).^2)./(2*R).^2);
Gauss=Ashift.*LoF;
GaussShift=ifftshift(Gauss);
InverseGauss=ifft2(GaussShift);
%High boost
f = double(InverseGauss);
[m n]=size(f);
J0 = f;
for i=3:m-2
for j=3:n-2
J0(i,j) = (-8*f(i,j))+(1*f(i-1,j))+(1*f(i+1,j))+(1*f(i,j-1))+(1*f(i,j+1))...
+(1*f(i-1,j-1))+(1*f(i+1,j+1))+(1*f(i-1,j+1))+(1*f(i+1,j-1));
end
end
%----visualizing the results----------------------------------------------
figure(1)
imshow(I);colormap gray
title('original image','fontsize',14)
figure(2)
imshow(abs(Ashift),[-12 300000]), colormap gray
title('fft of original image','fontsize',14)
figure(3)
imshow(abs(InverseGauss),[12 290]), colormap gray
title('low pass filtered image','fontsize',14)
figure(4)
imshow(abs(J0),[12 290]), colormap gray
title('final image','fontsize',14)
I think I do something wrong in the high boost. But I think I am doing the gaussian filter right?
Can someone help out with the high boost filter?
Best regards!
img = imread('moon.tif');
% create gaussian filter
h = fspecial('gaussian',5,2.5);
% blur the image
blurred_img = imfilter(img,h);
% subtract blurred image from original
diff_img = img - blurred_img;
% add difference to the original image
highboost_img = img + 3*diff_img;
subplot 221
imshow(img,[]);
title('Original Image')
subplot 222
imshow(blurred_img,[]);
title('Blurred Image')
subplot 223
imshow(diff_img,[]);
title('Difference Image')
subplot 224
imshow(highboost_img,[]);
title('HighBoosted Image')

How to show image and plot simultaneously in loop

I want to show image and plot in the loop. I want to show image in one figure and plot in other figure. So I try to use my code, but it does not work. Could you help me fix it? Thanks so much
x=0;
x_arr=[]
I=imread('peppers.png');
figure
for i=1:100
if mod(i,10)==0
pause(0.5);
x=i.^2+1;
x_arr=[x_arr x]
%show image
hold on
imshow(I);
hold off
%show plot
pause(0.5);
hold on
plot(y_arr);
hold off
end
end
you can do so by using figure for handeling 2 windows:
x=0;
x_arr=[]
I=imread('peppers.png');
for i=1:100
if mod(i,10)==0
pause(0.5);
x=i.^2+1;
x_arr=[x_arr x];
%show image
figure(1)
imshow(I);
%show plot
pause(0.5);
figure(2)
plot(x_arr);
end
end
or by using subplots to keep it in one window:
x=0; x_arr=[]
I=imread('peppers.png');
figure (1)
for i=1:100
if mod(i,10)==0
pause(0.5);
x=i.^2+1;
x_arr=[x_arr x] %show image
subplot(1,2,1);
imshow(I);
%show plot
pause(0.5);
subplot(1,2,2)
plot(x_arr);
end
end

create subplot with loop

i have following question and please help me to solve following task:
i want to create subplot in matlab which depend on loop variable,for example as i know to crate plot menu 2X2,we are doing like this
subplot(2,2,1)
subplot(2,2,2)
subplot(2,2,3)
subplot(2,2,4)
but can i do linear form?like 1:100?or something like this ,more generally like this
n=100;
for i=1:n
subplot(1,n,i)
?
thanks very much
EDITED CODE
function [order]=find_order(y,fs);
order=0;
n=length(y);
n1=nextpow2(n);
ndft=2^n1;
for i=1:floor(n/2)
[Pxx,f]=pburg(y,i,ndft,fs);
subplot(ndft,ndft,i);
plot(f,Pxx);
title(['order',num2str(i),'i']);
order=i;
end
end
picture :
i can't understand what happens
1-D Demo
Code
%%// Data
t = 0:0.01:15*pi;
y1 = sin(t);
%%// Plot
figure,
for k=1:4
subplot(4,1,k)
plot(t((k-1)*1000+1:k*1000),y1((k-1)*1000+1:k*1000))
xlim([0 40])
end
Output
2-D Demo
Code
%%// Data
t = 0:0.01:15*pi;
y1 = sin(t);
%%// Plot
colors=['r' 'g' ; 'y' 'k'];
figure,
for k1=1:2
for k2=1:2
subplot(2,2,(k1-1)*2+k2)
plot(t,y1,colors(k1,k2));
end
end
Output
Hopefully these demos would guide to you something meaningful for your case.
Yes, it is:
n=5;
for i=1:n
subplot(1,n,i)
end
gives
for pat=1: N % main loop
% Define the sublot grid
s1=3; % subplot rows
s2=3; % subplot columns
% find the figure number
fig_num=floor(pat/(s1*s2))+1 % Figure number
% Find the subplot number
sub_fig=mod(pat,s1*s2) % subplot number
% correct for corners
if(sub_fig==0)
sub_fig=s1*s2;
fig_num=fig_num-1;
end
% plot something
figure(fig_num);
subplot(s1,s2,sub_fig) ;
plot(1,1) % plot something
end % of main loop