Why for 2nd loop data not running for ip camera first

1 visualización (últimos 30 días)
Hi, i got some problem. i just write a code to compare test image and reference image. my idea is the reference image are already in the directory while the test image will be capture from ip camera. i want to loop my code 5 time, at first loop ip cam will actived and scan for green ROI. then the image will be caputure and save to directory by given name of it. so here the problem occured. when continue to loop2, the ip camera will be skip, and the code create new image based on data before not current data .
the process for 2nd loop should be like first loop. ip camera actived, scan for green ROI, save the image then running other function. So, may someone see my code and explain why the code not work as my plan
cam = ipcam('http://192.168.0.102:8080/video');
framesAcquired = 0;
H=0;
i=0;
a= imread('testing.png');
I=rgb2hsv(a); %Convert RGB to HSV
S=I(:,:,2);
S=im2double(S);
% Threshold the image
level = graythresh(S); %Otsu' Method
BW = im2bw(S,level);
A=im2double(a);
P=2000;
BW = bwareaopen(BW,P); %Remove small pixel
% Histogram Red, Green, Blue
R=A(:,:,1);
G=A(:,:,2);
B=A(:,:,3);
dR=R.*BW;
dG=G.*BW;
dB=B.*BW;
rgb=im2double(I);
rgb(:,:,1)=dR;
rgb(:,:,2)=dG;
rgb(:,:,3)=dB;
ReferenceImage=rgb;
%H=H+1;figure(H);
%imshow(ReferenceImage);
while(i<=5) %value sample to test
while (framesAcquired <= 50)
data = snapshot(cam);
framesAcquired = framesAcquired + 1;
diff_im = imsubtract(data(:,:,2),rgb2gray(data)); % subtracting green component from the gray image
diff_im = medfilt2(diff_im, [3 3]); % used in image processing to reduce noise and for filtering
diff_im = im2bw(diff_im,0.12); % convert image to binary image
stats = regionprops(diff_im, 'BoundingBox', 'Centroid'); % measures a set of properties for each connected component in the binary image
drawnow;
imshow(data);
hold on
for object = 1:length(stats)
bb = stats(object).BoundingBox;
bc = stats(object).Centroid;
rectangle('Position',bb,'EdgeColor','b','LineWidth',2)
plot(bc(1),bc(2), '-m+')
end
end
%imwrite(data, 'testing.png');
imwrite(data,sprintf('im%d.jpg',i))
%b= imread('%d.jpg',i);
b = imread(['D:\SEMESTER 5\idp\Ip camera Object Detect\im' num2str(i) '.jpg']);
I=rgb2hsv(b); %Convert RGB to HSV
S=I(:,:,2);
S=im2double(S);
% Threshold the image
level = graythresh(S); %Otsu' Method
BW = im2bw(S,level);
A=im2double(b);
P=2000;
BW = bwareaopen(BW,P); %Remove small pixel
% Histogram Red, Green, Blue
R=A(:,:,1);
G=A(:,:,2);
B=A(:,:,3);
dR=R.*BW;
dG=G.*BW;
dB=B.*BW;
rgb=im2double(I);
rgb(:,:,1)=dR;
rgb(:,:,2)=dG;
rgb(:,:,3)=dB;
TestImage=rgb;
H=H+1;figure(H);
imshow(TestImage);
% Image Comparing method
%------------------------------------------------------&
SubtractedImage = double(ReferenceImage)- double(TestImage);
%H=H+1;figure(H)
%imshow(SubtractedImage, []);
% k-segment method
%---------------------------------------------------%
lab_he = rgb2lab(SubtractedImage);
ab = lab_he(:,:,2:3);
ab = im2single(ab);
nColors = 5;
% repeat the clustering 3 times to avoid local minima
pixel_labels = imsegkmeans(ab,nColors,'NumAttempts',5);
%H=H+1;figure(H)
%imshow(pixel_labels, []);
t = graythresh(pixel_labels);
BW1 = imbinarize(pixel_labels,t);
%H=H+1;figure(H)
%imshow(BW1)
%Remove small Pixel
%---------------------------------------------------------%
Removesmallpixel = BW1;
BW2 = bwareaopen(Removesmallpixel, 700);
%H=H+1;figure(H)
%imshow(BW2);
% Find biggest blops(pixel)
%--------------------------------------------------%
BW3 = BW2;
CC = bwconncomp(BW3);
numPixels = cellfun(@numel,CC.PixelIdxList);
[biggest,idx] = max(numPixels);
%make background become 0
mask = BW3 > 0.5;
% Now everywhere that mask is true, set the original image to zero there.
BW3(mask) = 0;
disp('No contamination! Passes!')
disp('=======================================================')
disp('=======================================================')
disp('=======================================================')
%contamination area become 1
BW3(CC.PixelIdxList{idx}) = 1 ;
H=H+1;figure(H)
imshow(BW3);
%----------------------------------------------%
Measurements = regionprops(BW3, 'Area');
Area2 = [Measurements.Area];
% Perimeter2 = [Measurements.Perimeter];
% MajorAxis2 = [Measurements.MajorAxisLength];
% Extent2 = [Measurements.Extent];
Area_Testing_Shape = sum(Area2)/10000; %devide by 10k to get smaller value
% Perimeter_Testing_Shape = sum(Perimeter2);
% MajorAxis_Testing = sum(MajorAxis2);
% Extent_Testing = sum(Extent2);
fprintf('\t Area_Testing_Shape = %g\n',Area_Testing_Shape );
% fprintf('\t Perimeter_Testing = %g\n',Perimeter_Testing);
% fprintf('\t MajorAxis_Testing = %g\n',MajorAxis_Testing);
% fprintf('\t Extent_Testing = %g\n',Extent_Testing);
disp('=======================================================')
if (Area_Testing_Shape >= 9.7937)
disp('Contamination Detected! Need Eye inspection')
elseif (2.0891 >= Area_Testing_Shape <= 9.7936)
disp('Contamination Detected')
else
disp('Passed!')
end
disp('=======================================================')
disp('=======================================================')
disp('=======================================================')
i = i + 1;
end

Respuesta aceptada

Walter Roberson
Walter Roberson el 12 de En. de 2021
You do not reset framesAcquired = 0 inside your for i loop.

Más respuestas (0)

Categorías

Más información sobre Image Processing Toolbox en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by