我一直在努力为此MATLAB code取得类似的结果 这给了我正在寻找的结果,但是,我正在尝试使用OpenCV 3 + Python实现这一目标。
这是OpenCV 3 + Python中的类似实现:
import cv2
import numpy as np
class Segment:
def __init__(self,segments=5):
#define number of segments, with default 5
self.segments=segments
def kmeans(self,image):
image=cv2.GaussianBlur(image,(7,7),0)
vectorized=image.reshape(-1,3)
vectorized=np.float32(vectorized)
criteria=(cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
ret,label,center=cv2.kmeans(vectorized,self.segments,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS)
res = center[label.flatten()]
segmented_image = res.reshape((image.shape))
return label.reshape((image.shape[0],image.shape[1])),segmented_image.astype(np.uint8)
def extractComponent(self,image,label_image,label):
component=np.zeros(image.shape,np.uint8)
component[label_image==label]=image[label_image==label]
return component
if __name__=="__main__":
import argparse
import sys
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True, help = "Path to the image")
ap.add_argument("-n", "--segments", required = False, type = int,
help = "# of clusters")
args = vars(ap.parse_args())
image=cv2.imread(args["image"])
if len(sys.argv)==3:
seg = Segment()
label,result= seg.kmeans(image)
else:
seg=Segment(args["segments"])
label,result=seg.kmeans(image)
cv2.imshow("segmented",result)
result=seg.extractComponent(image,label,2)
cv2.imshow("extracted",result)
cv2.waitKey(0)
我正在寻找的是能够自己提取A和它周围的矩形和背景本身,所以我可以单独操纵它们。
在这里你可以看到原始图像+当前输出+所需的输出:
知道如何实现这一目标吗?
答案 0 :(得分:0)
我现在无法做到这一点,但您的实施与Matlab之间存在一些差异,您应该检讨:
$ jq --version
jq version 1.3
,就像Matlab的例子一样,对吧?您的默认值为5 ... segments=3
,而Matlab只有3