如何放大图像中的特定位置?
How do i zoom in around specific locations in an image?
我有图片:
img = [1 1 1 3 3 3 3 3 3;
1 1 1 3 3 3 3 3 3;
1 1 1 3 3 3 3 3 3;
1 1 2 3 3 3 3 3 3;
1 1 2 2 2 2 2 1 1;
1 2 2 2 2 2 1 1 1;
1 2 2 2 2 1 1 1 1];
假设我有兴趣查看特定位置的更详细信息:
Indx = [18; 47];
坐标:
rows = [4; 5] and cols = [3; 7]
我了解 “zoom on/off”
允许通过按下鼠标按钮进行交互式缩放。但是,除了这种手动方法之外,还有没有办法以编程方式要求 matlab 进行缩放 - 比如这些位置(或更多)的 3x3 neighbourhood
?每当调用 ‘imshow’
时?
拜托,我需要 help/advice/suggestions。非常感谢期待。
有一块 code 可以为您提供您点击的点的像素位置。您可以有效地使用此代码:
function [loc] = get_image_point (I)
figure('name','Doubleclick to set location');imshow(I);
[c r] = getpts(1);
loc = int32([c r]);
if size(loc)>1
loc = [loc(1,1) loc(1,2)];
end
close all;
end
有了那个像素位置,你可以创建一个具有指定尺寸的图形,假设你双击 image
中的像素位置 (x,y)
。然后,您可以简单地说 figure('Position', [0 0 screenWidth screenHeight]), imshow(image(x-x1:x+x1, y-y1:y+y1))
。确保 x+-x1
和 y+-y1
在范围内。
可能是 "overkill",但您可以使用 imwarp
函数:
imwarp
允许缩放位移(以及更多)。
假设:
(center_x, center_y)
是您的兴趣点。
- 输出图像(缩放后)与输入图像大小相同。
- 缩放后兴趣点应位于图像的中心。
我在感兴趣的点画了一个叉来测试。
我使用 'peppers.png'
图像进行演示。
这是我的代码示例:
I = imread('peppers.png');
w = size(I, 2); %Image width
h = size(I, 1); %Image height
zoom = 4; %Zoom factor x4
%Point of interest.
center_x = w/2 - 80;
center_y = h/2 - 50;
%Draw center cross for testing (thickness is 2 pixels):
I(center_y-1:center_y, center_x-5:center_x+4, :) = 255;
I(center_y-5:center_y+4, center_x-1:center_x, :) = 255;
figure;imshow(I);
%Compute displacement:
x0 = w/2 - zoom*center_x;
y0 = h/2 - zoom*center_y;
%Build transformation matrix T.
T = [zoom 0 0; ...
0 zoom 0; ...
x0 y0 1];
tform = affine2d(T); %Needed by imwarp
%J = imwarp(I, tform, 'OutputView', imref2d(size(I)), 'Interp', 'nearest'); %Select nearest interpolation.
%Apply transformation (dimensions of J will be the same as I).
J = imwarp(I, tform, 'OutputView', imref2d(size(I)), 'Interp', 'cubic'); %Select cubic interpolation.
figure;imshow(J);
输入图片(注意小十字):
输出图像:
我有图片:
img = [1 1 1 3 3 3 3 3 3;
1 1 1 3 3 3 3 3 3;
1 1 1 3 3 3 3 3 3;
1 1 2 3 3 3 3 3 3;
1 1 2 2 2 2 2 1 1;
1 2 2 2 2 2 1 1 1;
1 2 2 2 2 1 1 1 1];
假设我有兴趣查看特定位置的更详细信息:
Indx = [18; 47];
坐标:
rows = [4; 5] and cols = [3; 7]
我了解 “zoom on/off”
允许通过按下鼠标按钮进行交互式缩放。但是,除了这种手动方法之外,还有没有办法以编程方式要求 matlab 进行缩放 - 比如这些位置(或更多)的 3x3 neighbourhood
?每当调用 ‘imshow’
时?
拜托,我需要 help/advice/suggestions。非常感谢期待。
有一块 code 可以为您提供您点击的点的像素位置。您可以有效地使用此代码:
function [loc] = get_image_point (I)
figure('name','Doubleclick to set location');imshow(I);
[c r] = getpts(1);
loc = int32([c r]);
if size(loc)>1
loc = [loc(1,1) loc(1,2)];
end
close all;
end
有了那个像素位置,你可以创建一个具有指定尺寸的图形,假设你双击 image
中的像素位置 (x,y)
。然后,您可以简单地说 figure('Position', [0 0 screenWidth screenHeight]), imshow(image(x-x1:x+x1, y-y1:y+y1))
。确保 x+-x1
和 y+-y1
在范围内。
可能是 "overkill",但您可以使用 imwarp
函数:
imwarp
允许缩放位移(以及更多)。
假设:
(center_x, center_y)
是您的兴趣点。- 输出图像(缩放后)与输入图像大小相同。
- 缩放后兴趣点应位于图像的中心。
我在感兴趣的点画了一个叉来测试。
我使用 'peppers.png'
图像进行演示。
这是我的代码示例:
I = imread('peppers.png');
w = size(I, 2); %Image width
h = size(I, 1); %Image height
zoom = 4; %Zoom factor x4
%Point of interest.
center_x = w/2 - 80;
center_y = h/2 - 50;
%Draw center cross for testing (thickness is 2 pixels):
I(center_y-1:center_y, center_x-5:center_x+4, :) = 255;
I(center_y-5:center_y+4, center_x-1:center_x, :) = 255;
figure;imshow(I);
%Compute displacement:
x0 = w/2 - zoom*center_x;
y0 = h/2 - zoom*center_y;
%Build transformation matrix T.
T = [zoom 0 0; ...
0 zoom 0; ...
x0 y0 1];
tform = affine2d(T); %Needed by imwarp
%J = imwarp(I, tform, 'OutputView', imref2d(size(I)), 'Interp', 'nearest'); %Select nearest interpolation.
%Apply transformation (dimensions of J will be the same as I).
J = imwarp(I, tform, 'OutputView', imref2d(size(I)), 'Interp', 'cubic'); %Select cubic interpolation.
figure;imshow(J);
输入图片(注意小十字):
输出图像: