1

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.

1 Answers1

1

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);
rayryeng
  • 102,964
  • 22
  • 184
  • 193