【问题标题】:Correct understanding of imrect MATLAB operation正确理解imrect MATLAB操作
【发布时间】:2013-07-31 15:23:30
【问题描述】:

我有一张图片,

.

我想这样做:

做了一些操作后,我应该能够重新组合图像以获得最终结果。我的代码是这样的:

clc;
clear all;
close all;
tic
I = imread('ChanVese.jpg');
I = imresize(I, [128 128]);
Img = I;
I = double(I(:, :, 1));

figure();
imshow(Img);
% // As there are three figures
crop_pos = zeros(3, 4);
new_image = zeros(size(I));
c = cell(1, 3);
for i=1:3
    % // Sub-divide the image
    h = imrect(gca);
    % // To make the rect function bounded within the image size
    addNewPositionCallback(h, @(p) title(mat2str(p, 3)));
    fcn = makeConstrainToRectFcn('imrect', get(gca, 'XLim'), get(gca, 'YLim'));
    setPositionConstraintFcn(h, fcn);
    crop_area = wait(h)
    crop_pos(i, :) = (crop_area);
    % // Cropped is the new cropped image on which we will do our operation
    cropped = (imcrop(Img, crop_area));
    c{i} = cropped;


    % // Do operation on the image
    %***************************
    % Code to be written
    %***************************


    % // Insert the part-image back into the image
    new_image(crop_pos(i, 2):crop_pos(i, 4), crop_pos(i,1):crop_pos(i, 3)) = c{i};
end

imagesc(new_image, [0 255]),colormap(gray);axis on
toc

我的问题在于 imrect 函数:我将尝试举一个例子。即使我选择大小为 [128x128] 的整个图像, 我得到crop_pos的输出为

[x,y,w,h] = [0.5, 0.5, 128, 128]

其实应该是这样的

[x, y, w, h] = [1, 1, 128, 128];

有时宽度和高度以浮点数给出。为什么会这样?我相信 MATLAB 将图像处理为矩阵,并将它们转换为离散组件。所以所有的值都应该是整数。

我该如何解决这个问题?

【问题讨论】:

    标签: image matlab image-processing crop segment


    【解决方案1】:

    产生差异的原因是imrect、imcrop等使用的rect描述不是指像素中心,而是指像素边界。如 imcrop 文档中所述:

    因为 rect 是根据空间坐标指定的,所以宽度和 rect 的高度元素并不总是与大小完全对应 的输出图像。例如,假设 rect 是 [20 20 40 30],使用 默认空间坐标系。左上角的 指定的矩形是像素 (20,20) 的中心,并且 右下角是像素的中心 (50,60)。所结果的 输出图像是 31×41,而不是 30×40,因为输出图像 包括输入图像中完全或 部分封闭

    您的解决方案是将矩形向量转换为行和列索引,例如,使用如下函数:

    function [x,y] = rect2ind(rect)
    %RECT2IND convert rect vector to matrix index vectors
    % [x,y] = rect2ind(rect) converts a rect = [left top width height] vector
    % to index vectors x and y (column and row indices, respectively), taking
    % into account that rect specifies the location and size with respect to
    % the edge of pixels. 
    %
    % See also IMRECT, IMCROP
    
    left = rect(1);
    top = rect(2);
    width = rect(3);
    height = rect(4);
    
    x = round(left + 0.5):round(left + width - 0.5);
    y = round(top + 0.5):round(top + height - 0.5);
    

    【讨论】:

      【解决方案2】:

      对我来说,大多数情况下,写就够了

      crop_area = round(wait(h))
      

      而不是

      crop_area = wait(h)
      

      我注意到,imrect 在以下情况下表现异常:

      • 图像被放大或缩小,因此物理屏幕像素与图像像素一一对应(缩放级别 ~= 100%)
      • 矩形具有makeConstrainToRectFcn 的约束,然后被移动/调整到限制

      但这些是我个人的观察。在这种情况下甚至可能存在与平台相关的问题,我不知道。

      1st 问题可以通过imshow(Image, 'InitialMagnification',100); 解决,如果图像比屏幕小。否则你需要imscrollpanelimoverviewpanel

      【讨论】:

      • 您的评估是正确的。但是你能告诉我如何做到这一点,使物理屏幕像素与图像像素匹配吗?
      • @roni,您必须将缩放级别强制为 100%,请参阅我对答案的编辑
      • 谢谢,我会试试的
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多