日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

机器学习苹果识别——python+opencv实现物体特征提取

發布時間:2024/1/8 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 机器学习苹果识别——python+opencv实现物体特征提取 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

以水果為例。要用機器學習來實現水果識別,無論是訓練還是識別階段都需要提取圖片中水果的特征值。本篇將講述如何提取水果的周長、面積、顏色、長度、寬度7個特征值。

cv.findContours
cv.findContours將圖片中識別到的輪廓返回給contours變量,contours是一個list類型數據,里面存放了識別到的所有輪廓。有時候并不能很好的將目標輪廓完整的識別出來或者有沒有去除掉的噪點的干擾所以不能簡單粗暴的將獲取到的輪廓全部運用。
輪廓可以通過cv2.contourArea和cv2.arcLength(cnt,True)分別獲取面積和周長,但是因為輪廓是錯誤的,面積和周長求出來是不正確的。但是通過畫出來的矩形框可以明顯看出第二種方法是優于第一種方法的,所以我們要另找方法來獲得周長和面積等。

# https://blog.csdn.net/lovebyz/article/details/83276435 openCV獲取圖像特征點的方法NBNBN #!usr/env/bin python3 # 本文鏈接:https://blog.csdn.net/qq_36699423/article/details/84728238from math import * import cv2 as cv import numpy as npfile = 'S:\\AdobeppPS\\SKOO\\cc202.jpg' # p=file+'data.txt' f = open("S:\\AdobeppPS\\ceshi03.txt",'w+') #f = open(p, 'a')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.shapefor 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.shapefor 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)# 計算邊緣# 50和150參數必須符合1:3或者1:2edge_output = cv.Canny(xgrad, ygrad, 50, 150)contours, heriachy = cv.findContours(edge_output, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)# max = 0# maxA = 0num = []for i, contour in enumerate(contours):x, y, w, h = cv.boundingRect(contour)# if (w * h > maxA):# max = i# maxA = w * hif w < 50 or h < 50:continuenum.append(i)for i in num:# cv.drawContours(img, contours, i, (0, 0, 255), 2)# x, y, w, h = cv.boundingRect(contours[i])# img = cv.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)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)# f.write(str(area))# f.write(' ')# f.write(str(circumference))# f.write(' ')# f.write(str(longth))# f.write(' ')# f.write(str(width))# f.write(' ')# for i in range(3):# f.write(str(color[i]))# f.write(' ')# f.write('\n') def do():for i in range(1, 8, 1):print(i, ':') # path = file + str(i) + '.jpg'# src1 = cv.imread(path)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) # cv.destroyAllWindows()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实现物体特征提取的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。