How do I flip (mirror image) an image?
881 views (last 30 days)
Show older comments
I have an image which is being loaded upside-down and mirrored. So using imrotate, 180 I can have the image the right way around but still mirrored. What function would undo this mirroring?
Thanks
classdef ROITool < hgsetget
properties(GetAccess=public, SetAccess=private)
FigHandle
ImageControl
ScaledImage
ImSize
ROI
nROI
MaxnROIS=100
ToolBar
ToolBarItems
ToolBarSates={'circle','square','ellips','rectangle','line','delete','off'}
ToolBarImages={'Circle.png','Square.png','Ellips.png','Rectangle.png','line.png','Delete.png','MouseCursor.png'};
ROIColors={'blue','green','red','cyan','magenta','yellow'}
end
properties (Dependent = true, GetAccess=public, SetAccess = public)
SelectedType
end
methods
function obj=ROITool(ImageM)
if nargin==0
File='liveimage.jpg';
ImageM=double(imread(File));
ImageM = sum(ImageM, 3) / 3;
end
%Rescale Image
ImageM=double(ImageM);
Rescale=[min(ImageM(:)) max(ImageM(:))-min(ImageM(:))];
obj.ScaledImage=(ImageM-Rescale(1))./Rescale(2);
obj.DrawGUI;
set(obj.ImageControl.imageh, 'UserData',Rescale);
obj.ImageControl.update(obj.ScaledImage');
obj.nROI=0;
X=get(obj.ImageControl.imageh,'XData');
Y=get(obj.ImageControl.imageh,'YData');
obj.ImSize=[X(2) Y(2)];
set(obj.ImageControl.imageh,'ButtonDownFcn',{@obj.Buttondown});
obj.SelectedType='off';
end
function obj=Buttondown(obj,handle,~)
if handle~=obj.ImageControl.imageh;return;end
Mode=obj.SelectedType;
cp=get(obj.ImageControl.axesh,'Currentpoint');
MouseDownPosition=[cp(1,1) cp(1,2)];
switch lower(Mode)
case obj.ToolBarSates(5)
obj.AddLine(MouseDownPosition);
case obj.ToolBarSates(1:4)
obj.AddROI(MouseDownPosition,Mode);
end
end
function obj=AddLine(obj,StartPoint)
if obj.nROI>=obj.MaxnROIS;return;end
obj.nROI=obj.nROI+1;
color=obj.ROIColors{mod(obj.nROI,length(obj.ROIColors))+1};
obj.ROI{obj.nROI}=LineProfile(obj.ImSize,StartPoint,color,obj.nROI,obj.ImageControl.axesh);
addlistener(obj.ROI{obj.nROI},'DestroyObject',@obj.DeleteROI);
obj.SelectedType='off';
end
function obj=AddROI(obj,centerpoint,Type,radius)
if obj.nROI>=obj.MaxnROIS;return;end
if nargin==3;radius=[1 1];end
obj.nROI=obj.nROI+1;
color=obj.ROIColors{mod(obj.nROI,length(obj.ROIColors))+1};
obj.ROI{obj.nROI}=Roi(obj.ImSize,centerpoint,Type,radius,color,obj.nROI,obj.ImageControl.axesh);
addlistener(obj.ROI{obj.nROI},'DestroyObject',@obj.DeleteROI);
obj.SelectedType='off';
end
function obj=DeleteROI(obj,~,data)
obj.nROI=obj.nROI-1;
for i=data.ROINumber:obj.nROI
obj.ROI{i}=obj.ROI{i+1};
obj.ROI{i}.Number=i;
end
obj.SelectedType='off';
end
function SType=get.SelectedType(obj)
for i=1:length(obj.ToolBarSates)
State=get(obj.ToolBarItems(i),'State');
if strcmpi(State,'on');SType=obj.ToolBarSates{i};end
end
end
function set.SelectedType(obj,SType)
I=find(strcmpi(obj.ToolBarSates,SType));
if I>length(obj.ToolBarItems);return;end
State=get(obj.ToolBarItems(I),'State');
if strcmpi(State,'off') %prevent recursion
TempCallback=get(obj.ToolBarItems(I),'OnCallback');
set(obj.ToolBarItems(I),'OnCallback','');
set(obj.ToolBarItems(I),'State','on');
set(obj.ToolBarItems(I),'OnCallback',TempCallback);
end
for i=1:length(obj.ToolBarSates)
if i~=I;set(obj.ToolBarItems(i),'State','off');end
end
if strcmpi(SType,'delete');DeleteFlag=true;else DeleteFlag=false;end
for i=1:obj.nROI
obj.ROI{i}.DeleteOnClick=DeleteFlag;
end
end
function obj=SelectDrawMode(obj,~,~,index)
obj.SelectedType=obj.ToolBarSates{index};
end
function obj=DrawGUI(obj)
Position=[100 50 700 650];
obj.FigHandle=figure('Position',Position,'ToolBar','none','MenuBar','none','NumberTitle', 'off','Name','ROI and Line Profile Tool for 2D Images');
obj.ToolBar=uitoolbar;
obj.ImageControl=imagebox(obj.FigHandle,[50 100 600 500]);
for i=1:length(obj.ToolBarSates)
im=imread(obj.ToolBarImages{i});
obj.ToolBarItems(i)=uitoggletool('CData',im,'TooltipString',obj.ToolBarSates{i},'OnCallback',{@obj.SelectDrawMode,i});
end
end
end
end
0 Comments
Accepted Answer
Paulo Silva
on 5 Jul 2011
after the imrotate do
set(gca,'xdir','reverse')
Another example
I = imread('onion.png');
I2 = flipdim(I ,2); %# horizontal flip
I3 = flipdim(I ,1); %# vertical flip
I4 = flipdim(I3,2); %# horizontal+vertical flip
subplot(2,2,1), imshow(I)
subplot(2,2,2), imshow(I2)
subplot(2,2,3), imshow(I3)
subplot(2,2,4), imshow(I4)
6 Comments
Sachin Bisht
on 16 Oct 2019
Edited: Sachin Bisht
on 16 Oct 2019
Can someone give me the implementation of mirroring for bitmap images?
More Answers (2)
Thili Mahanama
on 29 Apr 2018
Edited: Image Analyst
on 25 Nov 2018
From scratch, for a gray scale image
% img=zeros(100,100); % img(50:75,20:35)=1; % img(10:20,10:20)=1;
img=imread('cameraman.tif')
subplot(1,2,1)
imshow(img)
[r,c]=size(img);
imgthili=zeros(100,100);
for i=1:r
for u=1:c
if (img(i,u)>=1 )
imgthili(i,c+1-u)=img(i,u);
else
imgthili(i,c+1-u)=img(i,u);
end
end
end
subplot(1,2,2)
intth=uint8(imgthili);
imshow(intth)
3 Comments
Image Analyst
on 25 Nov 2018
Yes, but it's not the way you'd do it - with a double for loop. You'd simply do
flippedImage = fliplr(grayImage);
It does the same thing, but more efficiently using a built-in function.
See Also
Categories
Find more on Explore and Edit Images with Image Viewer App in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!