Frq Khan
Frq Khan

Reputation: 175

Dividing the image into equal number of parts in Matlab

I have lena image in Matlab. First I need to find the centroid C, and then divide the image into equal number of parts. I can calculate the centroid of the image but from that how can I divide the image into equal number of parts as shown below. Please anyone help me.

Thanks

Image

Upvotes: 2

Views: 1287

Answers (2)

Santhan Salai
Santhan Salai

Reputation: 3898

Using poly2mask to create binary sectors and using the resulting sectors for indexing

Code:

im = imread('peppers.png');
r = 300;
out1 = ones(max(size(im,1),r*2)+2,max(size(im,2),r*2)+2,3).*255;

xoffset = floor((size(out1,2)-size(im,2))/2);
yoffset = floor((size(out1,1)-size(im,1))/2);

out1(yoffset:yoffset+size(im,1)-1,xoffset:xoffset+size(im,2)-1,:) = im(:,:,:);
im = out1;

cy = floor(size(im,1)/2);
cx = floor(size(im,2)/2);

figure;
imshow(uint8(im));
hold on
pos = [cx-r+1 cy-r+1 r*2 r*2];
rectangle('Position',pos,'Curvature',[1 1]);
x1 = [-r, 0, -r*cosd(45), -r*cosd(45); r, 0, r*cosd(45), r*cosd(45)]+cx+1;
y1 = [0, -r, -r*sind(45), r*sind(45); 0, r, r*sind(45), -r*sind(45)]+cy+1;
plot(x1,y1);
hold off

figure;
for i = 0:45:315
    t = linspace(-i,-i-45,128);
    x = [cx, cx+r*cosd(t), cx];
    y = [cy, cy+r*sind(t), cy];
    bw = poly2mask( x, y, size(im,1),size(im,2));
    bw = repmat(bw,1,1,3);
    out = ones(size(im,1),size(im,2),size(im,3)).*155;
    out(bw) = im(bw);
    subplot(2,4,(i/45)+1); imshow(uint8(out));
end;

Results:

Original Image

enter image description here

Partitions drawn over Original Image

enter image description here

Segments of the image

enter image description here

Update

for getting pixel values of the lines, by using Bresenham function from here

figure;
bw1 = zeros(size(im,1),size(im,2));
outmat = zeros(size(bw1));
[X,Y] = bresenham(cx+1-r,cy+1,cx+1+r,cy+1);
ind = sub2ind(size(outmat), Y, X);
outmat(ind) = 1;
[X,Y] = bresenham(cx+1,cy+1-r,cx+1,cy+1+r);
ind = sub2ind(size(outmat), Y, X);
outmat(ind) = 1;
[X,Y] = bresenham(cx+1-r*cosd(45),cy+1-r*sind(45),cx+1+r*cosd(45),cy+1+r*sind(45));
ind = sub2ind(size(outmat), Y, X);
outmat(ind) = 1;
[X,Y] = bresenham(cx+1-r*cosd(45),cy+1+r*sind(45),cx+1+r*cosd(45),cy+1-r*sind(45));
ind = sub2ind(size(outmat), Y, X);
outmat(ind) = 1;
se = strel('disk',5);    %// change the '5' value to affect thickness of the line
outmat = imdilate(outmat,se);
outmat = repmat(boolean(outmat),1,1,3);
outmat1 = zeros(size(outmat));
outmat1(outmat) = im(outmat);
imshow(uint8(outmat1));

Pixel values under each lines

enter image description here

Upvotes: 4

Check the following code. I just did it for a grayscale image. You can now change it to a color image as well. Check and pls confirm this is what you wanted.

clear all;

i = rgb2gray(imread('hestain.png'));
imshow(i);

cr = floor(size(i,1)/2);
cl = floor(size(i,2)/2);

r = min(cr, cl);
a = 90;

r1 = cr;
c1 = size(i,2);
v1=[c1 r1]-[cl cr];

i2 = zeros(size(i,1),size(i,2),ceil(360/a));

for ri = 1:size(i,1)
    for ci = 1:size(i,2)
        v2=[ci ri]-[cl cr];
        a2 = mod(-atan2(v1(1)*v2(2)-v1(2)*v2(1), v1*v2'), 2*pi) * 180/pi;
        d2 = pdist([ci ri; cl cr],'euclidean');
        if d2<=r
            if ceil(a2/a)==0
                a2 =1;
            end
            i2(ri,ci,ceil(a2/a)) = i(ri,ci);
        end
    end
end

figure;
for i=1:360/a
    subplot(2,180/a,i);
    imshow(mat2gray(i2(:,:,i)));
end

Sample output: enter image description here

Upvotes: 2

Related Questions