我将Matlab代码转换为python以在ros中进行仿真。虽然在处理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
如您所见,第一个数组的区域属性产生相同的结果,而第二个数组的区域属性与matlab产生不同的结果。您能帮我看看吗?
预先感谢:)