Help me with color patches

2 vues (au cours des 30 derniers jours)
Mohammad Alwardat
Mohammad Alwardat le 22 Mar 2020
Hi,
I have an image and I patched it into 64 patches and I stuided the similarity between these patches.
Now, I need to create color table for my patches and construct my images based on color patches.
This is my code
close all
clear all
clc
bw = imread('cameraman.tif');
%bw = im2bw(I,0.5);
figure, imshow(bw); title('The input image')
imSz = size(bw);
patchSz = [32 32];
xIdxs = [1:patchSz(2):imSz(2) imSz(2)+1];
yIdxs = [1:patchSz(1):imSz(1) imSz(1)+1];
patches = cell(length(yIdxs)-1,length(xIdxs)-1);
for i = 1:length(yIdxs)-1
Isub = bw(yIdxs(i):yIdxs(i+1)-1,:);
for j = 1:length(xIdxs)-1
patches{i,j} = Isub(:,xIdxs(j):xIdxs(j+1)-1);
end
end
patches = patches(:);
patches = cellfun(@(x) {double(x(:))}, patches);
A = zeros (numel(patches));
for i=1:numel(patches)
for j=1:numel(patches)
A(i, j) = getCosineSimilarity(patches{i},patches{j});
end
end
A(isnan(A)) = 0;
Is_A_Symm = issymmetric(A);
d = sum(A);
D = diag(d);
L = D - A; % determine the unnormilazied Laplacian matrix (L)
figure, imagesc (L); title ('imagesc L matrix ')
nL = (full(D)^(-0.5))*L*(full(D)^(-0.5)); % determine the normalized Laplacian matrix (nL)
figure, imagesc (L); title ('imagesc nL matrix ')
[E_vec_nL,E_val_nL] = eig(nL); % eign vectors and values of Normilized Laplacian matrix
[E_vec_L,E_val_L] = eig(full(L)); % eign vectors and values of unnormilazied Laplacian matrix
figure, imagesc (nL); title ('nL matrix')
figure, imagesc (L); title('L matirx')
figure, stem (E_val_nL); title ('egin values for nL matirx')
figure, stem (E_val_L); title ('egin values for L matirx')
figure, stem (E_vec_nL); title ('egin vectors for nL matirx')
figure, stem (E_vec_L); title ('egin vectors for L matirx')
r_m = kmeans(E_vec_L(:,1:3),3,'replicates',100,'emptyAction','singleton');
figure, stem(r_m); title('Stem r_m')
% ******************************************************
% get cosine similarity function
function Cs = getCosineSimilarity(x,y)
%
% call:
%
% Cs = getCosineSimilarity(x,y)
%
% Compute Cosine Similarity between vectors x and y.
% x and y have to be of same length. The interpretation of
% cosine similarity is analogous to that of a Pearson Correlation
%
% R.G. Bettinardi
% -----------------------------------------------------------------
if isvector(x)==0 || isvector(y)==0
error('x and y have to be vectors!')
end
if length(x)~=length(y)
error('x and y have to be same length!')
end
xy = dot(x,y);
nx = norm(x);
ny = norm(y);
nxny = nx*ny;
Cs = xy/nxny;
  6 commentaires
Image Analyst
Image Analyst le 22 Mar 2020
Still not sure what you want. Do you want to color each blob with a different color instead of using only 3 colors? Like you'd get with label2rgb()?
Mohammad Alwardat
Mohammad Alwardat le 22 Mar 2020
I need to color my patches. Give same color to the similar patches. My Dr recommended me using colorbar()but I don’t how to use it.

Connectez-vous pour commenter.

Réponses (1)

Image Analyst
Image Analyst le 22 Mar 2020
Do you mean like using label2rgb(), like this:
grayImage = imread('cameraman.tif');
subplot(2, 2, 1);
imshow(grayImage);
subplot(2, 2, 2);
histogram(grayImage);
grid on;
binaryImage = grayImage < 100;
subplot(2, 2, 3);
imshow(binaryImage);
labeledImage = bwlabel(binaryImage);
% Let's assign each blob a different color to visually show the user the distinct blobs.
coloredLabels = label2rgb (labeledImage, 'hsv', 'k', 'shuffle'); % pseudo random color labels
% coloredLabels is an RGB image. We could have applied a colormap instead (but only with R2014b and later)
subplot(2, 2, 4);
imshow(coloredLabels);
  5 commentaires
Image Analyst
Image Analyst le 22 Mar 2020
I don't know too much about graph theory, but attached are some examples of how to do "spectral clustering" on RGB images. One uses linear discriminant analysis based on regions you draw. One uses K Nearest Neighbors. And the other uses kmeans. I think there is a function called evalclusters that will let you pick the right k if you don't have a particular k in mind.
Mohammad Alwardat
Mohammad Alwardat le 22 Mar 2020
Thnak you so much ♥

Connectez-vous pour commenter.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by