2011年02月17日 星期四 19:08
最近因为《用python做科学计算》才接触python,就把以前经常碰到的问题,试着用python来解决,有一个图象处理的问题,就想用python先来处理试试。
根据PDF的 第十二章 2.2的形态学运算中的opencv_morphology_demo.py为例子,用到了
http://hyry.dip.jp:8000/scipybook/calc/docalc/35 的二值化实例,来进行图象处理,中间也请教了作者很多入门初级的问题...
主要是先进行二值化处理,然后最大连通域,最后进行形态学的处理
# -*- coding: utf-8 -*-
import pyopencv as cv
import numpy as np
from enthought.traits.api import HasTraits, Array, Enum, Int, Bool, Float, Range
from enthought.traits.ui.api import View, Item
import scipy.ndimage.measurements as M
class MorphologyDemo(HasTraits):
structing_element = Array(shape=(3,3),dtype=np.uint8)
process_type = Enum("dilate", "erode",
"MORPH_OPEN", "MORPH_CLOSE", "MORPH_GRADIENT", "MORPH_TOPHAT",
"MORPH_BLACKHAT")
erzhi_method = Enum("ADAPTIVE_THRESH_MEAN_C","ADAPTIVE_THRESH_GAUSSIAN_C")
yuzhi_type = Enum("THRESH_BINARY","THRESH_BINARY_INV","THRESH_MASK")
blocksize = Range(5, 51, 11)
canshu = Range(0, 20, 0)
iter = Int(1)
#bool = Bool(False)
bool_dilate =Bool(False)
bool_erode = Bool(False)
bool_open = Bool(False)
bool_close = Bool(False)
bool_gradient = Bool(False)
bool_tophat = Bool(False)
bool_blackhat = Bool(False)
view = View(
Item("erzhi_method", label=u"平均算法"),
Item("yuzhi_type", label=u"阈值类型"),
Item("blocksize", label=u"块大小"),
Item("canshu", label=u"参数"),
Item("bool", label=u"是否进行形态学运算"),
Item("structing_element", label=u"结构元素"),
Item("process_type", label=u"处理类型"),
Item("iter", label=u"迭代次数"),
Item("bool_dilate",label=u"膨胀"),
Item("bool_erode",label=u"腐蚀"),
Item("bool_open",label=u"开环运算"),
Item("bool_close",label=u"闭环运算"),
Item("bool_gradient",label=u"GRADIENT"),
Item("bool_tophat",label=u"TOPHAT"),
Item("bool_blackhat",label=u"BLACKHAT"),
title = u"Morphology Demo控制面板"
)
def __init__(self, *args, **kwargs):
super(MorphologyDemo, self).__init__(*args, **kwargs)
self.structing_element = np.ones((3,3), dtype=np.uint8)
img = cv.imread("bag.png")
cv.imshow("old",img)
self.img = cv.Mat()
cv.cvtColor(img, self.img, cv.CV_BGR2GRAY)
self.on_trait_change(self.redraw,
"erzhi_method,yuzhi_type,blocksize,canshu,structing_element,process_type,iter,bool,bool_erode,bool_dilate,bool_open,bool_close,bool_gradient,bool_tophat,bool_blackhat")
#self.redraw()
def redraw(self):
print "redraw"
img1 = cv.Mat()
img2 = cv.Mat()
method = getattr(cv,self.erzhi_method)
yuzhi_type = getattr(cv,self.yuzhi_type)
blocksize = self.blocksize/2*2+1
cv.adaptiveThreshold(self.img, img1, 255, method, yuzhi_type,
blocksize, self.canshu)
element = cv.asMat(self.structing_element, force_single_channel=True)
array = np.array([[0,1,0],[1,1,1],[0,1,0]],dtype=np.uint8)
element2 = cv.asMat(array, force_single_channel=True)
label, n = M.label(img1[:])
counts, edges = np.histogram(label, bins=n)
label_list = np.where(counts > 100)[0][1:]
index = np.zeros(n+1, dtype=np.int)
index[label_list] = label_list
label = index[label]
img1[label==0] = 0
if self.bool_dilate:
cv.dilate(img1, img2, element, iterations=self.iter)
img1 = img2
if self.bool_erode:
cv.erode(img1, img2, element, iterations=self.iter)
img1 = img2
if self.bool_gradient:
cv.morphologyEx(img1, img2, cv.MORPH_GRADIENT, element, iterations=self.iter)
img1 = img2
if self.bool_blackhat:
cv.morphologyEx(img1, img2, cv.MORPH_BLACKHAT, element, iterations=self.iter)
img1 = img2
if self.bool_tophat:
cv.morphologyEx(img1, img2, cv.MORPH_TOPHAT, element, iterations=self.iter)
img1 = img2
if self.bool_open:
cv.morphologyEx(img1, img2, cv.MORPH_OPEN, element, iterations=self.iter)
img1 = img2
if self.bool_close:
cv.morphologyEx(img1, img2, cv.MORPH_CLOSE, element, iterations=self.iter)
img1 = img2
cv.imshow("Morphology Demo",img1)
demo = MorphologyDemo()
demo.configure_traits()
在选择形态学计算时,只是用复选框简单的进行选择,形态学运算的顺序已经定了
学习当中主要是调用函数的参数类型造成了一些困难
还有就是红字部分:
刚开始认为已经定义了img,认为self.img就是img,就像在Class中定义了 blocksize,后面就可以用self. blocksize一样
adaptiveThreshold的src必须是单通道的灰度图片,因此需要用cvtColor转换
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
基本目标达到了,但是这样机械的形态学运算很难得到理想的效果,下一步把复选框改成按键,这样可以没有顺序的应用形态学运算。
2011年02月17日 星期四 19:52
这里贴代码比较难看,我贴到Code Library上了:
http://hyry.dip.jp/code.py?id=107
另外,关于调节形态学处理的顺序可以使用列表,可以参考本书的演示程序:
07-traitsui\traitsUI_simple_list_editors.py
下面是在线版本:
http://hyry.dip.jp:8000/scipybook/default/file/07-traitsui/traitsUI_simple_list_editors.py
可以使用此例中最右边那种候选控件。
Zeuux © 2024
京ICP备05028076号