Automatic reposition of mask on image
4 views (last 30 days)
Show older comments
Andraz Skoda
on 3 Oct 2016
Commented: Andraz Skoda
on 11 Dec 2016
Hello,
I'm beginner in digital image processing. I am working on application that requires automatic image masking. With image masking, I mean that I have to extract from the image not important areas with previously defined mask. I explain this by showing you image bellow:
As you can see in the image above, I used 'mask' in the shape of the star (red curved star shape), to extract only star from the image and get the resulting image that has only star on it. I simply did this by creating mask using matlab's roipoly() function, save created mask to HD, then multiply created mask with original image to 'mask' other features that are not star.
My problem is that I have to handle masking if star changes its position a little bit. For example star can move for 100px to the right. In this case the predefined mask (red star shaped) is not laying completely perfect on the star and masking is not correct. The problem I show you on the image below:
I ask you guys, if anybody can help me to handle the problem? I thought of some king of 'pattern matching' based on predefined object's shape, but I could not handle it. If the 'black star' is moved a little bit, the mask ('red star') should be also moved to mask the black star perfectly.
Thanks all for answers!
0 Comments
Accepted Answer
Gopichandh Danala
on 4 Oct 2016
Edited: Gopichandh Danala
on 4 Oct 2016
Hi, I tried to solve your question,
I took the original image and then extracted shapes from it, took the star for reference map.
Now as of ur question shifted the map and tried to reposition it automatically and mapped it on the original star shape.
If this solves your question accept the answer or if you have any doubts let me know
Here is my Code:
%%Read the img..
img = imread('img.png');
img = double(rgb2gray(img));
figure, imshow(img,[]);
[BW num] = bwlabel(img);
for i = 1: num
objseparate(:,:,i) = BW == i;
end
%%Get the labels..
figure,
subplot(221), imshow(objseparate(:,:,1),[]),title('LABEL1');
subplot(222), imshow(objseparate(:,:,2),[]),title('LABEL2');
subplot(223), imshow(objseparate(:,:,3),[]),title('LABEL3');
subplot(224), imshow(objseparate(:,:,4),[]),title('LABEL4');
shape = cell(1,num);
%%mapping to get shapes
for i=1:num
shape{i} = objseparate(:,:,i) .* img;
end
figure,
subplot(221), imshow(shape{1},[]),title('BACKGROUND');
subplot(222), imshow(shape{2},[]),title('STAR');
subplot(223), imshow(shape{3},[]),title('TRIANGLE');
subplot(224), imshow(shape{4},[]),title('SQUARE');
%%Get the Boundary map imgs
se = strel('disk',2);
starMap = shape{2}-imerode(shape{2},se);
trinagleMap = shape{3}-imerode(shape{3},se);
squareMap = shape{4}-imerode(shape{4},se);
figure, imshow(starMap,[]);
%%lets try what you want here iam only doing star map u can try rest
% shift star by a bit to try and reposition it to map as ur requirement
% Orignal star
star = starMap;
K = bwlabel(star);
centK = regionprops(K,'Centroid');
xK = centK.Centroid(1);
yK = centK.Centroid(2);
% shift
T = maketform('affine', [1 0 0; 0 1 0; 50 100 1]); %# represents translation
shiftstar = imtransform(star, T, ...
'XData',[1 size(star,2)], 'YData',[1 size(star,1)]);
figure,
subplot(121), imshow(star,[]), title(' initial map');
subplot(122), imshow(shiftstar,[]), title('Your Shifted map');
L = bwlabel(shiftstar);
centL = regionprops(L,'Centroid');
xL = centL.Centroid(1);
yL = centL.Centroid(2);
x_diff = xK - xL;
y_diff = yK - yL;
% doing circshift to reposition
newImg = circshift(shiftstar,[round((y_diff)) round((x_diff)) ]);
figure, imshow(newImg, []);
% Mapping to see if its correct...
mapnewImg = newImg .*shape{2} ;
figure, imshow(mapnewImg,[]), title('Mapping: shape and shifted mapnewImg');
%%final check..
figure,
subplot(221), imshow(img,[]), title('original img');
subplot(222), imshow(shape{2},[]), title('Original star shape');
subplot(223), imshow(shiftstar,[]), title('Your Shifted starMap');
subplot(224), imshow(mapnewImg,[]), title('Mapping: shape and shifted mapnewImg');
% i hope this is what you want was fun trying it..
I am attaching the finalIMG for reference...
Good Luck...Let me know
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!