Octave: Kmeans clustering not working on an image matrix - matlab

I have tried the following code.
img=imread("test1.jpg");
gimg=rgb2gray(img);
imshow(gimg);
bw = gimg < 255;
L = bwlabel(bw);
imshow(label2rgb(L, #jet, [.7 .7 .7]))
s = regionprops(L, 'PixelIdxList', 'PixelList');
s(1).PixelList(1:4, :)
idx = s(1).PixelIdxList;
sum_region1 = sum(gimg(idx));
x = s(1).PixelList(:, 1);
y = s(1).PixelList(:, 2);
xbar = sum(x .* double(gimg(idx))) / sum_region1
ybar = sum(y .* double(gimg(idx))) / sum_region1
hold on
for k = 1:numel(s)
idx = s(k).PixelIdxList;
pixel_values = double(gimg(idx));
sum_pixel_values = sum(pixel_values);
x = s(k).PixelList(:, 1);
y = s(k).PixelList(:, 2);
xbar = sum(x .* pixel_values) / sum_pixel_values;
ybar = sum(y .* pixel_values) / sum_pixel_values;
plot(xbar, ybar, '*')
end
hold off
a=round(xbar)-90;
b=round(xbar)+90;
c=round(ybar)-90;
d=round(ybar)+90;
roi=gimg(a:b,c:d);
imshow(roi);
roi(:,:,2)=0;
roi(:,:,3)=0;
se = strel('cube',20);
closeBW = imclose(roi,se);
figure
imshow(closeBW);
de=rgb2gray(closeBW);
ed=edge(de,"canny");
imshow(ed);
j=kmeans(ed,3);
What i did was take an image and extracted its grayscale.I concentrated on the part of image which has very high intensity.I then took the red component of the image and then applied closing operation on the resultant image.After that I applied edge detection using canny method.Then I tried to use kmeans on result of edge detection.
I get an error saying kmeans requires real matrix.
help would appreciated.

edge in MATLAB / Octave returns a binary / logical matrix. kmeans requires that the input be a double or single matrix.
Therefore, simply cast ed to double and continue:
ed=edge(de,"canny");
imshow(ed);
ed = double(ed); %// Change
j=kmeans(ed,3);

Related

Extrapolate Values MATLAB 2D [duplicate]

I am doing a 2-D interpolation using interp2. For some data values, the
interp2 command returns NaN because one of the dimensions are outside
of the range defined by the vector of known values.
Its possible to extrapolate with the interp1 command. However, Is
there a way to do this for interp2?
Thanks
Here is the code in which I am using the interp2 command:
function [Cla] = AirfoilLiftCurveSlope(obj,AFdata,Rc,M)
% Input:
% AFdata: Airfoil coordinates.
% Rc: Local Reynolds number.
% M: Mach number for Prandtle Glauert compressibility correction.
% Output:
% Cla: 2 dimensional lift curve slopea applicable to linear region of lift polar.
load('ESDU84026a.mat');
xi = size(AFdata);
if mod(xi(1,1),2) == 0
%number is even
AFupper = flipud(AFdata(1:(xi(1,1)/2),:));
AFlower = AFdata(((xi(1,1)/2)+1):end,:);
else
%number is odd
AFupper = flipud(AFdata(1:floor((xi(1,1)/2)),:));
AFlower = AFdata((floor(xi(1,1)/2)+1):end,:);
end
t_c = Airfoil.calculateThickness(AFdata(:,2));
Y90 = ((interp1(AFupper(:,1),AFupper(:,2),0.9,'linear')) - (interp1(AFlower(:,1),AFlower(:,2),0.9,'linear')))*100;
Y99 = ((interp1(AFupper(:,1),AFupper(:,2),0.99,'linear')) - (interp1(AFlower(:,1),AFlower(:,2),0.99,'linear')))*100;
Phi_TE = (2 * atan( ( (Y90/2) - (Y99/2) )/9))*180/pi; % Degrees
Tan_Phi_Te = ( (Y90/2) - (Y99/2) )/9;
Cla_corr = interp2(Tan_Phi,Rc_cla,cla_ratio,Tan_Phi_Te,Rc,'linear');
beta =sqrt((1-M^2)); % Prandtle Glauert correction
Cla_theory = 2*pi + 4.7*t_c*(1+0.00375 * Phi_TE); % per rad
Cla = (1.05/beta) * Cla_corr * Cla_theory; % per rad
if isnan(Cla) == 1 %|| Cla > 2*pi
Cla = 2*pi;
end
end
Yes, there are two ways to get interp2 to return a meaningful value out of bounds according to the docs.
Use the 'spline' interpolation method. Unlike option #2, this will actually extrapolate the data based on the boundary conditions of the spline.
Specify a final extrapval parameter. This constant will be returned instead of NaN for all other interpolation methods.
Unfortunately, there does not appear to be a way to specify something like "nearest neighbor on the grid" or something like that. If the out-of bounds elements are close to the edges, perhaps you could just expand the input array. For example like this:
x = [x(1, 1), x(1, :), x(1, end); ...
x(:, 1), x, x(:, end); ...
x(end, 1), x(end, :), x(end, end)]
Hey please find my code for interp2 it just take max bound values;
function vq = Linear2dInterpWithClipExtrap(x,y,v,xq,yq);
vq = interp2(x,y,v,xq,yq);
[XMax, idxVMax] = max(x);
[XMin, idxVMin] = min(x);
idxMax = xq > XMax;
idxMin = xq < XMin;
if ~isempty(yq(idxMax));
vq(idxMax) = LinearInterpWithClipExtrap(y,v(:,idxVMax),yq(idxMax));
end
if ~ isempty(yq(idxMin))
vq(idxMin) = LinearInterpWithClipExtrap(y,v(:,idxVMin),yq(idxMin));
end
[YMax, idyVMax] = max(y);
[YMin, idyVMin] = min(y);
idyMax = yq > YMax;
idyMin = yq < YMin;
if ~isempty(xq(idyMax));
vq(idyMax) = LinearInterpWithClipExtrap(x,v(idyVMax,:),xq(idyMax));
end
if ~ isempty(xq(idyMin));
vq(idyMin) = LinearInterpWithClipExtrap(x,v(idyVMin,:),xq(idyMin));
end
function vq = LinearInterpWithClipExtrap(x,v,xq);
vq = interp1(x,v,xq);
[XMax, idxVMax] = max(x);
[XMin, idxVMin] = min(x);
idxMax = xq > XMax;
idxMin = xq < XMin;
vq(idxMax) = v(idxVMax);
vq(idxMin) = v(idxVMin
);

Shortest line between boundary points that passes through the centroid of a shape

lesion image
I have an irregularly shaped object in which I have to find the greatest and smallest diameter.
To find the greatest diameter, I extracted the boundary points and found the distances between all the points. I took the maximum distance amongst those distances which gave me my greatest diameter.
boundaries = bwboundaries(binaryImage);
numberOfBoundaries = size(boundaries, 1);
for blobIndex = 1 : numberOfBoundaries
thisBoundary = boundaries{blobIndex};
x = thisBoundary(:, 2); % x = columns.
y = thisBoundary(:, 1); % y = rows.
% Find which two boundary points are farthest from each other.
maxDistance = -inf;
for k = 1 : length(x)
distances = sqrt( (x(k) - x) .^ 2 + (y(k) - y) .^ 2 );
[thisMaxDistance, indexOfMaxDistance] = max(distances);
if thisMaxDistance > maxDistance
maxDistance = thisMaxDistance;
index1 = k;
index2 = indexOfMaxDistance;
end
end
I have attached my image containing the longest diameter.
I also need a line segment that passes through the centroid connecting the two boundary points whose length is shortest. When I try to find the shortest diameter by modifying the above code, to find min(distances), I am getting an error that says:
Error using griddedInterpolant
The coordinates of the input points must be finite values; Inf and NaN are not permitted.
What do I need to do to find the shortest "diameter" (that is, passing through the centroid) of this object?
it's possible to use a polar image like this:
lesion = imread('lesion.jpg');
bw = lesion > 100;
c = regionprops(bw,'Centroid');
c = c.Centroid;
% polar args
t = linspace(0,2*pi,361);
t(end) = [];
r = 0:ceil(sqrt(numel(bw)/4));
[tg,rg] = meshgrid(t,r);
[xg,yg] = pol2cart(tg,rg);
xoff = xg + c(1);
yoff = yg + c(2);
% polar image
pbw = interp2(double(bw),xoff,yoff,'nearest') == 1;
[~,radlen] = min(pbw,[],1);
radlen(radlen == 1) = max(r);
n = numel(radlen);
% add two edges of line to form diameter
diamlen = radlen(1:n/2) + radlen(n/2+1:n);
% find min diameter
[mindiam,tminidx1] = min(diamlen);
tmin = t(tminidx1);
rmin = radlen(tminidx1);
tminidx2 = tminidx1 + n/2;
xx = [xoff(radlen(tminidx1),tminidx1) xoff(radlen(tminidx2),tminidx2)];
yy = [yoff(radlen(tminidx1),tminidx1) yoff(radlen(tminidx2),tminidx2)];
% viz
figure;
subplot(121);
imshow(pbw);
title('polar image');
subplot(122);
imshow(bw);
hold on
plot(c(1),c(2),'or')
plot(xx,yy,'g')
legend('centroid','shortest diameter');
and the output is:

Matlab interp2 extrapolation

I am doing a 2-D interpolation using interp2. For some data values, the
interp2 command returns NaN because one of the dimensions are outside
of the range defined by the vector of known values.
Its possible to extrapolate with the interp1 command. However, Is
there a way to do this for interp2?
Thanks
Here is the code in which I am using the interp2 command:
function [Cla] = AirfoilLiftCurveSlope(obj,AFdata,Rc,M)
% Input:
% AFdata: Airfoil coordinates.
% Rc: Local Reynolds number.
% M: Mach number for Prandtle Glauert compressibility correction.
% Output:
% Cla: 2 dimensional lift curve slopea applicable to linear region of lift polar.
load('ESDU84026a.mat');
xi = size(AFdata);
if mod(xi(1,1),2) == 0
%number is even
AFupper = flipud(AFdata(1:(xi(1,1)/2),:));
AFlower = AFdata(((xi(1,1)/2)+1):end,:);
else
%number is odd
AFupper = flipud(AFdata(1:floor((xi(1,1)/2)),:));
AFlower = AFdata((floor(xi(1,1)/2)+1):end,:);
end
t_c = Airfoil.calculateThickness(AFdata(:,2));
Y90 = ((interp1(AFupper(:,1),AFupper(:,2),0.9,'linear')) - (interp1(AFlower(:,1),AFlower(:,2),0.9,'linear')))*100;
Y99 = ((interp1(AFupper(:,1),AFupper(:,2),0.99,'linear')) - (interp1(AFlower(:,1),AFlower(:,2),0.99,'linear')))*100;
Phi_TE = (2 * atan( ( (Y90/2) - (Y99/2) )/9))*180/pi; % Degrees
Tan_Phi_Te = ( (Y90/2) - (Y99/2) )/9;
Cla_corr = interp2(Tan_Phi,Rc_cla,cla_ratio,Tan_Phi_Te,Rc,'linear');
beta =sqrt((1-M^2)); % Prandtle Glauert correction
Cla_theory = 2*pi + 4.7*t_c*(1+0.00375 * Phi_TE); % per rad
Cla = (1.05/beta) * Cla_corr * Cla_theory; % per rad
if isnan(Cla) == 1 %|| Cla > 2*pi
Cla = 2*pi;
end
end
Yes, there are two ways to get interp2 to return a meaningful value out of bounds according to the docs.
Use the 'spline' interpolation method. Unlike option #2, this will actually extrapolate the data based on the boundary conditions of the spline.
Specify a final extrapval parameter. This constant will be returned instead of NaN for all other interpolation methods.
Unfortunately, there does not appear to be a way to specify something like "nearest neighbor on the grid" or something like that. If the out-of bounds elements are close to the edges, perhaps you could just expand the input array. For example like this:
x = [x(1, 1), x(1, :), x(1, end); ...
x(:, 1), x, x(:, end); ...
x(end, 1), x(end, :), x(end, end)]
Hey please find my code for interp2 it just take max bound values;
function vq = Linear2dInterpWithClipExtrap(x,y,v,xq,yq);
vq = interp2(x,y,v,xq,yq);
[XMax, idxVMax] = max(x);
[XMin, idxVMin] = min(x);
idxMax = xq > XMax;
idxMin = xq < XMin;
if ~isempty(yq(idxMax));
vq(idxMax) = LinearInterpWithClipExtrap(y,v(:,idxVMax),yq(idxMax));
end
if ~ isempty(yq(idxMin))
vq(idxMin) = LinearInterpWithClipExtrap(y,v(:,idxVMin),yq(idxMin));
end
[YMax, idyVMax] = max(y);
[YMin, idyVMin] = min(y);
idyMax = yq > YMax;
idyMin = yq < YMin;
if ~isempty(xq(idyMax));
vq(idyMax) = LinearInterpWithClipExtrap(x,v(idyVMax,:),xq(idyMax));
end
if ~ isempty(xq(idyMin));
vq(idyMin) = LinearInterpWithClipExtrap(x,v(idyVMin,:),xq(idyMin));
end
function vq = LinearInterpWithClipExtrap(x,v,xq);
vq = interp1(x,v,xq);
[XMax, idxVMax] = max(x);
[XMin, idxVMin] = min(x);
idxMax = xq > XMax;
idxMin = xq < XMin;
vq(idxMax) = v(idxVMax);
vq(idxMin) = v(idxVMin
);

How to create 64 Gabor features at each scale and orientation in the spatial and frequency domain

Normally, a Gabor filter, as its name suggests, is used to filter an image and extract everything that it is oriented in the same direction of the filtering.
In this question, you can see more efficient code than written in this Link
Assume 16 scales of Filters at 4 orientations, so we get 64 gabor filters.
scales=[7:2:37], 7x7 to 37x37 in steps of two pixels, so we have 7x7, 9x9, 11x11, 13x13, 15x15, 17x17, 19x19, 21x21, 23x23, 25x25, 27x27, 29x29, 31x31, 33x33, 35x35 and 37x37.
directions=[0, pi/4, pi/2, 3pi/4].
The equation of Gabor filter in the spatial domain is:
The equation of Gabor filter in the frequency domain is:
In the spatial domain:
function [fSiz,filters,c1OL,numSimpleFilters] = init_gabor(rot, RF_siz)
image=imread('xxx.jpg');
image_gray=rgb2gray(image);
image_gray=imresize(image_gray, [100 100]);
image_double=double(image_gray);
rot = [0 45 90 135]; % we have four orientations
RF_siz = [7:2:37]; %we get 16 scales (7x7 to 37x37 in steps of two pixels)
minFS = 7; % the minimum receptive field
maxFS = 37; % the maximum receptive field
sigma = 0.0036*RF_siz.^2 + 0.35*RF_siz + 0.18; %define the equation of effective width
lambda = sigma/0.8; % it the equation of wavelength (lambda)
G = 0.3; % spatial aspect ratio: 0.23 < gamma < 0.92
numFilterSizes = length(RF_siz); % we get 16
numSimpleFilters = length(rot); % we get 4
numFilters = numFilterSizes*numSimpleFilters; % we get 16x4 = 64 filters
fSiz = zeros(numFilters,1); % It is a vector of size numFilters where each cell contains the size of the filter (7,7,7,7,9,9,9,9,11,11,11,11,......,37,37,37,37)
filters = zeros(max(RF_siz)^2,numFilters); % Matrix of Gabor filters of size %max_fSiz x num_filters, where max_fSiz is the length of the largest filter and num_filters the total number of filters. Column j of filters matrix contains a n_jxn_j filter (reshaped as a column vector and padded with zeros).
for k = 1:numFilterSizes
for r = 1:numSimpleFilters
theta = rot(r)*pi/180; % so we get 0, pi/4, pi/2, 3pi/4
filtSize = RF_siz(k);
center = ceil(filtSize/2);
filtSizeL = center-1;
filtSizeR = filtSize-filtSizeL-1;
sigmaq = sigma(k)^2;
for i = -filtSizeL:filtSizeR
for j = -filtSizeL:filtSizeR
if ( sqrt(i^2+j^2)>filtSize/2 )
E = 0;
else
x = i*cos(theta) - j*sin(theta);
y = i*sin(theta) + j*cos(theta);
E = exp(-(x^2+G^2*y^2)/(2*sigmaq))*cos(2*pi*x/lambda(k));
end
f(j+center,i+center) = E;
end
end
f = f - mean(mean(f));
f = f ./ sqrt(sum(sum(f.^2)));
p = numSimpleFilters*(k-1) + r;
filters(1:filtSize^2,p)=reshape(f,filtSize^2,1);
fSiz(p)=filtSize;
end
end
% Rebuild all filters (of all sizes)
nFilts = length(fSiz);
for i = 1:nFilts
sqfilter{i} = reshape(filters(1:(fSiz(i)^2),i),fSiz(i),fSiz(i));
%if you will use conv2 to convolve an image with this gabor, so you should also add the equation below. But if you will use imfilter instead of conv2, so do not add the equation below.
sqfilter{i} = sqfilter{i}(end:-1:1,end:-1:1); %flip in order to use conv2 instead of imfilter (%bug_fix 6/28/2007);
convv=imfilter(image_double, sqfilter{i}, 'same', 'conv');
figure;
imagesc(convv);
colormap(gray);
end
phi = ij*pi/4; % ij = 0, 1, 2, 3
theta = 3;
sigma = 0.65*theta;
filterSize = 7; % 7:2:37
G = zeros(filterSize);
for i=(0:filterSize-1)/filterSize
for j=(0:filterSize-1)/filterSize
xprime= j*cos(phi);
yprime= i*sin(phi);
K = exp(2*pi*theta*sqrt(-1)*(xprime+ yprime));
G(round((i+1)*filterSize),round((j+1)*filterSize)) =...
exp(-(i^2+j^2)/(sigma^2))*K;
end
end
As of R2015b release of the Image Processing Toolbox, you can create a Gabor filter bank using the gabor function in the image processing toolbox, and you can apply it to an image using imgaborfilt.
In the frequency domain:
sigma_u=1/2*pi*sigmaq;
sigma_v=1/2*pi*sigmaq;
u0=2*pi*cos(theta)*lambda(k);
v0=2*pi*sin(theta)*lambda(k);
for u = -filtSizeL:filtSizeR
for v = -filtSizeL:filtSizeR
if ( sqrt(u^2+v^2)>filtSize/2 )
E = 0;
else
v_theta = u*cos(theta) - v*sin(theta);
u_theta = u*sin(theta) + v*cos(theta);
E=(1/2*pi*sigma_u*sigma_v)*((exp((-1/2)*(((u_theta-u0)^2/sigma_u^2))+((v_theta-v0)^2/sigma_v^2))) + (exp((-1/2)*(((u_theta+u0)^2/sigma_u^2))+((v_theta+v0)^2/sigma_v^2))));
end
f(v+center,u+center) = E;
end
end

Implementation of shadow free 1d invariant image

I implemented a method for removing shadows based on invariant color features found in the paper Entropy Minimization for Shadow Removal. My implementation seems to be yielding similar computational results sometimes, but they are always off, and my grayscale image is blocky, maybe as a result of incorrectly taking the geometric mean.
Here is an example plot of the information potential from the horse image in the paper as well as my invariant image. Multiply the x-axis by 3 to get theta(which goes from 0 to 180):
And here is the grayscale Image my code outputs for the correct maximum theta (mine is off by 10):
You can see the blockiness that their image doesn't have:
Here is their information potential:
When dividing by the geometric mean, I have tried using NaN and tresholding the image so the smallest possible value is .01, but it doesn't seem to change my output.
Here is my code:
I = im2double(imread(strname));
[m,n,d] = size(I);
I = max(I, .01);
chrom = zeros(m, n, 3, 'double');
for i = 1:m
for j = 1:n
% if ((I(i,j,1)*I(i,j,2)*I(i,j,3))~= 0)
chrom(i,j, 1) = I(i,j,1)/((I(i,j,1)*I(i,j,2)*I(i,j, 3))^(1/3));
chrom(i,j, 2) = I(i,j,2)/((I(i,j,1)*I(i,j,2)*I(i,j, 3))^(1/3));
chrom(i,j, 3) = I(i,j,3)/((I(i,j,1)*I(i,j,2)*I(i,j, 3))^(1/3));
% else
% chrom(i,j, 1) = 1;
% chrom(i,j, 2) = 1;
% chrom(i,j, 3) = 1;
% end
end
end
p1 = mat2gray(log(chrom(:,:,1)));
p2 = mat2gray(log(chrom(:,:,2)));
p3 = mat2gray(log(chrom(:,:,3)));
X1 = mat2gray(p1*1/(sqrt(2)) - p2*1/(sqrt(2)));
X2 = mat2gray(p1*1/(sqrt(6)) + p2*1/(sqrt(6)) - p3*2/(sqrt(6)));
maxinf = 0;
maxtheta = 0;
data2 = zeros(1, 61);
for theta = 0:3:180
M = X1*cos(theta*pi/180) - X2*sin(theta*pi/180);
s = sqrt(std2(X1)^(2)*cos(theta*pi/180) + std2(X2)^(2)*sin(theta*pi/180));
s = abs(1.06*s*((m*n)^(-1/5)));
[m, n] = size(M);
length = m*n;
sources = zeros(1, length, 'double');
count = 1;
for x=1:m
for y = 1:n
sources(1, count) = M(x , y);
count = count + 1;
end
end
weights = ones(1, length);
sigma = 2*s;
[xc , Ak] = fgt_model(sources , weights , sigma , 10, sqrt(length) , 6 );
sum1 = sum(fgt_predict(sources , xc , Ak , sigma , 10 ));
sum1 = sum1/sqrt(2*pi*2*s*s);
data2(theta/3 + 1) = sum1;
if (sum1 > maxinf)
maxinf = sum1;
maxtheta = theta;
end
end
InvariantImage2 = cos(maxtheta*pi/180)*X1 + sin(maxtheta*pi/180)*X2;
Assume the Fast Gauss Transform is correct.
I don't know whether this makes any difference as it is more than a month now, but the blockiness and different information potential plot is simply caused by compression of the used image. You can't expect to be getting same results using this image as they had, because they have used raw, high resolution uncompressed version of it. I have to say I am fairly impressed with your results, especially with implementing the information potential. That thing went over my head a little.
John.