When I tried to calculate the variance of each of the block within an image, the same way in which I have tried to calculate mean, it throws an error like:
??? Error using ==> var at 56
First argument must be single or double.
Error in ==> #(x)var(x(:))
Error in ==> assignemt at 19
varValues = cellfun(#(x) var(x(:)),b);
Could anyone help in this regard?
The code I have written is
clc;
close all;
d=8;
a=imread('lena.jpg');
figure();
imshow(a);
b=mat2cell(a,[16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16[16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16]);
[m,n]=size(b);
[m,n]=size(a);
%calculate mean for host image
% for i=1:m
% for j=1:n
% meanValues = cellfun(#(x) mean(x(:)),b);
% end
% end
% calculate variance for host image
for i=1:m
for j=1:n
varValues = cellfun(#(x) var(x(:)),b);
end
end
You are using x(:) which is a cell. Try using [x{:}]
Your code does not run, the mat2cell-line contains two [ and none ] so I did not try it out.
Your image a is of type uint8 and therefore var is failing. Try converting the image to double either by im2double or double(a)/255.
BTW, why don't you use blockproc, nlfilt2 or similar functions?
Related
% Image Arithmetic Operations
% Image addition is done between two similar size of image, so image resize
% function is used to make size of both image same.
% I=I1+I2
clc
close all
I1=imread('test.jpg');
I2=imread('test_1.jpg');
subplot(2,2,1);imshow(I1);title('Original image I1');
subplot(2,2,2);imshow(I2);title('Original image I2');
I=I1+I2; % Addition of two images
subplot(2,2,3);imshow(I);title('Addition of image I1+I2');
I=I1-I2; % Subtraction of two images
subplot(2,2,4);imshow(I);title('Subtraction of image I1-I2');
figure;
subplot(2,2,1);imshow(I1);title('Original image I1');
I=I1+50;
subplot(2,2,2);imshow(I);title('Bright image I');
I=I1-100;
subplot(2,2,3);imshow(I);title('Dark image I');
M=imread('key.png');
M=im2bw(M); % Converts into binary image having 0s and 1s
I=uint8(I1).*uint8(M); % Type casting before multiplication
subplot(2,2,4);imshow(I);title('Masked Image I');
%clear all;
[filename,pathname]=uigetfile({'*.bmp;*.jpg;*.gif','Choose Image File'});
myimage=imread(filename);
if(size(myimage,3)==3)
myimage=rgb2gray(myimage);
end
[Rows,Cols]=size(myimage);
newimage=zeros(Rows,Cols);
k=1;
while k<5
for i=1:Rows
for j=1:Cols
if k==1
newimage(i,j)=myimage(i,j)-100;
end
if k==2
newimage(i,j)=myimage(i,j)-50;
end
if k==3
newimage(i,j)=myimage(i,j)+50;
end
if k==4
newimage(i,j)=myimage(i,j)+50;
end
end
end
subplot(2,2,k);imshow(newimage,[]);
k=k+1;
end
% calculate mean value
[Rows,Cols]=size(myimage);
newimage=zeros(Rows,Cols);
total=0;
for i=1:Rows
for j=1:Cols
total=total+myimage(i,j);
end
end
average=total/(Rows*Cols);
I am in big trouble because this error is not resolved and i am new to matlab coding so please help me and your solution is appreciated to me in future.
I faced an error mentioned below :
??? Error using ==> times Matrix dimensions must agree.
Error in ==> DIP_3 at 23 I=uint8(I1).*uint8(M); % Type casting before
multiplication
Assuming that the three images you are working with (test.jpg, test_1.jpg and test.png) are of the same size, the problem arises when you call the im2bw function
M=im2bw(M);
The input matrix M is a 3D matrix (eg. 100x100x3) while the output matrix is only a 2D matrix (100x100).
The error is generated because you multiply matrix I1 (which is also a 3D matrix) by a 2D matrix.
I=uint8(I1).*uint8(M);
You have therefore to make the matrix M a 3D matrix.
If you want to apply the same scaling factor on matrix I1 you can do something like:
M=imread('key.png');
M0=im2bw(M); % Converts into binary image having 0s and 1s
M(:,:,2)=M0;
M(:,:,3)=M0;
Otherwise, you have to define, somehow M(:,:,2) and M(:,:,3)
Hope this helps.
Could someone kindly explain what the following MATLAB line do?
fft( data3D, N, dim );
data3D is an array of size (NY, NX, NZ). MATLAB documentation says
fft(X,[],DIM) or fft(X,N,DIM) applies the fft operation across the dimension DIM.
What exactly fft calculates across a dimension when the input is a 3D array?
To expand on #Oliver Charlesworth, the command fft(X,[],DIM) should do this:
fft_values=zeros(size(X)); %pre-allocate the output array
if (DIM==1)
for I=1:size(X,2)
for J=1:size(X,3)
fft_values(:,I,J) = fft(squeeze(X(:,I,J));
end
end
elseif (DIM==2)
for I=1:size(X,1)
for J=1:size(X,3)
fft_values(I,:,J) = fft(squeeze(X(I,:,J));
end
end
elseif (DIM==3)
for I=1:size(X,1)
for J=1:size(X,2)
fft_values(I,J,:) = fft(squeeze(X(I,J,:));
end
end
end
I am trying to implement a simple function that its the same with the default hist() by MatLab.
We have two same images with different brightness and we have to convert them to grayscale and then use the default function of MatLab hist() to get the histograms (so far so good!).
Then we have to implement the function hist my_hist() , and when i am trying to count the frequency of the intensity the results are not the same.
It seems that it sums-up the frequency of 254 & 255 to 254 and 255 is zero!I dont know what the problem is, any help would be appreciated.
here is the code for the command line:
%Read the images and convert them from rgb to grayscale
i=imread('pic1.jpg');
j=rgb2gray(i);
x=imread('pic2.jpg');
y=rgb2gray(x);
%Display the two images
figure
imshow(j)
figure
imshow(y)
%Display the histogram of the two images
[a,b] = hist(j(:),0:1:255);
figure
plot(b,a)
[c,d]=hist(y(:),0:1:255);
figure
plot(d,c)
%Call of the built-in function
my_hist('pic1.jpg','pic2.jpg')
And here is the code of the self built function:
function []= my_hist( x,y)
%Read the images and convert them from rgb to grayscale
pic1=imread(x);
i=rgb2gray(pic1);
pic2=imread(y);
j=rgb2gray(pic2);
%Initialize two vectors to be the axis for histogram
plotx=0:255;
ploty=zeros(1,256);
%Take the dimensions of the first image pic1
[m,n] = size(i);
%With 2 loops we go through the matrix of the image and count how many
%pixels have the same intensity
for k=1:m
for l=1:n
num=i(k,l)+1;
ploty(num)=ploty(num)+1;
end
end
%Display the histogram for the first image pic1
figure
plot(plotx,ploty);
%Initialize two vectors to be the axis for histogram
plotx2=0:255;
ploty2=zeros(1,256);
%Take the dimensions of the second image pic2
[m2,n2] = size(j);
%With 2 loops we go through the matrix of the image and count how many
%pixels have the same intensity
for o=1:m2
for p=1:n2
num2=j(o,p)+1;
ploty2(num2)=ploty2(num2)+1;
end
end
%Display the histogram for the second image pic2
figure
plot(plotx2,ploty2);
end
And here are the images pic1 and pic2.
This is a problem due to your image being of integer type uint8 which can only range from 0-255:
>> a= uint8(255)
a =
255
>> a=a+1
a =
255
Convert your data to say type uint16 with
j = uint16(j);
y = uint16(y);
and your problem should be gone:
>> a=uint16(a)
a =
255
>> a=a+1
a =
256
Can someone explain why I am getting the following error in my code?
I=imread('lena.jpg');
[M,N]=size(I);
p = zeros(256,3);
for ii=1:256 p(ii,1)=ii-1; end;
p(:,2) = imhist(I);
p (p(:,2)==0,:) = []; % remove zero entries in p
% Calling Shannon procedure, return t1 value and its location in p
[T1,Loc]=Shannon(p);
% Calling Tsallis procedure of Part1
pLow= p(1:Loc,:); T2= Tsallis_Sqrt(pLow);
% Calling Tsallis procedure of Part2
pHigh=p(Loc+1:size(p),:); T3=Tsallis_Sqrt(pHigh);
% Cerate binary matrices f
f=zeros(M,N);
for i=1:M;
for j=1:N;
if ((I(i,j)>= T2)&(I(i,j)<T1))|(I(i,j)>= T3)
f(i,j)=1; end;
end;
end
% Calling EdgeDetector procedure, return edge detection image.
[g]= EdgeDetector(f);
figure;
imshow(g);
I use the Shannon Entropy for find the threshold value. For this I create tree functions:Shannon(p),Tsallis_Sqrt(p),EdgeDetector(f). But I am getting this error in main file. Please help me to solve this error.
??? Error using ==> iptcheckinput
Function IMHIST expected its first input, I or X, to be two-dimensional.
Error in ==> imhist>parse_inputs at 270
iptcheckinput(a, {'double','uint8','logical','uint16','int16','single'}, ...
Error in ==> imhist at 57
[a, n, isScaled, top, map] = parse_inputs(varargin{:});
Error in ==> MainProgram at 6
p(:,2) = imhist(I);
The first argument to imhist(I) has to be a grayscale (intensity) image. Your I is probably an RGB 3-channel image. RGB images have 3 dimensions but the function is expecting a two-dimensional matrix as its input, that's why you get the error.
You may consider converting your image to the grayscale format first
rgbimage = imread('lena.jpg');
I = rgb2gray(RGB);
I am trying to execute local binary pattern in MATLAB using the image processing toolbox. When i execute I can't get a LBP image and LBP histogram.
clear all;
close all;
clc;
I=imread('test.png');
figure,imshow(I)
%% Crop
I2 = imcrop(I);
figure, imshow(I2)
w=size(I2,1);
h=size(I2,2);
%% LBP
scale = 2.^[7 6 5; 0 -inf 4; 1 2 3];
for i=2:w-1
for j=2:h-1
J0=I2(i,j);
I3(i-1,j-1)=I2(i-1,j-1)>J0;
I3(i-1,j)=I2(i-1,j)>J0;
I3(i-1,j+1)=I2(i-1,j+1)>J0;
I3(i,j+1)=I2(i,j+1)>J0;
I3(i+1,j+1)=I2(i+1,j+1)>J0;
I3(i+1,j)=I2(i+1,j)>J0;
I3(i+1,j-1)=I2(i+1,j-1)>J0;
I3(i,j-1)=I2(i,j-1)>J0;
LBP(i,j)=I3(i-1,j-1)*2^7+I3(i-1,j)*2^6+I3(i-1,j+1)*2^5+I3(i,j+1)*2^4+I3(i+1,j+1)*2^3+I3(i+1,j)*2^2+I3(i+1,j-1)*2^1+I3(i,j-1)*2^0;
end
end
figure,imshow(LBP)
figure,imhist(LBP)
what is the issue.i am supposed to get numbers from 0 to 255.
I3(i-1,j-1)=I2(i-1,j-1)>J0; creates a logical as output. If you don't go and cast this to something else, you're image will only be zeros and ones.
The easiest way is to initialize I3 outside the loop, i.e. have I3=I2; before you start looping. This way, all your assignments inside the loop get converted to whatever class I2 was.
use :
figure,imshow(uint8(LBP));
Its becuase LBP image is in DOUBLE, you need to cast it.