skin cancer image
I want to apply K means clustering on grayscale image, code is as follow
im = imread('SSM1_2_orig.jpg');
im = rgb2gray(im);
[idx centroids]=kmeans(double(im(:)),3,'distance','sqEuclidean','Replicates',3);
%imseg = zeros(size(im,1),size(im,2));
%{for i=1:max(idx)
%imseg(idx==i)=i;
%end}
segmented_images = cell(1,3);
for k = 1:3
color = im;
color(im ~= k) = 0;
segmented_images{k} = color;
end
figure(),imshow(segmented_images{1});
figure(),imshow(segmented_images{2});
figure(),imshow(segmented_images{3});
but it gives me the black output only
Here is the working code. Notes:
You are never using the result of the clustering,you are comparing the original pixel values with k, instead of the clustered pixel values idx.
Also, remember to use imshow(____, []) if your images are not [0-1] or [0-255].
im = imread('https://i.stack.imgur.com/ZYp7r.jpg');
im = rgb2gray(im);
[idx, centroids]=kmeans(double(im(:)),3,'distance','sqEuclidean','Replicates',3);
segmented_images = cell(1,3);
for k = 1:3
color = zeros(size(im));
color(idx==k) = im(idx==k);
segmented_images{k} = color;
end
figure(),imshow(segmented_images{1},[]);
figure(),imshow(segmented_images{2},[]);
figure(),imshow(segmented_images{3},[]);
Related
I = imread('Sub1.png');
figure, imshow(I);
I = imcomplement(I);
I = double(I)/255;
I = adapthisteq(I,'clipLimit',0.0003,'Distribution','exponential');
k = 12;
beta = 2;
maxIter = 100;
for i=1:length(beta)
[seg,prob,mu,sigma,it(i)] = ICM(I, k, beta(i), maxIter,5);
pr(i) = prob(end);
hold on;
end
figure, imshow(seg,[]);
and ICM function is defined as
function [segmented_image,prob,mu,sigma,iter] = ICM(image, k, beta, max_iterations, neigh)
[width, height, bands] = size(image);
image = imstack2vectors(image);
segmented_image = init(image,k,1);
clear c;
iter = 0;
seg_old = segmented_image;
while(iter < max_iterations)
[mu, sigma] = stats(image, segmented_image, k);
E1 = energy1(image,mu,sigma,k);
E2 = energy2(segmented_image, beta, width, height, k);
E = E1 + E2;
[p2,~] = min(E2,[],2);
[p1,~] = min(E1,[],2);
[p,segmented_image] = min(E,[],2);
prob(iter+1) = sum(p);
%find mismatch with previous step
[c,~] = find(seg_old~=segmented_image);
mismatch = (numel(c)/numel(segmented_image))*100;
if mismatch<0.1
iter
break;
end
iter = iter + 1;
seg_old = segmented_image;
end
segmented_image = reshape(segmented_image,[width height]);
end
Output of my algorithm is a logical matrix (seg) of size 305-by-305. When I use
imshow(seg,[]);
I am able to display the image. It shows different component with varying gray value. But bwlabel returns 1. I want to display the connected components. I think bwlabel thresholds the image to 1. unique(seg) returns values 1 to 10 since number of classes used in k-means is 10. I used
[label n] = bwlabel(seg);
RGB = label2rgb(label);
figure, imshow(RGB);
I need all the ellipse-like structures which are in between the two squares close to the middle of the image. I don't know the number of classes present in it.
Input image:
Ground truth:
My output:
If you want to explode the label image to different connected components you need to use a loop to extract labels for each class and sum label images to get the out label image.
u = unique(seg(:));
out = zeros(size(seg));
num_objs = 0;
for k = 1: numel(u)
mask = seg==u(k);
[L,N] = bwlabel(mask);
L(mask) = L(mask) + num_objs;
out = out + L;
num_objs = num_objs + N ;
end
mp = jet(num_objs);
figure,imshow(out,mp)
Something like this is produced:
I have tried to do everything out of scratch. I wish it is of some help.
I have a treatment chain that get at first contours with parameters tuned on a trial-and-error basis, I confess. The last "image" is given at the bottom ; with it, you can easily select the connected components and do for example a reconstruction by markers using "imreconstruct" operator.
clear all;close all;
I = imread('C:\Users\jean-marie.becker\Desktop\imagesJPG10\spinalchord.jpg');
figure,imshow(I);
J = I(:,:,1);% select the blue channel because jpg image
J=double(J<50);% I haven't inverted the image
figure, imshow(J);
se = strel('disk',5);
J=J-imopen(J,se);
figure, imshow(J);
J=imopen(J,ones(1,15));% privilegizes long horizontal strokes
figure, imshow(J);
K=imdilate(J,ones(20,1),'same');
% connects verticaly not-to-far horizontal "segments"
figure, imshow(K);
I have implemented Niblack thresholding onto an image in MATLAB (R2014B) as such:
function [ ] = processing_local( )
x = imread('HW8.png');
% Resize the image.
size(x);
x = imresize(x,[500 800]);
figure;
imshow(x);
title('Original');
x = imadjust(x, [0.2,0.8],[0.5,1.0]);
% HSV plane, extracting the value part.
z = rgb2hsv(x);
v = z(:,:,3);
v = imadjust(v);
% Finding the mean and standard deviation.
m = mean(v(:));
s = std(v(:));
k = -.4;
value = m+ k*s;
temp = v;
% Niblack
for p = 1:1:500
for q = 1:1:800
pixel = temp(p,q);
if(pixel > value)
temp(p,q)= 1;
else
temp(p,q)= 0;
end
end
end
figure;
imshow(temp);
title('Niblack Result.');
end
The result I see is this:
Output of Niblack thresholding
As shown there is a lot of dark spots where the image has been degraded, how would I optimize this in MATLAB?
I would like to try to some uniform brightness but cannot implement this within the function. I have wrote it in a separate function like this:
function [ ] = practice_white( )
x = imread('HW4.png');
x = rgb2gray(x);
background = imopen(x,strel('disk',15));
white = imclose(x, background);
whiteAdjusted = x ./ (white)*0.85;
BW = imbinarize(whiteAdjusted, 0.2);
figure
imshow(BW); title('Test');
end
I am doing a project on plant disease detection. I need to extract diseased parts from images of leafs but I'm not able to separate out diseased regions accurately using k-means. Specifically, the rest of the leaf is visible on the image with the diseased parts segmented. Here is the original image and image after extracting diseased parts:original image image after separating diseased parts
Here is the code I have written
b=imread('12.jpeg');
G=fspecial('gaussian',[200 250],1);
Ig=imfilter(b,G,'same');
figure,imshow(Ig);
conversionform = makecform('srgb2lab');
lab_img = applycform(Ig,conversionform);
figure,imshow(lab_img);
ab = double(lab_img(:,:,2:3));
nrows = size(ab,1);
ncols = size(ab,2);
ab = reshape(ab,nrows*ncols,2);
nColors = 2;
[cluster_idx, cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean', ...,
'Replicates',3);
pixel_labels = reshape(cluster_idx,nrows,ncols);
figure, imshow(pixel_labels,[]), title('image labeled by cluster index');
segmented_images = cell(1,3);
rgb_label = repmat(pixel_labels,[1 1 3]);
for k = 1:nColors
color = lab_img;
color(rgb_label ~= k) = 0;
segmented_images{k} = color;
end
figure, imshow(segmented_images{1}), title('objects in cluster 1');
figure, imshow(segmented_images{2}), title('objects in cluster 2');
e=segmented_images{1};
figure,imshow(e);
conversionform = makecform('lab2srgb');
new_image=applycform(e,conversionform);
figure,imshow(new_image);
I want to extract only the diseased regions using K means clustering. I would be grateful if someone could help me with this. I am using matlab 2009a.
Here is a corrected code that will do what you expect:
function segmented_img = leaf_segmentation( original_img, nclusters )
original_img = im2double(original_img);
smoothed_img = imgaussfilt(original_img,1);
conversionform = makecform('srgb2lab');
lab_img = applycform(smoothed_img,conversionform);
ab_img = lab_img(:,:,2:3);
[nrows,ncols,~] = size(ab_img);
ab_img = reshape(ab_img,nrows*ncols,2);
cluster_idx = kmeans(ab_img,nclusters,'distance','sqEuclidean','Replicates',3);
cluster_img = reshape(cluster_idx,nrows,ncols);
%figure, imagesc(cluster_img), title('Clustering results');
segmented_img = cell(1,nclusters);
for k = 1:nclusters
segmented_img{k} = bsxfun( #times, original_img, cluster_img == k );
end
end
You can call it and visualise the results like so:
segmented = leaf_segmentation( original, 3 );
figure;
subplot(1,3,1), imshow(segmented{1}), title('Cluster 1');
subplot(1,3,2), imshow(segmented{2}), title('Cluster 2');
subplot(1,3,3), imshow(segmented{3}), title('Cluster 3');
Note that the order of the clusters may vary. You can order them a posteriori knowing that the leaf should be mostly green/yellow, and that the background should be mostly black.
i'm making image segmentation with self organizing map. the image segement by 3 cluster. Sample image is :
and i have type the matlab code like this bellow :
clear;
clc;
i=imread('DataSet/3.jpg');
I = imresize(i,0.5);
cform = makecform('srgb2lab');
lab_I = applycform(I,cform);
ab = double(lab_I(:,:,2:3));
nrows = size(ab,1);
ncols = size(ab,2);
ab = reshape(ab,nrows*ncols,2);
a = ab(:,1);
b = ab(:,2);
normA = (a-min(a(:))) ./ (max(a(:))-min(a(:)));
normB = (b-min(b(:))) ./ (max(b(:))-min(b(:)));
ab = [normA normB];
newnRows = size(ab,1);
newnCols = size(ab,2);
cluster = 3;
% Max number of iteration
N = 90;
% initial learning rate
eta = 0.3;
% exponential decay rate of the learning rate
etadecay = 0.2;
%random weight
w = rand(2,cluster);
%initial D
D = zeros(1,cluster);
% initial cluster index
clusterindex = zeros(newnRows,1);
% start
for t = 1:N
for data = 1 : newnRows
for c = 1 : cluster
D(c) = sqrt(((w(1,c)-ab(data,1))^2) + ((w(2,c)-ab(data,2))^2));
end
%find best macthing unit
[~, bmuindex] = min(D);
clusterindex(data)=bmuindex;
%update weight
oldW = w(:,bmuindex);
new = oldW + eta * (reshape(ab(data,:),2,1)-oldW);
w(:,bmuindex) = new;
end
% update learning rate
eta= etadecay * eta;
end
%Label Every Pixel in the Image Using the Results from KMEANS
pixel_labels = reshape(clusterindex,nrows,ncols);
%Create Images that Segment the I Image by Color.
segmented_images = cell(1,3);
rgb_label = repmat(pixel_labels,[1 1 3]);
for k = 1:cluster
color = I;
color(rgb_label ~= k) = 0;
segmented_images{k} = color;
end
figure,imshow(segmented_images{1}), title('objects in cluster 1');
figure,imshow(segmented_images{2}), title('objects in cluster 2');
figure,imshow(segmented_images{3}), title('objects in cluster 3');
and after runing the matlab code, there is no image segmentation result. Matlab show 3 figure, Figure 1 show the full image, figure 2 blank, figure 3 blank .
please anyone help me to revise my matlab code, is any wrong code or something?
new = oldW + eta * (reshape(ab(data,:),2,1)-oldW);
This line looks suspicious to me, why you are subtracting old weights here, i dont think this makes any sense there, just remove oldW from there and check your results again.
Thank You
I'm trying to compute the average of every pixel with just the left and right neighbors but at the end of my processing I get only a white image, I can't find where my error. Here's my code
imageIn = imread('Prueba.jpg');
imageIn = rgb2gray(imageIn);
imageOut = zeros(size(imageIn));
ny = size(imageIn, 1);
nx = size(imageIn, 2);
imshow(imageIn);
u = [];
v = [];
tic
for i = 1:ny
u = imageIn(i,:);
v = zeros(1, ny);
for k = 2:ny-1
v(k) = (uint32(u(k-1))+uint32(u(k))+uint32(u(k+1)))/3;
end
%Special cases first and last pixel
v(1) = (uint32(u(2))+uint32(u(1))+uint32(u(2)))/3;
v(ny) = (uint32(u(ny-1))+uint32(u(ny))+uint32(u(ny-1)))/3;
imageOut(i,:) = v;
end
toc
imshow(imageOut);
Any ideas?
Change the last line of your code to imagesc(imageOut) and you'll see that the image is not in fact white.
Your code is fine; the reason the image appears white using the imshow() function is because after applying your local average the range of pixel intensities is considerably smaller and the default scaling used by imshow() is insufficient to bring out the contrast of the image.
Read about the difference b/t imshow() and imagesc() and you'll see the confusion.
Why not just create a 2nd matrix which is a clone of the first, shift it over and then averate the two matrices?
imIn = imread('Prueba.jpg');
nx = size(d,1);
ny = size(d,2);
% Create temporary matrices padded with nan
tmp1 = [nan(ny,2), d];
tmp2 = [d, nan(ny,2)];
imOut = tmp1;
imOut(:,:,2) = tmp2;
% use nanmean so the mean is just the value of the 1 column
imOut = nanmean(imOut,3);
out = imOut(2:end-1,:);
Try to use this
imageIn = imread('Prueba.jpg');
imageIn = rgb2gray(imageIn);
imageOut = zeros(size(imageIn));
ny = size(imageIn, 1);
nx = size(imageIn, 2);
imshow(imageIn);
u = [];
v = [];
tic
for i = 1:ny
u = imageIn(i,:);
v = zeros(1, ny);
for k = 2:ny-1
v(k) = (uint32(u(k-1))+uint32(u(k))+uint32(u(k+1)))/3;
end
%Special cases first and last pixel
v(1) = (uint32(u(2))+uint32(u(1))+uint32(u(2)))/3;
v(ny) = (uint32(u(ny-1))+uint32(u(ny))+uint32(u(ny-1)))/3;
imageOut(i,:) = v;
end
toc
imshow(imageOut);