Opencv在此图像中显示黑色图像,但另一个尺寸较小的图像可以正常工作

2024-05-19 00:44:31 发布

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

我更简洁地编辑和写我的问题。我有一个问题的算法(在MATLAB中):

Require: Multivariable image (im) of size $n_1n_2 \times d$, the preorder (h_function) is a vector with $n_1n_2$ components and structuring element (se).

[.,b] = sort(h_function);

im_latt(b) = 1:(n1n2);

im_latt = reshape(im_latt, n1, n2);

im_ero = Erode(im_latt,SE);

im_out = reshape(im(b(im_ero(:)),:), n1, n2, d);

我正在尝试用Python编写这个程序。 我的图形名为“fig01”,它有一个主体(512,768,3),即n1=512,n2=768和d=3。 经过很多努力和帮助,我成功地创建了这个项目

import CV2 as CV
import numpy as np
matplotlib import pyplot as plt


im = cv.imread ("fig01.png", cv.IMREAD_COLOR)
im = cv.cvtColor (im, cv.COLOR_BGR2RGB) / 255

n1, n2, d = im.shape
vet = im.reshape(n1*n2, d)
def h_function(vet):
    return np.mean(vet,axis=1)
vet_h = h_function(vet)
im_h = vet_h.reshape(n1,n2)
b = np.argsort(vet_h).reshape(vet_h.shape)
im_latt = np.zeros(vet_h.shape, np.uint8)
im_latt[b] = range(0,n1*n2)
im_latt = im_latt.reshape((n1,n2))
kernel = np.ones((3,3),np.uint8)
im_ero = cv.erode(im_latt, kernel, iterations = 1)
ind = b[im_ero.reshape(vet_h.shape)]
im_out=vet[ind,:].reshape((n1,n2,d))

我的问题是这个数字完全是黑色的

PS:我使用Python的经验只有一周,我已经在这个网站opencv.org上学习过了。我安装了Anaconda Navigator,并且正在使用Jupyter和OpenPython 3

PS2.:我在这个网站opencv show black image after numpy vectorize上搜索了我的问题,我设法接近这个问题和这个解决方案。但我不明白

PS3。(编辑)我试图让我的代码和问题更加清晰


Tags: imageimport编辑asnpfunctioncvshape

热门问题