I'm working on a student project where I have 24 light sensors spaced 6 cm apart above a small conveyor belt. Boxes of various widths, lengths, and angles will pass through the sensors.
I'm considering using a 24x50 array to represent the detected shapes. When a box's leading edge triggers any sensor, a "1" is inserted at the corresponding position in the array. With scans every 20 ms, a 1-second interval would provide a snapshot of the object's shape.
Initially, I thought about using nested loops to identify the first, second, third, and fourth corners of the box. Then, I'd calculate the length and width based on the coordinates of the end points and use arctangent to determine the angle based on the line's slope.
length = sqrt((x2 - x1)^2 + (y2 - y1)^2)
angle = arctan((y2 - y1) / (x2 - x1))
However, I realized this approach might not be optimal, as variations in package angle could lead to inaccurate corner detection and unreliable results.
Instead, I'm looking for better solutions. Would anyone be able to guide me towards more robust methods for shape identification and analysis?
I think you better show us at least of a couple of an example snapshot (a text representation is enough, like 50 text rows each one containing 24 0/1 digits), together with how you represent the 24x50 values inside the code, to le us know how handle it. E.g. a 24x50 char array? Or an array of 50 unsigned longs (as a bit mapped with mask 0x00FFFFFF aka with the first 24 bits)?
And are all the boxes rectangular (including squares), right? Because if you detect leading edges only, I think there's no clue to tell if it's rectangular or triangle as an example.
A more robust approach would be to collect the data as a 2D image array (with time points being one axis) and analyze the image for corners, or for general shape. That way, all the data contribute to the analysis.
As far as I can understand, he already did it. The scanner seems to be alineear one with have 24 sensors in line, and the program takes 50 readings (20ms per reading, for 1 m of the underlying belt movement), with a "1" when a change has been detected, creating in fact a kinda "edge detection".
But unless the OP will show us how he's actually getting the data together with a practical result and its current storage method, I think we can make a lot of congectures and hypothesys and suggest a different hardware, but without solving his problem...
Probably the simplest approach once you have the image array is identifying the leftmost, rightmost, topmost and bottommost '1', perform edge-detection and then traverse the edge pixels between those 4 points. This should sort of work for an angled rectangle as is, but for one lined up with the array you'd need to also look for the NW, NE, SW, SE-most pixels as well, and see which works best.
If you have more processing power to throw at the problem you could try cross-correlation with suitable kernels, such as a rectangular corner, at all angles and positions.
If the object is known to be a square or rectangle, part of it is outside the range of the scanner. You could estimate the position of corner four by calculating the intersection of the two adjacent edges, then calculate the area, for example.
That is the point of taking all the measurements into account.