Why is my code showing the error:" Error using matlab.gra​phics.prim​itive.Imag​e/set; Invalid or deleted object.

조회 수: 17 (최근 30일)
I would love some assistance with the following code. Im setting up a security/motion detection camera as shown in a tutorial put on by mathworks. I have a little different hardware set up that I think may be part of the problem. I am simply using my built in camera on my surface to compare a reference photo to a snapshot taken from live video and seeing if there is motion detected. However, I am getting the following error as shown in the title:
Error using matlab.graphics.primitive.Image/set
Invalid or deleted object.
Error in Sentry_Cam (line 29)
set(hShow,'CData',object_detected);
Im new at all this and tried following the online example as closely as possible. Could it be a data type issue? hShow says its a 1x1 Image in the workspace window and Object_detected is assigned as a uint8. Could this be the issue with the line reading set(hShow,'CData',object_detected)?
I also tried looking at hShow specfically and it says it is a "handle to deleted Image" ? Where could my code be deleting it? Any assistance would be wonderful. Thanks for any help you can give!
function Sentry_Cam()
%%
clear all; close all; clc; imaqreset;
%% Setup Image Acquisition
hCamera= videoinput('winvideo',1);
% Create a hangle to an imshow figure for faster updating
hShow=imshow(zeros(480,640,3,'uint8'));title('Sentry Cam')
%% Acquire reference image
set(hCamera,'ReturnedColorSpace', 'RGB')%Need this to convert YUY2 to RBG to get rid of pink coloring
ref_vid_img=getsnapshot(hCamera);
%image(ref_vid_img)
%% Quantize images and outputing to the screen
frames=2000;
for i=1:frames
%Acquire an image form the webcam
vid_img=getsnapshot(hCamera);
% Call the live segmentation function. This is a seperate function that seems to working just fine when run line by line
object_detected=Segmentation_Fn(vid_img,ref_vid_img);
% Update the imshow handle with a new image
set(hShow,'CData',object_detected);
drawnow;
end
%% Shutdown webcam by deleting handle
clear hShow;
end
Outout following the my code:
Error using matlab.graphics.primitive.Image/set
Invalid or deleted object.
Error in Sentry_Cam (line 29)
set(hShow,'CData',object_detected);

채택된 답변

Image Analyst
Image Analyst 2020년 7월 4일
편집: Image Analyst 2020년 7월 4일
At some point hShow disappears. Set a breakpoint there and then stip through with the debugger and look at the workspace to find out which line of code is blowing it away. But I don't know why it disappears in the loop. For me it doesn't.
You can delete hCamera to shut down the video input rather than clearing hShow.
delete(hCamera);
  댓글 수: 3
Scott Kaiser
Scott Kaiser 2020년 7월 6일
Thank you Both! Turns out debugging led me to find that my error was all USER error, and I was indeed causing it by deleting my figure too soon. Thank you for your assistance and patience!
Abhishek Patil
Abhishek Patil 2021년 3월 10일
Can you please tell what changes you did to your code to resolve the error.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

제품


릴리스

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by