OpenCV FindContours用K-均值二进制群集找不到正确的边缘

发布于 2025-02-12 12:05:04 字数 2622 浏览 4 评论 0原文

这是的后续问题。

我使用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.

enter image description here

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.

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文