Remove a region in Image
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
0 投票
Hi,
I'm looking to remove a region from images. For example, I'd like to use freehand as shown in the image and delete that portion of the image, i.e., turn it in to white because some unpredictable regions like that are troublesome. I'd like to use it for as many time as necessary. I looked into roi, but it turns entire image black and the selected region white. Also, how can you make sure freehand is enclosed. Would you require both ends of the freehand going inside, so you're always sure it is enclosed. Will freehand be always enclosed? Looks like there is a small gap between two ends.
Thanks for the help!
採用された回答
Image Analyst
2013 年 12 月 16 日
編集済み: Image Analyst
2013 年 12 月 16 日
That is exactly what my freehand masking demo does. It's attached below in blue.

13 件のコメント
Kev
2013 年 12 月 16 日
Thanks, Image Analyst! I'm trying to understand your code. How did you achieve this? I'm trying to figure out how you put white portion in the third image on the far right. You would not add image 1 + image 2.
Image Analyst
2013 年 12 月 16 日
binaryImage is a logical index array. It says what pixel to operate on and what pixel to not operate on. So only pixels that are true will get set to white.
% Burn line into image by setting it to 255 wherever the mask is true.
burnedImage = grayImage; % Initialize all pixels to the original image.
burnedImage(binaryImage) = 255; % Only the "true" pixels get set to white.
Thanks ImageAnalyst,
It works. You can see the results. Answer Accepted.
Many many thanks!
Kev
2013 年 12 月 16 日
Image Analyst,
I'm using this feature to implement in GUI. How can you use this multiple times without removing previous, for loop is not ideal as you don't know how many times you may want to use this feature. Is there any better alternative?
for i=1:5
h = imfreehand();
binary = h.createMask();
xy = h.getPosition;
burnedImage = colour_to_gray;
burnedImage(binary) = 255;
axes(handles.axes1)
imshow(burnedImage)
end
Image Analyst
2013 年 12 月 16 日
You could either have it be the callback of a push button. Or you could go into an infinite loop where the opportunity to bail out is given at teh bottom of the loop:
while true
% some code to do masking
% Ask if user wants to do another one.
promptMessage = sprintf('Do you want to Continue masking,\nor Cancel to abort processing?');
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue');
if strcmpi(button, 'Cancel')
break;
end
end
Kev
2013 年 12 月 16 日
Hmm, so you can have additional push button to add (+1) freehand region removal. I should be able to do that, but I'll try your suggestion as it seems better.
Kev
2013 年 12 月 16 日
ImageAnalyst,
I know this is very simple question, but how can I retain previous image removal for the next freehand loop. On the second loop, previous roi gets removed.
Image Analyst
2013 年 12 月 16 日
You're overwriting grayImage every time, so I don't know why the white regions you drew are not being retained. Perhaps you're creating a new image somehow rather than overwriting the same image. I'd have to see the code.
Kev
2013 年 12 月 16 日
編集済み: Image Analyst
2013 年 12 月 16 日
filtered image = gray scale filtered image
while true
% some code to do masking
h = impoly();
binary = h.createMask();
xy = h.getPosition;
burnedImage = filtered_image;
burnedImage(binary) = 255;
axes(handles.axes10)
imshow(burnedImage)
% Ask if user wants to do another one.
promptMessage = sprintf('Do you want to Continue masking,\or Cancel to abort processing?');
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue');
if strcmpi(button, 'Cancel')
break;
end
end
Image Analyst
2013 年 12 月 16 日
編集済み: Image Analyst
2013 年 12 月 16 日
There, don't you see it? You're resetting burnedImage to filtered_image, which never changes after you've entered the loop. Put that line outside the loop and it should be fine:
burnedImage = gray_scale_filtered_image;
while true
% some code to do masking
h = impoly();
binary = h.createMask();
xy = h.getPosition;
burnedImage(binary) = 255;
axes(handles.axes10)
imshow(burnedImage)
% Ask if user wants to do another one.
promptMessage = sprintf('Do you want to Continue masking,\or Cancel to abort processing?');
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue');
if strcmpi(button, 'Cancel')
break;
end
end
Now burnedImage gets updated every time instead of being reset every time.
Kev
2013 年 12 月 16 日
Yeah, I missed it. I knew it was very simple. Thanks a lot for correcting me. I added a same line outside previously, but kept burned image inside the loop. Thanks! :)
ImageAnalyst,
Can you help with clearing workspace variables? I've set up a push button in the gui to clear variables and only retain the ratio value from calibration. I'm trying to clear the gui workspace because with the region removal like above, I'm unable to to remove regions in the second picture (second trial, after one measurement is done). It looks like variables need to be cleared and I'd like to clear all the variables (except ratio value) for the second picture.
function clearscreen_Callback(hObject, eventdata, handles)
global xy binary h burnedImage ratio_value
%clearvars xy binary h burnedImage
evalin('base','clear all');
cla;
I get following error on second trial:
Error using imroi/parseInputsForCreateMask (line 83)
Ambiguous syntax. Associated axes contains more than one image. Specify image handle
as input argument to resolve ambiguity. Type "help imroi/createMask" for more
information.
Error in imroi/createMask (line 245)
[obj,h_im] = parseInputsForCreateMask(varargin{:});
Error in FibGUI>blockremover_Callback (line 121)
binary = h.createMask();
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in FibGUI (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)FibGUI('blockremover_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
Image Analyst
2013 年 12 月 17 日
Don't use clear or clearvars or clearall in a GUI, and don't use evalin() - there's never a need to do that. That is what is causing problems. If your axes have more than one image, then call cla('reset') before you call imshow().
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
参考
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)
