Search code examples
matlabcomputer-visionmatlab-figure

How to detect irregular placed bottles in the red circle while exclude those regular circle bottle by Matlab?


I only want to detect those irregular bottles, for example, in red circle: enter image description here

However, I have tried several methods, but they find all edge bottle in the image, is there some way to dectect only slanting bottle or how I change my method?

f15 = imread('bottle_crate_15.png');
BW_15 = imbinarize(f15);

%Canny:
    BW_canny = edge(BW_15, 'Canny');
    imshow(BW_canny);

%bwboundaries 
[B,L,N,A] = bwboundaries(BW_15, 'noholes');
imshow(label2rgb(L, @jet, [.5 .5 .5]))
hold on
for k = 1:length(B)
   boundary = B{k};
   plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2)
end

%dilate:
im = mean(f15,4);
im = (im-min(im(:))) / (max(im(:))-min(im(:)));

bin = im2bw(im);
SE = strel('disk',1);
bin = ~imerode(~bin,SE);
bin = ~imerode(~bin,SE);

bin =~imdilate(~bin,SE);
bin =~imdilate(~bin,SE);
imshow(bin);

ps: the origin image:

enter image description here


Solution

  • If its already sufficient, one approach could be to classify if there is any bottle not correctly placed:

    1.) detect the grid structure in the beverage crate (hough lines). Then, you should be able to segment the image into the areas where the bottle should be placed.

    2.) For each placement area you can classify if it contains a bottle, is empty, or looks different. There are several ways to approach this. In case you have acess to multiple images showing beverage crates, you can train a classifier (using SVM or CNN). If you do not have enough training images, you can still use low level image processing techniques to classify the areas (e.g. using circle detection to detect placement areas which contain bottles).

    One approach to detect the wrongly placed bottle:

    Apply an object detector (e.g. https://www.mathworks.com/help/deeplearning/ug/object-detection-using-yolo-v2.html?s_tid=blogs_rc_6) to localize all bottles. Ignore the detections which are correctly within the placement areas (e.g. using hough lines). The remaining detections depict wrongly placed bottles.

    Also, you could directly train an object detector to detect missplaced bottles by collecting and annotating corresponding images (https://de.mathworks.com/help/vision/ref/trainrcnnobjectdetector.html).