Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2022-154
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
2022-154
2022-154
Commits
651ca451
Commit
651ca451
authored
Oct 09, 2022
by
shevon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Image processing code added
parent
43501cfd
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
133 additions
and
22 deletions
+133
-22
iot/eyebot/automatic_navigation.py
iot/eyebot/automatic_navigation.py
+133
-22
No files found.
iot/eyebot/automatic_navigation.py
View file @
651ca451
...
...
@@ -6,6 +6,9 @@ from picamera import PiCamera
import
time
import
mysql.connector
from
mysql.connector
import
errorcode
import
cv2
import
matplotlib.pyplot
import
numpy
as
np
# Direction pin from controller
DIR
=
10
...
...
@@ -51,11 +54,102 @@ cursor = conn.cursor()
sql_insert_blob_query
=
""" INSERT INTO plant_images
(img_ID, plant_ID, date, image) VALUES (
%
s,
%
s,
%
s,
%
s)"""
sql_insert_blob_query_procssed
=
""" INSERT INTO processed_images
(img_ID, plant_ID, date, image) VALUES (
%
s,
%
s,
%
s,
%
s)"""
#cnx = mysql.connector.connect(user="ThyagaSenatilaka@azdbmysqltest", password="Thagi.12345", host="azdbmysqltest.mysql.database.azure.com", port=3306, database="agroengine", ssl_verify_cert=False)
#if cnx.is_connected():
# print("Connection established")
def
processImage
(
image
):
#Read the path of the image
#------------------------------------------------------
# read image, support bmp,jpg,png,tiff format
#Read the path of the image in the computer
img
=
cv2
.
imread
(
image
)
#Change the image path to your own
#Set the RGB value in the image to a 64-bit floating point
img
=
np
.
array
(
img
,
dtype
=
'float64'
)
#Declare the three 0 matrices, and put the R,G, and B values of the picture into the three matrices.
b
=
np
.
zeros
((
img
.
shape
[
0
],
img
.
shape
[
1
]),
dtype
=
img
.
dtype
)
g
=
np
.
zeros
((
img
.
shape
[
0
],
img
.
shape
[
1
]),
dtype
=
img
.
dtype
)
r
=
np
.
zeros
((
img
.
shape
[
0
],
img
.
shape
[
1
]),
dtype
=
img
.
dtype
)
b
[:,:]
=
img
[:,:,
0
]
g
[:,:]
=
img
[:,:,
1
]
r
[:,:]
=
img
[:,:,
2
]
#A formula for generating grayscale images
new
=
2
*
g
-
r
-
b
w
=
new
.
min
()
e
=
new
.
max
()
new
=
new
-
w
new
=
new
/
e
*
255
new
=
np
.
array
(
new
,
dtype
=
'uint8'
)
#-----------------------------------------------------------------------
# Otsu filtering method and fill the hole
#-----------------------------------------------------------------------
# A Otsu filtering method is used for filtering
ret2
,
th2
=
cv2
.
threshold
(
new
,
0
,
255
,
cv2
.
THRESH_BINARY
+
cv2
.
THRESH_OTSU
)
#Check the threshold of Otsu filtering method
print
(
"threshold of Otsu filtering:"
,
ret2
)
# Otsu filtering's results are replicated to hole
hole
=
th2
.
copy
()
#Find the hole and fill it
cv2
.
floodFill
(
hole
,
None
,(
0
,
0
),
255
)
hole
=
cv2
.
bitwise_not
(
hole
)
filledEdgesOut
=
cv2
.
bitwise_or
(
th2
,
hole
)
#---------------------------------------------------------------------
#The image of corrosion
#-------------------------------------------------------------------------------
#A circle of diameter 5 is used as the corrosion structure
kernel
=
cv2
.
getStructuringElement
(
cv2
.
MORPH_ELLIPSE
,(
5
,
5
))
#image of corrosion
eroded
=
cv2
.
erode
(
filledEdgesOut
,
kernel
)
#-------------------------------------------------------------------------------
#Eliminate connected region
#-------------------------------------------------------------------------------
def
baweraopen
(
image
,
size
):
output
=
image
.
copy
()
nlabels
,
labels
,
stats
,
centroids
=
cv2
.
connectedComponentsWithStats
(
image
)
for
i
in
range
(
1
,
nlabels
-
1
):
regions_size
=
stats
[
i
,
4
]
if
regions_size
<
size
:
x0
=
stats
[
i
,
0
]
y0
=
stats
[
i
,
1
]
x1
=
stats
[
i
,
0
]
+
stats
[
i
,
2
]
y1
=
stats
[
i
,
1
]
+
stats
[
i
,
3
]
for
row
in
range
(
y0
,
y1
):
for
col
in
range
(
x0
,
x1
):
if
labels
[
row
,
col
]
==
i
:
output
[
row
,
col
]
=
0
return
output
im2
=
baweraopen
(
eroded
,
180
)
#200 is the size of the connected region to be eliminated.
#---------------------------------------------------------------------------------
# Count the number of pixels in the plant
#---------------------------------------------------------------------------------
print
(
"number of pixels in the plant:"
,
len
(
im2
.
nonzero
()[
0
]))
#distance is 50
distance_top
=
50
Area
=
(
pow
((
0.000122
*
(
distance_top
-
0.304
)
/
0.304
),
2
)
*
len
(
im2
.
nonzero
()[
0
]))
print
(
"leaf area:"
,
round
(
Area
,
2
))
#---------------------------------------------------------------------------------
#show the new image.
#---------------------------------------------------------------------------------
img
[:,:,
2
]
=
im2
*
r
img
[:,:,
1
]
=
im2
*
g
img
[:,:,
0
]
=
im2
*
b
matplotlib
.
pyplot
.
imshow
((
img
*
255
)
.
astype
(
np
.
uint8
))
#---------------------------------------------------------------------------------
#save the new image
filename
=
"p_"
+
image
cv2
.
imwrite
(
filename
,
img
)
def
tower_rotate_cw
(
x
):
print
(
"tower cw rotate "
,
x
)
GPIO
.
output
(
DIR
,
CW
)
...
...
@@ -122,7 +216,21 @@ def capture(index):
insert_blob_tuple
=
(
name
,
str
(
index
),
date
,
blob
)
result
=
cursor
.
execute
(
sql_insert_blob_query
,
insert_blob_tuple
)
conn
.
commit
()
print
(
"Uploaded Successfully"
)
print
(
"Raw Image Uploaded Successfully"
)
time
.
sleep
(
2
)
processImage
(
pic_name
)
processed_img
=
"p_"
+
pic_name
blob
=
convertToBinaryData
(
processed_img
)
# Convert data into tuple format
insert_blob_tuple
=
(
"p_"
+
name
,
str
(
index
),
date
,
blob
)
result
=
cursor
.
execute
(
sql_insert_blob_query_procssed
,
insert_blob_tuple
)
conn
.
commit
()
print
(
"Processed Image Uploaded Successfully"
)
time
.
sleep
(
2
)
...
...
@@ -144,49 +252,49 @@ try:
#
tower_rotate_cw(0)
tower_rotate_cw
(
0
)
capture
(
0
)
#
camera_movement_cw(1)
camera_movement_cw
(
1
)
capture
(
1
)
#
camera_movement_cw(2)
camera_movement_cw
(
2
)
capture
(
2
)
#
camera_movement_cw(3)
camera_movement_cw
(
3
)
capture
(
3
)
#
camera_movement_cw(4)
camera_movement_cw
(
4
)
capture
(
4
)
#
tower_rotate_cw(12200)
tower_rotate_cw
(
12200
)
capture
(
5
)
#
camera_movement_ccw(1)
camera_movement_ccw
(
1
)
capture
(
6
)
#
camera_movement_ccw(2)
camera_movement_ccw
(
2
)
capture
(
7
)
#
camera_movement_ccw(3)
camera_movement_ccw
(
3
)
capture
(
8
)
#
camera_movement_ccw(4)
camera_movement_ccw
(
4
)
capture
(
9
)
#
tower_rotate_cw(12200)
tower_rotate_cw
(
12200
)
capture
(
10
)
#
camera_movement_cw(1)
camera_movement_cw
(
1
)
capture
(
11
)
#
camera_movement_cw(2)
camera_movement_cw
(
2
)
capture
(
12
)
#
camera_movement_cw(3)
camera_movement_cw
(
3
)
capture
(
13
)
#
camera_movement_cw(4)
camera_movement_cw
(
4
)
capture
(
14
)
#
camera_movement_ccw(1)
#
camera_movement_ccw(2)
#
camera_movement_ccw(3)
#
camera_movement_ccw(4)
camera_movement_ccw
(
1
)
camera_movement_ccw
(
2
)
camera_movement_ccw
(
3
)
camera_movement_ccw
(
4
)
#
tower_rotate_ccw(24400)
tower_rotate_ccw
(
24400
)
if
conn
.
is_connected
():
cursor
.
close
()
...
...
@@ -199,3 +307,6 @@ except KeyboardInterrupt:
GPIO
.
cleanup
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment