本文主要是介绍python手记(33),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
python+OPENCV彩色转灰度
#!/usr/bin/env python
#-*- coding: utf-8 -*-
#code:myhaspl@qq.com
import cv2
import numpy as npfn="test2.jpg"if __name__ == '__main__':print 'http://blog.csdn.net/myhaspl'print 'myhaspl@qq.com'printprint 'loading %s ...' % fnprint '正在处理中',img = cv2.imread(fn)w=img.shape[1]h=img.shape[0] ii=0#彩色转换到灰度#强度公式为(b*0.299+g*0.587+r*0.114)/3#b, g, r = cv2.split(img)b=b*0.299g=g*0.587r=r*0.114mycolor=(b+g+r)/3#直接通过索引改变色彩分量 img[:,:,0]=mycolorimg[:,:,1]=mycolorimg[:,:,2]=mycolor#加上水印cv2.putText(img,"http://blog.csdn.net/myhaspl", (20,20),cv2.FONT_HERSHEY_PLAIN, 2.0, (0, 0, 0), thickness = 2)cv2.putText(img,"code by myhaspl:myhaspl@qq.com", (20,100),cv2.FONT_HERSHEY_PLAIN, 2.0, (0, 0, 0), thickness = 2) cv2.namedWindow('img') cv2.imshow('img', img)cv2.waitKey()cv2.destroyAllWindows()
也可以使用通道合并
#!/usr/bin/env python
#-*- coding: utf-8 -*-
#code:myhaspl@qq.com
import cv2
import numpy as npfn="test2.jpg"if __name__ == '__main__':print 'http://blog.csdn.net/myhaspl'print 'myhaspl@qq.com'printprint 'loading %s ...' % fnprint '正在处理中',img = cv2.imread(fn)w=img.shape[1]h=img.shape[0] ii=0
#生成负片#b, g, r = cv2.split(img)b=255-bg=255-gr=255-r#通道合并 imgxx = cv2.merge([b,g,r])#加上水印cv2.putText(img,"http://blog.csdn.net/myhaspl", (20,20),cv2.FONT_HERSHEY_PLAIN, 2.0, (0, 0, 0), thickness = 2)cv2.putText(img,"code by myhaspl:myhaspl@qq.com", (20,100),cv2.FONT_HERSHEY_PLAIN, 2.0, (0, 0, 0), thickness = 2) cv2.namedWindow('img') cv2.imshow('img', img)cv2.imshow("Merged", imgxx) cv2.waitKey()cv2.destroyAllWindows()
这篇关于python手记(33)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!