Help me with color patches

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 Commenti

Ameer Hamza
Ameer Hamza il 22 Mar 2020
Can you show us an example of your expected output?
Mohammad Alwardat
Mohammad Alwardat il 22 Mar 2020
Modificato: Image Analyst il 22 Mar 2020
I need to cluster my image using similarity algorithm.
The expected result is like this:
darova
darova il 22 Mar 2020
I see only 3 colors. What about binarization? bwlabel?
Yes it’s only 3 colors so it has 3 cluster. The photo is gray scale not binary. “bw” it is just a label not indicate to binary image.
Image Analyst
Image Analyst il 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()?
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.

Accedi per commentare.

Risposte (1)

Image Analyst
Image Analyst il 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 Commenti

No, I need to cluster my image using similarity algorithms. First of all, I patched my image into 64 patches, then I studied the similarity between the patches. Then I give the same color to the similar patch. Finally, I will have a cluster image using similarity algorithm.
Image Analyst
Image Analyst il 22 Mar 2020
Not sure what you want. What needs to be clustered? You can't cluster an image - that's too vague. What is it about the image that needs to be clustered? The gray levels? The region sizes? Something else? How many clusters do you want? Is the number of clusters a constant or variable?
Have you tried the Classification Learner app on the Apps tab of the tool ribbon? I'm not sure what your variables are that should be clustered.
Mohammad Alwardat
Mohammad Alwardat il 22 Mar 2020
Modificato: Mohammad Alwardat il 22 Mar 2020
I’m studying MSc in wireless engineering. This is my midterm exam of graph theory course. Now, the number of cluster is variable, it is depend on eign values of normalized Laplacian matrix. I will attach the photo of my midterm exam and you will understand what I need . ?
Image Analyst
Image Analyst il 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.
Thnak you so much ♥

Accedi per commentare.

Categorie

Scopri di più su Images in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by