我有浸泡在碘溶液中的苹果片的照片。目标是将苹果分割成各个感兴趣的区域并评估每个区域的淀粉水平。这是一个学校项目,所以我的目标是测试不同的分割方法,并客观地找到最佳解决方案,无论是单一技术还是多种技术的组合。
问题是到目前为止我只接近一种方法。该方法使用 HoughCircles。我原本计划使用分水岭方法、形态学操作或简单的阈值处理。当我无法修改其中任何一个以使其工作时,该计划失败了。
原始图像看起来与此类似,苹果的深色深浅不同
我尝试使用带有 HSV 值的 cv2.inRange 来删除背景托盘,但它不适用于较深的苹果。
这就是 HoughCircles 在应用了灰度和中值模糊以及尝试对托盘进行遮罩的原始图像上产生的结果。
任何关于下一步该去哪里的建议或方向将不胜感激。如果有帮助的话,我可以提供我正在使用的代码。
谢谢你!
编辑1:添加一些代码并澄清问题
感谢您的回复。我真正的问题是,还有其他适合这种情况的细分方法吗?我想尝试几种不同的方法并比较大量照片的结果。我接下来要尝试的是使用 k 均值分割。另外,我将在下面添加一些代码来展示我到目前为止所尝试的内容。
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