I found the joint array through this method, I guess this is a continuation.
I figured out a quick and dirty solution. This only works with perfectly horizontal/vertical alignment, and if there is a gap in the columns, it's not handled.
# First dilate the image
kernel = np.ones((5,5),np.uint8)
dilation = cv.dilate(img,kernel,iterations = 1)
# Find contours then points
(img, contours, hierarchy) = cv.findContours(dilation, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
points = []
for con in contours:
if (cv.contourArea(con)>0):
M = cv.moments(con)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
points.append([cY, cX])
# attempt at finding rectangles
map = {}
for p in points:
map[p[1]] = []
for p in points:
map[p[1]].append(p[0])
# Check for rectangles
keys = sorted(map.keys(), key=int)
for i in range(len(keys)-1):
one = np.array(map[keys[i]])
two = np.array(map[keys[i+1]])
intersect = np.in1d(one,two)
intersect2 = np.in1d(two,one)
# If two horizontal collections have an intersection it's likely a cell
if (sum(intersect) >= 2):
intersects = sorted(one[intersect], key=int)
for x in range(len(intersects)-1):
rect = [keys[i], intersects[x],keys[i+1], intersects[x+1]]
showimg(rois[numimg][rect[1]:rect[3],rect[0]:rect[2]])