# -*- coding: utf-8 -*-
"""
Created on Mon Oct 1 09:32:37 2018 @author:
"""
import numpy as np
from tkinter import *
#import tkinter
from PIL import Image, ImageTk
from scipy.misc import imread
import matplotlib.pyplot as plt
from sklearn.svm import SVC class SVM_Classifier(Frame):
def __init__(self, master=None):
self.root = Tk()#tkinter.TK()
Frame.__init__(self, master)
Pack.config(self)
self.menus()
self.createWidgets()
self.after(10, self.callback)
self.state=0
global X_list
X_list=[]
global y_list
y_list=[]
def menus(self):
allmenu = Menu(self.root)#tkinter.Menu
# 添加子菜单
menu1 = Menu(allmenu, tearoff=0)
menu2=Menu(allmenu, tearoff=0)
# 添加选项卡
menu1.add_command(label='前景', command=self.target)
menu1.add_command(label='背景', command=self.background)
allmenu.add_cascade(label='样本标注', menu=menu1)
menu2.add_command(label='SVM学习并显示结果', command=self.processing)
allmenu.add_cascade(label='分析处理', menu=menu2)
self.root.config(menu=allmenu)
def target(self):
self.state=1
def background(self):
self.state=2
def processing(self):
self.state=0
X=np.array(X_list)
y=np.array(y_list)
print(X)
print(y)
#将X,Y写入txt文件
# np_X=[]
# np_y=[]
# np_X.append(X)
# np_y.append(y)
svm_x='svm_x.txt'
svm_y='svm_y.txt'
X1_string = '\n'.join(str(x) for x in X)
with open(svm_x,'w') as svm_file:
svm_file.write(X1_string)
y1_string = '\n'.join(str(x) for x in y)
with open(svm_y,'w') as svm_file:
svm_file.write(y1_string) #支持向量机学习
clf=SVC(kernel="linear", C=0.025)#SVC(gamma=2, C=1)
clf.fit(X, y)#SVM学习
score = clf.score(X,y)
print('score=',score)
image = imread("WIN_20190110_16_56_25_Pro.jpg")#fruits.png
XX=[]
for i in range(image.shape[0]):
for j in range(image.shape[1]):
XX.append([image[i,j,0],image[i,j,1],image[i,j,2]])
Z=clf.decision_function(XX)
ZZ=np.array(Z)
ZZ=ZZ.reshape(image.shape[0],image.shape[1])
for i in range(image.shape[0]):
for j in range(image.shape[1]):
if ZZ[i,j]<0:
image[i,j,0]=0
image[i,j,1]=0
image[i,j,2]=0
# for i in range(image.shape[0]):
# for j in range(image.shape[1]):
# Z = clf.decision_function([[image[i,j,0],image[i,j,1],image[i,j,2]]])
# if Z[0]<0:
# image[i,j,0]=0
# image[i,j,1]=0
# image[i,j,2]=0
plt.imshow(image)
plt.axis('off')
plt.title('SVM')
plt.show() def createWidgets(self):
## The playing field
self.draw = Canvas(self, width=640, height=480)
self.im=Image.open('WIN_20190110_16_56_25_Pro.jpg')#fruits.png
self.tkimg=ImageTk.PhotoImage(self.im)
self.myImg=self.draw.create_image(10,10,anchor=NW,image=self.tkimg) self.draw.pack(side=LEFT)
def mouse_pick(self,event):
rgb=self.im.getpixel((event.x-10,event.y-10))
print("clicked at:x=", event.x-10,'y=',event.y-10,' r=',rgb[0],'g=',rgb[1],'b=',rgb[2])
X_list.append([np.uint8(rgb[0]),np.uint8(rgb[1]),np.uint8(rgb[2])])
if self.state==1:
self.pick_points = self.draw.create_oval((event.x - 2),(event.y - 2),(event.x + 2),(event.y + 2),fill="red")
y_list.append(1)#添加入前景标签
if self.state==2:
self.pick_points = self.draw.create_oval((event.x - 2),(event.y - 2),(event.x + 2),(event.y + 2),fill="green")
y=y_list.append(-1)#添加入背景标签
def callback(self, *args):
self.draw.tag_bind(self.myImg, "<Button-1>", self.mouse_pick) game = SVM_Classifier()
game.mainloop()

最新文章

  1. std::unique_lock&lt;std::mutex&gt; or std::lock_guard&lt;std::mutex&gt; C++11 区别
  2. SqlServer切换MySql总结
  3. eclipse最有用快捷键整理
  4. bootstrap-4
  5. JavaScript 事件——“事件类型”中“复合事件”和“变动事件”的注意要点(转)
  6. was7 安装遇到问题(Linux平台Redhat 6)
  7. STM32学习笔记:系统时钟和SysTick定时器
  8. How do I list all tables/indices contained in an SQLite database
  9. 第二百八十五天 how can I 坚持
  10. [转] 考验你的JavaScript底细
  11. 2015 UESTC Winter Training #8【The 2011 Rocky Mountain Regional Contest】
  12. opals 开发记录
  13. ReactiveSwift源码解析(十一) Atomic的代码实现以及其中的Defer延迟、Posix互斥锁、递归锁
  14. 【学习】原生js学习笔记1:添加class和使input为不可用
  15. Java的一些良好习惯及细节------持续更新中...
  16. 打印十字图 JAVA 递归实现
  17. Android之日志管理(Log)
  18. -1.记libgdx初次接触
  19. Let&#39;s-Bug修复日志
  20. 用turtle画图

热门文章

  1. HTTP请求报文
  2. (转载)java list排序
  3. 使用jquery.mCustomScrollbar自定义滚动条(3)callback onCreate
  4. Hive数据类型及文本文件数据编码
  5. 倒数第N个字符串
  6. github----awesome-typescript-projects
  7. MySQL5.7修改默认密码、随机密码
  8. css一般性
  9. Android重打包+重新签名工具Apktool Box
  10. spring 中几种注解的说明