def animpingpong(self):
print self
print self.Object
print self.Object.Name
obj=self.Object
img = cv2.imread(obj.imageFile)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
gray = np.float32(gray)
dst = cv2.cornerHarris(gray,3,3,0.00001)
dst = cv2.dilate(dst,None)
img[dst>0.01*dst.max()]=[0,0,255]
from matplotlib import pyplot as plt
plt.subplot(121),plt.imshow(img,cmap = 'gray')
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(dst,cmap = 'gray')
plt.title('Corner Image'), plt.xticks([]), plt.yticks([])
plt.show()
python类cornerHarris()的实例源码
def animpingpong(self):
obj=self.Object
img=None
if not obj.imageFromNode:
img = cv2.imread(obj.imageFile)
else:
img = obj.imageNode.ViewObject.Proxy.img.copy()
print (obj.blockSize,obj.ksize,obj.k)
try:
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
gray = np.float32(gray)
print "normale"
except:
im2=cv2.cvtColor(img,cv2.COLOR_GRAY2RGB)
gray = cv2.cvtColor(im2,cv2.COLOR_RGB2GRAY)
print "except"
dst = cv2.cornerHarris(gray,obj.blockSize,obj.ksize*2+1,obj.k/10000)
dst = cv2.dilate(dst,None)
img[dst>0.01*dst.max()]=[0,0,255]
dst2=img.copy()
dst2[dst<0.01*dst.max()]=[255,255,255]
dst2[dst>0.01*dst.max()]=[0,0,255]
if not obj.matplotlib:
cv2.imshow(obj.Label,img)
else:
from matplotlib import pyplot as plt
plt.subplot(121),plt.imshow(img,cmap = 'gray')
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(dst2,cmap = 'gray')
plt.title('Corner Image'), plt.xticks([]), plt.yticks([])
plt.show()
self.img=img
def createCV_cornerharris():
print "create CV cornerharris ... 2"
obj= createCV(True)
obj.Label='Harris'
obj.addProperty('App::PropertyInteger','blockSize',"cornerHarris").blockSize=2
obj.addProperty('App::PropertyInteger','ksize',"cornerHarris").ksize=3
obj.addProperty('App::PropertyFloat','k',"cornerHarris").k=1.0
_CV_cornerharris(obj,__dir__+ '/icons/icon2.svg')
miki2=miki.Miki2(MyApp,s6,obj)
return obj
def animpingpong(self):
obj=self.Object
img=None
if not obj.imageFromNode:
img = cv2.imread(obj.imageFile)
else:
print "copy image ..."
img = obj.imageNode.ViewObject.Proxy.img.copy()
print "cpied"
print " loaded"
print (obj.blockSize,obj.ksize,obj.k)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
gray = np.float32(gray)
# dst = cv2.cornerHarris(gray,3,3,0.00001)
dst = cv2.cornerHarris(gray,obj.blockSize,obj.ksize*2+1,obj.k/10000)
dst = cv2.dilate(dst,None)
img[dst>0.01*dst.max()]=[0,0,255]
if True:
print "zeige"
cv2.imshow(obj.Label,img)
print "gezeigt"
else:
from matplotlib import pyplot as plt
plt.subplot(121),plt.imshow(img,cmap = 'gray')
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(dst,cmap = 'gray')
plt.title('Corner Image'), plt.xticks([]), plt.yticks([])
plt.show()
print "fertig"
self.img=img
def createCV_demo():
print "create CV demo ..."
obj=FreeCAD.ActiveDocument.addObject('App::DocumentObjectGroupPython','Image')
obj.addProperty('App::PropertyFile','imageFile',"base").imageFile='/home/thomas/Bilder/c1.png'
obj.addProperty('App::PropertyLink','imageNode',"base")
obj.addProperty('App::PropertyBool','imageFromNode',"base").imageFromNode=False
obj.addProperty('App::PropertyInteger','blockSize',"cornerHarris").blockSize=2
obj.addProperty('App::PropertyInteger','ksize',"cornerHarris").ksize=3
obj.addProperty('App::PropertyFloat','k',"cornerHarris").k=1.0
_CV_demo(obj,'/icons/bounder.png')
_ViewProviderCV_demo(obj.ViewObject,__dir__+ '/icons/icon1.svg')
app=MyApp()
miki2=miki.Miki()
miki2.app=app
app.root=miki2
app.obj=obj
obj.ViewObject.Proxy.cmenu.append(["Dialog",lambda:miki2.run(MyApp.s6)])
obj.ViewObject.Proxy.edit= lambda:miki2.run(MyApp.s6)
return obj
#
# derived classes
#
def harris(gray):
dst = cv2.cornerHarris(gray, 2, 3, 0.04)
dst = cv2.dilate(dst,None)
return dst
def Harris_Corner(self):
self.threshold = 0.999999999999
temp_i = self.image_i.copy()
temp1_i = self.image_i.copy()
gray_i = cv2.cvtColor(temp_i, cv2.COLOR_BGR2GRAY)
gray_i = numpy.float32(gray_i)
dst_i = cv2.cornerHarris(gray_i,2,3,0.025)
dst_i = cv2.dilate(dst_i,None)
# Threshold for an optimal value, it may vary depending on the image.
temp_i[dst_i<0.01*dst_i.max()]=[0,0,0]
temp_i[dst_i>=0.01*dst_i.max()]=[255,255,255]
temp1_i[dst_i>0.01*dst_i.max()]=[0,0,255]
hist_i = cv2.calcHist([temp_i], [0], None, [256], [0, 256])
temp_j = self.image_j.copy()
temp1_j = self.image_j.copy()
gray_j = cv2.cvtColor(temp_j, cv2.COLOR_BGR2GRAY)
gray_j = numpy.float32(gray_j)
dst_j = cv2.cornerHarris(gray_j,2,3,0.025)
dst_j = cv2.dilate(dst_j,None)
# Threshold for an optimal value, it may vary depending on the image.
temp_j[dst_j<0.01*dst_j.max()]=[0,0,0]
temp_j[dst_j>=0.01*dst_j.max()]=[255,255,255]
temp1_j[dst_j>0.01*dst_j.max()]=[0,0,255]
hist_j = cv2.calcHist([temp_j], [0], None, [256], [0, 256])
self.measure = cv2.compareHist(hist_i, hist_j, cv.CV_COMP_CORREL)
self.assertGreater(self.measure, self.threshold)
print self.measure
cv2.imshow('Input X',temp1_i)
cv2.waitKey(0)
cv2.imshow('Input Y',temp1_j)
cv2.waitKey(0)
def Harris_Corner(self):
self.threshold = 0.999999999999
temp_i = self.image_i.copy()
temp1_i = self.image_i.copy()
gray_i = cv2.cvtColor(temp_i, cv2.COLOR_BGR2GRAY)
gray_i = numpy.float32(gray_i)
dst_i = cv2.cornerHarris(gray_i,2,3,0.025)
dst_i = cv2.dilate(dst_i,None)
# Threshold for an optimal value, it may vary depending on the image.
temp_i[dst_i<0.01*dst_i.max()]=[0,0,0]
temp1_i[dst_i>0.01*dst_i.max()]=[0,0,255]
hist_i = cv2.calcHist([temp_i], [0], None, [256], [0, 256])
temp_j = self.image_j.copy()
temp1_j = self.image_j.copy()
gray_j = cv2.cvtColor(temp_j, cv2.COLOR_BGR2GRAY)
gray_j = numpy.float32(gray_j)
dst_j = cv2.cornerHarris(gray_j,2,3,0.025)
dst_j = cv2.dilate(dst_j,None)
# Threshold for an optimal value, it may vary depending on the image.
temp_j[dst_j<0.01*dst_j.max()]=[0,0,0]
temp1_j[dst_j>0.01*dst_j.max()]=[0,0,255]
hist_j = cv2.calcHist([temp_j], [0], None, [256], [0, 256])
self.measure = cv2.compareHist(hist_i, hist_j, cv.CV_COMP_CORREL)
self.assertGreater(self.measure, self.threshold)
def harris_corners(image):
i = 2
j = 11
k = 4
copy = np.copy(image)
corners = cv2.cornerHarris(grayscale(copy), i, j, k/20)
# dst = cv2.dilate(dst, None)
# copy[dst > 0.01 * dst.max()] = [0, 0, 255]
# cv2.imwrite('{}-{}-{}.png'.format(i, j, k), copy)
return [cv2.KeyPoint(corner[0], corner[1], 10) for corner in corners]
def detect(self, image):
harrisResponse = cv2.cornerHarris(image,
self._block_size,
self._aperture_size,
self._alpha)
points = np.argwhere(harrisResponse > harrisResponse.max() * self._quality_level)
return [cv2.KeyPoint(point[0], point[1], self._feature_size) for point in points]
def retrieve_subsections(img):
"""Yield coordinates of boxes that contain interesting images
Yields x, y coordinates; width and height as a tuple
An example use:
images = []
for x, y, w, h in retrieve_subsections(img):
image.append(img[y:y+h,x:x+w])
"""
if len(img.shape) == 3:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
else:
gray = img
results = cv2.cornerHarris(gray, 9, 3, 0.04)
# Normalise harris points between 0 and 1
hmin = results.min()
hmax = results.max()
results = (results - hmin)/(hmax-hmin)
# Blur so we retrieve the surrounding details
results = cv2.GaussianBlur(results, (31, 31), 5)
# Create a threshold collecting the most interesting areas
threshold = np.zeros(results.shape, dtype=np.uint8)
threshold[results>results.mean() * 1.01] = 255
# Find the bounding box of each threshold, and yield the image
contour_response = cv2.findContours(threshold, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
# Different versions of cv2 return a different number of attributes
if len(contour_response) == 3:
contours = contour_response[1]
else:
contours = contour_response[0]
for contour in contours:
# x, y, w, h
yield cv2.boundingRect(contour)