该程序不打印结果

2024-06-02 10:15:13 发布

您现在位置:Python中文网/ 问答频道 /正文

现在,我正在用下面的程序,但我不能得到图像的时刻,质心等,我需要。你知道吗

代码:

import cv2
import numpy

from matplotlib.pyplot import imread
from numpy import mgrid, sum

image = imread('imagemoment.png')

def moments2e(image):
    assert len(image.shape) == 2  # only for grayscale images
    x, y = mgrid[:image.shape[0], :image.shape[1]]
    moments = {}
    moments['mean_x'] = sum(x * image) / sum(image)
    moments['mean_y'] = sum(y * image) / sum(image)

    # raw or spatial moments
    moments['m00'] = sum(image)
    moments['m01'] = sum(x * image)
    moments['m10'] = sum(y * image)
    moments['m11'] = sum(y * x * image)
    moments['m02'] = sum(x ** 2 * image)
    moments['m20'] = sum(y ** 2 * image)
    moments['m12'] = sum(x * y ** 2 * image)
    moments['m21'] = sum(x ** 2 * y * image)
    moments['m03'] = sum(x ** 3 * image)
    moments['m30'] = sum(y ** 3 * image)

    # central moments
    moments['mu01']= sum((y-moments['mean_y'])*image) # should be 0
    moments['mu10']= sum((x-moments['mean_x'])*image) # should be 0
    moments['mu11'] = sum((x - moments['mean_x']) * (y - moments['mean_y']) * image)
    moments['mu02'] = sum((y - moments['mean_y']) ** 2 * image)  # variance
    moments['mu20'] = sum((x - moments['mean_x']) ** 2 * image)  # variance
    moments['mu12'] = sum((x - moments['mean_x']) * (y - moments['mean_y']) ** 2 * image)
    moments['mu21'] = sum((x - moments['mean_x']) ** 2 * (y - moments['mean_y']) * image)
    moments['mu03'] = sum((y - moments['mean_y']) ** 3 * image)
    moments['mu30'] = sum((x - moments['mean_x']) ** 3 * image)

    # opencv versions
    # moments['mu02'] = sum(image*(x-m01/m00)**2)
    # moments['mu02'] = sum(image*(x-y)**2)

    # wiki variations
    # moments['mu02'] = m20 - mean_y*m10
    # moments['mu20'] = m02 - mean_x*m01

    # central standardized or normalized or scale invariant moments
    moments['nu11'] = moments['mu11'] / sum(image) ** (2 / 2 + 1)
    moments['nu12'] = moments['mu12'] / sum(image) ** (3 / 2 + 1)
    moments['nu21'] = moments['mu21'] / sum(image) ** (3 / 2 + 1)
    moments['nu20'] = moments['mu20'] / sum(image) ** (2 / 2 + 1)
    moments['nu03'] = moments['mu03'] / sum(image) ** (3 / 2 + 1)  # skewness
    moments['nu30'] = moments['mu30'] / sum(image) ** (3 / 2 + 1)  # skewness

    return moments

你能帮我解决这个问题吗? 非常感谢你。你知道吗


Tags: orfromimageimportnumpymeansumshape
1条回答
网友
1楼 · 发布于 2024-06-02 10:15:13

我第一眼看到的一个问题是,您已经定义了函数moments2e(image),但还没有调用它。您需要在函数定义之外调用moments2e(image)函数。你知道吗

import cv2
import numpy
from matplotlib.pyplot import imread
from numpy import mgrid, sum

image = imread('imagemoment.png')

def moments2e(image):
    assert len(image.shape) == 2  # only for grayscale images
    x, y = mgrid[:image.shape[0], :image.shape[1]]
    .
    .
    .
    return moments
moments = moments2e(image)
print(moments)

相关问题 更多 >