以水果为例。要用机器学习来实现水果识别,无论是训练还是识别阶段都需要提取图片中水果的特征值。本篇将讲述如何提取水果的周长、面积、颜色、长度、宽度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实现物体特征提取相关推荐

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

    文章目录 一.用python+opencv实现物体特征值提取 1.读取图像.转为灰度图像并降噪 2.获取水果轮廓 将最大轮廓画入最开始的img图像并显示 将小于某一规模的轮廓删除 3.提取水果的面积周 ...

  2. python手写汉字识别_中文简历表格提取,手写汉字识别(Python+OpenCV)

    原标题:中文简历表格提取,手写汉字识别(Python+OpenCV) 所有代码获取: 简历 网上对表格框的提取的相关资料较少,尤其是Python+OpenCV的实现方面. 整体流程 如今OpenCV官 ...

  3. 数字图像处理二维码识别 python+opencv实现二维码实时识别

    数字图像处理二维码识别 python+opencv实现二维码实时识别 特点: (1)可以实现普通二维码,条形码: (2)解决了opencv输出中文乱码的问题 (3)增加网页自动跳转功能 (4)实现二维 ...

  4. php-opencv身份证识别,python opencv实现证件照换底功能

    本文实例为大家分享了python opencv实现证件照换底功能的具体代码,供大家参考,具体内容如下 思路:先转到HSV空间,利用颜色提取背景制作掩模版mask,然后通过按位操作提取人像和制作新背景, ...

  5. python图片识别-Python+Opencv识别两张相似图片

    在网上看到python做图像识别的相关文章后,真心感觉python的功能实在太强大,因此将这些文章总结一下,建立一下自己的知识体系. 当然了,图像识别这个话题作为计算机科学的一个分支,不可能就在本文简 ...

  6. python opencv数字识别_基于模板匹配的手写数字识别(python+opencv)

    智能计算课第一周的实验是做基于模板匹配的手写数字识别,光听见就很感兴趣,于是决定认真做做这个实验,本实验基于python3+opencv的python版本,所用到的知识都比较简单,基本上边学边做,技术 ...

  7. 大数据毕设项目 深度学习火焰检测识别 python opencv

    文章目录 0 前言 1 基于YOLO的火焰检测与识别 2 课题背景 3 卷积神经网络 3.1 卷积层 3.2 池化层 3.3 激活函数: 3.4 全连接层 3.5 使用tensorflow中keras ...

  8. 【毕业设计】深度学习实现行人重识别 - python opencv yolo Reid

    文章目录 0 前言 1 课题背景 2 效果展示 3 行人检测 4 行人重识别 5 其他工具 6 最后 0 前言

  9. 基于python+opencv的图像目标区域自动提取

    向AI转型的程序员都关注了这个号???????????? 机器学习AI算法工程   公众号:datayx 一.提取纸张中的内容 一张照片中的感兴趣区域总是沿着x,y,z三个轴都有一定倾斜(如下图),要 ...

最新文章

  1. php图片加边框,php在图片上增加矩形框并加入水印
  2. gis快速接地开关_一种基于扫描电镜和能谱仪的GIS放电异物来源分析方法
  3. 关于个别网段上网时断时续的问题解决
  4. 详解:数据库名、实例名、ORACLE_SID、数据库域名、全局数据库名、服务名及手工脚本创建oracle数据库...
  5. C#中数据流(文件流、内存流、网络流等)相关知识点梳理
  6. 基于动态混合高斯模型的商品价格模型算法
  7. linux 转码软件,分享|Linux 桌面中 4 个开源媒体转换工具
  8. J2ME移动应用开发实战视频教程
  9. [Swift]LeetCode862. 和至少为 K 的最短子数组 | Shortest Subarray with Sum at Least K
  10. 本人博客已经转到简书,不再更新51cto,谢谢大家。
  11. 【VS2015】 C++实现硬件ID的查询
  12. S4 HANA 1809 FPS03 Standard装机总结(刘欣2019.11.7)
  13. Happiness is a Choise
  14. 解决升级Flutter3.0后出现警告Operand of null-aware operation ‘!‘ has type ‘WidgetsBinding‘ which excludes null
  15. Dreamweaver cs3快捷键一览
  16. java计算抛物线的标准方程_抛物线及其标准方程
  17. 【locust】使用locust + boomer实现对接口的压测
  18. 使用Nginx访问图片报404
  19. 小数点如何用计算机二进制表示,计算机二进制小数点表示法
  20. 【Python学习随笔】依赖倒置原则 + 简单工厂模式

热门文章

  1. nacos 安装包下载 linux+windows
  2. 7-1 计算职工工资c语言,C语言职工工资管理系统
  3. MVC路由自定义及视图找寻规则
  4. Direct3D初始化失败的原因
  5. 网上那些代理IP是哪儿来的
  6. 团队博客-第二周:需求规格说明书(科利尔拉弗队)
  7. 面试官:什么是静态代理?什么是动态代理?注解、反射你会吗?
  8. Java基础----【异常、线程】
  9. 腾讯开源Spring Cloud Tencent 是什么
  10. php红包退回通知,PHP红包算法