基于鼠标点击的图像分割 [英] Image segmentation based on mouse click

查看:146
本文介绍了基于鼠标点击的图像分割的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用python框架,我们可以创建图像片段,如附件中所示。现在,基于图像片段中的鼠标单击,我们需要突出显示具有特定颜色的片段。

Using python framework we are able to create image segments as shown in attachment. Now, based on the mouse click in the image segment we need to highlight the segment with specific color.

基于鼠标单击,我可以获得x / y坐标具体位置。请建议我如何查看坐标属于哪个图像片段?

Based on the mouse click I am able to get x/y coordinates of the specific location. Please suggest me how can I check on which image segment the coordinates belongs to?

以下是代码片段:

from skimage.segmentation import felzenszwalb, slic,quickshift
from skimage.segmentation import mark_boundaries
from skimage.segmentation import find_boundaries
from skimage.util import img_as_float
from skimage import io

import matplotlib.pyplot as plt

from skimage import measure
from skimage import restoration
from skimage import img_as_float

import numpy as np

coords = []

def find_nearest(array,value):

    idx = (np.abs(array-value)).argmin()

    return array[idx]

def onclick(event):

    global ix, iy

    ix, iy = event.xdata, event.ydata

    print ('ix ',ix)

    print ("iy ",iy)

    color = np.float64([1,0,1]) # red color

    image[segments == 14] = color

    mark_boundaries(image, segments)

    ax.imshow(mark_boundaries(image, segments))

    coords.append((ix, iy))

    return

image=img_as_float(io.imread("amazon.jpg"))

segments = quickshift(image, ratio=1.0, kernel_size=20, max_dist=10,
           return_tree=False, sigma=0, convert2lab=True, random_seed=42)

fig = plt.figure("Superpixels -- %d segments" % (500))

ax = fig.add_subplot(1, 1, 1)

fig,ax = plt.subplots()

color = np.float64([1,0,0]) 

image[segments == 14] = color  # desired segment to be colored

fig.canvas.mpl_connect('button_press_event', onclick)

ax.imshow(mark_boundaries(image, segments))

plt.axis("off")

plt.show()

推荐答案

根据< a href =http://scikit-image.org/docs/dev/api/skimage.segmentation.html#skimage.segmentation.quickshift =nofollow noreferrer>文档, quickshift 返回一个指示段标签的整数掩码。如果您知道用户点击了哪个像素,您可以检查变量 segments 中该像素的值,以获取细分数。

According to the documentation, quickshift returns an integer mask indicating segment labels. If you know what pixel the user clicked on, you can check the value of that pixel in your variable segments to get the segment number.

在单击处理程序中,您可以使用
clicked_segment = segments [event.xdata,event.ydata]

In you click handler, you can use clicked_segment = segments[event.xdata, event.ydata]

这篇关于基于鼠标点击的图像分割的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆