在Matlab中将图像分成相同数量的部分

时间:2015-04-18 06:32:47

标签: image matlab image-processing computer-vision

我在Matlab中有lena图像。首先,我需要找到质心 C ,然后将图像分成相同数量的部分。我可以计算出图像的质心,但是从中我可以将图像分成相同数量的部分,如下所示。请有人帮助我。

由于

Image

2 个答案:

答案 0 :(得分:4)

使用poly2mask创建二进制扇区并将结果扇区用于indexing

代码:

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;

结果:

原始图片

enter image description here

在原始图片上绘制的分区

enter image description here

图片细分

enter image description here

更新

用于获取行的像素值,使用here

中的Bresenham函数
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));

每行下的像素值

enter image description here

答案 1 :(得分:2)

检查以下代码。我刚刚为灰度图像做了这件事。您现在也可以将其更改为彩色图像。检查并确认这是您想要的。

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

示例输出: enter image description here