I have this input image, and I want to grid it into cells 22 x 10
to quantify blocks' color later.
Note that this dynamic approach is necessary, as I might have a different number of cells, resolution, etc...
Can you please tell me what is wrong with my modification of the original solution? thanks in advance.
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import numpy as np
import cv2
def gridWorkspace(roi, gridSize=(10, 22), verbose=True):
"""
Function: gridWorkspace, to find the contours of the red markers.
---
Parameters:
@param: roi, nd array, cropped region of interest.
@param: gridSize, tuple, lenght/width or the Workspace.
@param: verbose, boolean, to show the output of the function.
---
@return: cellList, list, cells coordinates list,
cellCenters, list, cells centers list.
"""
# Store a deep copy for results:
roi_copy = roi.copy()
# Divide the image into a grid:
verticalCells = gridSize[1]
horizontalCells = gridSize[0]
# Cell dimensions
bigRectWidth = roi.shape[1]
bigRectHeight = roi.shape[0]
cellWidth = bigRectWidth // verticalCells
cellHeight = bigRectHeight // horizontalCells
# Store the cells here:
cellList = []
# Store cell centers here:
cellCenters = []
# Loop thru vertical dimension:
for j in range(verticalCells):
# Cell starting y position:
yo = j * cellHeight
# Loop thru horizontal dimension:
for i in range(horizontalCells):
# Cell starting x position:
xo = i * cellWidth
# Cell Dimensions:
cX = int(xo)
cY = int(yo)
# Crop current cell:
currentCell = roi[cY:cY cellHeight, cX:cX cellWidth]
# into the cell list:
cellList.append(currentCell)
# Store cell center:
cellCenters.append((cX 0.5 * cellWidth, cY 0.5 * cellHeight))
# Draw Cell
cv2.rectangle(roi_copy, (cX, cY), (cX cellWidth, cY cellHeight), (100, 100, 255), 1)
# Visualize results
if(verbose):
cv2.namedWindow("Grid", cv2.WINDOW_NORMAL)
cv2.imshow("Grid", roi_copy)
cv2.waitKey(0)
return cellList, cellCenters
roi = cv2.imread("DsUYY.png")
res = gridWorkspace(roi)
CodePudding user response:
check verticalCells, horizontalCells and gridSize[index] twice!!
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import numpy as np
import cv2
def gridWorkspace(roi, gridSize=(22, 10), verbose=True):
"""
Function: gridWorkspace, to find the contours of the red markers.
---
Parameters:
@param: roi, nd array, cropped region of interest.
@param: gridSize, tuple, lenght/width or the Workspace.
@param: verbose, boolean, to show the output of the function.
---
@return: cellList, list, cells coordinates list,
cellCenters, list, cells centers list.
"""
# Store a deep copy for results:
roi_copy = roi.copy()
# Divide the image into a grid:
verticalCells = gridSize[1]
horizontalCells = gridSize[0]
# Cell dimensions
bigRectWidth = roi.shape[1]
bigRectHeight = roi.shape[0]
cellWidth = bigRectWidth // horizontalCells
cellHeight = bigRectHeight // verticalCells
# Store the cells here:
cellList = []
# Store cell centers here:
cellCenters = []
# Loop thru vertical dimension:
for j in range(verticalCells):
# Cell starting y position:
yo = j * cellHeight
# Loop thru horizontal dimension:
for i in range(horizontalCells):
# Cell starting x position:
xo = i * cellWidth
# Cell Dimensions:
cX = int(xo)
cY = int(yo)
# Crop current cell:
currentCell = roi[cY:cY cellHeight, cX:cX cellWidth]
# into the cell list:
cellList.append(currentCell)
# Store cell center:
cellCenters.append((cX 0.5 * cellWidth, cY 0.5 * cellHeight))
# Draw Cell
cv2.rectangle(roi_copy, (cX, cY), (cX cellWidth, cY cellHeight), (100, 100, 255), 1)
# Visualize results
if(verbose):
cv2.namedWindow("Grid", cv2.WINDOW_NORMAL)
cv2.imshow("Grid", roi_copy)
cv2.waitKey(0)
return cellList, cellCenters
roi = cv2.imread("/Users/buenos/buenos/playground/python/assets/DsUYY.png")
res = gridWorkspace(roi)