Skip to content

Commit

Permalink
code feature extractor update
Browse files Browse the repository at this point in the history
  • Loading branch information
alirezaahmadi committed May 13, 2022
1 parent 4894247 commit 05ae250
Show file tree
Hide file tree
Showing 9 changed files with 421 additions and 1,338 deletions.
6 changes: 3 additions & 3 deletions configs/params.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ stationaryDebug: True
# Mode 2: Driving forward with back camera
# Mode 3: Driving backwards with back camera
# Mode 4: Driving backwards with front camera
navigationMode: 2
navigationMode: 1
# angular velocity scaler
maxOmega: 0.05
minOmega: 0.01
Expand All @@ -44,11 +44,11 @@ matching_keypoints_th: 10
# scanner window params
winSweepStart: 0
winSweepEnd: 1280
winMinWidth: 128
winMinWidth: 0
# W/8 for small plants, W/5 for big plants
winSize: 200
# for filtering contours
min_contour_area: 50
min_contour_area: 10
# for dividing bushy rows
max_coutour_height: 120
# in case of using bigger size image size, we suggest to set ROI
Expand Down
68 changes: 0 additions & 68 deletions scripts/Controller.py

This file was deleted.

161 changes: 80 additions & 81 deletions scripts/contours.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

import cv2 as cv
import numpy as np
from geometric import *

def getCCenter(self, contours):
def getCCenter(contours):
"""function to compute the center points of the contours
Args:
Expand All @@ -23,14 +26,11 @@ def getCCenter(self, contours):
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
contCenterPTS[i,:] = [cy,cx]
# draw point on countur centers
self.processedIMG[cy-3:cy+3,cx-3:cx+3] = [255, 0, 255]

contCenterPTS = contCenterPTS[~np.all(contCenterPTS == 0, axis=1)]
return contCenterPTS


def getClosedRegions(self, binrayMask):
def getClosedRegions(binrayMask, min_contour_area, bushy=False):
# find contours
contours = cv.findContours(binrayMask, cv.RETR_TREE, cv.CHAIN_APPROX_NONE)[1]

Expand All @@ -41,13 +41,13 @@ def getClosedRegions(self, binrayMask):
# self.filtered_contours = contours
filtered_contours = list()
for i in range(len(contours)):
if cv.contourArea(contours[i]) > self.min_contour_area:
if cv.contourArea(contours[i]) > min_contour_area:

if self.bushy :
if bushy :
cn_x, cn_y, cnt_w, cn_h = cv.boundingRect(contours[i])

# split to N contours h/max_coutour_height
sub_contours = self.splitContours(contours[i], cn_x, cn_y, cnt_w, cn_h)
sub_contours = splitContours(contours[i], cn_x, cn_y, cnt_w, cn_h)
for j in sub_contours:
if j != []:
filtered_contours.append(j)
Expand All @@ -58,78 +58,77 @@ def getClosedRegions(self, binrayMask):
# filtered_contours.append(contours[i])
return filtered_contours

def splitContours(self, contour, x, y, w, h):
"""splits larg contours in smaller regions
Args:
contour (_type_): _description_
x (_type_): _description_
y (_type_): _description_
w (_type_): _description_
h (_type_): _description_
Returns:
_type_: sub polygons (seperated countours)
"""
sub_polygon_num = h // self.max_coutour_height
sub_polys = list()
def splitContours(contour, x, y, w, h, max_coutour_height):
"""splits larg contours in smaller regions
Args:
contour (_type_): _description_
x (_type_): _description_
y (_type_): _description_
w (_type_): _description_
h (_type_): _description_
Returns:
_type_: sub polygons (seperated countours)
"""
sub_polygon_num = h // max_coutour_height
sub_polys = list()
subContour = list()
vtx_idx = list()
contour = [contour.squeeze().tolist()]
for subPoly in range(1, sub_polygon_num + 1):
for vtx in range(len(contour[0])):
if (subPoly - 1 * max_coutour_height) -1 <= contour[0][vtx][1] and \
(subPoly * max_coutour_height) -1 >= contour[0][vtx][1] and \
vtx not in vtx_idx:
subContour.append([contour[0][vtx]])
vtx_idx.append(vtx)

sub_polys.append(np.array(subContour))
subContour = list()
vtx_idx = list()
# contour = [sorted(contour.squeeze().tolist(), key=operator.itemgetter(0), reverse=True)]
contour = [contour.squeeze().tolist()]
for subPoly in range(1, sub_polygon_num + 1):
for vtx in range(len(contour[0])):
if (subPoly - 1 * self.max_coutour_height) -1 <= contour[0][vtx][1] and \
(subPoly * self.max_coutour_height) -1 >= contour[0][vtx][1] and \
vtx not in vtx_idx:
subContour.append([contour[0][vtx]])
vtx_idx.append(vtx)

sub_polys.append(np.array(subContour))
subContour = list()

return sub_polys

def sortContours(self, cnts, method="left-to-right"):
"""initialize the reverse flag and sort index

Args:
cnts (_type_): _description_
method (str, optional): _description_. Defaults to "left-to-right".
Returns:
_type_: sorted countours, bboxes
"""
reverse = False
i = 0
# handle if we need to sort in reverse
if method == "right-to-left" or method == "bottom-to-top":
reverse = True
# handle if we are sorting against the y-coordinate rather than
# the x-coordinate of the bounding box
if method == "top-to-bottom" or method == "bottom-to-top":
i = 1
# construct the list of bounding boxes and sort them from top to
# bottom
boundingBoxes = [cv.boundingRect(c) for c in cnts]
(cnts, boundingBoxes) = zip(*sorted(zip(cnts, boundingBoxes),
key=lambda b:b[1][i], reverse=reverse))
# return the list of sorted contours and bounding boxes
return cnts, boundingBoxes

def getContoursInWindow(self, contourCenters, box):
"""iflters out countours inside a box
Args:
contourCenters (_type_): _description_
box (_type_): _description_
Returns:
_type_: contour centers
"""
points = []
for cnt in range(len(contourCenters[1])):
x, y = contourCenters[0][cnt], contourCenters[1][cnt]
if self.isInBox(list(box), [x, y]):
points.append([x, y])
return points
return sub_polys

def sortContours(contours, method="left-to-right"):
"""initialize the reverse flag and sort index
Args:
cnts (_type_): _description_
method (str, optional): _description_. Defaults to "left-to-right".
Returns:
_type_: sorted countours, bboxes
"""
reverse = False
i = 0
# handle if we need to sort in reverse
if method == "right-to-left" or method == "bottom-to-top":
reverse = True
# handle if we are sorting against the y-coordinate rather than
# the x-coordinate of the bounding box
if method == "top-to-bottom" or method == "bottom-to-top":
i = 1
# construct the list of bounding boxes and sort them from top to
# bottom
boundingBoxes = [cv.boundingRect(c) for c in contours]
(contours, boundingBoxes) = zip(*sorted(zip(contours, boundingBoxes),
key=lambda b:b[1][i], reverse=reverse))
# return the list of sorted contours and bounding boxes
return contours, boundingBoxes

def getContoursInWindow(contourCenters, box):
"""iflters out countours inside a box
Args:
contourCenters (_type_): _description_
box (_type_): _description_
Returns:
_type_: contour centers
"""
points = []
for cnt in range(len(contourCenters[1])):
x, y = contourCenters[0][cnt], contourCenters[1][cnt]
if isInBox(list(box), [x, y]):
points.append([x, y])
return points
Loading

0 comments on commit 05ae250

Please sign in to comment.