If I understand you correctly the sensor array can't discern bags and pallet, all you get is a matrix with zeroes and ones (representing "something" and "nothing").
If all the bags are within the bounds of the pallet, and assuming the pallet is rectangular, then the image will look very much like a rectangle.
What I would do is collect your matrix, then calculate its "convex hull" (Google!).
Ideally the hull would have four edges and be a rectangle, matching the pallet.
But poorly stacked pallets as well as discretization errors, will cause deviations.
Once you have the hull, the following pseudo-C# code could be used to check whether the image fits a pallet or not:
bool palletOK=false
foreach(edge E of convex hull) {
if (edge is very short with respect to pallet) continue
bool goodEdge=true
foreach(point P of convex hull) {
calculate orthogonal projection of P on (infinite line through) E
if {orthogonal distance is > pallet width) goodEdge=false
}
foreach(point P of convex hull) {
calculate orthogonal projection of P on E
identify first and last projected point on E
}
if (distance between first & last pt on E > length of pallet) goodEdge=false
if (goodEdge) palletOK=true
}
What that code does is assume some edge E matches a long side of the pallet, and check if it is stacked properly then.
Notes:
- you can shortcircuit some of the code (as soon as goodEdge goes false, go test the next edge; and as soon as palltOK is true you're done)
- You may want to allow some tolerance (e.g. one sensor pitch) when comparing distances.
- Calculating a distance or a projection takes a little math, once again make Google your friend.