600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > 机器学习苹果识别——python+opencv实现物体特征提取

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

时间:2023-01-02 09:17:44

相关推荐

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

以水果为例。要用机器学习来实现水果识别,无论是训练还是识别阶段都需要提取图片中水果的特征值。本篇将讲述如何提取水果的周长、面积、颜色、长度、宽度7个特征值。

cv.findContours

cv.findContours将图片中识别到的轮廓返回给contours变量,contours是一个list类型数据,里面存放了识别到的所有轮廓。有时候并不能很好的将目标轮廓完整的识别出来或者有没有去除掉的噪点的干扰所以不能简单粗暴的将获取到的轮廓全部运用。

轮廓可以通过cv2.contourArea和cv2.arcLength(cnt,True)分别获取面积和周长,但是因为轮廓是错误的,面积和周长求出来是不正确的。但是通过画出来的矩形框可以明显看出第二种方法是优于第一种方法的,所以我们要另找方法来获得周长和面积等。

# /lovebyz/article/details/83276435 openCV获取图像特征点的方法NBNBN#!usr/env/bin python3# 本文链接:/qq_36699423/article/details/84728238from math import *import cv2 as cvimport 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 版权协议,转载请附上原文出处链接和本声明。

本文链接:/qq_36699423/article/details/84728238

————————————————

版权声明:本文为CSDN博主「qq_36699423」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:/qq_36699423/article/details/84728238

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。