生活随笔
收集整理的這篇文章主要介紹了
机器学习苹果识别——python+opencv实现物体特征提取
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
以水果為例。要用機器學習來實現水果識別,無論是訓練還是識別階段都需要提取圖片中水果的特征值。本篇將講述如何提取水果的周長、面積、顏色、長度、寬度7個特征值。
cv.findContours
cv.findContours將圖片中識別到的輪廓返回給contours變量,contours是一個list類型數據,里面存放了識別到的所有輪廓。有時候并不能很好的將目標輪廓完整的識別出來或者有沒有去除掉的噪點的干擾所以不能簡單粗暴的將獲取到的輪廓全部運用。
輪廓可以通過cv2.contourArea和cv2.arcLength(cnt,True)分別獲取面積和周長,但是因為輪廓是錯誤的,面積和周長求出來是不正確的。但是通過畫出來的矩形框可以明顯看出第二種方法是優于第一種方法的,所以我們要另找方法來獲得周長和面積等。
from math
import *
import cv2 as cv
import numpy as np
file = 'S:\\AdobeppPS\\SKOO\\cc202.jpg'
f
= open
("S:\\AdobeppPS\\ceshi03.txt",
'w+')
def myCut
(img, x, y, w, h
):
cut = img
[y:y + h, x:x + w
]cv.imshow
("cut",
cut)return cutdef GetColor
(img, point_height, point_width
):R
= 0G
= 0B
= 0count
= 0color
= []for i in range
(0, len
(point_height
),
1):count
+= 1R
+= img
[point_height
[i
], point_width
[i
]][0]G
+= img
[point_height
[i
], point_width
[i
]][1]B
+= img
[point_height
[i
], point_width
[i
]][2]R
= int
(R / count
)G
= int
(G / count
)B
= int
(B / count
)color.append
(R
)color.append
(G
)color.append
(B
)return color
def GetArea
(img
):count
= 0point_height
= []point_width
= []height, width
= img.shape
for h in range
(0, height,
1):
for w in range
(0, width,
1):
if (img
[h, w
] == 0):count
+= 1point_height.append
(h
)point_width.append
(w
)return count, point_width, point_height
def GetCircumference
(img
):count
= 0height, width
= img.shape
for h in range
(0, height,
1):
for w in range
(0, width,
1):
if (img
[h, w
] == 255):count
+= 1return countdef edge
(img
):gray
= cv.cvtColor
(img, cv.COLOR_BGR2GRAY
)blurred
= cv.GaussianBlur
(gray,
(3,
3),
0)xgrad
= cv.Sobel
(blurred, cv.CV_16SC1,
1,
0)ygrad
= cv.Sobel
(blurred, cv.CV_16SC1,
0,
1)edge_output
= cv.Canny
(xgrad, ygrad,
50,
150)contours, heriachy
= cv.findContours
(edge_output, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE
)num
= []for i, contour
in enumerate
(contours
):x, y, w, h
= cv.boundingRect
(contour
)if w
< 50 or h
< 50:
continuenum.append
(i
)for i in num:
if i
== 0:
continuecontours
[0] = np.concatenate
((contours[i], contours[0]))cv.imshow
('img', img
)x, y, w, h
= cv.boundingRect
(contours
[0])cut_img
= myCut
(img, x, y, w, h
)cut_blurred
= myCut
(blurred, x, y, w, h
)cv.imshow
('cut', cut_blurred
)ret, binary
= cv.threshold
(cut_blurred,
70,
255, cv.THRESH_BINARY
)cv.imshow
("bi", binary
) edge
= cv.Canny
(binary,
40,
100)cv.imshow
("edge", edge
) longth
= 0width
= 0if w
> h:longth
= wwidth
= helse:longth
= hwidth
= warea, point_width, point_height
= GetArea
(binary
)circumference
= GetCircumference
(edge
)color
= GetColor
(cut_img, point_height, point_width
)print
('area:', area,
'circumference:', circumference,
'longth:', longth,
'width:', width,
'color:', color
)
def do
():
for i in range
(1,
8,
1):print
(i,
':')
src1
= cv.imread
(file
)size
= src1.shapesrc
= cv.resize
(src1,
((int)(size[1] / 5), (int)(size[0] / 5)), cv.INTER_LINEAR
)edge
(src
)cv.waitKey
(0)
f.closed
()
do
()
版權聲明:本文為博主原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接和本聲明。
本文鏈接:https://blog.csdn.net/qq_36699423/article/details/84728238
————————————————
版權聲明:本文為CSDN博主「qq_36699423」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qq_36699423/article/details/84728238
總結
以上是生活随笔為你收集整理的机器学习苹果识别——python+opencv实现物体特征提取的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。