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?

7 Comments

Nobody will have any idea what's wrong with your code if you don't post your code.
What is the code?
What is the intended output?
What is the result?
If you translate this, you need to increase the background as well. What you expect? Show us your code and requirements.
This is my code
% resizing the shape (a) according to the scale factor
a=imresize(a,[round(msize/scale(i)),round(msize/scale(i))],'nearest');
ma=round(size(a,1)/2);
na=round(size(a,1)/2);
% normalizing the image
a=double(a);
a=a/max(a(:));
a=round(a);
% placing the shape in the right position according to the
% center coordnates
Pad=sparse(2*ma+msize+1,2*na+nsize+1);
% extracting the indices of the top left corner of the image to be
% included in the pad where centy and cent x new center for shape
ip=round((1-ycenty(i))*msize)+ma+1;
jp=round(centx(i)*nsize)+na+1;
Pad(ip-ma:ip-ma+size(a,1)-1,jp-na:jp-na+size(a,2)-1)=sparse(a);
Pad=sparse(Pad(ma:ma+msize-1,na:na+nsize-1));
% imagesc(flipud(Pad));
M(:,i)=Pad(:);
@KSSV If I want to keep the background smae like original one I have to use specific numbers during translate also?
if msize, nsize are the height and width of a, why are both terms of the imresize vector based on msize?
I'm not really sure what the intent is here or why this doesn't suit it. It does work once all the missing pieces are filled in, but I'm assuming that all the rest of your code works as intended.
Like IA mentioned, I imagine you could adapt imtranslate to your needs. If you need the output to be sparse, you could convert the output from intranslate into a sparse array.
a = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/801479/image.jpeg');
centy = 0.4;
centx = 0.4;
scale = 1;
[msize nsize] = size(a);
% resizing the shape (a) according to the scale factor
a = imresize(a,[round(msize/scale),round(nsize/scale)],'nearest');
ma = round(size(a,1)/2);
na = round(size(a,1)/2);
% normalizing the image?
%a = im2double(a); % this rescales an image to unit scale (0-1)
%a = mat2gray(a); % this normalizes an image to its extrema
a = im2double(a) >= 0.5; % this does what your code did
% placing the shape in the right position according to the
% center coordnates
Pad = sparse(2*ma+msize+1,2*na+nsize+1);
% extracting the indices of the top left corner of the image to be
% included in the pad where centy and cent x new center for shape
ip = round((1-centy)*msize)+ma+1;
jp = round(centx*nsize)+na+1;
Pad(ip-ma:ip-ma+size(a,1)-1,jp-na:jp-na+size(a,2)-1) = sparse(a);
Pad = sparse(Pad(ma:ma+msize-1,na:na+nsize-1));
imshow(full(Pad))
@DGM thank you
Could you please tell me why if we take number more than 1 for center this shape go out the frame of the image
Is this because we use binary image or what?
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.

Sign in to comment.

Answers (2)

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

Categories

Find more on Images in Help Center and File Exchange

Asked:

on 16 Nov 2021

Answered:

DGM
on 16 Nov 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!