生活随笔
收集整理的這篇文章主要介紹了
机器学习苹果识别——python+opencv实现物体特征提取
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
以水果為例。要用機(jī)器學(xué)習(xí)來實(shí)現(xiàn)水果識(shí)別,無論是訓(xùn)練還是識(shí)別階段都需要提取圖片中水果的特征值。本篇將講述如何提取水果的周長(zhǎng)、面積、顏色、長(zhǎng)度、寬度7個(gè)特征值。
cv.findContours
cv.findContours將圖片中識(shí)別到的輪廓返回給contours變量,contours是一個(gè)list類型數(shù)據(jù),里面存放了識(shí)別到的所有輪廓。有時(shí)候并不能很好的將目標(biāo)輪廓完整的識(shí)別出來或者有沒有去除掉的噪點(diǎn)的干擾所以不能簡(jiǎn)單粗暴的將獲取到的輪廓全部運(yùn)用。
輪廓可以通過cv2.contourArea和cv2.arcLength(cnt,True)分別獲取面積和周長(zhǎng),但是因?yàn)檩喞清e(cuò)誤的,面積和周長(zhǎng)求出來是不正確的。但是通過畫出來的矩形框可以明顯看出第二種方法是優(yōu)于第一種方法的,所以我們要另找方法來獲得周長(zhǎng)和面積等。
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
()
版權(quán)聲明:本文為博主原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請(qǐng)附上原文出處鏈接和本聲明。
本文鏈接:https://blog.csdn.net/qq_36699423/article/details/84728238
————————————————
版權(quán)聲明:本文為CSDN博主「qq_36699423」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qq_36699423/article/details/84728238
總結(jié)
以上是生活随笔為你收集整理的机器学习苹果识别——python+opencv实现物体特征提取的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。