0
我寫了下面的代碼,通過它我使用加載按鈕加載圖像,然後使用繪製按鈕在圖像上繪製不同的矩形並將它們保存到文本文件。添加和刪除圖像上的可拖動矩形並保存到文件
我在圖片上做了3個按鈕。
1.load(它加載GUI上的圖像) 2.Draw(通過按下它用戶將能夠利用圖像拖動的矩形。 3.Save(使用該按鈕用戶能夠保存在文本文件中的所有矩形)。
負載
function loadButton_Callback(hObject, eventdata, handles)
% hObject handle to loadButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
I=imread('http://ichef.bbci.co.uk/corporate2/images/width/live/p0/0l/3r/p00l3rnt.jpg/624');
imshow(I);
rectangles=cell(1,1);
count=0;
handles.rectangles=rectangles;
handles.count=count;
guidata(hObject,handles);
繪製
function drawButton_Callback(hObject, eventdata, handles)
% hObject handle to drawButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
rectangles=handles.rectangles;
count=handles.count;
count=count+1;
h=imrect;
rectangles{count,1}=h;
handles.count=count;
handles.rectangles=rectangles;
guidata(hObject,handles);
保存
function saveButton_Callback(hObject, eventdata, handles)
% hObject handle to saveButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
rectangles=handles.rectangles;
for i=1:length(rectangles)
dlmwrite('P:\bla.txt',rectangles{i}.getPosition,'-append','delimiter',' ','newline','pc');
end
上面的代碼工作很好地與下面的問題。如果我刪除一個矩形(一個或多個),然後下面的錯誤出現?
**Invalid or deleted object**.
代碼不運行,因爲ishandle總是返回0,所以我在if條件中使用了negation。但是當我刪除了一個矩形「刪除的對象」時出現了同樣的錯誤 –
我沒有圖像工具箱來測試,但這個想法應該是正確的。你可以用'isvalid'來代替嗎? (以防萬一是一個繼承'handle'類的對象) – 2016-09-20 11:12:04
恐怕這個想法只有當我捕捉到刪除矩形時觸發的事件纔有效。 –