Extracting center most submatrix from kspace

I have an image of [256,256]
I want the center most submatrix of [32,32] and equate the rest of kspace to zero

1 Comment

You flagged your own Question as being a Duplicate, but you have only posted this Question, so it is not clear what it is a duplicate of ?

Sign in to comment.

 Accepted Answer

Pretty easy. Just get the coordinate of the cluster centroid, then use sort() to get the 32 points closest to the centroid. Is this homework (sounds like it)? Here's a start
% [idx,C,sumd,D] = kmeans(___) returns distances from each point to every centroid in the n-by-k matrix D.
meanCoords = mean(X, 2) % X is N rows (one for each point) by k columns (one for each dimension).
distances = sqrt((X(:, 1) - meanCoords(1)).^ 2 + .........one for each k) you can use a for loop if k is variable/unknown.
[sortedDistances, sortOrder] = sort(distances, 'ascend');
closest32Indexes = sortOrder(1:32)
x32 = X(closest32Indexes, :);

More Answers (1)

YourImage = imread('cameraman.tif');
subsize = [32, 32];
[r, c, p] = size(YourImage);
start_r = floor((r-subsize(1))/2);
start_c = floor((c-subsize(2))/2);
kspace = zeros(size(YourImage), 'like', YourImage);
kspace(start_r:start_r+subsize(1)-1, start_c:start_c+subsize(2)-1,:) = YourImage(start_r:start_r+subsize(1)-1, start_c:start_c+subsize(2)-1,:);
imshow(YourImage)
imshow(kspace)

Categories

Community Treasure Hunt

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

Start Hunting!