How can i read images for filtering using if else condition in GUI?
1 次查看(过去 30 天)
显示 更早的评论
function popupmenu3_Callback(hObject, eventdata, handles)
popupimage=get(popupmenu3.handles,'value');
img=imread('flower.jpg');
img1=imread('balloon.jpg');
%average
if
popupimage==img
e=fspecial('average',[33])
A=imfilter(img,e);
axes(handles.axes2);
imshow(A);
else if popupimage==img1
e=fspecial('average',[33])
B=imfilter(img1,e);
axes(handles.axes2);
imshow(B);
end
采纳的回答
Image Analyst
2014-12-5
Try this:
selectedItem = get(popupmenu3.handles,'value');
img=imread('flower.jpg');
img1=imread('balloon.jpg');
%average
e=fspecial('average',[33])
axes(handles.axes2);
if selectedItem == 1
A=imfilter(img,e);
imshow(A);
elseif selectedItem == 2
B=imfilter(img1,e);
imshow(B);
end
16 个评论
Image Analyst
2014-12-5
You have to give me more information, because when I selected an item number, and used standard MATLAB demo images, it worked just fine:
selectedItem = 2; % get(popupmenu3.handles,'value');
img=imread('onion.png');
img1=imread('cameraman.tif');
%average
e=fspecial('average',[33])
% axes(handles.axes2);
if selectedItem==1
A=imfilter(img,e);
imshow(A);
elseif selectedItem==2
B=imfilter(img1,e);
imshow(B);
end
Does the code above work for you? If so, investigate why your is different.
Image Analyst
2014-12-5
Siti's "Answer" moved here:
thanks again,now i can do the filtering process..but sometimes the output showing the different image. Actually what i'm trying to do is to read the images from files
function Graybutton_Callback(hObject, eventdata, handles)
path='G:\IP.projek\grayImg';
filter='*.jpg';
selectedFile=uigetfile(fullfile(path, filter));
axes(handles.axes1);
imshow(selectedFile);
function ColorButton_Callback(hObject, eventdata, handles)
path='G:\IP.projek\color img';
filter='*.jpg';
selectedFile=uigetfile(fullfile(path, filter));
axes(handles.axes1);
imshow(selectedFile);
and then i choose flower image in color img folder for example,then i want to apply filtering process.Currently i'm using popup menu for the list of filters .So now i'm just a little bit confuse.does i need to use switch case for every filter that i'm going to use? And in every case should i put if else condition for different images that i'm going to filter?.if yes,how to do it? i'm sorry for asking this but i really don't know.
Image Analyst
2014-12-5
No. You should have a listbox that you load up with image names. Then you can have a listbox or popup that you load up with filter names. In the callback of each control, you should call a function called FilterSelectedImage(handles) that you write. Inside FilterSelectedImage() you get the string and value of the listbox, then read in the selected image into a variable in that function. Then you get the value of the filter listbox or popup. You have a switch or if to filter the image according to the different filter types, then display it of course. This way no matter which you click on - the image file name, or the filter name - you will read in an original image and perform the filter on it.
siti
2014-12-7
i try to use listbox to load the image.but i got error..whats wrong with my code?
function listboxImg_Callback(hObject, eventdata, handles)
img = get(handles.listboxImg, 'Value');
if(img==1)
A=imread('flower.jpg');
axes(handles.axes1);
imshow(A);
elseif(img==2)
B=imread('balloon.jpg');
axes(handles.axes1);
imshow(B);
elseif(img==3)
C=imread('Grayflower.jpg');
axes(handles.axes1);
imshow(C);
elseif(img==4)
D=imread('Grayballoon.jpg');
axes(handles.axes1);
imshow(D);
end
Image Analyst
2014-12-7
You don't need any of that. In your callback, just have this:
% Get selected index.
selectedItem = get(handles.listboxImg, 'Value');
% If more than one is selected, bail out.
if length(selectedItem) > 1
return;
end
% If only one is selected, display it.
% First you need to get a list of all the items in the listbox.
ListOfImageNames = get(handles.lstImageList, 'string');
% Now construct the filename.
baseImageFileName = cell2mat(ListOfImageNames(selectedItem));
fullImageFileName = [handles.imageFolder '\' baseImageFileName]; % Prepend folder.
% Now read in the image.
rgbImage = imread(fullImageFileName);
% Now display the image.
axes(handles.axes1);
imshow(rgbImage);
title(baseImageFileName, 'FontSize', 20);
Image Analyst
2014-12-7
siti's "Answer" moved here since it contains another question and appears not to be a final answer to the original question, but a reply to me.
Thanks,i already use this coding.
function LoadImg_Callback(hObject, eventdata, handles)
% hObject handle to LoadImg (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
contents = get (handles.listbox1,'Value');
switch contents
case 1
A=imread('flower.jpg');
axes(handles.axes1);
imshow(A);
case 2
A=imread('balloon.jpg');
axes(handles.axes1);
imshow(A);
case 3
A=imread('Grayflower.jpg');
axes(handles.axes1);
imshow(A);
case 4
A=imread('Grayballoon.jpg');
axes(handles.axes1);
imshow(A);
end
And i wanna ask.how can i get images from listbox to be filter in popup menu?i dont know how to write the code so that all the images in listbox can be used to filter in popup menu.
Image Analyst
2014-12-7
编辑:Image Analyst
2014-12-7
It looks like you ignored my recommendations and went ahead with your prior code. Not sure why you didn't like my code.
Anyway, what do you have? A listbox with image names, and a popup with filter names, or what? Please attach your m and fig files or a screenshot. Do you want the user to click on an image name in the listbox and a filter showing in the popup will automatically be applied and the resulting image displayed in the axes?
siti
2014-12-7
i'm sorry.it is not that i don't like your code.i'm try to figure out my coding,and i got it before you reply that comment.
yes.i have image names in listbox and filter names in popup.so when user click average filter for example,it will apply to any images that user choose in listbox.
Image Analyst
2014-12-7
First try this. Have the callbacks of both the listbox and the popup call the same function called DisplayAndFilterImage. Nothing else, just simple a line like this:
DisplayAndFilterImage(handles)
Now define that function like this:
function DisplayAndFilterImage(handles)
% Get selected listbox index.
selectedItem = get(handles.listboxImg, 'Value');
% If more than one is selected, bail out.
if length(selectedItem) > 1
return;
end
% If only one is selected, display it.
% First you need to get a list of all the items in the listbox.
ListOfImageNames = get(handles.lstImageList, 'string');
% Now construct the filename.
baseImageFileName = cell2mat(ListOfImageNames(selectedItem));
fullImageFileName = [handles.imageFolder '\' baseImageFileName]; % Prepend folder.
% Now read in the image.
rgbImage = imread(fullImageFileName);
% Now display the image in axes1.
axes(handles.axes1);
imshow(rgbImage);
title(baseImageFileName, 'FontSize', 20);
% Now get what filter they chose.
selectedPopupIndex = get(handles.popup1, 'Value');
axes(handles.axes2); % We'll show result in axes 2.
switch selectedPopupIndex
case 1
% Median filter, or whatever.
filteredImage = medfilt2(rgbImage);
imshow(filteredImage);
case 2
% Your own custom filter, or whatever.
filteredImage = MyFilter(rgbImage);
imshow(filteredImage);
end
siti
2014-12-8
fullImageFileName = [handles.imageFolder '\' baseImageFileName];
for this coding,imageFolder is a folder that i created for my images right?
and then
function DisplayAndFilterImage(handles)
where should i put it?is it in both listbox and popup callback?
Image Analyst
2014-12-8
That looks like old code. Now I use code like this:
fullImageFileName = fullfile(handles.imageFolder, baseImageFileName);
Yes, handles.imageFolder is whatever folder you have your images in.
You put DisplayAndFilterImage() elsewhere in your file, like down at the bottom, as the last function.
siti
2014-12-8
why i got this error 'Reference to non-existent field 'images'. ' ? when i use this :
baseImageFileName = cell2mat(ListOfImageNames(selectedItem));
fullImageFileName = fullfile(handles.images, baseImageFileName);
whats wrong with my code?
Image Analyst
2014-12-8
You need to give it the folder with your images in it. If you didn't store the image folder string attached to your handles structure, then where are you storing it?
siti
2014-12-8
i already store my images in the folder..but why it gives me this 'Reference to non-existent field 'images'. 'error?
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)