You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How can i read images for filtering using if else condition in GUI?
2 views (last 30 days)
Show older comments
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
Accepted Answer
Image Analyst
on 5 Dec 2014
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 Comments
siti
on 5 Dec 2014
thanks for your answer.i can read the image but,why i still cannot filter the image?
Image Analyst
on 5 Dec 2014
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
on 5 Dec 2014
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
on 5 Dec 2014
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
on 7 Dec 2014
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
on 7 Dec 2014
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
on 7 Dec 2014
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
on 7 Dec 2014
Edited: Image Analyst
on 7 Dec 2014
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
on 7 Dec 2014
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
on 7 Dec 2014
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
on 8 Dec 2014
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
on 8 Dec 2014
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
on 8 Dec 2014
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
on 8 Dec 2014
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
on 8 Dec 2014
i already store my images in the folder..but why it gives me this 'Reference to non-existent field 'images'. 'error?
More Answers (0)
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Tags
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)