Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reflecting points in an RGB image over a reflection line (MATLAB)

I have an RGB image of size MxNx3. Let's imagine we have a reflection line somewhere in the bottom half of the image. How do I reflect all the points above the line onto the line? I know what I need to do but I can't get it right on MATLAB. Thanks for any help.

For example, in the image below the blue line is the reflection line.

enter image description here

like image 832
saviok Avatar asked Dec 09 '25 05:12

saviok


1 Answers

Code

%%// Read input image
img =imread(IMG_FILEPATH);

%%// Let user select the mask, top of which will basically act 
%%// as the reflection line
figure,imshow(img)
[mask,xi,yi] = roipoly(img);

%%// Remove the last element as that is same as the first one
xi(end)=[];
yi(end)=[];

%%// Find the two corner points each on the left and right sides of the mask
pt_matrix = [xi yi]
[val,ind] = sort(xi)
left_two_pts = pt_matrix(ind(1:2),:)
right_two_pts = pt_matrix(ind(end-1:end),:)
four_pts = round([left_two_pts;right_two_pts])

%%// Remove a 5x5 neighborhood around the four corners, so that biggest
%%// blob that is the line could be separated out
BW1 = edge(mask,'canny');
for k = 1:4
    BW1(four_pts(k,2)-2:four_pts(k,2)+2,four_pts(k,1)-2:four_pts(k,2)+1) = 0;
end

%%// Get the biggest blob that is the reflection line
[L, num] = bwlabel(BW1);
counts = sum(bsxfun(@eq,L(:),1:num));
[~,ind] = max(counts);
BW1 = (L==ind);

%%// Connect the endpoints of the line to left and right sides of the image
xlimit = [find(sum(BW1,1),1) find(sum(BW1,1),1,'last')];
[row1,col1] = ind2sub(size(BW1),find(BW1));
BW1(row1(1),1:col1(1)-1)=1;
BW1(row1(end),col1(end)+1:end)=1;

%%// Select only one per column for the reflection
[xt0,yt0] = find(BW1);
[yt1,a2,a3] =unique(yt0,'first');
xt1=xt0(a2);
sz1 = size(BW1,1)-xt1;

%%// Perform the reflection
for k = 1:numel(yt1)
    img(xt1(k):end,k,:) = img(xt1(k):-1:xt1(k)-sz1(k),k,:);
end

figure,imshow(img)

Typical mask with roipoly would look like -

enter image description here

Output

enter image description here

Note: User has to select exactly two points on the left side to represent the left side border of the mask and exactly two points on the right side for the right side border. Also, there must enough image pixels on top of the line to reflect over the line.

like image 127
Divakar Avatar answered Dec 11 '25 22:12

Divakar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!