python图像处理之反色实现方法

WBOY
Release: 2016-06-10 15:11:16
Original
3189 people have browsed it

本文实例讲述了python图像处理之反色实现方法。分享给大家供大家参考。具体如下:

我们先加载一个8位灰度图像

每一个像素对应的灰度值从0-255

则只需要读取每个像素的灰度值A,再将255-A写入

这样操作一遍后,图像就会反色了

这里运行环境为:

Python为:Python2.7.6
OpenCV2.4.10版(可到http://sourceforge.net/projects/opencvlibrary/files/opencv-win/下载)
numpy为:numpy-1.9.1-win32-superpack-python2.7(可到http://sourceforge.net/projects/numpy/files/NumPy/1.9.1/下载)

具体python代码如下:

import cv2.cv as cv
image = cv.LoadImage('angelababy.jpg',0)
size = (image.width,image.height)
iTmp = cv.CreateImage(size,image.depth,image.nChannels)
for i in range(image.height):
  for j in range(image.width):
    iTmp[i,j] = 255 - image[i,j]
cv.NamedWindow('image')
cv.NamedWindow('iTmp')
cv.ShowImage('image',image)
cv.ShowImage('iTmp',iTmp)
cv.WaitKey(0)

Copy after login

运行结果如下图所示:

我们再稍微改动一下上面的代码

对于彩色图片,对于每个像素点,OpenCV在Python中是以tuple(R,G,B)的形式存储的

所以对于彩色图片的反色,只需要获得tuple(255-R,255-G,255-B)就行了

代码如下:

import cv2.cv as cv
image = cv.LoadImage('angelababy.jpg',1)
size = (image.width,image.height)
iTmp = cv.CreateImage(size,image.depth,image.nChannels)
for i in range(image.height):
  for j in range(image.width):
    iTmp[i,j] = (255-image[i,j][0],255-image[i,j][1],255-image[i,j][2])
cv.NamedWindow('image')
cv.NamedWindow('iTmp')
cv.ShowImage('image',image)
cv.ShowImage('iTmp',iTmp)
cv.WaitKey(0)

Copy after login

运行效果如下图所示:

希望本文所述对大家的Python程序设计有所帮助。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!