Edge detection using fuzzy threshold value in matlab - matlab

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);

Related

How to pass Imagesc frame into Matlab imwrite and imread?

I take a gray-colormap frame of imagesc, try to gray2ind-ind2rgb for imwrite and then read it again by imread but I get the error which indicates that the dimensions do not match.
Assignment has more non-singleton rhs dimensions than non-singleton subscripts
Error in ind2rgb (line 34)
rout(:,:,1) = r;
Error in test_imagesc_output_imwrite (line 14)
Crgb = ind2rgb(Cind, parula(256)); % https://stackoverflow.com/a/39968435/54964
Code where I am uncertain how to pass 1-gray(1024) colormap details to gray2ind correctly; I think gray2ind(I,256) can lose pieces of information; also, ind2rgb(Cind, parula(256) is not correct but I cannot use there 1-gray(1024) directly
clear all; close all; clc;
x = [5 8];
y = [3 6];
C = [0 2 4 6; 8 10 12 14; 16 18 20 22];
f=figure;
hax=axes(f);
imagesc(hax, x,y,C) % I could not use here I=imagesc and then I.CData for some reason
colormap(hax, 1-gray(1024));
I=getframe(hax);
I=I.cdata;
assert(isa(I, 'uint8'), sprintf('I is not uint8 but %s', class(I)));
Cind = gray2ind(I, 256);
% TODO here something
Crgb = ind2rgb(Cind, parula(256)); % https://stackoverflow.com/a/39968435/54964
imwrite(Crgb, '/home/masi/Images/1.png');
I=imread('/home/masi/Images/1.png');
assert(isa(I, 'uint8'), sprintf('I is not uint8 but %s', class(I)));
f2=figure;
hax=axes(f2);
imagesc(hax2, I);
Fig. 1 Imagsc gray image which is getframe and tried to be stored by imwrite and read by imread
Matlab: 2016a
OS: Debian 8.5 64 bit
Hardware: Asus Zenbook UX303UA
Motivation: I am getting artefact in export_fig as described here but also now observing phase shifts from 1-gray to gray when saving [I,alpha]=export_fig(...) so trying with Matlab imwrite/imread
Your variabel I is RGB data (M x N x 3) so when you pass it to gray2ind you are getting back an M x N x 3 matrix of indices. ind2rgb accepts a 2D array of indices and not the 3D array that you're passing it.
I'm not quite sure what you expect, but you could first convert I to a true grayscale image first using rgb2gray
I = getframe(hax);
I = rgb2gray(I.cdata);
Cind = gray2ind(I, 256);
Crgb = ind2rgb(Cind, parula(256));
Or you could skip all of this and just set the colormap of your figure to parula and pass I.cdata directly to imwrite
imagesc(C, 'Parent', hax);
% Use an inverted parula colormap which seems to be what you're trying to do
colormap(flipud(parula(256)));
I = getframe(hax);
imwrite(I.cdata, '1.png')

Error in matlab code for matrix dimension

% 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.

Read Index RGB File and convert to Grey Scale Image - What is double, thinning and Inverted Images Format

I am trying to read GIF file and display in below following format -
Grey Scale
Resized
Double Image
Thinning
Inverted
Below is my code to do the same (incomplete):
clear all;
close all;
clc;
%Various preprocessing of Images
checkimage=imread('CheckSign/sign.gif');
checkimage_resize=imresize(checkimage,[512, 512]);
checkimage_grey=rgb2gray(checkimage_resize);
[m n p] = size(checkimage_grey)
for i=1:n
for j=1:m
if(checkimage_grey(i,j) ~= 0)
bimage(i,j) = 1;
else
bimage(i,j) = 0;
end
end
end
subplot (2,3,1),imshow(checkimage),title('Original Image');
subplot (2,3,2),imshow(checkimage_resize),title('Resized Image');
subplot (2,3,3),imshow(checkimage_grey),title('Grey Scale Image');
subplot (2,3,4),imshow(bimage),title('Binary Image');
But I get the below error:
Error using rgb2gray>parse_inputs (line 81)
MAP must be a m x 3 array.
Error in rgb2gray (line 35)
X = parse_inputs(varargin{:});
Error in preprocessing (line 8)
checkimage_grey=rgb2gray(checkimage_resize);
On viewing the image in imtool I see a pixel info as:
Pixel (X,Y) index [R,G,B]
<213>
R 0.80
G 0.80
B 1.00
Now I am not sure how to read this kind of index file and convert to grey scale one?
Also how do I change the image in concern to double, thinning and Inverted Imagesformat?
If I not wrong is inverted image as 1 - bimage?
For indexed images you need to read the index as well as the colorap
[ind map] = imread( 'CheckSign/sign.gif' );
Once you have all the information you need you can use ind2rgb to convert the index map into RGB image
checkimage_rgb = ind2rgb( ind, map );
You do not need a nested for-loop to "invert" the colors, it's enough
bimage = checkimage_grey ~= 0;
inverted = 1 - bimage;

What is the proper procedure for using MATLAB's poly2mask() with a shapefile?

I have a shapefile (example here) which I would like to convert to a binary region of interest (ROI) mask using MATLAB's poly2mask().
MATLAB's description is as follows:
BW = poly2mask(x, y, m, n)
BW = poly2mask(x, y, m, n) computes a binary region of interest (ROI)
mask, BW, from an ROI polygon, represented by the vectors x and y. The
size of BW is m-by-n. poly2mask sets pixels in BW that are inside the
polygon (X,Y) to 1 and sets pixels outside the polygon to 0.
poly2mask closes the polygon automatically if it isn't already closed.
This is the script I am using to convert my shapefile:
s = 'D:\path\to\studyArea.shp'
shp = shaperead(s)
x = [shp.X];
y = [shp.Y];
% use bounding box to define m and n
m = shp.BoundingBox(2) - shp.BoundingBox(1)
n = shp.BoundingBox(3) - shp.BoundingBox(1)
mask = poly2mask(x,y, m, n)
Results in the following error:
Error using poly2mask Expected input number 1, X, to be finite.
Error in poly2mask (line 49)
validateattributes(x,{'double'},{'real','vector','finite'},mfilename,'X',1);
Error in createMask (line 11) mask = poly2mask(x,y, m, n)
I suspect there may be an issue with UTM coordinates rather than Lat/Long, however, I could use some input from someone who has experience in this. Where am I going wrong here?
your x and y values have NaN's as the last coordinate.
poly2mask won't work if there are NaN's in the coordinates. (hence the error 'values must be finite').
If this is the case you can use a quick fix..
x = length(x)-1;
y = length(y)-1;
more info
http://www.mathworks.com/help/map/understanding-vector-geodata.html
-James
See the changes here -
s = 'studyArea.shp' %// Copy this file to working directory
shp = shaperead(s)
x = [shp.X];
y = [shp.Y];
%// Threw error before because there were NaNs there in x or y or both. So one assumption could be that you want to remove all x and y where any x or y is a NaN.
cond1 = isnan(x) | isnan(y);
x(cond1)=[];
y(cond1)=[];
% use bounding box to define m and n
m = shp.BoundingBox(2) - shp.BoundingBox(1)
n = shp.BoundingBox(3) - shp.BoundingBox(1)
%mask = poly2mask(x,y, m, n); %// Threw error
mask = poly2mask(x,y, round(m/20), round(n/20)); %//Worked fine for smaller 3rd and 4th arguments

calculate variance within each block of an image

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?