You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
what means this error , while trying to use vision.blobAnalysis to detect motions?
4 views (last 30 days)
Show older comments
what means this error , while trying to use vision.blobAnalysis to detect motions?
Error using coder.internal.errorIf (line 8)
The POSITION matrix must have four columns for shape Rectangle
Error in insertShape>errIf1 (line 796)
coder.internal.errorIf(condition, msgID, strArg);
Error in insertShape>crossCheckShapePosition (line 347)
errIf1(errCond, 'vision:insertShape:posColsNot4ForRect', shape);
Error in insertShape>crossCheckInputs (line 317)
crossCheckShapePosition(shape, position);
Error in insertShape>validateAndParseInputs (line 176)
crossCheckInputs(shape2, position, color);
Error in insertShape (line 103)
validateAndParseInputs(I, shape, position, varargin{:});
Error in btw (line 134)
result = insertShape(frame, 'Rectangle', bbox, 'Color', 'green');
Answers (1)
Walter Roberson
on 22 Nov 2015
Check size(bbox) . The bbox that you are passing in must have 4 columns, such as a row vector of 4 elements.
12 Comments
syhem samti
on 23 Nov 2015
blob generation works well in my code, but when i try to extract MajorAxisLength, MinorAxisLength of each blob...this error appears
Walter Roberson
on 23 Nov 2015
Edited: Walter Roberson
on 23 Nov 2015
Please show your call to vision.BlobAnalysis and your step() command
Image Analyst
on 23 Nov 2015
This is not what you originally said. You said there was a problem with rectangle and bbox. Did you solve that problem and now have this new one???? Aren't MajorAxisLength and MinorAxisLength computed by regionprops()? You don't even show that code or even mention that in your original question. It's hard for people to answer questions about code that is not shown, don't you think?
Walter Roberson
on 23 Nov 2015
The blob analysis can be configured to output major and minor axis lengths, but the order of the outputs becomes positional according to the exact options you have enabled, rather than by structure. The order of the positional outputs is not well defined in the blobanalysis step() documentation, and would have to be presumed that any one further down the list would appear to the right of any one further up higher in the list.
Chances are that the poster is expecting the outputs in the wrong order and passing what they think should be the bbox output but is a different output.
Image Analyst
on 23 Nov 2015
I don't have that toolbox. I know the Computer Vision Toolbox requires the Image Processing Toolbox so I wouldn't be surprised if it called regionprops() internally.
The link you gave says "The order of the returned values when there are multiple outputs are in the order they are described below:"
It looks like if you call
[AREA,CENTROID,BBOX] = step(H,BW)
you get those 3 results. Then, if you want any others, you have to call them in a very specific order - the order listed in the help. It seems weird and inconvenient that you have to call
[___, someResult] = step(H,BW);
a bunch of times serially to get what you want rather than just asking for it directly like you can with regionprops(). Anyway, the next two after those 3 are MajorAxis and MinorAxis.
Again, the poster does not seem to show any of this code where MajorAxis is asked for.
Walter Roberson
on 23 Nov 2015
You do not call serially, it is
[AREA*, CENTROID*, BBOX*, MAJORAXIS*, MINORAXIS*, ORIENTATION*...] = step(H, BW)
where each * is intended to indicate here that the corresponding output might be omitted depending on which *OutputPort you configured. For example you might end up with
[CENTROID, MAJORAXIS] = step(H,BW)
if you turned off AreaOutputPort and BoundingBoxOutputPort and turned on MajorAxisLengthOutputPort .
Vision.BlobAnalysis works in the System Objects paradigm, with the ability to generate optimized code that reduces memory transfers. I doubt it calls into regionprops().
syhem samti
on 23 Nov 2015
Edited: syhem samti
on 24 Nov 2015
here the code is:
function videoAnalysis()
clear all;
close all;
clc;
foregroundDetector = vision.ForegroundDetector('NumGaussians', 3, 'NumTrainingFrames', 50);
videoReader = vision.VideoFileReader('video6.avi');
% fRate = videoReader.info.VideoFrameRate;
% disp(fRate);
for i = 1:50
frame = step(videoReader);
foreground = step(foregroundDetector, frame);
end
imwrite(foreground,'foreground1.jpg')
BW = imread('foreground1.jpg');
%Calculate the perimeters of objects in the image.
BWs = bwperim(BW,8);
%Display the original image and the perimeters side-by-side.
%imshowpair(BW,BWs,'montage')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
se90 = strel('line', 3, 90);
se0 = strel('line', 3, 0);
%The binary gradient mask is dilated using the vertical structuring element followed by the horizontal structuring element. The imdilate function dilates the image.
BWsdil = imdilate(BWs, [se90 se0]);
%figure, imshow(BWsdil), title('dilated gradient mask');
BWdfill = imfill(BWsdil, 'holes');
% figure, imshow(BWdfill);
% title('binary image with filled holes');
BWnobord = imclearborder(BWdfill, 4);
%figure, imshow(BWnobord), title('cleared border image');
seD = strel('diamond',9);
BWfinal = imerode(BWnobord,seD);
BWfinal = imerode(BWfinal,seD);
%figure, imshow(BWfinal), title('segmented image');
BWoutline = bwperim(BWfinal);
Segout = BW;
Segout(BWoutline) = 255;
%figure, imshow(Segout), title('outlined original image');
se = strel('square', 3);
fF = imopen(Segout, se)
se1 = strel('square', 5);%masque
ForF = imdilate(fF, se1);
%figure; imshow(ForF); title('finaal foreground ')
L = bwlabel(ForF);% connected component (seulement sur les images binaires
s = regionprops(L, 'Area');
s(1)
area_values = [s.Area]
idx3 = find((5500 <= area_values) & (area_values <= 1900000))
bw3 = ismember(L, idx3);
figure, imshow(bw3);title(' finaal foreground ')
se = strel('square', 3);
filteredForeground = imdilate(bw3, se)
figure, imshow(filteredForeground);
title('Fforeground')
blobAnalysis = vision.BlobAnalysis('BoundingBoxOutputPort', true, ...
'AreaOutputPort', false, 'CentroidOutputPort', false, ...
'MinimumBlobArea', 350 , 'MaximumBlobArea', 15000, ...
'MajorAxisLengthOutputPort' , true, 'MaximumCount', 5);
[centroid, bbox] = step(blobAnalysis, filteredForeground)
result = insertShape(frame, 'Rectangle', bbox, 'Color', 'green');
numPerso = size(bbox, 1);
result = insertText(result, [10 10], numPerso, 'BoxOpacity', 1, 'FontSize', 14);
videoPlayer = vision.VideoPlayer('Name', 'Detected Cars');
videoPlayer.Position(3:4) = [650,400]; % window size: [width, height]
% se = strel('square', 3); % morphological filter for noise removal
i = 0;
while ~isDone(videoReader)
% if ( mode(i,10) == 0 )
% disp(i);
% end
% i = i + 1;
frame = step(videoReader); % read the next video frame
% Detect the foreground in the current video frame
foreground = step(foregroundDetector, frame);
% Use morphological opening to remove noise in the foreground
% filteredForeground = imopen(foreground, se);
%%by me
se = strel('square', 3);
filteredForeground = imdilate(bw3, se)
%figure, imshow(filteredForeground);
%title('Fforeground')
% Detect the connected components with the specified minimum area, and
% compute their bounding boxes
% Draw bounding boxes around the detected cars
result = insertShape(frame, 'Rectangle', bbox, 'Color', 'blue');
% imshow(result);
%
% Display the number of cars found in the video frame
numCars = size(bbox, 1);
result = insertText(result, [10 10], numPerso, 'BoxOpacity', 1, 'FontSize', 14);
%if there is moving object
if numPerso == 0
result = insertText(result, [100 20], 'non motion detected', 'BoxOpacity', 1, 'FontSize', 14, 'BoxColor', 'green');
else
result = insertText(result, [100 20], 'motion detected', 'BoxOpacity', 1, 'FontSize', 14, 'BoxColor', 'red');
end
step(videoPlayer, result); % display
end
release(videoReader); % close
end
Walter Roberson
on 24 Nov 2015
Look at your code again:
blobAnalysis = vision.BlobAnalysis('BoundingBoxOutputPort', true, ...
'AreaOutputPort', false, 'CentroidOutputPort', false, ...
'MinimumBlobArea', 350 , 'MaximumBlobArea', 15000, ...
'MajorAxisLengthOutputPort' , true, 'MaximumCount', 5);
[centroid, bbox] = step(blobAnalysis, filteredForeground)
You have 'CentroidOutputPort', false so the centroid output is not going to be produced. The bounding box is going to be the first output (and there is not going to be a second output)
bbox = step(blobAnalysis, filteredForeground);
Walter Roberson
on 24 Nov 2015
[bbox, MajorAxisLength] = step(blobAnalysis, filteredForeground);
I overlooked that you had that turned on as well.
syhem samti
on 26 Nov 2015
thank you for you answers...i tried this too but another error appears...anyway i just found another way to extract those attributes
NIHARIKA MITTAL
on 12 Nov 2017
Can you please share the other way. I am also stuck in this same situation
See Also
Categories
Find more on Image Processing and Computer Vision 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 (한국어)