原图-》灰度化-》阈值分割-》形态学变换-》距离变换-》轮廓查找
import cv2 as cv
import matplotlib.pyplot as plt
image = cv.imread('img/img.png')
gray_image = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
ret, binary = cv.threshold(gray_image, 127, 255, cv.THRESH_BINARY)
# 寻找轮廓
contours, hierarchy = cv.findContours(binary, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
# 在原始图像的副本上绘制轮廓并标注序号
image_with_contours = image.copy()
for i, contour in enumerate(contours):
cv.drawContours(image_with_contours, [contour], -1, (122, 55, 215), 2)
# 标注轮廓序号
cv.putText(image_with_contours, str(i+1), tuple(contour[0][0]), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
# 使用 matplotlib 显示结果
plt.subplot(121), plt.imshow(cv.cvtColor(image, cv.COLOR_BGR2RGB)), plt.title('Original Image')
plt.subplot(122), plt.imshow(cv.cvtColor(image_with_contours, cv.COLOR_BGR2RGB)), plt.title('Image with Contours')
plt.show()
print (len(contours))
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
image = cv.imread('img/img.png')
gray_image= cv.cvtColor(image, cv.COLOR_BGR2GRAY)
kernel = np.ones((16, 16), np.uint8)
gray_image=cv.morphologyEx(gray_image, cv.MORPH_OPEN, kernel)
ret, binary = cv.threshold(gray_image, 127, 255, cv.THRESH_BINARY)
# 寻找轮廓
contours, hierarchy = cv.findContours(binary, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
# 在原始图像的副本上绘制轮廓并标注序号
image_with_contours = image.copy()
for i, contour in enumerate(contours):
cv.drawContours(image_with_contours, [contour], -1, (122, 55, 215), 2)
# 标注轮廓序号
cv.putText(image_with_contours, str(i+1), tuple(contour[0][0]), cv.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
# 使用 matplotlib 显示结果
plt.subplot(121), plt.imshow(cv.cvtColor(image, cv.COLOR_BGR2RGB)), plt.title('Original Image')
plt.subplot(122), plt.imshow(cv.cvtColor(image_with_contours, cv.COLOR_BGR2RGB)), plt.title('Image with Contours')
plt.show()
print (len(contours))
import cv2
# 读取图片
image = cv2.imread("path/to/your/image.png")
cv2.imshow("Original Image", image)
cv2.waitKey(0)
import cv2
import numpy as np
# 形态学处理
kernel = np.ones((16, 16), np.uint8)
morphology_result = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel)
cv2.imshow("Morphology Result", morphology_result)
cv2.waitKey(0)
import cv2
# 灰度转换
gray_image = cv2.cvtColor(morphology_result, cv2.COLOR_BGR2GRAY)
# 二值化
_, binary_image = cv2.threshold(gray_image, 100, 255, cv2.THRESH_OTSU)
cv2.imshow("Binary Image", binary_image)
cv2.waitKey(0)
import cv2
# 寻找轮廓
contours, _ = cv2.findContours(binary_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# 在原图上绘制轮廓
contour_image = image.copy()
cv2.drawContours(contour_image, contours, -1, (0, 255, 0), 2)
cv2.imshow("Contours", contour_image)
cv2.waitKey(0)
import cv2
# 遍历轮廓
for i, contour in enumerate(contours):
area = cv2.contourArea(contour)
if area < 500:
continue
# 获取轮廓的位置
(x, y, w, h) = cv2.boundingRect(contour)
# 在原图上绘制矩形
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
# 在矩形位置写上计数
cv2.putText(image, str(i), (x, y), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 2)
cv2.imshow("Count Result", image)
cv2.waitKey(0)
import cv2
import numpy as np
# 读取图片
image = cv2.imread("path/to/your/image.png")
cv2.imshow("Original Image", image)
# 形态学处理
kernel = np.ones((3, 3), np.uint8)
morphology_result = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel)
cv2.imshow("Morphology Result", morphology_result)
# 灰度转换
gray_image = cv2.cvtColor(morphology_result, cv2.COLOR_BGR2GRAY)
# 二值化
_, binary_image = cv2.threshold(gray_image, 100, 255, cv2.THRESH_OTSU)
cv2.imshow("Binary Image", binary_image)
# 寻找轮廓
contours, _ = cv2.findContours(binary_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# 统计药片数量并标记轮廓
count = 0
for i, contour in enumerate(contours):
area = cv2.contourArea(contour)
if area < 500:
continue
# 获取轮廓的位置
(x, y, w, h) = cv2.boundingRect(contour)
# 在原图上绘制矩形
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
# 在矩形位置写上计数
cv2.putText(image, str(count), (x, y), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 2)
count += 1
cv2.imshow("Count Result", image)
print("药片检测个数:", count)
cv2.waitKey(0)
cv2.destroyAllWindows()
当然,让我们逐行解释上述代码:
import cv2
import numpy as np
# 读取图片
image = cv2.imread("path/to/your/image.png")
cv2.imshow("Original Image", image)
# 形态学处理
kernel = np.ones((3, 3), np.uint8)
morphology_result = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel)
cv2.imshow("Morphology Result", morphology_result)
# 灰度转换
gray_image = cv2.cvtColor(morphology_result, cv2.COLOR_BGR2GRAY)
# 二值化
_, binary_image = cv2.threshold(gray_image, 100, 255, cv2.THRESH_OTSU)
cv2.imshow("Binary Image", binary_image)
# 寻找轮廓
contours, _ = cv2.findContours(binary_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# 统计药片数量并标记轮廓
count = 0
for i, contour in enumerate(contours):
area = cv2.contourArea(contour)
if area < 500:
continue
# 获取轮廓的位置
(x, y, w, h) = cv2.boundingRect(contour)
# 在原图上绘制矩形
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
# 在矩形位置写上计数
cv2.putText(image, str(count), (x, y), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 2)
count += 1
cv2.imshow("Count Result", image)
print("药片检测个数:", count)
cv2.waitKey(0)
cv2.destroyAllWindows()
sure_fg
(肯定是前景的区域),以便将其用作分水岭算法的标记点。这个过程涉及以下几步:距离变换: 通过距离变换,我们得到了一个灰度图,其中像素值表示每个像素到最近的零像素点的距离。这个距离图范围是浮点数,通常需要进行归一化。
dist_transform = cv2.distanceTransform(binary_image, cv2.DIST_L2, 3)
归一化: 将距离变换后的图像进行归一化,使其范围在0到1之间。
normalized_distance = cv2.normalize(dist_transform, 0, 1, cv2.NORM_MINMAX)
再次二值化: 对归一化后的图像进行二值化,以获取肯定是前景的区域。这是通过设置一个阈值,将距离较大的区域认定为前景。
_, sure_fg = cv2.threshold(normalized_distance, 0.4, 1, cv2.THRESH_BINARY)
这样,sure_fg
中的像素值为 1 的区域就被认为是明确的前景区域,而不是可能的边界区域。这种区域将被用作分水岭算法的种子点。