-
-
Save sencagri/f08f8708f2c5abdfc94eeeb10762807e to your computer and use it in GitHub Desktop.
computer vision 4 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import cv2 | |
import numpy as np | |
from sklearn.naive_bayes import GaussianNB | |
from sklearn.neighbors import KNeighborsClassifier | |
from sklearn.neural_network import MLPClassifier | |
shapeData = list() | |
label = [4,3,2,2,3,3,4,1,2,1,1,1,1,4,4,1,3,1,2,1,1,3,3,4] | |
def calculateEccentricity(M): | |
xhat = M['m10'] / M['m00'] | |
yhat = M['m01'] / M['m00'] | |
u20 = M['m20'] / M['m00'] - xhat ** 2 | |
u02 = M['m02'] / M['m00'] - yhat ** 2 | |
u11 = M['m11'] / M['m00'] - xhat * yhat | |
e1 = (u20 + u02) / 2 + (4 * u11 ** 2 + (u20 - u02) ** 2) ** (1 / 2) / 0.5 | |
e2 = (u20 + u02) / 2 - (4 * u11 ** 2 + (u20 - u02) ** 2) ** (1 / 2) / 0.5 | |
res = (1 - e2 / e1) ** 0.5 | |
return res | |
def extractFeatures(clf): | |
testImg = cv2.imread("test.jpg") | |
gray = cv2.cvtColor(testImg, cv2.COLOR_BGR2GRAY) | |
ret, thresh = cv2.threshold(gray, 0, 127, cv2.THRESH_OTSU) | |
(im2, contours, hierarcy) = cv2.findContours(thresh, cv2.RETR_TREE, | |
cv2.CHAIN_APPROX_SIMPLE) | |
i = 0 | |
for cnt in contours: | |
if i == 0: | |
i += 1 | |
continue | |
# put text position finding | |
pos = sum(cnt) / len(cnt) | |
pos = pos[0] | |
x = int(pos[0]) - 15 | |
y = int(pos[1]) | |
# hull convexity - region based feature 1 | |
hull = cv2.convexHull(cnt) | |
# feature moments - region based feature 2 | |
M = cv2.moments(cnt) | |
m0 = M['m00'] | |
cx = int(M['m10'] / M['m00']) | |
cy = int(M['m01'] / M['m00']) | |
cx = int(M['mu20'] / M['mu02']) | |
cy = int(M['m01'] / M['m00']) | |
# corner approximation - contour based feature 1 | |
peri = cv2.arcLength(cnt, True) | |
approx = cv2.approxPolyDP(cnt, 0.04 * peri, True) | |
eccentricity = calculateEccentricity(M) | |
aday = np.array([[len(approx),len(hull)/peri,eccentricity]]) | |
sonuc = clf.predict(aday) | |
sonuc = str(sonuc[0]) | |
if sonuc == "1": | |
sonuc = "kare" | |
elif sonuc == "2": | |
sonuc = "ucgen" | |
elif sonuc == "3": | |
sonuc = "cember" | |
elif sonuc == "4": | |
sonuc = "elips" | |
cv2.putText(testImg, | |
str(sonuc), | |
(x, y), | |
cv2.FONT_HERSHEY_SIMPLEX, | |
0.7, | |
(0, 0, 255), | |
1,) | |
return testImg | |
def bayesClf(): | |
global shapeData | |
clf = GaussianNB() | |
shapeData = np.array(shapeData) | |
clf.fit(shapeData, label) | |
cv2.imshow("bayes",extractFeatures(clf)) | |
def knnClf(): | |
global shapeData | |
clf = KNeighborsClassifier(n_neighbors=3) | |
clf.fit(shapeData, label) | |
cv2.imshow("knn",extractFeatures(clf)) | |
def neuralClf(): | |
global shapeData | |
clf = MLPClassifier() | |
clf.fit(shapeData, label) | |
cv2.imshow("neural", extractFeatures(clf)) | |
def main(): | |
org = cv2.imread('shapes.jpg') | |
gray = cv2.cvtColor(org, cv2.COLOR_BGR2GRAY) | |
(ret, thresh) = cv2.threshold(gray, 0, 127, cv2.THRESH_OTSU) | |
(im2, contours, hierarcy) = cv2.findContours(thresh, cv2.RETR_TREE, | |
cv2.CHAIN_APPROX_SIMPLE) | |
cv2.drawContours(org, contours, -1, (255, 25, 0), 2) | |
i = 0 | |
for cnt in contours: | |
if i == 0: | |
i += 1 | |
continue | |
# put text position finding | |
pos = sum(cnt) / len(cnt) | |
pos = pos[0] | |
x = int(pos[0]) - 15 | |
y = int(pos[1]) | |
# hull convexity - region based feature 1 | |
hull = cv2.convexHull(cnt) | |
# feature moments - region based feature 2 | |
M = cv2.moments(cnt) | |
m0 = M['m00'] | |
cx = int(M['m10'] / M['m00']) | |
cy = int(M['m01'] / M['m00']) | |
cx = int(M['mu20'] / M['mu02']) | |
cy = int(M['m01'] / M['m00']) | |
# corner approximation - contour based feature 1 | |
peri = cv2.arcLength(cnt, True) | |
approx = cv2.approxPolyDP(cnt, 0.04 * peri, True) | |
# eccentricity | |
eccentricity = calculateEccentricity(M) | |
cv2.putText(org, | |
'i : ' + str(i), | |
(x, y), | |
cv2.FONT_HERSHEY_SIMPLEX, | |
0.4, | |
(0, 0, 255), | |
1,) | |
cv2.putText(org, | |
's : ' + str(len(approx)), | |
(x, y + 15), | |
cv2.FONT_HERSHEY_SIMPLEX, | |
0.4, | |
(0, 0, 255), | |
1,) | |
cv2.putText(org, | |
'h : ' + str('{:.2f}'.format(len(hull) / peri)), | |
(x, y + 30), | |
cv2.FONT_HERSHEY_SIMPLEX, | |
0.4, | |
(0, 0, 255), | |
1,) | |
cv2.putText(org, | |
'e: ' + str('{:.2f}'.format(eccentricity)), | |
(x, y - 15), | |
cv2.FONT_HERSHEY_SIMPLEX, | |
0.4, | |
(0, 0, 255), | |
1,) | |
i += 1 | |
shapeData.append([len(approx),len(hull)/peri,eccentricity]) | |
print("[" + str(i) + "," + str(len(approx)) + "," + str('{:.2f}'.format(len(hull) / peri)) + "," +str('{:.2f}'.format(eccentricity)) + "],",sep='') | |
bayesClf() | |
knnClf() | |
neuralClf() | |
cv2.imshow('shapes', org) | |
cv2.waitKey(0) | |
cv2.destroyAllWindows() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment