用opencv-python数玉米粒数并且判断玉米品质

openCV是一个非常强大的视觉计算的模块,我们可以通过python来调用它对图像进行一些列的操作完成计算机视觉的任务。比如我们可以给出如下的玉米图片,来让python自动输出图片中玉米的颗粒数,并且判定玉米的品质。
用opencv-python数玉米粒数并且判断玉米品质_第1张图片
大致的思路按照二值化、腐蚀、距离变换、轮廓提取的流程进行,通过内切圆与整体面积比来判定玉米颗粒的饱满度,我们可以写出一下程序:

# -*- coding: utf-8 -*-
import cv2
import numpy as np
import time
font = cv2.FONT_HERSHEY_COMPLEX
kernel = np.ones((7, 7), np.uint8)
img = cv2.imread(r"P:\\Projects\\visualstudio\\OpenCv_project\\OC\\picture\\corns.jpg")
cv2.imshow('Original Image', img)
cv2.waitKey(0)
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  # 灰度处理
cv2.imshow('GrayImg', gray_img)
cv2.waitKey(0)
ret, th1 = cv2.threshold(gray_img, 120, 255, cv2.THRESH_BINARY)
cv2.imshow('Threshold', th1)
cv2.waitKey(0)
erosion = cv2.erode(th1, kernel, iterations=1)  # 腐蚀
cv2.imshow('Erosion', erosion)
cv2.waitKey(0)
dist_img = cv2.distanceTransform(erosion, cv2.DIST_L1, cv2.DIST_MASK_3)  # 距离变换
cv2.imshow('DistanceTransformation', dist_img)
cv2.waitKey(0)
dist_output = cv2.normalize(dist_img, 0, 1.0, cv2.NORM_MINMAX)  # 归一化
cv2.imshow('Normalize', dist_output * 80)
cv2.waitKey(0)
ret, th2 = cv2.threshold(dist_output * 80, 0.3, 255, cv2.THRESH_BINARY)
cv2.imshow('Threshold', th2)
cv2.waitKey(0)
kernel = np.ones((5, 5), np.uint8)
opening = cv2.morphologyEx(th2, cv2.MORPH_OPEN, kernel)
cv2.imshow('Opening', opening)
cv2.waitKey(0)
opening = np.array(opening, np.uint8)
contours, hierarchy = cv2.findContours(opening, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)  # 轮廓提取
count = 0
for cnt in contours:
	(x, y), radius = cv2.minEnclosingCircle(cnt)
	center = (int(x), int(y))
	radius = int(radius)
	circle_img = cv2.circle(opening, center, radius, (255, 255, 255), 1)
	area = cv2.contourArea(cnt)
	area_circle = 3.14 * radius * radius
	# print(area/area_circle)
	if area / area_circle <= 0.5:
		# img = cv2.drawContours(img, cnt, -1, (0,0,255), 5)#差(红色)
		img = cv2.putText(img, 'bad', center, font, 0.5, (0, 0, 255))
	elif area / area_circle >= 0.6:
		# img = cv2.drawContours(img, cnt, -1, (0,255,0), 5)#优(绿色)
		img = cv2.putText(img, 'good', center, font, 0.5, (0, 0, 255))
	else:
		# img = cv2.drawContours(img, cnt, -1, (255,0,0), 5)#良(蓝色)
		img = cv2.putText(img, 'normal', center, font, 0.5, (0, 0, 255))
	count += 1
img = cv2.putText(img, ('sum=' + str(count)), (50, 50), font, 1, (255, 0, 0))
cv2.imshow('circle_img', img)
cv2.waitKey(0)
print('玉米粒数量:', count)

最终,我们将得到以下结果,注意在显示图片的时候,我们需要将cv2.imshow()与waitKey()函数配合使用才能正确显示图像,最终结果如下:
用opencv-python数玉米粒数并且判断玉米品质_第2张图片

你可能感兴趣的:(#,python习题)