MATLAB: How to discriminate defective objects amongst others

homeworkimage alignmentImage Processing Toolboxmultiple object trackingonline pill inspectionschool assignment

Hello,
I have a video which shows a production line of pills. My goal is to detect the defective ones. Below I show a frame of the video to give you an idea.
The cylinders move to the left while spinning, making the pills spin around themselves. The defective ones spin irregularly while moving along and the good ones seem like they just move horizontally. The video is recorded by a cellphone so the camera is not 100% steady. Thus, eventhough the speed of the horizontal motion is constant, it didn't help me much. In order to make things easier, I made a new video in which only the pills appear. The code I used and a frame of the produced video are shown below:
load('defWorkspace.mat'); %contains the video
%preallocation
th=zeros(601,1);
pr=zeros(320,568,571); %the produced video
%convert the video frame by frame
for i=1:571
ff=(double(dip(:,:,3,i))./mean(double(dip(:,:,:,i)),3));
th(i)=graythresh(ff);
pr(:,:,i)=((ff<th(i)).*double(rgb2gray((dip(:,:,:,i)))));
end
Also I made a different version of the video using
d=diff(pr,1,3);
This video consists of frames like this one:
In everyone of these videos the basic notion I tried to use was image alignment because if there's a good match between the good pills in each frame, the only differencies will be in the locations of the defective ones. But because of the moving camera the geometric transform is not exactly a translation.
An when I tried using imwarp to make an affine transformation, it changed the image size so I couldn't align them properly
In conclusion, If anyone has any ideas how to detect the defective pills or at least how to align two frames properly I'd be really grateful!

Best Answer

Do color segmentation, then try to separate the pills using this method:
Then use regionprops to get the orientation. Any orientation that is not fairly vertical is bad.
Related Question