I want to make translation for this shape in binary image inside dictionary why when I try to move the center of the shape to point more than 1 this shape go out image frame?
2 次查看(过去 30 天)
显示 更早的评论
7 个评论
DGM
2021-11-16
centx and centy describe the location of the displaced image in normalized image coordinates. Assuming no scaling occurs, if you use centx,centy = 0.5, the image remains unchanged. If you use centy = 0.5 and centx = 1, the point which used to be in the center of the image is now translated to the center of the right edge. Using a center of 1.5 would push all possible image content out of frame.
I'm assuming the use of normalized image coordinates is by design, so I was assuming that was your choice.
回答(2 个)
DGM
2021-11-16
Not knowing the details of what you're trying to really do, this essentially replicates the behavior, but using IPT tools. It's probably not faster or anything, but it does allow for the center to extend to negative values, which would be required for translations beyond the south or west edges.
a = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/801479/image.jpeg')>=128;
newcenter = [0.5 0.8]; % [x y]
scale = 0.5;
% resize without changing image geometry
s0 = [size(a,1) size(a,2)];
if scale > 1
a = imresize(a,scale,'nearest');
s = [size(a,1) size(a,2)];
ps = fliplr((s - s0)/2);
a = imcrop(a,[round(ps) fliplr(s0)-1]);
elseif scale < 1
a = imresize(a,scale,'nearest');
ps = (s0 - [size(a,1) size(a,2)])/2;
a = padarray(a,ceil(ps),0,'pre');
a = padarray(a,floor(ps),0,'post');
end
% translate
t = (newcenter - [0.5 0.5]).*[s0(2) -s0(1)];
a = imtranslate(a,t,'fillvalues',0);
imshow(a)
Again, the result can just be converted to sparse if needed
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!