提取的圆的像素坐标(使用 scikit 图像)从图像上绘制的圆的实际位置(使用 openCV)偏移

Pixel coordinates of the circle extracted (using scikit image) shifts from the actual position of the circle drawn over an image(using openCV)

我正在使用 openCV 在图像上绘制一个圆(用鼠标),然后我使用圆的半径和圆心使用 random_walker() 方法对图像的一部分进行分割脱脂图像。我对使用鼠标单击提取的像素坐标有疑问。每当用鼠标在图像上绘制一个圆圈时,圆圈的像素坐标都会从绘制的圆圈的实际位置偏移一点。请看图片: Images

如你所见,第一张图是鼠标画的圆,第二张图是随机游走者提取的圆的像素坐标(用于分割椎骨)。由于圆的坐标发生了偏移,所以分割出来的图像部分就变得错误了。这是我的代码:

import math
import matplotlib.pyplot as plt
import numpy as np
from skimage import io
from skimage.color import rgb2gray
from skimage import data
import skimage.segmentation as seg
import skimage.draw as draw
import cv2

#To store center and cicumference points
circle_center = (0,0)
circumference = (0,0)

def image_show(image, nrows=1, ncols=1, cmap='gray'):
    fig, ax = plt.subplots(nrows=nrows, ncols=ncols, figsize=(8,8))
    ax.imshow(image, cmap='gray')
    ax.axis('off')
    return fig, ax

def circle_points(resolution, center, radius):
    
        #Generate points which define a circle on an image.Centre refers to the centre of the circle
        radians = np.linspace(0, 2*np.pi, resolution)
        c = center[1] + radius*np.cos(radians)#polar co-ordinates
        r = center[0] + radius*np.sin(radians)
        
        return np.array([c,r]).T

def drawCircle(event, x, y, flags, params):
    #referencing global variables
    global circle_center, circumference
    #action to be taken when left mouse button is pressed
    if(event ==  cv2.EVENT_LBUTTONDOWN):
        circle_center = (x,y)
        
        #draw the center
        cv2.circle(imageClone, circle_center, 2, (0, 0, 255), 1, cv2.LINE_AA)

    #action to be taken when left mouse button is released
    elif event == cv2.EVENT_LBUTTONUP:
        circumference = (x,y)

        #calculate radius of the circle
        radius1 = math.sqrt(math.pow(circle_center[0] - circumference[0], 2)+ math.pow(circle_center[1] - circumference[1], 2))
        print(radius1)
        #draw the circle
        cv2.circle(imageClone, circle_center, int(radius1), (0, 0, 255), 1, cv2.LINE_AA)
        cv2.imshow("image", imageClone)

        points = circle_points(200, [y,x], radius1)[:-1]
        image_labels = np.zeros((image_gray.shape), dtype=np.uint8)
        indices = draw.circle_perimeter(y,x,5)

        image_labels[indices] = 1
        image_labels[points[:,1].astype(np.int), points[:,0].astype(np.int)] = 2
        image_show(image_labels)

        image_segmented = seg.random_walker(image_gray, image_labels, beta = 3500)
        
        #Check our results
        fig, ax = image_show(image)
        ax.imshow(image_segmented == 1, alpha=0.3)
        plt.show()

image = cv2.imread('image.png')
image_gray = rgb2gray(image)

#Create a clone of input image to work on
imageClone = image_gray.copy()

#displaying the image 
cv2.imshow('image', image)

#setup callback for mouse event
cv2.setMouseCallback("image", drawCircle)

while True:
    cv2.imshow('image', imageClone)
    #wait for a widow to be closed
    k = cv2.waitKey(100)
    if k == 27:
        print('ESC')
        cv2.destroyAllWindows()
        break        
    if cv2.getWindowProperty('image',cv2.WND_PROP_VISIBLE) < 1:        
        break 

#close all the opened windows
cv2.destroyAllWindows()

有人能告诉我解决这个问题的正确方法吗?

这很微妙!您正在绘制以鼠标释放为中心的第二个圆圈(indices = draw.circle_perimeter(y,x,5),但第一个圆圈以鼠标按下为中心。