Image 正确理解imrect MATLAB操作
我有一个形象 我想这样做: 在做了一些操作之后,我应该能够重新组合图像以获得最终结果。我的代码是:Image 正确理解imrect MATLAB操作,image,matlab,image-processing,crop,segment,Image,Matlab,Image Processing,Crop,Segment,我有一个形象 我想这样做: 在做了一些操作之后,我应该能够重新组合图像以获得最终结果。我的代码是: 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)
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将图像处理为矩阵,并将其转换为离散组件。所以所有的值都应该是整数
我怎样才能解决这个问题呢?对我来说,在大多数情况下,写作就足够了
crop_area = round(wait(h))
而不是
crop_area = wait(h)
我注意到,imrect
在以下情况下行为异常:
- 图像被放大或缩小,因此物理屏幕像素与图像像素一对一不匹配(缩放级别~=100%)
- 矩形具有带有
的约束,然后被移动/调整到极限makeConstrainToRectFcn
第一个问题可通过
imshow解决(图像“初始放大”,100)代码>如果图像较小,则显示屏幕。否则,您将需要imscrollpanel
和imoverviewpanel
造成差异的原因是imrect、imcrop等使用的rect描述不是指像素中心,而是指像素边界。如imcrop文件所述:
因为rect是根据空间坐标指定的,所以宽度和
rect的高度元素并不总是与大小完全对应
输出图像的一部分。例如,假设rect为[20 40 30],使用
默认的空间坐标系。屏幕的左上角
指定的矩形是像素(20,20)的中心和
右下角是像素(50,60)的中心。结果
输出图像是31乘41,而不是30乘40,因为输出图像
包括输入图像中完全或完全相同的所有像素
部分封闭
解决方案是使用以下函数将rect向量转换为行和列索引:
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);
你的评估是正确的。但是,你能告诉我如何做到这一点,使物理屏幕像素与图像像素匹配吗?@roni,你必须强制缩放级别精确到100%,请参见我的答案编辑谢谢,我会尝试一下