OpenCV FindContours用K-均值二进制群集找不到正确的边缘
这是的后续问题。
我使用cv2.kmeans
将左Auricle DICOM图像分为掩码。
以下代码是我在OpenCV中处理K-Means二进制聚类的方式。
import cv2
import numpy as np
# read input and convert to range 0-1
image = cv2.imread('1.jpg')
h, w, c = image.shape
# reshape to 1D array
image_2d = image.reshape(h*w, c).astype(np.float32)
# set number of colors
numcolors = 2
numiters = 10
epsilon = 1
attempts = 10
# do kmeans processing
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, numiters, epsilon)
ret, labels, centers = cv2.kmeans(image_2d, numcolors, None, criteria, attempts, cv2.KMEANS_RANDOM_CENTERS)
# reconstitute 2D image of results
centers = np.uint8(centers)
newimage = centers[labels.flatten()]
newimage = newimage.reshape(image.shape)
#cv2.imwrite("1_test.jpg", newimage)
#cv2.imshow('new image', newimage)
#cv2.waitKey(0)
k = 0
for center in centers:
# select color and create mask
#print(center)
layer = newimage.copy()
mask = cv2.inRange(layer, center, center)
# apply mask to layer
layer[mask == 0] = [0,0,0]
#cv2.imshow('layer', layer)
#cv2.waitKey(0)
# save kmeans clustered image and layer
if(k == 0):
cv2.imwrite("1_test{0}.jpg".format(k), layer)
k = k + 1
但是,在尝试cv2.findcontours
之后,我无法绘制左上光环的正确边缘。
我的边缘检测代码来了。
import cv2
import numpy as np
import os
img = cv2.imread('1_test0.jpg')
#gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
implt = (img * 255).astype(np.uint8)
implt = np.asarray(implt)
implt = implt[:, :, -1]
im2 = cv2.resize(implt, (350, 350), interpolation=cv2.INTER_CUBIC)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(11, 11))
im2 = cv2.erode(im2,kernel)
ret,thresh = cv2.threshold(im2,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
#ret,binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
#_,contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours,hierarchy = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)[-2:]
draw_img3 = cv2.drawContours(img.copy(), contours, -1, (0, 0, 255), 3)
cv2.imwrite('1_test0_edge.jpg', draw_img3)
This is the follow-up question from here.
I use cv2.kmeans
to segment the left auricle DICOM image as mask.
The following code is how I deal with the k-means binary clustering in OpenCV.
import cv2
import numpy as np
# read input and convert to range 0-1
image = cv2.imread('1.jpg')
h, w, c = image.shape
# reshape to 1D array
image_2d = image.reshape(h*w, c).astype(np.float32)
# set number of colors
numcolors = 2
numiters = 10
epsilon = 1
attempts = 10
# do kmeans processing
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, numiters, epsilon)
ret, labels, centers = cv2.kmeans(image_2d, numcolors, None, criteria, attempts, cv2.KMEANS_RANDOM_CENTERS)
# reconstitute 2D image of results
centers = np.uint8(centers)
newimage = centers[labels.flatten()]
newimage = newimage.reshape(image.shape)
#cv2.imwrite("1_test.jpg", newimage)
#cv2.imshow('new image', newimage)
#cv2.waitKey(0)
k = 0
for center in centers:
# select color and create mask
#print(center)
layer = newimage.copy()
mask = cv2.inRange(layer, center, center)
# apply mask to layer
layer[mask == 0] = [0,0,0]
#cv2.imshow('layer', layer)
#cv2.waitKey(0)
# save kmeans clustered image and layer
if(k == 0):
cv2.imwrite("1_test{0}.jpg".format(k), layer)
k = k + 1
But after I try to cv2.findContours
, I cannot draw the correct edge of the left auricle.
Here comes my edge detection code.
import cv2
import numpy as np
import os
img = cv2.imread('1_test0.jpg')
#gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
implt = (img * 255).astype(np.uint8)
implt = np.asarray(implt)
implt = implt[:, :, -1]
im2 = cv2.resize(implt, (350, 350), interpolation=cv2.INTER_CUBIC)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(11, 11))
im2 = cv2.erode(im2,kernel)
ret,thresh = cv2.threshold(im2,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
#ret,binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
#_,contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours,hierarchy = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)[-2:]
draw_img3 = cv2.drawContours(img.copy(), contours, -1, (0, 0, 255), 3)
cv2.imwrite('1_test0_edge.jpg', draw_img3)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论