【问题标题】:matlab image: return pixel values along the boundingbox in an image?matlab image:沿图像中的边界框返回像素值?
【发布时间】:2012-10-03 03:11:49
【问题描述】:

所以,我有一个 RGB 图像,并且在图像的一个区域周围放置了一个矩形(边界框)。但是,我无法获得沿该矩形周边的像素值。

我尝试查看函数regionprops,但没有发现任何有用的东西。

我以为我可以通过知道边界框上的(x,y) 点列表(x_inity_initx_widthy_width)来获得像素值,但没有具体的功能。有人可以帮忙吗?

【问题讨论】:

    标签: image-processing pixel matlab


    【解决方案1】:

    不知道图像处理工具箱中是否有专门的功能可以做到这一点,但是你描述的功能很简单,可以自己实现:

    function pixel_vals = boundingboxPixels(img, x_init, y_init, x_width, y_width)
    
        if x_init > size(img,2) 
            error('x_init lies outside the bounds of the image.'); end
        if y_init > size(img,1)
            error('y_init lies outside the bounds of the image.'); end
    
        if y_init+y_width > size(img,1) || x_init+x_width > size(img,2) || ...
           x_init < 1 || y_init < 1
            warning([...
                'Given rectangle partially falls outside image. ',... 
                'Resizing rectangle...']);
        end
    
        x_min   = max(1, uint16(x_init));
        y_min   = max(1, uint16(y_init));
        x_max   = min(size(img,2), x_min+uint16(x_width));
        y_max   = min(size(img,1), y_min+uint16(y_width));
        x_range = x_min : x_max;
        y_range = y_min : y_max;
    
        Upper = img( x_range, y_min  , :);
        Left  = img(   x_min, y_range, :);
        Right = img(   x_max, y_range, :);
        Lower = img( x_range, y_max  , :);
    
        pixel_vals = [...
           Upper
           permute(Left, [2 1 3]) 
           permute(Right, [2 1 3])
           Lower];
    
    end
    

    【讨论】:

    • 所以我用图像的像素矩阵替换了“img”
    • 但出现错误 -> 下标索引必须是正整数或逻辑数。
    • 所以我需要为上、左、右、下做一个 for 循环?有没有更简单的处理方法?
    • 我实际上在图像上绘制了边界框,它看起来不像在图像之外。
    • @user1715908:好的,看看最新的编辑。也许您的边界框不受像素限制,而是亚像素(例如,x_init = 104.14253...)。试试最新的代码。如果失败,请发布边界框的值和图像的大小。
    【解决方案2】:

    对于任何其他参考这个问题,我有同样的问题并使用了Rody Oldenhuis方法,但它在我的情况下效果不佳。

    您可以为此使用 matlab 内置函数:

      imgRect=getrect;//get a rectangle region in image
      cropedImg=imcrop(orgImg,[xtopleft ytopleft width height]);//in croppedImg you have the value of specified region
    

    【讨论】:

      猜你喜欢
      • 2015-01-08
      • 1970-01-01
      • 1970-01-01
      • 2010-12-18
      • 1970-01-01
      • 2016-07-02
      • 2021-03-31
      • 2019-01-31
      • 2019-09-19
      相关资源
      最近更新 更多