【问题标题】:Dividing the image into equal number of parts in Matlab在Matlab中将图像分成相等数量的部分
【发布时间】:2015-06-25 04:01:40
【问题描述】:

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

谢谢

【问题讨论】:

  • 您希望如何处理包含空白的部分?像白色的?
  • 您还希望最终图像的方向如何?例如。如果你把它切成 16 个部分并且没有水平/垂直切割
  • 顶部/底部和左/右的白色部分将被零值替换。实际上,首先我想将图像划分为如上所示。然后我想计算每一行下的像素
  • 你只需要每行下的像素吗?这听起来完全是一个不同的问题/方法
  • 最后一步是的..但我只想这样画。

标签: image matlab image-processing computer-vision


【解决方案1】:

使用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;

结果:

原始图像

在原始图像上绘制的分区

图像的片段

更新

使用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));

每个行下的像素值

【讨论】:

  • 如何计算每条彩色线下的像素数?
  • @ frqkhan'''''?你是什​​么意思?不。每个扇区的像素数?
  • 我的意思是我想计算在0,45,90度 span>的每行中的所有像素值。
  • 如何获得不同角度线的值? span>
  • @FrqKhan 如果您不再需要此问题的帮助,请考虑接受答案
【解决方案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

示例输出:

【讨论】:

  • 非常感谢您展示示例代码。我想知道,如何画一条线来分割图像,如上图所示?
  • 由于我的最终目标是计算线下的像素数,我该怎么做呢?
  • 我认为您现在可以轻松地继续使用此代码 :-)
  • Hwathanie:你能解释一下我如何用这段代码做到这一点吗?
  • 一种更简单的方法可能是,遍历图像中的每个像素并找到像素与中心形成的角度,如上面的代码所示。然后,您可以根据需要计算每条线上的像素数。
猜你喜欢
  • 1970-01-01
  • 2012-11-25
  • 2011-02-15
  • 1970-01-01
  • 1970-01-01
  • 2015-05-23
  • 1970-01-01
  • 1970-01-01
  • 2020-12-23
相关资源
最近更新 更多