opencv ---冈萨雷斯《图像处理》课后作业
显微应用中一个预处理步骤是从两组或更多组重叠的类似颗粒中分离出单个独立的一种圆颗粒。假设所有颗粒的大小都相同,提出一种产生3幅图像的形态学算法,这3幅图像分别仅由如下物体组成:
(a)仅与图像边界融合一块的颗粒
(b)仅彼此重叠的颗粒
(c)没有重叠的颗粒
# -*- coding=GBK -*-
import cv2 as cv
src = cv.imread("point.jpg")
cv.imshow("before", src)
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
image1 = binary.copy()
image1[::] = 0
image2 = image1.copy()
image3 = image1.copy()
kernel = cv.getStructuringElement(cv.MORPH_RECT, (3, 3))
mid_image = cv.morphologyEx(binary, cv.MORPH_ERODE, kernel)
binary = cv.Canny(mid_image, 50, 150, 3)
cv.imshow("edge", binary)
contours, hierarchy = cv.findContours(binary, cv.RETR_CCOMP, cv.CHAIN_APPROX_NONE)
for i in range(len(contours)):
area = cv.contourArea(contours[i])
if area > 350:
cv.drawContours(image1, contours, i, 255, cv.FILLED, 8, hierarchy)
elif area < 350.0 and area > 150.0:
cv.drawContours(image2, contours, i, 255, cv.FILLED, 8, hierarchy)
image3 = cv.absdiff(mid_image, image1)
image3 = cv.absdiff(image3, image2)
image1 = cv.morphologyEx(image1, cv.MORPH_DILATE, kernel)
image2 = cv.morphologyEx(image2, cv.MORPH_DILATE, kernel)
image3 = cv.morphologyEx(image3, cv.MORPH_OPEN, kernel, anchor=(1, 1), iterations=2)
cv.imshow("overlap_point", image1)
cv.imshow("single_point", image2)
cv.imshow("boundary_point", image3)
cv.waitKey(0)
cv.destroyAllWindows()