現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
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 件のコメント
siti
2014 年 12 月 5 日
thanks for your answer.i can read the image but,why i still cannot filter the image?
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!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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 (한국어)