在matlab和python中得到不同的regionprops结果

2024-09-27 07:33:20 发布

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

我正在把一个matlab代码转换成python来进行模拟罗斯。等一下在这两种语言中处理regionprops时,matlab和python只显示了一些不同的结果数组。用于剩下的数组,两个都显示相同的结果。你知道吗

以下是我的matlab代码:

Z = ones(1000,1000);
c = [17.49;0;0;1000;1000;17.49];
c1 = [0;17.49;38;0;0];
r = [30.37;45.12;1000;1000;348.55;30.37];
r1 = [45.12;30.37;0;0;45.12];          
BW = roipoly(Z,c,r);
GI = mat2gray(BW);
s = regionprops(BW, GI, {'Centroid'});
BW1 = roipoly(Z,c1,r1);
GI1 = mat2gray(BW);
s1 = regionprops(BW1, GI1, {'Centroid'});
s.Centroid
s1.Centroid

下面是我的python代码:

#!/usr/bin/env python
from __future__ import division
import numpy as np
import cv2
from skimage.measure import label, regionprops

c = np.array([[17.49,0,0,1000,1000,17.49],
              [0,17.49,38,0,0]])
r = np.array([[30.37,45.12,1000,1000,348.55,30.37],
              [45.12,30.37,0,0,45.12]])

X = c[0]
Y = r[0]
X1 = c[1]
Y1 = r[1]
mask = np.zeros((1000,1000),dtype = 'uint8')
mask1 = np.zeros((1000,1000),dtype = 'uint8')
rc = np.array((X,Y)).T
rc1 = np.array((X1,Y1)).T
cv2.drawContours(mask,[rc.astype(int)],0,255,-1)
cv2.drawContours(mask1,[rc1.astype(int)],0,255,-1)
for i in range(1000):
  for j in range(1000):
    if mask[i,j] ==255:
       mask[i,j] = 1
for i in range(1000):
  for j in range(1000):
    if mask1[i,j] ==255:
       mask1[i,j] = 1
out = np.zeros(mask.shape)
GI = cv2.normalize(mask, out, 1.0, 0.0, cv2.NORM_MINMAX)
GI1 = cv2.normalize(mask1, out, 1.0, 0.0, cv2.NORM_MINMAX)
GI = label(GI)
GI1 = label(GI1)
regions = regionprops(GI)
regions1 = regionprops(GI1)
for props in regions:
    y0, x0 = props.centroid
for props in regions1:
    y1, x1 = props.centroid
print(x0)
print(y0)
print(x1)
print(y1)

来自matlab的结果:

ans =
  467.4359  588.3356
ans =
   13.5307   16.3722

python的结果是:

466.44296973332285
587.3801993195406
499.9950519678513
499.99219205932434

如您所见,第一个数组regionprops产生相同的结果,而第二个数组产生不同的结果matlab。可以你帮我看看这个?你知道吗

提前感谢:)


Tags: inimportfornpmask数组arraycv2

热门问题