我有被碘溶液浸泡过的苹果片的照片。目标是将苹果分割成感兴趣的各个区域,并评估每个区域的淀粉水平。这是一个学校项目,所以我的目标是测试不同的分割方法,客观地找到最好的解决方案,无论是单一技术还是多种技术的组合
问题是,到目前为止,我只接近于一种方法。这种方法是使用霍夫圆。我最初计划使用分水岭方法、形态学操作或简单阈值。这个计划失败了,因为我无法修改它们中的任何一个
原始图像看起来与此相似,苹果的黑暗程度各不相同
我尝试过使用cv2.inRange和HSV值来移除背景托盘,但是对于深色的苹果来说效果不好
这是HoughCircles在原始图像上生成的,应用了灰度和中值模糊,还尝试了托盘遮罩
任何关于下一步行动方向的建议都将不胜感激。如果有帮助的话,我可以提供我正在使用的代码
谢谢大家!
编辑1:添加一些代码并澄清问题
谢谢你的回复。我真正的问题是,有没有其他的分割方法适合这个场景?我想尝试两种不同的方法,并在一大组照片上比较结果。我下一步要尝试的是使用k-means分割。此外,我将在下面添加一些代码,以显示我迄今为止所做的尝试
HSV颜色过滤
import cv2
import numpy as np
# Load image
image = cv2.imread('ApplePic.jpg')
# Set minimum and max HSV values to display
lower = np.array([0, 0, 0])
upper = np.array([105, 200, 255])
# Create HSV Image and threshold into a range.
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, lower, upper)
maskedImage = cv2.bitwise_and(image, image, mask=mask)
# Show Image
cv2.imshow('HSV Mask', image)
cv2.waitKey(0)
霍格圈
# import the necessary packages
import numpy as np
import argparse
import cv2
import os
directory = os.fsencode('Photos\\Sample N 100')
for file in os.listdir(directory):
filename = os.fsdecode(file)
if filename.endswith('.jpg'):
# Load the image
image = cv2.imread('Photos\\Sample N 100\\' + filename)
# Calculate scale
scale_factor = 800 / image.shape[0]
width = int(image.shape[1] * scale_factor)
height = 800
dimension = (width, height)
min_radius = int((width / 10) * .8)
max_radius = int((width / 10) * 1.2)
# Resize image
image = cv2.resize(image, dimension, interpolation=cv2.INTER_AREA)
# Copy Image
output = image.copy()
# Grayscale Image
gray = cv2.medianBlur(cv2.cvtColor(image, cv2.COLOR_BGR2GRAY), 5)
# Detect circles in image
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, min_radius * 2, 4, 60, 20, min_radius, max_radius)
# ensure at least some circles were found
if circles is not None:
# convert the (x, y) coordinates and radius of the circles to integers
circles = np.round(circles[0, :]).astype("int")
# loop over the (x, y) coordinates and radius of the circles
for (x, y, r) in circles:
# draw the circle in the output image, then draw a rectangle
# corresponding to the center of the circle
cv2.circle(output, (x, y), r, (0, 255, 0), 4)
cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)
cv2.putText(output, '(' + str(x) + ',' + str(y) + ',' + str(r) + ')', (x, y),
cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, 255)
# show the output image
cv2.imshow("output", np.hstack([image, output, maskedImage]))
cv2.waitKey(0)
continue
else:
continue
分割苹果的另一种方法是在阈值化之前执行Kmeans颜色分割,然后使用轮廓滤波分离苹果对象:
应用Kmeans颜色分割。我们加载图像,使用^{} 调整较小的大小,然后应用Kmeans颜色分割。根据簇的数量,我们可以将图像分割成所需数量的颜色
获得二值图像。接下来我们将转换为灰度、高斯模糊和大津阈值
使用轮廓近似进行过滤。我们过滤掉非圆形轮廓和小噪声
形态学操作。我们执行变形接近以填充相邻轮廓
使用轮廓面积作为过滤器绘制最小封闭圆。我们找到轮廓并绘制近似圆。为此,我们使用两个部分,一个是有一个好的阈值,另一个是我们近似半径
Kmeans使用
clusters=3
和二值图像进行颜色量化变形闭合与结果
使用
cv2.minEnclosingCircle
自动计算半径的“良好”等高线以绿色突出显示,而近似等高线以teal突出显示。这些近似轮廓没有从阈值过程中很好地分割出来,因此我们平均“良好”轮廓半径,并使用该半径绘制圆代码
相关问题 更多 >
编程相关推荐