【问题标题】:MATLAB help cropping an imageMATLAB 帮助裁剪图像
【发布时间】:2013-04-20 02:07:23
【问题描述】:

我正在使用 MATLAB R2012a,我试图让用户在不使用内置函数的情况下裁剪图像。

这是我的代码:

[x, y] = ginput(2);
m1 = [x(1), y(1)];
m2 = [x(2), y(2)];
m1 = int16(m1);
m2 = int16(m2);
[m, n] = size(manip);
s1 = (m2(1) - m1(1))+1;
s2 = (m2(2) - m2(2))+1;
temp = zeros([s1, s2],('uint8'));
p1 = 0;
p2 = 0;
for c1 = 1:m
    if ((c1 <= m1(2)) && (c1 >= m2(2)))
        for c2 = 1:n
            if ((c2 <= m1(1)) && (c2 >= m2(1)))
                temp(p1, p2) = manip(c1, c2);
            end
            p2 = p2 + 1;
        end
    end
    p1 = p1 + 1;
end
out = temp;



这是我的结果:


关于我做错了什么的任何想法,我似乎都能看到。谢谢。

【问题讨论】:

    标签: image matlab crop


    【解决方案1】:

    我想你的错误在这里:s2 = (m2(2) - m2(2))+1; 这不应该是s2 = (m2(2) - m1(2))+1; 吗?

    但是你根本不需要那个循环:

    Iold = rand(300);
    %crop 10 pixels off each side
    Inew = Iold(11:end - 10, 11: end - 10);
    

    或者,如果您需要相同大小的图像,但裁剪位的位置为零:

    Inew = zeros(size(Iold));
    Inew(11:end - 10, 11: end - 10) = Iold(11:end - 10, 11: end - 10);    
    

    或者概括一下:

    Inew(xmin:xmax, ymin:ymax) = Iold(xmin:xmax, ymin:ymax);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-05
      • 2012-02-12
      • 1970-01-01
      • 2011-09-03
      • 1970-01-01
      • 1970-01-01
      • 2017-05-22
      相关资源
      最近更新 更多