当前位置: 首页 > 知识库问答 >
问题:

在python中实现区域增长,不需要种子

桓喜
2023-03-14

我正在尝试在python中实现区域增长分割算法,但不允许使用种子点。到目前为止我的想法是这样的:

from PIL import Image
from scipy.spatial import distance
import statistics
import numpy as np
import sys
sys.setrecursionlimit(10**9)
# SCRIPT: color palette reduction applier script
# SCRIPT: this is the second method. region growing segmentation

# list of red values of pixels
rList = []
# list of green values of pixels
gList = []
# list of blue values of pixels
bList = []
# this matrix will be initially 0, then every region growth
# will be updated to its number
overlayMatrix = []
# starting area number
currentArea = 1

def isValidPixel(x, y, width, height):
    if x < 0 or x >= width:
        return False
    if y < 0 or y >= height:
        return False

    return True

def get_average_color():

    global rList
    global gList
    global bList

    # set values to none
    r = None
    g = None
    b = None

    # get average value for each chanel
    if rList != []:
        r = sum(rList)/len(rList)
        g = sum(gList)/len(gList)
        b = sum(bList)/len(bList)

        # make values integers to be used as pixel
        r = int(r)
        g = int(g)
        b = int(b)

    # return values
    return (r, g, b)

def add_pixel_to_lists(pixel):
    global rList
    global gList
    global bList

    rList.append(pixel[0])
    gList.append(pixel[1])
    bList.append(pixel[2])

def region_growing(x, y, pixel, img, currentArea):
    global overlayMatrix
    global rList
    global gList
    global bList
    # get width, heihgt
    width, height = img.size
    # set this pixel to be visited on current area
    overlayMatrix[x][y] = currentArea
    # set a list for all possible neighbours
    neighbouringPixels = [(x-1, y), (x-1, y-1), (x-1, y+1), (x, y-1), (x, y+1), (x+1, y), (x+1, y-1), (x+1, y+1)]
    # filter to get only valid neighbours
    validPixels = [x for x in neighbouringPixels if isValidPixel(x[0], x[1], width, height) == True]

    # filter pixels to be not visited
    notVisitedPixels = [x for x in validPixels if overlayMatrix[x[0]][x[1]] == 0]

    # set a threshold value
    threshold = 5

    # filter to get only pixels in threshold
    thresholdPixels = []
    thresholdPixels = [x for x in notVisitedPixels if distance.euclidean(img.getpixel(x), pixel) < threshold]

    # set the list for pixels to make recursive calls
    toVisitRecursive = []

    # for every pixel that is a valid neighbour, add it to the toVisit list in recursive call
    # and add its rgb values to the lists so an average can be computed
    for pixel in thresholdPixels:
        toVisitRecursive.append(pixel)
        add_pixel_to_lists(img.getpixel(pixel))

    # compute average
    averagePixel = get_average_color()

    # if I still have neighoburs that haven't been visited
    # and are within the threshold, get first from list
    # remove it so we don't do the same again, then apply
    # the algorithm for it
    if toVisitRecursive != []:
        pixel = toVisitRecursive[0]
        toVisitRecursive.remove(pixel)
        region_growing(pixel[0], pixel[1], averagePixel, img, currentArea)

    # finally, return
    return (averagePixel, currentArea+1)


def write_image():
    pass
    # this will write to the image based on the list of tuples
    # average color to the area with number X

def palette_reduction_mt2(input_image):
    # open original image
    img = Image.open(input_image)
    tuple_list = []
    # get width, height
    width, height = img.size

    # make overlay matrix of 0, initial
    global overlayMatrix
    overlayMatrix = np.zeros((width, height), dtype=np.int32)
    # create new image
    newimg = Image.new("RGB", (width, height), "white")
    currentArea = 1
    # iterate through image pixels
    for y in range(0, height-1):
        for x in range(0, width-1):
            global rList
            global gList
            global bList

            # get pixel from edge image
            p = img.getpixel((x, y))
            # apply region growing

            average, currentArea = region_growing(x, y, p, img, currentArea)
            tuple_list.append((average, currentArea-1))

            # reset color lists to compute new average
            rList = []
            gList = []
            bList = []
    print(tuple_list)
    # Save image
    newimg.save("images/reduced_mt1.jpg")
    # return the name of the image
    return "images/reduced_mt1.jpg"

谁能给我指出正确的方向吗?

谢谢你!

共有1个答案

明松
2023-03-14

看来您实际上并没有迭代ToVisitRecursive列表。也许您应该添加一个循环,如:

 for pixel in toVisitRecursive:
   // region_growing...

如果toVisitRecursive!=[]:,则更改该行

 类似资料:
  • 本文向大家介绍Python简单实现区域生长方式,包括了Python简单实现区域生长方式的使用技巧和注意事项,需要的朋友参考一下 区域生长是一种串行区域分割的图像分割方法。区域生长是指从某个像素出发,按照一定的准则,逐步加入邻近像素,当满足一定的条件时,区域生长终止。区域生长的好坏决定于1.初始点(种子点)的选取。2.生长准则。3.终止条件。区域生长是从某个或者某些像素点出发,最后得到整个区域,进而

  • 本文向大家介绍关于初始种子自动选取的区域生长实例(python+opencv),包括了关于初始种子自动选取的区域生长实例(python+opencv)的使用技巧和注意事项,需要的朋友参考一下 算法中,初始种子可自动选择(通过不同的划分可以得到不同的种子,可按照自己需要改进算法),图分别为原图(自己画了两笔为了分割成不同区域)、灰度图直方图、初始种子图、区域生长结果图。 另外,不管时初始种子选择还是

  • 问题内容: 有没有办法在Python中建立一个自动增长的列表?我的意思是制作一个列表,该列表将在引用不存在的索引时增长。基本上是Ruby数组的行为。 提前致谢! 问题答案: 当然有可能,您只需要使用list的子类即可。 用法:

  • 问题内容: 我的春季启动应用程序中有一个存储库类。首先,我用添加了注释,然后实现了。现在我摆脱了注释,它仍然有效。 我看到有注释。 这是如何运作的?还是这不行,我的应用程序中发生了一些奇怪的事情? 问题答案: 确实没有必要将注释放在扩展的接口上;Spring通过扩展预定义接口之一来识别存储库。 注释的目的是防止Spring本身将该特定接口视为存储库。该接口具有此批注,因为它本身不是存储库,它是由您

  • 如果可能的话,我如何用更少的代码将下面所示代码的结尾部分移到另一行或修改文本,以实现所需的结果。我键入了以下代码: 我试图实现的是显示以下数据:-仅显示,仅显示Dakota Spitfire和Hurricane或Dakota和Spitfire或Dakota和两个Spitfire,如果它们显示在数据表明细表中,则完整代码如下。需要编辑的是从Southport=开始的行: 当我运行代码时,我得到以下回

  • 我是Pact.io的新手,正在尝试在我们的平台上设置合同测试。应用程序是这样设置的,每个客户帐户都有自己的数据库模式,直接绑定到一个URL子域。当发出API请求时,除了授权标头之外,还必须提供该URL子域。我可以创建一个静态令牌来与使用者测试一起传递,但是当Pact发送请求时,它不知道要使用哪个帐户。我看不出有什么方法可以通过一个URL子域作为消费者测试的一部分,并且不确定如何强制它使用提供者端的