image - 正确理解不正确的MATLAB运算

标签 image matlab image-processing crop segment

我有一张图片,

3 FIGURES .

我想这样做:

3 FIGURES

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

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];

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

我该如何解决这个问题?

最佳答案

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

crop_area = round(wait(h))

代替

crop_area = wait(h)

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

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

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

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

关于image - 正确理解不正确的MATLAB运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17964667/

相关文章:

matlab - 为什么 eigs( 'lm' ) 比 eigs ('sm' 快得多)

matlab - MATLAB rgb2hsv()与OpenCV cvtColor()

opencv - Color 上的模板匹配行为

c++ - 模板匹配亚像素精度

android - Android中线程加载的Web图像

html - CSS:在 <p> 中将鼠标悬停在文本上时显示图片

Django ImageField 和 BinaryField 如何处理图像

matlab - 在 MATLAB 中使用 patch 时出错

python - 如何从两幅相似图像中提取噪声?

java - ImageIO.read(URL) 需要很长时间