本文实例讲述了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)
运行结果如下图所示:
我们再稍微改动一下上面的代码
对于彩色图片,对于每个像素点,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)
运行效果如下图所示:
希望本文所述对大家的Python程序设计有所帮助。

Pythonusesahybridmodelofcompilationandinterpretation:1)ThePythoninterpretercompilessourcecodeintoplatform-independentbytecode.2)ThePythonVirtualMachine(PVM)thenexecutesthisbytecode,balancingeaseofusewithperformance.

Pythonisbothinterpretedandcompiled.1)It'scompiledtobytecodeforportabilityacrossplatforms.2)Thebytecodeistheninterpreted,allowingfordynamictypingandrapiddevelopment,thoughitmaybeslowerthanfullycompiledlanguages.

Forloopsareidealwhenyouknowthenumberofiterationsinadvance,whilewhileloopsarebetterforsituationswhereyouneedtoloopuntilaconditionismet.Forloopsaremoreefficientandreadable,suitableforiteratingoversequences,whereaswhileloopsoffermorecontrolandareusefulf

Forloopsareusedwhenthenumberofiterationsisknowninadvance,whilewhileloopsareusedwhentheiterationsdependonacondition.1)Forloopsareidealforiteratingoversequenceslikelistsorarrays.2)Whileloopsaresuitableforscenarioswheretheloopcontinuesuntilaspecificcond

Pythonisnotpurelyinterpreted;itusesahybridapproachofbytecodecompilationandruntimeinterpretation.1)Pythoncompilessourcecodeintobytecode,whichisthenexecutedbythePythonVirtualMachine(PVM).2)Thisprocessallowsforrapiddevelopmentbutcanimpactperformance,req

ToconcatenatelistsinPythonwiththesameelements,use:1)the operatortokeepduplicates,2)asettoremoveduplicates,or3)listcomprehensionforcontroloverduplicates,eachmethodhasdifferentperformanceandorderimplications.

Pythonisaninterpretedlanguage,offeringeaseofuseandflexibilitybutfacingperformancelimitationsincriticalapplications.1)InterpretedlanguageslikePythonexecuteline-by-line,allowingimmediatefeedbackandrapidprototyping.2)CompiledlanguageslikeC/C transformt

Useforloopswhenthenumberofiterationsisknowninadvance,andwhileloopswheniterationsdependonacondition.1)Forloopsareidealforsequenceslikelistsorranges.2)Whileloopssuitscenarioswheretheloopcontinuesuntilaspecificconditionismet,usefulforuserinputsoralgorit


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
