在anaconda和python下提取图像的freeman码
先利用opencv中的cv2.findContours 函数描述图片的轮廓,然后再返回值中得到一个有关于图像轮廓的数组,因为我们要的是freeman码,根据网上对于freeman.码 的定义,对于得到的数组进行相对于的操作即可得到对应的Freeman
以下是在anaconda下的python的代码
'''
提取一个图像的Freeman码
导入opencv中的cv2.findContours函数提取图像的轮http://blog.csdn.net/dcrmg/article/details/51987348
在该函数返回之中的 contours 包含了轮廓的属性,保存在一个数组中
通过对数组的计算,及可得到Freeman码
'''
import cv2
import numpy as np
import pandas as pd
img = cv2.imread('4.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, binary =cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
binary,contours, hierarchy =cv2.findContours(binary,cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
#cv2.drawContours(img,contours,-1,(0,0,255),3)
#cv2.imshow("img", img)
#cv2.waitKey(0)
#cv2.RETR_TREE
#print (type(contours))
#print (type(contours[0]))
#print (len(contours[0]))
#print (contours[0]-contours[0])
columns = []
for i in range(81):
columns.append(contours[0][i]-contours[0][i - 1])
#print (len(columns))
#print (columns[1][0][0])
a = []
for i in range(81):
if columns[i][0][0] == 0 and columns[i][0][1] == -1:
a.append(6)
elif columns[i][0][0] == 0 and columns[i][0][1] == 1:
a.append(2)
elif columns[i][0][0] == 1 and columns[i][0][1] == 1:
a.append(1)
elif columns[i][0][0] == 1 and columns[i][0][1] == 0:
a.append(0)
elif columns[i][0][0] == 1 and columns[i][0][1] == -1:
a.append(7)
elif columns[i][0][0] == -1 and columns[i][0][1] == 1:
a.append(3)
elif columns[i][0][0] == -1 and columns[i][0][1] == 0:
a.append(4)
elif columns[i][0][0] == -1 and columns[i][0][1] == -1:
a.append(5)
print(a)