How do I flip (mirror image) an image?

614 次查看(过去 30 天)
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

采纳的回答

Paulo Silva
Paulo Silva 2011-7-5
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 个评论
Bharath Lohray
Bharath Lohray 2015-10-8
Now deprecated. Use `flip` instead of `flipdim`
Sachin  Bisht
Sachin Bisht 2019-10-16
编辑:Sachin Bisht 2019-10-16
Can someone give me the implementation of mirroring for bitmap images?

请先登录,再进行评论。

更多回答(2 个)

Goku
Goku 2017-10-8
编辑:Goku 2017-10-8
To mirror flip an "Image_Original" you can just use flip or rotate and transpose the image :
Image_Flip = flip(Image_Original,2);
Image_Flip = imrotate(Image_Original,90)';

Thili Mahanama
Thili Mahanama 2018-4-29
编辑:Image Analyst 2018-11-25
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 个评论
Image Analyst
Image Analyst 2018-11-25
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.
Stephen23
Stephen23 2018-11-25
Or just using basic indexing:
new = img(:,end:-1:1,:)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by